libxml-ruby 3.2.0 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,2 @@
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <Name>François</Name>
@@ -13,115 +13,115 @@ class TestDocument < Minitest::Test
13
13
  @doc = nil
14
14
  end
15
15
 
16
- # def test_klass
17
- # assert_instance_of(LibXML::XML::Document, @doc)
18
- # end
19
- #
20
- # def test_context
21
- # context = @doc.context
22
- # assert_instance_of(LibXML::XML::XPath::Context, context)
23
- # end
24
- #
25
- # def test_find
26
- # set = @doc.find('/ruby_array/fixnum')
27
- # assert_instance_of(LibXML::XML::XPath::Object, set)
28
- # assert_raises(NoMethodError) {
29
- # set.xpath
30
- # }
31
- # end
32
- #
33
- # def test_compression
34
- # if LibXML::XML.enabled_zlib?
35
- # 0.upto(9) do |i|
36
- # assert_equal(i, @doc.compression = i)
37
- # assert_equal(i, @doc.compression)
38
- # end
39
- #
40
- # 9.downto(0) do |i|
41
- # assert_equal(i, @doc.compression = i)
42
- # assert_equal(i, @doc.compression)
43
- # end
44
- #
45
- # 10.upto(20) do |i|
46
- # # assert_equal(9, @doc.compression = i)
47
- # assert_equal(i, @doc.compression = i) # This works around a bug in Ruby 1.8
48
- # assert_equal(9, @doc.compression)
49
- # end
50
- #
51
- # -1.downto(-10) do |i|
52
- # # assert_equal(0, @doc.compression = i)
53
- # assert_equal(i, @doc.compression = i) # FIXME This bug should get fixed ASAP
54
- # assert_equal(0, @doc.compression)
55
- # end
56
- # end
57
- # end
58
- #
59
- # def test_version
60
- # assert_equal('1.0', @doc.version)
61
- #
62
- # doc = LibXML::XML::Document.new('6.9')
63
- # assert_equal('6.9', doc.version)
64
- # end
65
- #
66
- # def test_write_root
67
- # @doc.root = LibXML::XML::Node.new('rubynet')
68
- # assert_instance_of(LibXML::XML::Node, @doc.root)
69
- # assert_instance_of(LibXML::XML::Document, @doc.root.doc)
70
- # assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rubynet/>\n",
71
- # @doc.to_s(:indent => false))
72
- # end
73
- #
74
- # def test_doc_node_type
75
- # assert_equal(LibXML::XML::Node::DOCUMENT_NODE, LibXML::XML::Document.new.node_type)
76
- # end
77
- #
78
- # def test_doc_node_type_name
79
- # assert_equal('document_xml', LibXML::XML::Document.new.node_type_name)
80
- # end
81
- #
82
- # def test_xhtml
83
- # doc = LibXML::XML::Document.new
84
- # assert(!doc.xhtml?)
85
- # LibXML::XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", nil, doc, true
86
- # assert(doc.xhtml?)
87
- # end
88
- #
89
- # def test_document_root
90
- # doc1 = LibXML::XML::Document.string("<one/>")
91
- # doc2 = LibXML::XML::Document.string("<two/>")
92
- #
93
- # error = assert_raises(LibXML::XML::Error) do
94
- # doc1.root = doc2.root
95
- # end
96
- # assert_equal(" Nodes belong to different documents. You must first import the node by calling LibXML::XML::Document.import.",
97
- # error.to_s)
98
- #
99
- # doc2.root << doc2.import(doc1.root)
100
- # assert_equal('<one/>', doc1.root.to_s)
101
- # assert_equal('<two><one/></two>', doc2.root.to_s(:indent => false))
102
- #
103
- # assert(!doc1.root.equal?(doc2.root))
104
- # assert(doc1.root.doc != doc2.root.doc)
105
- # end
106
- #
107
- # def test_import_node
108
- # doc1 = LibXML::XML::Parser.string('<nums><one></one></nums>').parse
109
- # doc2 = LibXML::XML::Parser.string('<nums><two></two></nums>').parse
110
- #
111
- # node = doc1.root.child
112
- #
113
- # error = assert_raises(LibXML::XML::Error) do
114
- # doc2.root << node
115
- # end
116
- #
117
- # assert_equal(" Nodes belong to different documents. You must first import the node by calling LibXML::XML::Document.import.",
118
- # error.to_s)
119
- #
120
- # doc2.root << doc2.import(node)
121
- #
122
- # assert_equal("<nums><two/><one/></nums>",
123
- # doc2.root.to_s(:indent => false))
124
- # end
16
+ def test_klass
17
+ assert_instance_of(LibXML::XML::Document, @doc)
18
+ end
19
+
20
+ def test_context
21
+ context = @doc.context
22
+ assert_instance_of(LibXML::XML::XPath::Context, context)
23
+ end
24
+
25
+ def test_find
26
+ set = @doc.find('/ruby_array/fixnum')
27
+ assert_instance_of(LibXML::XML::XPath::Object, set)
28
+ assert_raises(NoMethodError) {
29
+ set.xpath
30
+ }
31
+ end
32
+
33
+ def test_compression
34
+ if LibXML::XML.enabled_zlib?
35
+ 0.upto(9) do |i|
36
+ assert_equal(i, @doc.compression = i)
37
+ assert_equal(i, @doc.compression)
38
+ end
39
+
40
+ 9.downto(0) do |i|
41
+ assert_equal(i, @doc.compression = i)
42
+ assert_equal(i, @doc.compression)
43
+ end
44
+
45
+ 10.upto(20) do |i|
46
+ # assert_equal(9, @doc.compression = i)
47
+ assert_equal(i, @doc.compression = i) # This works around a bug in Ruby 1.8
48
+ assert_equal(9, @doc.compression)
49
+ end
50
+
51
+ -1.downto(-10) do |i|
52
+ # assert_equal(0, @doc.compression = i)
53
+ assert_equal(i, @doc.compression = i) # FIXME This bug should get fixed ASAP
54
+ assert_equal(0, @doc.compression)
55
+ end
56
+ end
57
+ end
58
+
59
+ def test_version
60
+ assert_equal('1.0', @doc.version)
61
+
62
+ doc = LibXML::XML::Document.new('6.9')
63
+ assert_equal('6.9', doc.version)
64
+ end
65
+
66
+ def test_write_root
67
+ @doc.root = LibXML::XML::Node.new('rubynet')
68
+ assert_instance_of(LibXML::XML::Node, @doc.root)
69
+ assert_instance_of(LibXML::XML::Document, @doc.root.doc)
70
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rubynet/>\n",
71
+ @doc.to_s(:indent => false))
72
+ end
73
+
74
+ def test_doc_node_type
75
+ assert_equal(LibXML::XML::Node::DOCUMENT_NODE, LibXML::XML::Document.new.node_type)
76
+ end
77
+
78
+ def test_doc_node_type_name
79
+ assert_equal('document_xml', LibXML::XML::Document.new.node_type_name)
80
+ end
81
+
82
+ def test_xhtml
83
+ doc = LibXML::XML::Document.new
84
+ assert(!doc.xhtml?)
85
+ LibXML::XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", nil, doc, true
86
+ assert(doc.xhtml?)
87
+ end
88
+
89
+ def test_document_root
90
+ doc1 = LibXML::XML::Document.string("<one/>")
91
+ doc2 = LibXML::XML::Document.string("<two/>")
92
+
93
+ error = assert_raises(LibXML::XML::Error) do
94
+ doc1.root = doc2.root
95
+ end
96
+ assert_equal(" Nodes belong to different documents. You must first import the node by calling LibXML::XML::Document.import.",
97
+ error.to_s)
98
+
99
+ doc2.root << doc2.import(doc1.root)
100
+ assert_equal('<one/>', doc1.root.to_s)
101
+ assert_equal('<two><one/></two>', doc2.root.to_s(:indent => false))
102
+
103
+ assert(!doc1.root.equal?(doc2.root))
104
+ assert(doc1.root.doc != doc2.root.doc)
105
+ end
106
+
107
+ def test_import_node
108
+ doc1 = LibXML::XML::Parser.string('<nums><one></one></nums>').parse
109
+ doc2 = LibXML::XML::Parser.string('<nums><two></two></nums>').parse
110
+
111
+ node = doc1.root.child
112
+
113
+ error = assert_raises(LibXML::XML::Error) do
114
+ doc2.root << node
115
+ end
116
+
117
+ assert_equal(" Nodes belong to different documents. You must first import the node by calling LibXML::XML::Document.import.",
118
+ error.to_s)
119
+
120
+ doc2.root << doc2.import(node)
121
+
122
+ assert_equal("<nums><two/><one/></nums>",
123
+ doc2.root.to_s(:indent => false))
124
+ end
125
125
 
126
126
  def test_nonet
127
127
  xml_string = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
@@ -137,8 +137,8 @@ class TestParserContext < Minitest::Test
137
137
  context = LibXML::XML::Parser::Context.new
138
138
  assert(!context.replace_entities?)
139
139
 
140
- # context.options = 1
141
- # assert(context.replace_entities?)
140
+ context.options = LibXML::XML::Parser::Options::NOENT
141
+ assert(context.replace_entities?)
142
142
 
143
143
  context.options = 0
144
144
  assert(!context.replace_entities?)
@@ -16,25 +16,48 @@ class TestWriter < Minitest::Test
16
16
 
17
17
  def test_empty_doc
18
18
  writer = LibXML::XML::Writer.string
19
- document writer
19
+ document(writer)
20
20
  assert_equal(writer.result.strip!, '<?xml version="1.0"?>')
21
21
 
22
22
  writer = LibXML::XML::Writer.string
23
- document writer, { :encoding => LibXML::XML::Encoding::ISO_8859_1 }
23
+ document(writer, :encoding => LibXML::XML::Encoding::ISO_8859_1)
24
24
  assert_equal(writer.result.strip!, '<?xml version="1.0" encoding="ISO-8859-1"?>')
25
25
 
26
26
  writer = LibXML::XML::Writer.string
27
- document writer, { :standalone => 1 }
27
+ document(writer, :standalone => 1)
28
28
  assert_equal(writer.result.strip!, '<?xml version="1.0" standalone="yes"?>')
29
29
 
30
30
  writer = LibXML::XML::Writer.string
31
- document writer, { :standalone => 1, :encoding => LibXML::XML::Encoding::ISO_8859_1, :foo => :bar }
31
+ document(writer, :standalone => 1, :encoding => LibXML::XML::Encoding::ISO_8859_1, :foo => :bar)
32
32
  assert_equal(writer.result.strip!, '<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>')
33
33
  end
34
34
 
35
+ def test_file_encoding
36
+ value = "François".encode(Encoding::UTF_8)
37
+
38
+ File.open('test.xml', 'wb', encoding: 'UTF-8') do |file|
39
+ writer = LibXML::XML::Writer::io(file)
40
+ document(writer, encoding: LibXML::XML::Encoding::UTF_8) do
41
+ writer.write_element('Name', value)
42
+ end
43
+ end
44
+ end
45
+
46
+ def test_io_encoding
47
+ value = "François".encode(Encoding::UTF_8)
48
+ expected = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<Name>François</Name>".encode(Encoding::UTF_8)
49
+
50
+ io = StringIO.new
51
+ writer = LibXML::XML::Writer::io(io)
52
+ document(writer, encoding: LibXML::XML::Encoding::UTF_8) do
53
+ writer.write_element('Name', value)
54
+ end
55
+ assert_equal(expected, io.string.strip)
56
+ end
57
+
35
58
  def test_single_root
36
59
  writer = LibXML::XML::Writer.string
37
- document writer do
60
+ document(writer) do
38
61
  element writer, 'root'
39
62
  end
40
63
 
@@ -45,7 +68,7 @@ class TestWriter < Minitest::Test
45
68
  expected = "<?xml version=\"1.0\"?>\n<?php echo \"foo\";?>"
46
69
 
47
70
  writer = LibXML::XML::Writer.string
48
- document writer do
71
+ document(writer) do
49
72
  assert(writer.start_pi('php'))
50
73
  assert(writer.write_string('echo "foo";'))
51
74
  assert(writer.end_pi)
@@ -53,7 +76,7 @@ class TestWriter < Minitest::Test
53
76
  assert_equal(writer.result.strip!, expected)
54
77
 
55
78
  writer = LibXML::XML::Writer.string
56
- document writer do
79
+ document(writer) do
57
80
  assert(writer.write_pi('php', 'echo "foo";'))
58
81
  end
59
82
  assert_equal(writer.result.strip!, expected)
@@ -63,7 +86,7 @@ class TestWriter < Minitest::Test
63
86
  expected = "<?xml version=\"1.0\"?>\n<!--foo-->"
64
87
 
65
88
  writer = LibXML::XML::Writer.string
66
- document writer do
89
+ document(writer) do
67
90
  assert(writer.start_comment)
68
91
  assert(writer.write_string 'foo')
69
92
  assert(writer.end_comment)
@@ -71,7 +94,7 @@ class TestWriter < Minitest::Test
71
94
  assert_equal(writer.result.strip!, expected)
72
95
 
73
96
  writer = LibXML::XML::Writer.string
74
- document writer do
97
+ document(writer) do
75
98
  assert(writer.write_comment 'foo')
76
99
  end
77
100
  assert_equal(writer.result.strip!, expected)
@@ -81,7 +104,7 @@ class TestWriter < Minitest::Test
81
104
  expected = "<?xml version=\"1.0\"?>\n<root><![CDATA[<?php echo $foo; ?>]]></root>"
82
105
 
83
106
  writer = LibXML::XML::Writer.string
84
- document writer do
107
+ document(writer) do
85
108
  element writer, 'root' do
86
109
  assert(writer.start_cdata)
87
110
  assert(writer.write_string '<?php echo $foo; ?>')
@@ -91,7 +114,7 @@ class TestWriter < Minitest::Test
91
114
  assert_equal(writer.result.strip!, expected)
92
115
 
93
116
  writer = LibXML::XML::Writer.string
94
- document writer do
117
+ document(writer) do
95
118
  element writer, 'root' do
96
119
  assert(writer.write_cdata '<?php echo $foo; ?>')
97
120
  end
@@ -101,13 +124,13 @@ class TestWriter < Minitest::Test
101
124
 
102
125
  def test_write_empty_elements
103
126
  writer = LibXML::XML::Writer.string
104
- document writer do
127
+ document(writer) do
105
128
  assert(writer.write_element 'foo')
106
129
  end
107
130
  assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<foo/>")
108
131
 
109
132
  writer = LibXML::XML::Writer.string
110
- document writer do
133
+ document(writer) do
111
134
  assert(writer.write_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
112
135
  end
113
136
  assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<" + XSL_PREFIX + ":stylesheet xmlns:xsl=\"" + XSL_URI + "\"/>")
@@ -117,7 +140,7 @@ class TestWriter < Minitest::Test
117
140
  expected = "<?xml version=\"1.0\"?>\n<abc>123<def>456<ghi>789</ghi>cueillir des cerises</def>nous irons au bois</abc>"
118
141
 
119
142
  writer = LibXML::XML::Writer.string
120
- document writer do
143
+ document(writer) do
121
144
  assert(writer.start_element 'abc')
122
145
  assert(writer.write_string '123')
123
146
  assert(writer.start_element 'def')
@@ -133,7 +156,7 @@ class TestWriter < Minitest::Test
133
156
  assert_equal(writer.result.strip!, expected)
134
157
 
135
158
  writer = LibXML::XML::Writer.string
136
- document writer do
159
+ document(writer) do
137
160
  assert(writer.start_element 'abc')
138
161
  assert(writer.write_string '123')
139
162
  assert(writer.start_element 'def')
@@ -157,7 +180,7 @@ class TestWriter < Minitest::Test
157
180
  "</xsl:stylesheet>"
158
181
 
159
182
  writer = LibXML::XML::Writer.string
160
- document writer do
183
+ document(writer) do
161
184
  assert(writer.start_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
162
185
  assert(writer.start_element_ns XSL_PREFIX, 'attribute-set')
163
186
  assert(writer.start_element_ns XSL_PREFIX, 'attribute')
@@ -172,7 +195,7 @@ class TestWriter < Minitest::Test
172
195
  assert_equal(writer.result.strip!, expected)
173
196
 
174
197
  writer = LibXML::XML::Writer.string
175
- document writer do
198
+ document(writer) do
176
199
  assert(writer.start_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
177
200
  assert(writer.start_element_ns XSL_PREFIX, 'attribute-set')
178
201
  assert(writer.write_element_ns XSL_PREFIX, 'attribute', nil, '20px')
@@ -185,7 +208,7 @@ class TestWriter < Minitest::Test
185
208
 
186
209
  def test_attribute
187
210
  writer = LibXML::XML::Writer.string
188
- document writer do
211
+ document(writer) do
189
212
  element writer, 'root' do
190
213
  element writer, 'child' do
191
214
  assert(writer.start_attribute 'foo')
@@ -197,7 +220,7 @@ class TestWriter < Minitest::Test
197
220
  assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root><child foo=\"bar\"/></root>")
198
221
 
199
222
  writer = LibXML::XML::Writer.string
200
- document writer do
223
+ document(writer) do
201
224
  element writer, 'root' do
202
225
  element writer, 'child' do
203
226
  assert(writer.write_attribute 'abc', 'def')
@@ -212,7 +235,7 @@ class TestWriter < Minitest::Test
212
235
  expected = "<?xml version=\"1.0\"?>\n<root><link xlink:href=\"abc\" xhtml:class=\"def\"/></root>"
213
236
 
214
237
  writer = LibXML::XML::Writer.string
215
- document writer do
238
+ document(writer) do
216
239
  element writer, 'root' do
217
240
  element writer, 'link' do
218
241
  assert(writer.write_attribute_ns 'xlink', 'href', nil, 'abc')
@@ -223,7 +246,7 @@ class TestWriter < Minitest::Test
223
246
  assert_equal(writer.result.strip!, expected)
224
247
 
225
248
  writer = LibXML::XML::Writer.string
226
- document writer do
249
+ document(writer) do
227
250
  element writer, 'root' do
228
251
  element writer, 'link' do
229
252
  assert(writer.start_attribute_ns 'xlink', 'href')
@@ -242,7 +265,7 @@ class TestWriter < Minitest::Test
242
265
  if LibXML::XML::Writer.method_defined? :set_quote_char
243
266
  writer = LibXML::XML::Writer.string
244
267
  writer.set_quote_char "'"
245
- document writer do
268
+ document(writer) do
246
269
  element writer, 'root' do
247
270
  assert(writer.start_attribute 'abc')
248
271
  assert(writer.write_string 'def')
@@ -257,7 +280,7 @@ class TestWriter < Minitest::Test
257
280
  if LibXML::XML::Writer.method_defined? :set_indent
258
281
  writer = LibXML::XML::Writer.string
259
282
  assert(writer.set_indent true)
260
- document writer do
283
+ document(writer) do
261
284
  element writer, 'root' do
262
285
  element writer, 'child' do
263
286
  assert(writer.start_attribute 'abc')
@@ -275,7 +298,7 @@ class TestWriter < Minitest::Test
275
298
  writer = LibXML::XML::Writer.string
276
299
  assert(writer.set_indent true)
277
300
  assert(writer.set_indent_string ' ' * 4)
278
- document writer do
301
+ document(writer) do
279
302
  element writer, 'root' do
280
303
  element writer, 'child' do
281
304
  assert(writer.start_attribute 'abc')
@@ -395,7 +418,7 @@ class TestWriter < Minitest::Test
395
418
  iso = 'éloïse'.encode 'ISO-8859-1'
396
419
 
397
420
  writer = LibXML::XML::Writer.string
398
- document writer do
421
+ document(writer) do
399
422
  assert(writer.write_element iso)
400
423
  end
401
424
  assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<éloïse/>")