libxml-ruby 2.8.0 → 3.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +5 -5
  2. data/HISTORY +842 -775
  3. data/LICENSE +20 -20
  4. data/MANIFEST +166 -166
  5. data/README.rdoc +217 -184
  6. data/Rakefile +90 -78
  7. data/ext/libxml/extconf.h +3 -0
  8. data/ext/libxml/extconf.rb +61 -116
  9. data/ext/libxml/libxml.c +80 -76
  10. data/ext/libxml/ruby_libxml.h +67 -75
  11. data/ext/libxml/ruby_xml.c +933 -893
  12. data/ext/libxml/ruby_xml.h +10 -10
  13. data/ext/libxml/ruby_xml_attr.c +333 -333
  14. data/ext/libxml/ruby_xml_attr.h +12 -12
  15. data/ext/libxml/ruby_xml_attr_decl.c +153 -153
  16. data/ext/libxml/ruby_xml_attr_decl.h +11 -11
  17. data/ext/libxml/ruby_xml_attributes.c +275 -275
  18. data/ext/libxml/ruby_xml_attributes.h +15 -15
  19. data/ext/libxml/ruby_xml_cbg.c +85 -85
  20. data/ext/libxml/ruby_xml_document.c +1123 -1147
  21. data/ext/libxml/ruby_xml_document.h +11 -11
  22. data/ext/libxml/ruby_xml_dtd.c +248 -268
  23. data/ext/libxml/ruby_xml_dtd.h +9 -9
  24. data/ext/libxml/ruby_xml_encoding.c +250 -260
  25. data/ext/libxml/ruby_xml_encoding.h +16 -19
  26. data/ext/libxml/ruby_xml_error.c +996 -996
  27. data/ext/libxml/ruby_xml_error.h +12 -12
  28. data/ext/libxml/ruby_xml_html_parser.c +89 -92
  29. data/ext/libxml/ruby_xml_html_parser.h +10 -10
  30. data/ext/libxml/ruby_xml_html_parser_context.c +337 -338
  31. data/ext/libxml/ruby_xml_html_parser_context.h +10 -10
  32. data/ext/libxml/ruby_xml_html_parser_options.c +46 -46
  33. data/ext/libxml/ruby_xml_html_parser_options.h +10 -10
  34. data/ext/libxml/ruby_xml_input_cbg.c +191 -191
  35. data/ext/libxml/ruby_xml_input_cbg.h +20 -20
  36. data/ext/libxml/ruby_xml_io.c +47 -50
  37. data/ext/libxml/ruby_xml_io.h +10 -10
  38. data/ext/libxml/ruby_xml_namespace.c +153 -153
  39. data/ext/libxml/ruby_xml_namespace.h +10 -10
  40. data/ext/libxml/ruby_xml_namespaces.c +293 -293
  41. data/ext/libxml/ruby_xml_namespaces.h +9 -9
  42. data/ext/libxml/ruby_xml_node.c +1402 -1452
  43. data/ext/libxml/ruby_xml_node.h +13 -11
  44. data/ext/libxml/ruby_xml_parser.c +91 -94
  45. data/ext/libxml/ruby_xml_parser.h +12 -12
  46. data/ext/libxml/ruby_xml_parser_context.c +999 -1001
  47. data/ext/libxml/ruby_xml_parser_context.h +10 -10
  48. data/ext/libxml/ruby_xml_parser_options.c +66 -66
  49. data/ext/libxml/ruby_xml_parser_options.h +12 -12
  50. data/ext/libxml/ruby_xml_reader.c +1239 -1228
  51. data/ext/libxml/ruby_xml_reader.h +17 -17
  52. data/ext/libxml/ruby_xml_relaxng.c +110 -111
  53. data/ext/libxml/ruby_xml_relaxng.h +10 -10
  54. data/ext/libxml/ruby_xml_sax2_handler.c +326 -328
  55. data/ext/libxml/ruby_xml_sax2_handler.h +10 -10
  56. data/ext/libxml/ruby_xml_sax_parser.c +116 -120
  57. data/ext/libxml/ruby_xml_sax_parser.h +10 -10
  58. data/ext/libxml/ruby_xml_schema.c +278 -301
  59. data/ext/libxml/ruby_xml_schema.h +809 -809
  60. data/ext/libxml/ruby_xml_schema_attribute.c +109 -109
  61. data/ext/libxml/ruby_xml_schema_attribute.h +15 -15
  62. data/ext/libxml/ruby_xml_schema_element.c +95 -94
  63. data/ext/libxml/ruby_xml_schema_element.h +14 -14
  64. data/ext/libxml/ruby_xml_schema_facet.c +52 -52
  65. data/ext/libxml/ruby_xml_schema_facet.h +13 -13
  66. data/ext/libxml/ruby_xml_schema_type.c +232 -259
  67. data/ext/libxml/ruby_xml_schema_type.h +9 -9
  68. data/ext/libxml/ruby_xml_version.h +9 -9
  69. data/ext/libxml/ruby_xml_writer.c +1133 -1137
  70. data/ext/libxml/ruby_xml_writer.h +10 -10
  71. data/ext/libxml/ruby_xml_xinclude.c +16 -16
  72. data/ext/libxml/ruby_xml_xinclude.h +11 -11
  73. data/ext/libxml/ruby_xml_xpath.c +194 -188
  74. data/ext/libxml/ruby_xml_xpath.h +13 -13
  75. data/ext/libxml/ruby_xml_xpath_context.c +360 -361
  76. data/ext/libxml/ruby_xml_xpath_context.h +9 -9
  77. data/ext/libxml/ruby_xml_xpath_expression.c +81 -81
  78. data/ext/libxml/ruby_xml_xpath_expression.h +10 -10
  79. data/ext/libxml/ruby_xml_xpath_object.c +338 -335
  80. data/ext/libxml/ruby_xml_xpath_object.h +17 -17
  81. data/ext/libxml/ruby_xml_xpointer.c +99 -99
  82. data/ext/libxml/ruby_xml_xpointer.h +11 -11
  83. data/ext/vc/libxml_ruby.sln +17 -15
  84. data/lib/libxml-ruby.rb +30 -0
  85. data/lib/libxml.rb +3 -33
  86. data/lib/libxml/node.rb +2 -78
  87. data/lib/libxml/parser.rb +0 -266
  88. data/lib/libxml/sax_parser.rb +0 -17
  89. data/lib/libxml/schema.rb +47 -66
  90. data/lib/libxml/schema/attribute.rb +19 -19
  91. data/lib/libxml/schema/element.rb +19 -27
  92. data/lib/libxml/schema/type.rb +21 -29
  93. data/libxml-ruby.gemspec +48 -44
  94. data/script/benchmark/depixelate +634 -634
  95. data/script/benchmark/hamlet.xml +9054 -9054
  96. data/script/benchmark/parsecount +170 -170
  97. data/script/benchmark/throughput +41 -41
  98. data/script/test +6 -6
  99. data/setup.rb +0 -1
  100. data/test/c14n/given/example-1.xml +14 -14
  101. data/test/c14n/given/example-2.xml +11 -11
  102. data/test/c14n/given/example-3.xml +18 -18
  103. data/test/c14n/given/example-4.xml +9 -9
  104. data/test/c14n/given/example-5.xml +12 -12
  105. data/test/c14n/given/example-6.xml +2 -2
  106. data/test/c14n/given/example-7.xml +11 -11
  107. data/test/c14n/given/example-8.xml +11 -11
  108. data/test/c14n/given/example-8.xpath +9 -9
  109. data/test/c14n/result/1-1-without-comments/example-1 +3 -3
  110. data/test/c14n/result/1-1-without-comments/example-2 +10 -10
  111. data/test/c14n/result/1-1-without-comments/example-3 +13 -13
  112. data/test/c14n/result/1-1-without-comments/example-4 +8 -8
  113. data/test/c14n/result/1-1-without-comments/example-5 +2 -2
  114. data/test/c14n/result/with-comments/example-1 +5 -5
  115. data/test/c14n/result/with-comments/example-2 +10 -10
  116. data/test/c14n/result/with-comments/example-3 +13 -13
  117. data/test/c14n/result/with-comments/example-4 +8 -8
  118. data/test/c14n/result/with-comments/example-5 +3 -3
  119. data/test/c14n/result/without-comments/example-1 +3 -3
  120. data/test/c14n/result/without-comments/example-2 +10 -10
  121. data/test/c14n/result/without-comments/example-3 +13 -13
  122. data/test/c14n/result/without-comments/example-4 +8 -8
  123. data/test/c14n/result/without-comments/example-5 +2 -2
  124. data/test/model/atom.xml +12 -12
  125. data/test/model/bands.iso-8859-1.xml +4 -4
  126. data/test/model/bands.utf-8.xml +4 -4
  127. data/test/model/bands.xml +4 -4
  128. data/test/model/books.xml +153 -153
  129. data/test/model/cwm_1_0.xml +11336 -0
  130. data/test/model/merge_bug_data.xml +58 -58
  131. data/test/model/ruby-lang.html +238 -238
  132. data/test/model/rubynet.xml +79 -79
  133. data/test/model/shiporder.rnc +28 -28
  134. data/test/model/shiporder.rng +86 -86
  135. data/test/model/shiporder.xml +22 -22
  136. data/test/model/shiporder.xsd +39 -39
  137. data/test/model/soap.xml +27 -27
  138. data/test/model/xinclude.xml +4 -4
  139. data/test/test.xml +2 -0
  140. data/test/{tc_attr.rb → test_attr.rb} +23 -25
  141. data/test/{tc_attr_decl.rb → test_attr_decl.rb} +13 -14
  142. data/test/{tc_attributes.rb → test_attributes.rb} +11 -18
  143. data/test/{tc_canonicalize.rb → test_canonicalize.rb} +36 -41
  144. data/test/test_deprecated_require.rb +12 -0
  145. data/test/{tc_document.rb → test_document.rb} +32 -27
  146. data/test/test_document_write.rb +146 -0
  147. data/test/{tc_dtd.rb → test_dtd.rb} +28 -29
  148. data/test/{tc_encoding.rb → test_encoding.rb} +129 -126
  149. data/test/{tc_encoding_sax.rb → test_encoding_sax.rb} +7 -6
  150. data/test/test_error.rb +178 -0
  151. data/test/test_helper.rb +3 -10
  152. data/test/test_html_parser.rb +162 -0
  153. data/test/test_html_parser_context.rb +23 -0
  154. data/test/test_namespace.rb +60 -0
  155. data/test/{tc_namespaces.rb → test_namespaces.rb} +34 -44
  156. data/test/{tc_node.rb → test_node.rb} +68 -47
  157. data/test/{tc_node_cdata.rb → test_node_cdata.rb} +12 -13
  158. data/test/{tc_node_comment.rb → test_node_comment.rb} +7 -8
  159. data/test/{tc_node_copy.rb → test_node_copy.rb} +4 -6
  160. data/test/{tc_node_edit.rb → test_node_edit.rb} +23 -41
  161. data/test/{tc_node_pi.rb → test_node_pi.rb} +37 -40
  162. data/test/{tc_node_text.rb → test_node_text.rb} +10 -12
  163. data/test/{tc_node_write.rb → test_node_write.rb} +18 -29
  164. data/test/test_node_xlink.rb +28 -0
  165. data/test/test_parser.rb +324 -0
  166. data/test/{tc_parser_context.rb → test_parser_context.rb} +41 -42
  167. data/test/{tc_properties.rb → test_properties.rb} +6 -7
  168. data/test/test_reader.rb +363 -0
  169. data/test/test_relaxng.rb +53 -0
  170. data/test/{tc_sax_parser.rb → test_sax_parser.rb} +36 -37
  171. data/test/{tc_schema.rb → test_schema.rb} +43 -37
  172. data/test/test_suite.rb +38 -40
  173. data/test/{tc_traversal.rb → test_traversal.rb} +5 -6
  174. data/test/{tc_writer.rb → test_writer.rb} +468 -448
  175. data/test/{tc_xinclude.rb → test_xinclude.rb} +4 -5
  176. data/test/test_xml.rb +262 -0
  177. data/test/{tc_xpath.rb → test_xpath.rb} +31 -32
  178. data/test/{tc_xpath_context.rb → test_xpath_context.rb} +8 -9
  179. data/test/test_xpath_expression.rb +37 -0
  180. data/test/{tc_xpointer.rb → test_xpointer.rb} +16 -18
  181. metadata +117 -95
  182. data/lib/libxml/ns.rb +0 -22
  183. data/lib/libxml/properties.rb +0 -23
  184. data/lib/libxml/reader.rb +0 -29
  185. data/lib/libxml/xpath_object.rb +0 -16
  186. data/test/etc_doc_to_s.rb +0 -21
  187. data/test/ets_doc_file.rb +0 -17
  188. data/test/ets_doc_to_s.rb +0 -23
  189. data/test/ets_gpx.rb +0 -28
  190. data/test/ets_node_gc.rb +0 -23
  191. data/test/ets_test.xml +0 -2
  192. data/test/ets_tsr.rb +0 -11
  193. data/test/tc_deprecated_require.rb +0 -13
  194. data/test/tc_document_write.rb +0 -196
  195. data/test/tc_error.rb +0 -180
  196. data/test/tc_html_parser.rb +0 -153
  197. data/test/tc_html_parser_context.rb +0 -24
  198. data/test/tc_namespace.rb +0 -62
  199. data/test/tc_node_xlink.rb +0 -29
  200. data/test/tc_parser.rb +0 -381
  201. data/test/tc_reader.rb +0 -400
  202. data/test/tc_relaxng.rb +0 -54
  203. data/test/tc_xml.rb +0 -226
  204. data/test/tc_xpath_expression.rb +0 -38
@@ -1,15 +1,14 @@
1
1
  # encoding: UTF-8
2
2
 
3
- require './test_helper'
4
- require 'test/unit'
3
+ require_relative './test_helper'
5
4
 
6
- class TestTranversal < Test::Unit::TestCase
5
+ class TestTranversal < Minitest::Test
7
6
  ROOT_NODES_LENGTH = 27
8
7
  ROOT_ELEMENTS_LENGTH = 13
9
8
 
10
9
  def setup
11
10
  filename = File.join(File.dirname(__FILE__), 'model/books.xml')
12
- @doc = XML::Document.file(filename)
11
+ @doc = LibXML::XML::Document.file(filename)
13
12
  end
14
13
 
15
14
  def teardown
@@ -144,10 +143,10 @@ class TestTranversal < Test::Unit::TestCase
144
143
  end
145
144
 
146
145
  def test_doc_class
147
- assert_instance_of(XML::Document, @doc)
146
+ assert_instance_of(LibXML::XML::Document, @doc)
148
147
  end
149
148
 
150
149
  def test_root_class
151
- assert_instance_of(XML::Node, @doc.root)
150
+ assert_instance_of(LibXML::XML::Node, @doc.root)
152
151
  end
153
152
  end
@@ -1,448 +1,468 @@
1
- # encoding: UTF-8
2
-
3
- require './test_helper'
4
- require 'stringio'
5
- require 'test/unit'
6
-
7
- class TestWriter < Test::Unit::TestCase
8
- XSL_PREFIX = 'xsl'
9
- XSL_URI = 'http://www.w3.org/1999/XSL/Transform'
10
-
11
- def test_generic_failure
12
- writer = XML::Writer.string
13
- writer.start_document
14
- assert(!writer.end_element)
15
- writer.end_document
16
- end
17
-
18
- def test_empty_doc
19
- writer = XML::Writer.string
20
- document writer
21
- assert_equal(writer.result.strip!, '<?xml version="1.0"?>')
22
-
23
- writer = XML::Writer.string
24
- document writer, { :encoding => XML::Encoding::ISO_8859_1 }
25
- assert_equal(writer.result.strip!, '<?xml version="1.0" encoding="ISO-8859-1"?>')
26
-
27
- writer = XML::Writer.string
28
- document writer, { :standalone => 1 }
29
- assert_equal(writer.result.strip!, '<?xml version="1.0" standalone="yes"?>')
30
-
31
- writer = XML::Writer.string
32
- document writer, { :standalone => 1, :encoding => XML::Encoding::ISO_8859_1, :foo => :bar }
33
- assert_equal(writer.result.strip!, '<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>')
34
- end
35
-
36
- def test_single_root
37
- writer = XML::Writer.string
38
- document writer do
39
- element writer, 'root'
40
- end
41
-
42
- assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root/>")
43
- end
44
-
45
- def test_pi
46
- expected = "<?xml version=\"1.0\"?>\n<?php echo \"foo\";?>"
47
-
48
- writer = XML::Writer.string
49
- document writer do
50
- assert(writer.start_pi('php'))
51
- assert(writer.write_string('echo "foo";'))
52
- assert(writer.end_pi)
53
- end
54
- assert_equal(writer.result.strip!, expected)
55
-
56
- writer = XML::Writer.string
57
- document writer do
58
- assert(writer.write_pi('php', 'echo "foo";'))
59
- end
60
- assert_equal(writer.result.strip!, expected)
61
- end
62
-
63
- def test_comment
64
- expected = "<?xml version=\"1.0\"?>\n<!--foo-->"
65
-
66
- writer = XML::Writer.string
67
- document writer do
68
- assert(writer.start_comment)
69
- assert(writer.write_string 'foo')
70
- assert(writer.end_comment)
71
- end
72
- assert_equal(writer.result.strip!, expected)
73
-
74
- writer = XML::Writer.string
75
- document writer do
76
- assert(writer.write_comment 'foo')
77
- end
78
- assert_equal(writer.result.strip!, expected)
79
- end
80
-
81
- def test_cdata
82
- expected = "<?xml version=\"1.0\"?>\n<root><![CDATA[<?php echo $foo; ?>]]></root>"
83
-
84
- writer = XML::Writer.string
85
- document writer do
86
- element writer, 'root' do
87
- assert(writer.start_cdata)
88
- assert(writer.write_string '<?php echo $foo; ?>')
89
- assert(writer.end_cdata)
90
- end
91
- end
92
- assert_equal(writer.result.strip!, expected)
93
-
94
- writer = XML::Writer.string
95
- document writer do
96
- element writer, 'root' do
97
- assert(writer.write_cdata '<?php echo $foo; ?>')
98
- end
99
- end
100
- assert_equal(writer.result.strip!, expected)
101
- end
102
-
103
- def test_write_empty_elements
104
- writer = XML::Writer.string
105
- document writer do
106
- assert(writer.write_element 'foo')
107
- end
108
- assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<foo/>")
109
-
110
- writer = XML::Writer.string
111
- document writer do
112
- assert(writer.write_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
113
- end
114
- assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<" + XSL_PREFIX + ":stylesheet xmlns:xsl=\"" + XSL_URI + "\"/>")
115
- end
116
-
117
- def test_valued_element
118
- expected = "<?xml version=\"1.0\"?>\n<abc>123<def>456<ghi>789</ghi>cueillir des cerises</def>nous irons au bois</abc>"
119
-
120
- writer = XML::Writer.string
121
- document writer do
122
- assert(writer.start_element 'abc')
123
- assert(writer.write_string '123')
124
- assert(writer.start_element 'def')
125
- assert(writer.write_string '456')
126
- assert(writer.start_element 'ghi')
127
- assert(writer.write_string '789')
128
- assert(writer.end_element)
129
- assert(writer.write_string 'cueillir des cerises')
130
- assert(writer.end_element)
131
- assert(writer.write_string 'nous irons au bois')
132
- assert(writer.end_element)
133
- end
134
- assert_equal(writer.result.strip!, expected)
135
-
136
- writer = XML::Writer.string
137
- document writer do
138
- assert(writer.start_element 'abc')
139
- assert(writer.write_string '123')
140
- assert(writer.start_element 'def')
141
- assert(writer.write_string '456')
142
- assert(writer.write_element 'ghi', '789')
143
- assert(writer.write_string 'cueillir des cerises')
144
- assert(writer.end_element)
145
- assert(writer.write_string 'nous irons au bois')
146
- assert(writer.end_element)
147
- end
148
- assert_equal(writer.result.strip!, expected)
149
- end
150
-
151
- def test_valued_element_ns
152
- expected = "<?xml version=\"1.0\"?>\n" +
153
- "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
154
- "<xsl:attribute-set>" +
155
- "<xsl:attribute>20px</xsl:attribute>" +
156
- "<xsl:attribute>solid</xsl:attribute>" +
157
- "</xsl:attribute-set>" +
158
- "</xsl:stylesheet>"
159
-
160
- writer = XML::Writer.string
161
- document writer do
162
- assert(writer.start_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
163
- assert(writer.start_element_ns XSL_PREFIX, 'attribute-set')
164
- assert(writer.start_element_ns XSL_PREFIX, 'attribute')
165
- assert(writer.write_string '20px')
166
- assert(writer.end_element)
167
- assert(writer.start_element_ns XSL_PREFIX, 'attribute')
168
- assert(writer.write_string 'solid')
169
- assert(writer.end_element)
170
- assert(writer.end_element)
171
- assert(writer.end_element)
172
- end
173
- assert_equal(writer.result.strip!, expected)
174
-
175
- writer = XML::Writer.string
176
- document writer do
177
- assert(writer.start_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
178
- assert(writer.start_element_ns XSL_PREFIX, 'attribute-set')
179
- assert(writer.write_element_ns XSL_PREFIX, 'attribute', nil, '20px')
180
- assert(writer.write_element_ns XSL_PREFIX, 'attribute', nil, 'solid')
181
- assert(writer.end_element)
182
- assert(writer.end_element)
183
- end
184
- assert_equal(writer.result.strip!, expected)
185
- end
186
-
187
- def test_attribute
188
- writer = XML::Writer.string
189
- document writer do
190
- element writer, 'root' do
191
- element writer, 'child' do
192
- assert(writer.start_attribute 'foo')
193
- assert(writer.write_string 'bar')
194
- assert(writer.end_attribute)
195
- end
196
- end
197
- end
198
- assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root><child foo=\"bar\"/></root>")
199
-
200
- writer = XML::Writer.string
201
- document writer do
202
- element writer, 'root' do
203
- element writer, 'child' do
204
- assert(writer.write_attribute 'abc', 'def')
205
- assert(writer.write_string 'ghi') # must be done after attributes
206
- end
207
- end
208
- end
209
- assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root><child abc=\"def\">ghi</child></root>")
210
- end
211
-
212
- def test_attribute_ns
213
- expected = "<?xml version=\"1.0\"?>\n<root><link xlink:href=\"abc\" xhtml:class=\"def\"/></root>"
214
-
215
- writer = XML::Writer.string
216
- document writer do
217
- element writer, 'root' do
218
- element writer, 'link' do
219
- assert(writer.write_attribute_ns 'xlink', 'href', nil, 'abc')
220
- assert(writer.write_attribute_ns 'xhtml', 'class', nil, 'def')
221
- end
222
- end
223
- end
224
- assert_equal(writer.result.strip!, expected)
225
-
226
- writer = XML::Writer.string
227
- document writer do
228
- element writer, 'root' do
229
- element writer, 'link' do
230
- assert(writer.start_attribute_ns 'xlink', 'href')
231
- assert(writer.write_string 'abc')
232
- assert(writer.end_attribute)
233
- assert(writer.start_attribute_ns 'xhtml', 'class')
234
- assert(writer.write_string 'def')
235
- assert(writer.end_attribute)
236
- end
237
- end
238
- end
239
- assert_equal(writer.result.strip!, expected)
240
- end
241
-
242
- def test_quote_char
243
- if XML::Writer.method_defined? :set_quote_char
244
- writer = XML::Writer.string
245
- writer.set_quote_char "'"
246
- document writer do
247
- element writer, 'root' do
248
- assert(writer.start_attribute 'abc')
249
- assert(writer.write_string 'def')
250
- assert(writer.end_attribute)
251
- end
252
- end
253
- assert(writer.result.strip!.end_with? "<root abc='def'/>")
254
- end
255
- end
256
-
257
- def test_indentation_on
258
- if XML::Writer.method_defined? :set_indent
259
- writer = XML::Writer.string
260
- assert(writer.set_indent true)
261
- document writer do
262
- element writer, 'root' do
263
- element writer, 'child' do
264
- assert(writer.start_attribute 'abc')
265
- assert(writer.write_string 'def')
266
- assert(writer.end_attribute)
267
- end
268
- end
269
- end
270
- assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root>\n <child abc=\"def\"/>\n</root>")
271
- end
272
- end
273
-
274
- def test_indentation_string
275
- if XML::Writer.method_defined? :set_indent_string
276
- writer = XML::Writer.string
277
- assert(writer.set_indent true)
278
- assert(writer.set_indent_string ' ' * 4)
279
- document writer do
280
- element writer, 'root' do
281
- element writer, 'child' do
282
- assert(writer.start_attribute 'abc')
283
- assert(writer.write_string 'def')
284
- assert(writer.end_attribute)
285
- end
286
- end
287
- end
288
- assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root>\n <child abc=\"def\"/>\n</root>")
289
- end
290
- end
291
-
292
- def test_dtd_declaration
293
- writer = XML::Writer.string
294
- dtd writer, 'html'
295
- assert_equal(writer.result, '<!DOCTYPE html>')
296
-
297
- writer = XML::Writer.string
298
- dtd writer, 'html', '-//W3C//DTD XHTML 1.0 Strict//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
299
- assert_equal(writer.result, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">')
300
- end
301
-
302
- def test_dtd_attlist
303
- expected = '<!DOCTYPE http [<!ATTLIST method (get|post) "get">]>'
304
-
305
- writer = XML::Writer.string
306
- dtd writer, 'http' do
307
- assert(writer.start_dtd_attlist 'method')
308
- assert(writer.write_string '(get|post) "get"')
309
- assert(writer.end_dtd_attlist)
310
- end
311
- assert_equal(writer.result, expected)
312
-
313
- writer = XML::Writer.string
314
- dtd writer, 'http' do
315
- assert(writer.write_dtd_attlist 'method', '(get|post) "get"')
316
- end
317
- assert_equal(writer.result, expected)
318
- end
319
-
320
- def test_dtd_element
321
- expected = '<!DOCTYPE html [<!ELEMENT dl (dt|dd)+>]>'
322
-
323
- writer = XML::Writer.string
324
- dtd writer, 'html' do
325
- assert(writer.start_dtd_element 'dl')
326
- assert(writer.write_string '(dt|dd)+')
327
- assert(writer.end_dtd_element)
328
- end
329
- assert_equal(writer.result, expected)
330
-
331
- writer = XML::Writer.string
332
- dtd writer, 'html' do
333
- assert(writer.write_dtd_element 'dl', '(dt|dd)+')
334
- end
335
- assert_equal(writer.result, expected)
336
- end
337
-
338
- def test_dtd_entity
339
- # parameterized entity
340
- expected = '<!DOCTYPE html [<!ENTITY % special.pre "br | span | bdo | map"><!ENTITY % special "%special.pre; | object | img">]>'
341
-
342
- writer = XML::Writer.string
343
- dtd writer, 'html' do
344
- assert(writer.start_dtd_entity 'special.pre', true)
345
- assert(writer.write_string 'br | span | bdo | map')
346
- assert(writer.end_dtd_entity)
347
- assert(writer.start_dtd_entity 'special', true)
348
- assert(writer.write_string '%special.pre; | object | img')
349
- assert(writer.end_dtd_entity)
350
- end
351
- assert_equal(writer.result, expected)
352
-
353
- writer = XML::Writer.string
354
- dtd writer, 'html' do
355
- assert(writer.write_dtd_internal_entity 'special.pre', 'br | span | bdo | map', true)
356
- assert(writer.write_dtd_internal_entity 'special', '%special.pre; | object | img', true)
357
- end
358
- assert_equal(writer.result, expected)
359
-
360
- # non parameterized entity
361
- expected = '<!DOCTYPE html [<!ENTITY Alpha "&#913;">]>'
362
-
363
- writer = XML::Writer.string
364
- dtd writer, 'html' do
365
- assert(writer.start_dtd_entity 'Alpha')
366
- assert(writer.write_string '&#913;')
367
- assert(writer.end_dtd_entity)
368
- end
369
- assert_equal(writer.result, expected)
370
-
371
- writer = XML::Writer.string
372
- dtd writer, 'html' do
373
- assert(writer.start_dtd_entity 'Alpha', false)
374
- assert(writer.write_string '&#913;')
375
- assert(writer.end_dtd_entity)
376
- end
377
- assert_equal(writer.result, expected)
378
-
379
- writer = XML::Writer.string
380
- dtd writer, 'html' do
381
- assert(writer.write_dtd_internal_entity 'Alpha', '&#913;', false)
382
- end
383
- assert_equal(writer.result, expected)
384
- end
385
-
386
- def test_dtd_notation
387
- writer = XML::Writer.string
388
- dtd writer, 'pictures' do
389
- assert(writer.write_dtd_notation 'GIF89a', '-//Compuserve//NOTATION Graphics Interchange Format 89a//EN', nil)
390
- assert(writer.write_dtd_external_entity 'pictures', nil, 'images/plage.gif', 'GIF89a', false)
391
- end
392
- assert_equal(writer.result, '<!DOCTYPE pictures [<!NOTATION GIF89a PUBLIC "-//Compuserve//NOTATION Graphics Interchange Format 89a//EN"><!ENTITY pictures SYSTEM "images/plage.gif" NDATA GIF89a>]>')
393
- end
394
-
395
- def test_encoding
396
- if defined?(Encoding)
397
- iso = 'éloïse'.encode 'ISO-8859-1'
398
-
399
- writer = XML::Writer.string
400
- document writer do
401
- assert(writer.write_element iso)
402
- end
403
- assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<éloïse/>")
404
- end
405
- end
406
-
407
- def test_flush
408
- writer = XML::Writer.string
409
- assert(writer.start_document)
410
- assert_equal(writer.flush.strip!, '<?xml version="1.0"?>')
411
- assert(writer.start_element 'foo')
412
- assert(writer.end_element)
413
- assert(writer.end_document)
414
- writer.flush false
415
- assert_equal(writer.result.strip, '<foo/>')
416
- end
417
-
418
- def test_nil_pe_issue
419
- expected = '<!DOCTYPE html [<!ENTITY special.pre "br | span | bdo | map"><!ENTITY special "%special.pre; | object | img">]>'
420
-
421
- writer = XML::Writer.string
422
- dtd writer, 'html' do
423
- assert(writer.write_dtd_internal_entity 'special.pre', 'br | span | bdo | map', nil)
424
- assert(writer.write_dtd_internal_entity 'special', '%special.pre; | object | img', nil)
425
- end
426
- assert_equal(writer.result, expected)
427
- end
428
-
429
- private
430
-
431
- def document(writer, options = {})
432
- assert(writer.start_document options)
433
- yield if block_given?
434
- assert(writer.end_document)
435
- end
436
-
437
- def dtd(writer, name, pubid = nil, sysid = nil)
438
- assert(writer.start_dtd name, pubid, sysid)
439
- yield if block_given?
440
- assert(writer.end_dtd)
441
- end
442
-
443
- def element(writer, localname)
444
- assert(writer.start_element localname)
445
- yield if block_given?
446
- assert(writer.end_element)
447
- end
448
- end
1
+ # encoding: UTF-8
2
+
3
+ require_relative './test_helper'
4
+ require 'stringio'
5
+
6
+ class TestWriter < Minitest::Test
7
+ XSL_PREFIX = 'xsl'
8
+ XSL_URI = 'http://www.w3.org/1999/XSL/Transform'
9
+
10
+ def test_generic_failure
11
+ writer = LibXML::XML::Writer.string
12
+ writer.start_document
13
+ assert(!writer.end_element)
14
+ writer.end_document
15
+ end
16
+
17
+ def test_empty_doc
18
+ writer = LibXML::XML::Writer.string
19
+ document(writer)
20
+ assert_equal(writer.result.strip!, '<?xml version="1.0"?>')
21
+
22
+ writer = LibXML::XML::Writer.string
23
+ document(writer, :encoding => LibXML::XML::Encoding::ISO_8859_1)
24
+ assert_equal(writer.result.strip!, '<?xml version="1.0" encoding="ISO-8859-1"?>')
25
+
26
+ writer = LibXML::XML::Writer.string
27
+ document(writer, :standalone => 1)
28
+ assert_equal(writer.result.strip!, '<?xml version="1.0" standalone="yes"?>')
29
+
30
+ writer = LibXML::XML::Writer.string
31
+ document(writer, :standalone => 1, :encoding => LibXML::XML::Encoding::ISO_8859_1, :foo => :bar)
32
+ assert_equal(writer.result.strip!, '<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>')
33
+ end
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
+
58
+ def test_single_root
59
+ writer = LibXML::XML::Writer.string
60
+ document(writer) do
61
+ element writer, 'root'
62
+ end
63
+
64
+ assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root/>")
65
+ end
66
+
67
+ def test_pi
68
+ expected = "<?xml version=\"1.0\"?>\n<?php echo \"foo\";?>"
69
+
70
+ writer = LibXML::XML::Writer.string
71
+ document(writer) do
72
+ assert(writer.start_pi('php'))
73
+ assert(writer.write_string('echo "foo";'))
74
+ assert(writer.end_pi)
75
+ end
76
+ assert_equal(writer.result.strip!, expected)
77
+
78
+ writer = LibXML::XML::Writer.string
79
+ document(writer) do
80
+ assert(writer.write_pi('php', 'echo "foo";'))
81
+ end
82
+ assert_equal(writer.result.strip!, expected)
83
+ end
84
+
85
+ def test_comment
86
+ expected = "<?xml version=\"1.0\"?>\n<!--foo-->"
87
+
88
+ writer = LibXML::XML::Writer.string
89
+ document(writer) do
90
+ assert(writer.start_comment)
91
+ assert(writer.write_string 'foo')
92
+ assert(writer.end_comment)
93
+ end
94
+ assert_equal(writer.result.strip!, expected)
95
+
96
+ writer = LibXML::XML::Writer.string
97
+ document(writer) do
98
+ assert(writer.write_comment 'foo')
99
+ end
100
+ assert_equal(writer.result.strip!, expected)
101
+ end
102
+
103
+ def test_cdata
104
+ expected = "<?xml version=\"1.0\"?>\n<root><![CDATA[<?php echo $foo; ?>]]></root>"
105
+
106
+ writer = LibXML::XML::Writer.string
107
+ document(writer) do
108
+ element writer, 'root' do
109
+ assert(writer.start_cdata)
110
+ assert(writer.write_string '<?php echo $foo; ?>')
111
+ assert(writer.end_cdata)
112
+ end
113
+ end
114
+ assert_equal(writer.result.strip!, expected)
115
+
116
+ writer = LibXML::XML::Writer.string
117
+ document(writer) do
118
+ element writer, 'root' do
119
+ assert(writer.write_cdata '<?php echo $foo; ?>')
120
+ end
121
+ end
122
+ assert_equal(writer.result.strip!, expected)
123
+ end
124
+
125
+ def test_write_empty_elements
126
+ writer = LibXML::XML::Writer.string
127
+ document(writer) do
128
+ assert(writer.write_element 'foo')
129
+ end
130
+ assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<foo/>")
131
+
132
+ writer = LibXML::XML::Writer.string
133
+ document(writer) do
134
+ assert(writer.write_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
135
+ end
136
+ assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<" + XSL_PREFIX + ":stylesheet xmlns:xsl=\"" + XSL_URI + "\"/>")
137
+ end
138
+
139
+ def test_valued_element
140
+ expected = "<?xml version=\"1.0\"?>\n<abc>123<def>456<ghi>789</ghi>cueillir des cerises</def>nous irons au bois</abc>"
141
+
142
+ writer = LibXML::XML::Writer.string
143
+ document(writer) do
144
+ assert(writer.start_element 'abc')
145
+ assert(writer.write_string '123')
146
+ assert(writer.start_element 'def')
147
+ assert(writer.write_string '456')
148
+ assert(writer.start_element 'ghi')
149
+ assert(writer.write_string '789')
150
+ assert(writer.end_element)
151
+ assert(writer.write_string 'cueillir des cerises')
152
+ assert(writer.end_element)
153
+ assert(writer.write_string 'nous irons au bois')
154
+ assert(writer.end_element)
155
+ end
156
+ assert_equal(writer.result.strip!, expected)
157
+
158
+ writer = LibXML::XML::Writer.string
159
+ document(writer) do
160
+ assert(writer.start_element 'abc')
161
+ assert(writer.write_string '123')
162
+ assert(writer.start_element 'def')
163
+ assert(writer.write_string '456')
164
+ assert(writer.write_element 'ghi', '789')
165
+ assert(writer.write_string 'cueillir des cerises')
166
+ assert(writer.end_element)
167
+ assert(writer.write_string 'nous irons au bois')
168
+ assert(writer.end_element)
169
+ end
170
+ assert_equal(writer.result.strip!, expected)
171
+ end
172
+
173
+ def test_valued_element_ns
174
+ expected = "<?xml version=\"1.0\"?>\n" +
175
+ "<xsl:stylesheet xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">" +
176
+ "<xsl:attribute-set>" +
177
+ "<xsl:attribute>20px</xsl:attribute>" +
178
+ "<xsl:attribute>solid</xsl:attribute>" +
179
+ "</xsl:attribute-set>" +
180
+ "</xsl:stylesheet>"
181
+
182
+ writer = LibXML::XML::Writer.string
183
+ document(writer) do
184
+ assert(writer.start_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
185
+ assert(writer.start_element_ns XSL_PREFIX, 'attribute-set')
186
+ assert(writer.start_element_ns XSL_PREFIX, 'attribute')
187
+ assert(writer.write_string '20px')
188
+ assert(writer.end_element)
189
+ assert(writer.start_element_ns XSL_PREFIX, 'attribute')
190
+ assert(writer.write_string 'solid')
191
+ assert(writer.end_element)
192
+ assert(writer.end_element)
193
+ assert(writer.end_element)
194
+ end
195
+ assert_equal(writer.result.strip!, expected)
196
+
197
+ writer = LibXML::XML::Writer.string
198
+ document(writer) do
199
+ assert(writer.start_element_ns XSL_PREFIX, 'stylesheet', XSL_URI)
200
+ assert(writer.start_element_ns XSL_PREFIX, 'attribute-set')
201
+ assert(writer.write_element_ns XSL_PREFIX, 'attribute', nil, '20px')
202
+ assert(writer.write_element_ns XSL_PREFIX, 'attribute', nil, 'solid')
203
+ assert(writer.end_element)
204
+ assert(writer.end_element)
205
+ end
206
+ assert_equal(writer.result.strip!, expected)
207
+ end
208
+
209
+ def test_attribute
210
+ writer = LibXML::XML::Writer.string
211
+ document(writer) do
212
+ element writer, 'root' do
213
+ element writer, 'child' do
214
+ assert(writer.start_attribute 'foo')
215
+ assert(writer.write_string 'bar')
216
+ assert(writer.end_attribute)
217
+ end
218
+ end
219
+ end
220
+ assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root><child foo=\"bar\"/></root>")
221
+
222
+ writer = LibXML::XML::Writer.string
223
+ document(writer) do
224
+ element writer, 'root' do
225
+ element writer, 'child' do
226
+ assert(writer.write_attribute 'abc', 'def')
227
+ assert(writer.write_string 'ghi') # must be done after attributes
228
+ end
229
+ end
230
+ end
231
+ assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root><child abc=\"def\">ghi</child></root>")
232
+ end
233
+
234
+ def test_attribute_ns
235
+ expected = "<?xml version=\"1.0\"?>\n<root><link xlink:href=\"abc\" xhtml:class=\"def\"/></root>"
236
+
237
+ writer = LibXML::XML::Writer.string
238
+ document(writer) do
239
+ element writer, 'root' do
240
+ element writer, 'link' do
241
+ assert(writer.write_attribute_ns 'xlink', 'href', nil, 'abc')
242
+ assert(writer.write_attribute_ns 'xhtml', 'class', nil, 'def')
243
+ end
244
+ end
245
+ end
246
+ assert_equal(writer.result.strip!, expected)
247
+
248
+ writer = LibXML::XML::Writer.string
249
+ document(writer) do
250
+ element writer, 'root' do
251
+ element writer, 'link' do
252
+ assert(writer.start_attribute_ns 'xlink', 'href')
253
+ assert(writer.write_string 'abc')
254
+ assert(writer.end_attribute)
255
+ assert(writer.start_attribute_ns 'xhtml', 'class')
256
+ assert(writer.write_string 'def')
257
+ assert(writer.end_attribute)
258
+ end
259
+ end
260
+ end
261
+ assert_equal(writer.result.strip!, expected)
262
+ end
263
+
264
+ def test_quote_char
265
+ if LibXML::XML::Writer.method_defined? :set_quote_char
266
+ writer = LibXML::XML::Writer.string
267
+ writer.set_quote_char "'"
268
+ document(writer) do
269
+ element writer, 'root' do
270
+ assert(writer.start_attribute 'abc')
271
+ assert(writer.write_string 'def')
272
+ assert(writer.end_attribute)
273
+ end
274
+ end
275
+ assert(writer.result.strip!.end_with? "<root abc='def'/>")
276
+ end
277
+ end
278
+
279
+ def test_indentation_on
280
+ if LibXML::XML::Writer.method_defined? :set_indent
281
+ writer = LibXML::XML::Writer.string
282
+ assert(writer.set_indent true)
283
+ document(writer) do
284
+ element writer, 'root' do
285
+ element writer, 'child' do
286
+ assert(writer.start_attribute 'abc')
287
+ assert(writer.write_string 'def')
288
+ assert(writer.end_attribute)
289
+ end
290
+ end
291
+ end
292
+ assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root>\n <child abc=\"def\"/>\n</root>")
293
+ end
294
+ end
295
+
296
+ def test_indentation_string
297
+ if LibXML::XML::Writer.method_defined? :set_indent_string
298
+ writer = LibXML::XML::Writer.string
299
+ assert(writer.set_indent true)
300
+ assert(writer.set_indent_string ' ' * 4)
301
+ document(writer) do
302
+ element writer, 'root' do
303
+ element writer, 'child' do
304
+ assert(writer.start_attribute 'abc')
305
+ assert(writer.write_string 'def')
306
+ assert(writer.end_attribute)
307
+ end
308
+ end
309
+ end
310
+ assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<root>\n <child abc=\"def\"/>\n</root>")
311
+ end
312
+ end
313
+
314
+ def test_dtd_declaration
315
+ writer = LibXML::XML::Writer.string
316
+ dtd writer, 'html'
317
+ assert_equal(writer.result, '<!DOCTYPE html>')
318
+
319
+ writer = LibXML::XML::Writer.string
320
+ dtd writer, 'html', '-//W3C//DTD XHTML 1.0 Strict//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd'
321
+ assert_equal(writer.result, '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">')
322
+ end
323
+
324
+ def test_dtd_attlist
325
+ expected = '<!DOCTYPE http [<!ATTLIST method (get|post) "get">]>'
326
+
327
+ writer = LibXML::XML::Writer.string
328
+ dtd writer, 'http' do
329
+ assert(writer.start_dtd_attlist 'method')
330
+ assert(writer.write_string '(get|post) "get"')
331
+ assert(writer.end_dtd_attlist)
332
+ end
333
+ assert_equal(writer.result, expected)
334
+
335
+ writer = LibXML::XML::Writer.string
336
+ dtd writer, 'http' do
337
+ assert(writer.write_dtd_attlist 'method', '(get|post) "get"')
338
+ end
339
+ assert_equal(writer.result, expected)
340
+ end
341
+
342
+ def test_dtd_element
343
+ expected = '<!DOCTYPE html [<!ELEMENT dl (dt|dd)+>]>'
344
+
345
+ writer = LibXML::XML::Writer.string
346
+ dtd writer, 'html' do
347
+ assert(writer.start_dtd_element 'dl')
348
+ assert(writer.write_string '(dt|dd)+')
349
+ assert(writer.end_dtd_element)
350
+ end
351
+ assert_equal(writer.result, expected)
352
+
353
+ writer = LibXML::XML::Writer.string
354
+ dtd writer, 'html' do
355
+ assert(writer.write_dtd_element 'dl', '(dt|dd)+')
356
+ end
357
+ assert_equal(writer.result, expected)
358
+ end
359
+
360
+ def test_dtd_entity
361
+ # parameterized entity
362
+ expected = '<!DOCTYPE html [<!ENTITY % special.pre "br | span | bdo | map"><!ENTITY % special "%special.pre; | object | img">]>'
363
+
364
+ writer = LibXML::XML::Writer.string
365
+ dtd writer, 'html' do
366
+ assert(writer.start_dtd_entity 'special.pre', true)
367
+ assert(writer.write_string 'br | span | bdo | map')
368
+ assert(writer.end_dtd_entity)
369
+ assert(writer.start_dtd_entity 'special', true)
370
+ assert(writer.write_string '%special.pre; | object | img')
371
+ assert(writer.end_dtd_entity)
372
+ end
373
+ assert_equal(writer.result, expected)
374
+
375
+ writer = LibXML::XML::Writer.string
376
+ dtd writer, 'html' do
377
+ assert(writer.write_dtd_internal_entity 'special.pre', 'br | span | bdo | map', true)
378
+ assert(writer.write_dtd_internal_entity 'special', '%special.pre; | object | img', true)
379
+ end
380
+ assert_equal(writer.result, expected)
381
+
382
+ # non parameterized entity
383
+ expected = '<!DOCTYPE html [<!ENTITY Alpha "&#913;">]>'
384
+
385
+ writer = LibXML::XML::Writer.string
386
+ dtd writer, 'html' do
387
+ assert(writer.start_dtd_entity 'Alpha')
388
+ assert(writer.write_string '&#913;')
389
+ assert(writer.end_dtd_entity)
390
+ end
391
+ assert_equal(writer.result, expected)
392
+
393
+ writer = LibXML::XML::Writer.string
394
+ dtd writer, 'html' do
395
+ assert(writer.start_dtd_entity 'Alpha', false)
396
+ assert(writer.write_string '&#913;')
397
+ assert(writer.end_dtd_entity)
398
+ end
399
+ assert_equal(writer.result, expected)
400
+
401
+ writer = LibXML::XML::Writer.string
402
+ dtd writer, 'html' do
403
+ assert(writer.write_dtd_internal_entity 'Alpha', '&#913;', false)
404
+ end
405
+ assert_equal(writer.result, expected)
406
+ end
407
+
408
+ def test_dtd_notation
409
+ writer = LibXML::XML::Writer.string
410
+ dtd writer, 'pictures' do
411
+ assert(writer.write_dtd_notation 'GIF89a', '-//Compuserve//NOTATION Graphics Interchange Format 89a//EN', nil)
412
+ assert(writer.write_dtd_external_entity 'pictures', nil, 'images/plage.gif', 'GIF89a', false)
413
+ end
414
+ assert_equal(writer.result, '<!DOCTYPE pictures [<!NOTATION GIF89a PUBLIC "-//Compuserve//NOTATION Graphics Interchange Format 89a//EN"><!ENTITY pictures SYSTEM "images/plage.gif" NDATA GIF89a>]>')
415
+ end
416
+
417
+ def test_encoding
418
+ iso = 'éloïse'.encode 'ISO-8859-1'
419
+
420
+ writer = LibXML::XML::Writer.string
421
+ document(writer) do
422
+ assert(writer.write_element iso)
423
+ end
424
+ assert_equal(writer.result.strip!, "<?xml version=\"1.0\"?>\n<éloïse/>")
425
+ end
426
+
427
+ def test_flush
428
+ writer = LibXML::XML::Writer.string
429
+ assert(writer.start_document)
430
+ assert_equal(writer.flush.strip!, '<?xml version="1.0"?>')
431
+ assert(writer.start_element 'foo')
432
+ assert(writer.end_element)
433
+ assert(writer.end_document)
434
+ writer.flush false
435
+ assert_equal(writer.result.strip, '<foo/>')
436
+ end
437
+
438
+ def test_nil_pe_issue
439
+ expected = '<!DOCTYPE html [<!ENTITY special.pre "br | span | bdo | map"><!ENTITY special "%special.pre; | object | img">]>'
440
+
441
+ writer = LibXML::XML::Writer.string
442
+ dtd writer, 'html' do
443
+ assert(writer.write_dtd_internal_entity 'special.pre', 'br | span | bdo | map', nil)
444
+ assert(writer.write_dtd_internal_entity 'special', '%special.pre; | object | img', nil)
445
+ end
446
+ assert_equal(writer.result, expected)
447
+ end
448
+
449
+ private
450
+
451
+ def document(writer, options = {})
452
+ assert(writer.start_document options)
453
+ yield if block_given?
454
+ assert(writer.end_document)
455
+ end
456
+
457
+ def dtd(writer, name, pubid = nil, sysid = nil)
458
+ assert(writer.start_dtd name, pubid, sysid)
459
+ yield if block_given?
460
+ assert(writer.end_dtd)
461
+ end
462
+
463
+ def element(writer, localname)
464
+ assert(writer.start_element localname)
465
+ yield if block_given?
466
+ assert(writer.end_element)
467
+ end
468
+ end