1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
# encoding: UTF-8
require './test_helper'
require 'test/unit'
class TestTextNode < Test::Unit::TestCase
def test_content
node = XML::Node.new_text('testdata')
assert_instance_of(XML::Node, node)
assert_equal('testdata', node.content)
end
def test_invalid_content
error = assert_raise(TypeError) do
node = XML::Node.new_text(nil)
end
assert_equal('wrong argument type nil (expected String)', error.to_s)
end
# We use the same facility that libXSLT does here to disable output escaping.
# This lets you specify that the node's content should be rendered unaltered
# whenever it is being output. This is useful for things like <script> and
# <style> nodes in HTML documents if you don't want to be forced to wrap them
# in CDATA nodes. Or if you are sanitizing existing HTML documents and want
# to preserve the content of any of the text nodes.
#
def test_output_escaping
textnoenc = 'if (a < b || c > d) return "e";'
text = "if (a < b || c > d) return \"e\";"
node = XML::Node.new_text(textnoenc)
assert node.output_escaping?
assert_equal text, node.to_s
node.output_escaping = false
assert_equal textnoenc, node.to_s
node.output_escaping = true
assert_equal text, node.to_s
node.output_escaping = nil
assert_equal textnoenc, node.to_s
node.output_escaping = true
assert_equal text, node.to_s
end
# Just a sanity check for output escaping.
def test_output_escaping_sanity
node = XML::Node.new_text('testdata')
assert_equal 'text', node.name
assert node.output_escaping?
node.output_escaping = false
assert_equal 'textnoenc', node.name
assert ! node.output_escaping?
node.output_escaping = true
assert_equal 'text', node.name
assert node.output_escaping?
node.output_escaping = nil
assert_equal 'textnoenc', node.name
assert ! node.output_escaping?
node.output_escaping = true
assert_equal 'text', node.name
assert node.output_escaping?
end
end
|