File: tc_node.rb

package info (click to toggle)
ruby-libxml 2.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 1,812 kB
  • sloc: xml: 9,628; ruby: 7,119; ansic: 6,665; makefile: 2
file content (209 lines) | stat: -rw-r--r-- 5,421 bytes parent folder | download
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
# encoding: UTF-8

require './test_helper'
require 'test/unit'

class TestNode < Test::Unit::TestCase
  def setup
    @file_name = "model/bands.utf-8.xml"

    # Strip spaces to make testing easier
    XML.default_keep_blanks = false
    file = File.join(File.dirname(__FILE__), @file_name)
    @doc = XML::Document.file(file)
  end
  
  def teardown
    XML.default_keep_blanks = true
    @doc = nil
  end

  def nodes
    # Find all nodes with a country attributes
    @doc.find('*[@country]')
  end

  def test_doc_class
    assert_instance_of(XML::Document, @doc)
  end

  def test_doc_node_type
    assert_equal XML::Node::DOCUMENT_NODE, @doc.node_type
  end

  def test_root_class
    assert_instance_of(XML::Node, @doc.root)
  end

  def test_root_node_type
    assert_equal XML::Node::ELEMENT_NODE, @doc.root.node_type
  end

  def test_node_class
    for n in nodes
      assert_instance_of(XML::Node, n)
    end
  end

  def test_context
    node = @doc.root
    context = node.context
    assert_instance_of(XML::XPath::Context, context)
  end

  def test_find
    assert_instance_of(XML::XPath::Object, self.nodes)
  end

  def test_node_child_get
    assert_instance_of(TrueClass, @doc.root.child?)
    assert_instance_of(XML::Node, @doc.root.child)

    if defined?(Encoding)
      assert_equal(Encoding::UTF_8, @doc.root.child.name.encoding)
      assert_equal("m\u00F6tley_cr\u00FCe", @doc.root.child.name)
    else
      assert_equal("m\303\266tley_cr\303\274e", @doc.root.child.name)
    end
  end

  def test_node_doc
    for n in nodes
      assert_instance_of(XML::Document, n.doc) if n.document?
    end
  end
  
  def test_name
    node = @doc.root.children.last
    assert_equal("iron_maiden", node.name)
  end

  def test_node_find
    nodes = @doc.root.find('./fixnum')
    for node in nodes
      assert_instance_of(XML::Node, node)
    end
  end

  def test_equality
    node_a = @doc.find_first('*[@country]')
    node_b = @doc.root.child

    assert(node_a == node_b)
    assert(node_a.eql?(node_b))
    assert(node_a.equal?(node_b))

    file = File.join(File.dirname(__FILE__), @file_name)
    doc2 = XML::Document.file(file)

    node_a2 = doc2.find_first('*[@country]')

    assert_equal(node_a.to_s, node_a2.to_s)
    assert_equal(node_a, node_a2)
    assert(node_a.eql?(node_a2))
    assert(!node_a.equal?(node_a2))
  end

  def test_equality_nil
    node = @doc.root
    assert(node != nil)
  end

  def test_equality_wrong_type
    node = @doc.root

    assert_raise(TypeError) do
      assert(node != 'abc')
    end
  end

  def test_content
    node = @doc.root.last
    assert_equal("Iron Maiden is a British heavy metal band formed in 1975.",
                 node.content)
  end

  def test_base
    doc = XML::Parser.string('<person />').parse
    assert_nil(doc.root.base_uri)
  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
		text = '<bad-script>if (a &lt; b || b &gt; c) { return "text"; }<stop/>return "&gt;&gt;&gt;snip&lt;&lt;&lt;";</bad-script>'
    node = XML::Parser.string(text).parse.root
		assert_equal text, node.to_s

		text_noenc = '<bad-script>if (a < b || b > c) { return "text"; }<stop/>return ">>>snip<<<";</bad-script>'
		node.output_escaping = false
		assert_equal text_noenc, node.to_s

		node.output_escaping = true
		assert_equal text, node.to_s

		node.output_escaping = nil
		assert_equal text_noenc, 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
		text = '<bad-script>if (a &lt; b || b &gt; c) { return "text"; }<stop/>return "&gt;&gt;&gt;snip&lt;&lt;&lt;";</bad-script>'
    node = XML::Parser.string(text).parse.root
		affected = node.find('//text()')

		check_escaping = lambda do |flag|
			assert_equal('bad-script', node.name)
			assert_equal(flag, node.output_escaping?)
			affected.each do |x|
				assert_equal(flag ? 'text' : 'textnoenc', x.name)
				assert_equal(flag, x.output_escaping?)
			end
		end

		node.output_escaping = false
		check_escaping[false]

		node.output_escaping = true
		check_escaping[true]

		node.output_escaping = nil
		check_escaping[false]

		node.output_escaping = true
		check_escaping[true]

		affected.first.output_escaping = true
		affected.last.output_escaping = false
		assert node.output_escaping?.nil?
  end

  def test_space_preserve
    node = @doc.root

    node.space_preserve = false
    assert_equal XML::Node::SPACE_DEFAULT, node.space_preserve

    node.space_preserve = true
    assert_equal XML::Node::SPACE_PRESERVE, node.space_preserve
  end

  def test_empty
    text = '<name> </name>'
    doc = XML::Parser.string(text).parse

    node = doc.root
    assert(!node.empty?)
    
    text_node = node.first
    assert(text_node.empty?)
  end
end