libxml-ruby 2.0.0-x86-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (163) hide show
  1. data/HISTORY +516 -0
  2. data/LICENSE +23 -0
  3. data/MANIFEST +165 -0
  4. data/README.rdoc +161 -0
  5. data/Rakefile +82 -0
  6. data/ext/libxml/extconf.rb +122 -0
  7. data/ext/libxml/libxml.c +93 -0
  8. data/ext/libxml/ruby_libxml.h +101 -0
  9. data/ext/libxml/ruby_xml.c +893 -0
  10. data/ext/libxml/ruby_xml.h +10 -0
  11. data/ext/libxml/ruby_xml_attr.c +352 -0
  12. data/ext/libxml/ruby_xml_attr.h +14 -0
  13. data/ext/libxml/ruby_xml_attr_decl.c +171 -0
  14. data/ext/libxml/ruby_xml_attr_decl.h +13 -0
  15. data/ext/libxml/ruby_xml_attributes.c +277 -0
  16. data/ext/libxml/ruby_xml_attributes.h +17 -0
  17. data/ext/libxml/ruby_xml_cbg.c +85 -0
  18. data/ext/libxml/ruby_xml_document.c +958 -0
  19. data/ext/libxml/ruby_xml_document.h +17 -0
  20. data/ext/libxml/ruby_xml_dtd.c +257 -0
  21. data/ext/libxml/ruby_xml_dtd.h +9 -0
  22. data/ext/libxml/ruby_xml_encoding.c +221 -0
  23. data/ext/libxml/ruby_xml_encoding.h +16 -0
  24. data/ext/libxml/ruby_xml_error.c +1004 -0
  25. data/ext/libxml/ruby_xml_error.h +14 -0
  26. data/ext/libxml/ruby_xml_html_parser.c +92 -0
  27. data/ext/libxml/ruby_xml_html_parser.h +12 -0
  28. data/ext/libxml/ruby_xml_html_parser_context.c +308 -0
  29. data/ext/libxml/ruby_xml_html_parser_context.h +12 -0
  30. data/ext/libxml/ruby_xml_html_parser_options.c +40 -0
  31. data/ext/libxml/ruby_xml_html_parser_options.h +12 -0
  32. data/ext/libxml/ruby_xml_input_cbg.c +191 -0
  33. data/ext/libxml/ruby_xml_input_cbg.h +20 -0
  34. data/ext/libxml/ruby_xml_io.c +30 -0
  35. data/ext/libxml/ruby_xml_io.h +9 -0
  36. data/ext/libxml/ruby_xml_namespace.c +170 -0
  37. data/ext/libxml/ruby_xml_namespace.h +12 -0
  38. data/ext/libxml/ruby_xml_namespaces.c +295 -0
  39. data/ext/libxml/ruby_xml_namespaces.h +11 -0
  40. data/ext/libxml/ruby_xml_node.c +1386 -0
  41. data/ext/libxml/ruby_xml_node.h +13 -0
  42. data/ext/libxml/ruby_xml_parser.c +94 -0
  43. data/ext/libxml/ruby_xml_parser.h +14 -0
  44. data/ext/libxml/ruby_xml_parser_context.c +982 -0
  45. data/ext/libxml/ruby_xml_parser_context.h +12 -0
  46. data/ext/libxml/ruby_xml_parser_options.c +68 -0
  47. data/ext/libxml/ruby_xml_parser_options.h +14 -0
  48. data/ext/libxml/ruby_xml_reader.c +1057 -0
  49. data/ext/libxml/ruby_xml_reader.h +14 -0
  50. data/ext/libxml/ruby_xml_relaxng.c +111 -0
  51. data/ext/libxml/ruby_xml_relaxng.h +10 -0
  52. data/ext/libxml/ruby_xml_sax2_handler.c +334 -0
  53. data/ext/libxml/ruby_xml_sax2_handler.h +12 -0
  54. data/ext/libxml/ruby_xml_sax_parser.c +136 -0
  55. data/ext/libxml/ruby_xml_sax_parser.h +12 -0
  56. data/ext/libxml/ruby_xml_schema.c +159 -0
  57. data/ext/libxml/ruby_xml_schema.h +11 -0
  58. data/ext/libxml/ruby_xml_version.h +9 -0
  59. data/ext/libxml/ruby_xml_xinclude.c +18 -0
  60. data/ext/libxml/ruby_xml_xinclude.h +13 -0
  61. data/ext/libxml/ruby_xml_xpath.c +107 -0
  62. data/ext/libxml/ruby_xml_xpath.h +12 -0
  63. data/ext/libxml/ruby_xml_xpath_context.c +390 -0
  64. data/ext/libxml/ruby_xml_xpath_context.h +11 -0
  65. data/ext/libxml/ruby_xml_xpath_expression.c +83 -0
  66. data/ext/libxml/ruby_xml_xpath_expression.h +12 -0
  67. data/ext/libxml/ruby_xml_xpath_object.c +336 -0
  68. data/ext/libxml/ruby_xml_xpath_object.h +19 -0
  69. data/ext/libxml/ruby_xml_xpointer.c +101 -0
  70. data/ext/libxml/ruby_xml_xpointer.h +13 -0
  71. data/ext/mingw/Rakefile +34 -0
  72. data/ext/mingw/build.rake +41 -0
  73. data/ext/vc/libxml_ruby.sln +26 -0
  74. data/lib/1.8/libxml_ruby.so +0 -0
  75. data/lib/1.9/libxml_ruby.so +0 -0
  76. data/lib/libxml.rb +30 -0
  77. data/lib/libxml/attr.rb +113 -0
  78. data/lib/libxml/attr_decl.rb +80 -0
  79. data/lib/libxml/attributes.rb +14 -0
  80. data/lib/libxml/document.rb +192 -0
  81. data/lib/libxml/error.rb +90 -0
  82. data/lib/libxml/hpricot.rb +78 -0
  83. data/lib/libxml/html_parser.rb +96 -0
  84. data/lib/libxml/namespace.rb +62 -0
  85. data/lib/libxml/namespaces.rb +38 -0
  86. data/lib/libxml/node.rb +399 -0
  87. data/lib/libxml/ns.rb +22 -0
  88. data/lib/libxml/parser.rb +367 -0
  89. data/lib/libxml/properties.rb +23 -0
  90. data/lib/libxml/reader.rb +29 -0
  91. data/lib/libxml/sax_callbacks.rb +180 -0
  92. data/lib/libxml/sax_parser.rb +58 -0
  93. data/lib/libxml/tree.rb +29 -0
  94. data/lib/libxml/xpath_object.rb +16 -0
  95. data/lib/xml.rb +16 -0
  96. data/lib/xml/libxml.rb +10 -0
  97. data/libxml-ruby.gemspec +50 -0
  98. data/script/benchmark/depixelate +634 -0
  99. data/script/benchmark/hamlet.xml +9055 -0
  100. data/script/benchmark/parsecount +170 -0
  101. data/script/benchmark/sock_entries.xml +507 -0
  102. data/script/benchmark/throughput +41 -0
  103. data/script/test +6 -0
  104. data/setup.rb +1585 -0
  105. data/test/etc_doc_to_s.rb +21 -0
  106. data/test/ets_doc_file.rb +17 -0
  107. data/test/ets_doc_to_s.rb +23 -0
  108. data/test/ets_gpx.rb +28 -0
  109. data/test/ets_node_gc.rb +23 -0
  110. data/test/ets_test.xml +2 -0
  111. data/test/ets_tsr.rb +11 -0
  112. data/test/model/atom.xml +13 -0
  113. data/test/model/bands.iso-8859-1.xml +5 -0
  114. data/test/model/bands.utf-8.xml +5 -0
  115. data/test/model/bands.xml +5 -0
  116. data/test/model/books.xml +146 -0
  117. data/test/model/merge_bug_data.xml +58 -0
  118. data/test/model/ruby-lang.html +238 -0
  119. data/test/model/rubynet.xml +79 -0
  120. data/test/model/rubynet_project +1 -0
  121. data/test/model/shiporder.rnc +28 -0
  122. data/test/model/shiporder.rng +86 -0
  123. data/test/model/shiporder.xml +23 -0
  124. data/test/model/shiporder.xsd +31 -0
  125. data/test/model/soap.xml +27 -0
  126. data/test/model/xinclude.xml +5 -0
  127. data/test/rb-magic-comment.rb +33 -0
  128. data/test/tc_attr.rb +181 -0
  129. data/test/tc_attr_decl.rb +133 -0
  130. data/test/tc_attributes.rb +135 -0
  131. data/test/tc_deprecated_require.rb +13 -0
  132. data/test/tc_document.rb +119 -0
  133. data/test/tc_document_write.rb +187 -0
  134. data/test/tc_dtd.rb +125 -0
  135. data/test/tc_error.rb +138 -0
  136. data/test/tc_html_parser.rb +140 -0
  137. data/test/tc_namespace.rb +62 -0
  138. data/test/tc_namespaces.rb +177 -0
  139. data/test/tc_node.rb +258 -0
  140. data/test/tc_node_cdata.rb +51 -0
  141. data/test/tc_node_comment.rb +33 -0
  142. data/test/tc_node_copy.rb +42 -0
  143. data/test/tc_node_edit.rb +160 -0
  144. data/test/tc_node_text.rb +71 -0
  145. data/test/tc_node_write.rb +108 -0
  146. data/test/tc_node_xlink.rb +29 -0
  147. data/test/tc_parser.rb +336 -0
  148. data/test/tc_parser_context.rb +189 -0
  149. data/test/tc_properties.rb +39 -0
  150. data/test/tc_reader.rb +298 -0
  151. data/test/tc_relaxng.rb +54 -0
  152. data/test/tc_sax_parser.rb +276 -0
  153. data/test/tc_schema.rb +53 -0
  154. data/test/tc_traversal.rb +222 -0
  155. data/test/tc_xinclude.rb +21 -0
  156. data/test/tc_xml.rb +226 -0
  157. data/test/tc_xpath.rb +195 -0
  158. data/test/tc_xpath_context.rb +80 -0
  159. data/test/tc_xpath_expression.rb +38 -0
  160. data/test/tc_xpointer.rb +74 -0
  161. data/test/test_helper.rb +14 -0
  162. data/test/test_suite.rb +39 -0
  163. metadata +254 -0
@@ -0,0 +1,39 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+ require 'test/unit'
6
+
7
+ # attributes is deprecated - use attributes instead.
8
+ # Tests for backwards compatibility
9
+
10
+ class Testattributes < Test::Unit::TestCase
11
+ def setup()
12
+ xp = XML::Parser.string('<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
13
+ @doc = xp.parse
14
+ end
15
+
16
+ def teardown()
17
+ @doc = nil
18
+ end
19
+
20
+ def test_traversal
21
+ attributes = @doc.root.attributes
22
+
23
+ assert_instance_of(XML::Attributes, attributes)
24
+ attribute = attributes.first
25
+ assert_equal('uga', attribute.name)
26
+ assert_equal('booga', attribute.value)
27
+
28
+ attribute = attribute.next
29
+ assert_instance_of(XML::Attr, attribute)
30
+ assert_equal('foo', attribute.name)
31
+ assert_equal('bar', attribute.value)
32
+ end
33
+
34
+ def test_no_attributes
35
+ attributes = @doc.root.child.attributes
36
+ assert_instance_of(XML::Attributes, attributes)
37
+ assert_equal(0, attributes.length)
38
+ end
39
+ end
@@ -0,0 +1,298 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'stringio'
5
+ require 'test/unit'
6
+
7
+ class TestReader < Test::Unit::TestCase
8
+ XML_FILE = File.join(File.dirname(__FILE__), 'model/atom.xml')
9
+
10
+ def verify_simple(reader)
11
+ node_types = []
12
+
13
+ # Read each node
14
+ 26.times do
15
+ assert(reader.read)
16
+ node_types << reader.node_type
17
+ end
18
+
19
+ # There are no more nodes
20
+ assert(!reader.read)
21
+
22
+ # Check what was read
23
+ expected = [XML::Reader::TYPE_PROCESSING_INSTRUCTION,
24
+ XML::Reader::TYPE_ELEMENT,
25
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
26
+ XML::Reader::TYPE_COMMENT,
27
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
28
+ XML::Reader::TYPE_ELEMENT,
29
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
30
+ XML::Reader::TYPE_ELEMENT,
31
+ XML::Reader::TYPE_CDATA,
32
+ XML::Reader::TYPE_END_ELEMENT,
33
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
34
+ XML::Reader::TYPE_ELEMENT,
35
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
36
+ XML::Reader::TYPE_ELEMENT,
37
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
38
+ XML::Reader::TYPE_ELEMENT,
39
+ XML::Reader::TYPE_TEXT,
40
+ XML::Reader::TYPE_END_ELEMENT,
41
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
42
+ XML::Reader::TYPE_END_ELEMENT,
43
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
44
+ XML::Reader::TYPE_END_ELEMENT,
45
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
46
+ XML::Reader::TYPE_END_ELEMENT,
47
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
48
+ XML::Reader::TYPE_END_ELEMENT]
49
+
50
+ assert_equal(expected, node_types)
51
+ end
52
+
53
+ def test_document
54
+ reader = XML::Reader.document(XML::Document.file(XML_FILE))
55
+ verify_simple(reader)
56
+ end
57
+
58
+ def test_file
59
+ reader = XML::Reader.file(XML_FILE)
60
+ verify_simple(reader)
61
+ end
62
+
63
+ def test_invalid_file
64
+ assert_raise(XML::Error) do
65
+ XML::Reader.file('/does/not/exist')
66
+ end
67
+ end
68
+
69
+ def test_string
70
+ reader = XML::Reader.string(File.read(XML_FILE))
71
+ verify_simple(reader)
72
+ end
73
+
74
+ def test_io
75
+ File.open(XML_FILE, 'rb') do |io|
76
+ reader = XML::Reader.io(io)
77
+ verify_simple(reader)
78
+ end
79
+ end
80
+
81
+ def test_io_gc
82
+ # Test that the reader keeps a reference
83
+ # to the io object
84
+ file = File.open(XML_FILE, 'rb')
85
+ reader = XML::Reader.io(file)
86
+ file = nil
87
+ GC.start
88
+ assert(reader.read)
89
+ end
90
+
91
+ def test_string_io
92
+ data = File.read(XML_FILE)
93
+ string_io = StringIO.new(data)
94
+ reader = XML::Reader.io(string_io)
95
+ verify_simple(reader)
96
+ end
97
+
98
+ def test_error
99
+ reader = XML::Reader.string('<foo blah')
100
+
101
+ error = assert_raise(XML::Error) do
102
+ reader.read
103
+ end
104
+ assert_equal("Fatal error: Couldn't find end of Start Tag foo at :1.", error.to_s)
105
+ end
106
+
107
+ def test_deprecated_error_handler
108
+ called = false
109
+ reader = XML::Reader.string('<foo blah')
110
+ reader.set_error_handler do |error|
111
+ called = true
112
+ end
113
+
114
+ assert_raise(XML::Error) do
115
+ reader.read
116
+ end
117
+
118
+ assert(called)
119
+ end
120
+
121
+ def test_deprecated_reset_error_handler
122
+ called = false
123
+ reader = XML::Reader.string('<foo blah')
124
+ reader.set_error_handler do |error|
125
+ called = true
126
+ end
127
+ reader.reset_error_handler
128
+
129
+ assert_raise(XML::Error) do
130
+ reader.read
131
+ end
132
+
133
+ assert(!called)
134
+ end
135
+
136
+ def test_attr
137
+ parser = XML::Reader.string("<foo x='1' y='2'/>")
138
+ assert(parser.read)
139
+ assert_equal('foo', parser.name)
140
+ assert_equal('1', parser['x'])
141
+ assert_equal('1', parser[0])
142
+ assert_equal('2', parser['y'])
143
+ assert_equal('2', parser[1])
144
+ assert_equal(nil, parser['z'])
145
+ assert_equal(nil, parser[2])
146
+ end
147
+
148
+ def test_value
149
+ parser = XML::Reader.string("<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>")
150
+ assert(parser.read)
151
+ assert_equal('foo', parser.name)
152
+ assert_equal(nil, parser.value)
153
+ 3.times do |i|
154
+ assert(parser.read)
155
+ assert_equal(XML::Reader::TYPE_ELEMENT, parser.node_type)
156
+ assert_equal('bar', parser.name)
157
+ assert(parser.read)
158
+ assert_equal(XML::Reader::TYPE_TEXT, parser.node_type)
159
+ assert_equal((i + 1).to_s, parser.value)
160
+ assert(parser.read)
161
+ assert_equal(XML::Reader::TYPE_END_ELEMENT, parser.node_type)
162
+ end
163
+ end
164
+
165
+ def test_expand
166
+ reader = XML::Reader.file(XML_FILE)
167
+ reader.read
168
+ node = reader.expand
169
+ assert_nil(node.doc)
170
+ reader.close
171
+ GC.start
172
+ end
173
+
174
+ def test_mode
175
+ reader = XML::Reader.string('<xml/>')
176
+ assert_equal(XML::Reader::MODE_INITIAL, reader.read_state)
177
+ reader.read
178
+ assert_equal(XML::Reader::MODE_EOF, reader.read_state)
179
+ end
180
+
181
+ def test_bytes_consumed
182
+ reader = XML::Reader.file(XML_FILE)
183
+ reader.read
184
+ assert_equal(416, reader.byte_consumed)
185
+ end
186
+
187
+ def test_node
188
+ XML.default_line_numbers = true
189
+ reader = XML::Reader.file(XML_FILE)
190
+
191
+ # first try to get a node
192
+ assert_nil(reader.node)
193
+
194
+ reader.read
195
+ assert_instance_of(XML::Node, reader.node)
196
+ end
197
+
198
+ def test_base_uri
199
+ # UTF8:
200
+ # ö - c3 b6 in hex, \303\266 in octal
201
+ # ü - c3 bc in hex, \303\274 in octal
202
+ xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?><bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
203
+ reader = XML::Reader.string(xml, :base_uri => "http://libxml.rubyforge.org")
204
+
205
+ reader.read
206
+ assert_equal(reader.base_uri, "http://libxml.rubyforge.org")
207
+ assert_equal(::Encoding::UTF_8, reader.base_uri.encoding) if defined?(::Encoding)
208
+ end
209
+
210
+ def test_options
211
+ xml = <<-EOS
212
+ <!DOCTYPE foo [<!ENTITY foo 'bar'>]>
213
+ <test>
214
+ <cdata><![CDATA[something]]></cdata>
215
+ <entity>&foo;</entity>
216
+ </test>
217
+ EOS
218
+
219
+ # Parse normally
220
+ reader = XML::Reader.string(xml)
221
+ reader.read # foo
222
+ reader.read # test
223
+ reader.read # text
224
+ reader.read # cdata
225
+ reader.read # cdata-section
226
+ assert_equal(XML::Node::CDATA_SECTION_NODE, reader.node_type)
227
+
228
+ # Convert cdata section to text
229
+ reader = XML::Reader.string(xml, :options => XML::Parser::Options::NOCDATA)
230
+ reader.read # foo
231
+ reader.read # test
232
+ reader.read # text
233
+ reader.read # cdata
234
+ reader.read # cdata-section
235
+ assert_equal(XML::Node::TEXT_NODE, reader.node_type)
236
+ end
237
+
238
+ def test_encoding
239
+ # ISO_8859_1:
240
+ # ö - f6 in hex, \366 in octal
241
+ # ü - fc in hex, \374 in octal
242
+ xml = "<?xml version=\"1.0\" encoding=\"iso-8859-1\"?><bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
243
+
244
+ reader = XML::Reader.string(xml, :encoding => XML::Encoding::ISO_8859_1)
245
+ reader.read
246
+
247
+ if defined?(Encoding)
248
+ assert_equal(Encoding::ISO8859_1, reader.read_outer_xml.encoding)
249
+ assert_equal(Encoding::ISO8859_1, reader.read_inner_xml.encoding)
250
+ assert_equal(Encoding::ISO8859_1, reader.read_string.encoding)
251
+
252
+ assert_equal("<bands genre=\"metal\">\n <m\xC3\xB6tley_cr\xC3\xBCe country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\xC3\xB6tley_cr\xC3\xBCe>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>".force_encoding(Encoding::ISO8859_1),
253
+ reader.read_outer_xml)
254
+ assert_equal("\n <m\xC3\xB6tley_cr\xC3\xBCe country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\xC3\xB6tley_cr\xC3\xBCe>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n".force_encoding(Encoding::ISO8859_1),
255
+ reader.read_inner_xml)
256
+ assert_equal("\n An American heavy metal band formed in Los Angeles, California in 1981.\n British heavy metal band formed in 1975.\n".force_encoding(Encoding::ISO8859_1),
257
+ reader.read_string)
258
+ else
259
+ assert_equal("<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
260
+ reader.read_outer_xml)
261
+ end
262
+ end
263
+
264
+ def test_invalid_encoding
265
+ # ISO_8859_1:
266
+ # ö - f6 in hex, \366 in octal
267
+ # ü - fc in hex, \374 in octal
268
+ xml = "<bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
269
+
270
+ reader = XML::Reader.string(xml)
271
+ error = assert_raise(XML::Error) do
272
+ node = reader.read
273
+ end
274
+
275
+ assert_equal("Fatal error: Input is not proper UTF-8, indicate encoding !\nBytes: 0xF6 0x74 0x6C 0x65 at :2.",
276
+ error.to_s)
277
+
278
+ end
279
+
280
+ def test_file_encoding
281
+ reader = XML::Reader.file(XML_FILE)
282
+ reader.read
283
+ assert_equal(XML::Encoding::UTF_8, reader.encoding)
284
+ assert_equal(Encoding::UTF_8, reader.value.encoding) if defined?(Encoding)
285
+ end
286
+
287
+ def test_string_encoding
288
+ # ISO_8859_1:
289
+ # ö - f6 in hex, \366 in octal
290
+ # ü - fc in hex, \374 in octal
291
+ xml = "<bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden>\n</bands>"
292
+ reader = XML::Reader.string(xml, :encoding => XML::Encoding::ISO_8859_1)
293
+ reader.read
294
+
295
+ # Encoding is always null for strings, very annoying!
296
+ assert_equal(reader.encoding, XML::Encoding::NONE)
297
+ end
298
+ end
@@ -0,0 +1,54 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+ require 'test/unit'
6
+
7
+ class TestRelaxNG < Test::Unit::TestCase
8
+ def setup
9
+ file = File.join(File.dirname(__FILE__), 'model/shiporder.xml')
10
+ @doc = XML::Document.file(file)
11
+ end
12
+
13
+ def teardown
14
+ @doc = nil
15
+ end
16
+
17
+ def relaxng
18
+ document = XML::Document.file(File.join(File.dirname(__FILE__), 'model/shiporder.rng'))
19
+ relaxng = XML::RelaxNG.document(document)
20
+ end
21
+
22
+ def test_from_doc
23
+ assert_instance_of(XML::RelaxNG, relaxng)
24
+ end
25
+
26
+ def test_valid
27
+ assert(@doc.validate_relaxng(relaxng))
28
+ end
29
+
30
+ def test_invalid
31
+ new_node = XML::Node.new('invalid', 'this will mess up validation')
32
+ @doc.root << new_node
33
+
34
+ error = assert_raise(XML::Error) do
35
+ @doc.validate_relaxng(relaxng)
36
+ end
37
+
38
+ assert_not_nil(error)
39
+ assert_kind_of(XML::Error, error)
40
+ assert(error.message.match(/Error: Did not expect element invalid there/))
41
+ assert_equal(XML::Error::RELAXNGV, error.domain)
42
+ assert_equal(XML::Error::LT_IN_ATTRIBUTE, error.code)
43
+ assert_equal(XML::Error::ERROR, error.level)
44
+ assert(error.file.match(/shiporder\.xml/))
45
+ assert_nil(error.line)
46
+ assert_equal('invalid', error.str1)
47
+ assert_nil(error.str2)
48
+ assert_nil(error.str3)
49
+ assert_equal(0, error.int1)
50
+ assert_equal(0, error.int2)
51
+ assert_not_nil(error.node)
52
+ assert_equal('invalid', error.node.name)
53
+ end
54
+ end
@@ -0,0 +1,276 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'stringio'
5
+ require 'test/unit'
6
+
7
+ class DocTypeCallback
8
+ include XML::SaxParser::Callbacks
9
+ def on_start_element(element, attributes)
10
+ end
11
+ end
12
+
13
+ class TestCaseCallbacks
14
+ include XML::SaxParser::Callbacks
15
+
16
+ attr_accessor :result
17
+
18
+ def initialize
19
+ @result = Array.new
20
+ end
21
+
22
+ def on_cdata_block(cdata)
23
+ @result << "cdata: #{cdata}"
24
+ end
25
+
26
+ def on_characters(chars)
27
+ @result << "characters: #{chars}"
28
+ end
29
+
30
+ def on_comment(text)
31
+ @result << "comment: #{text}"
32
+ end
33
+
34
+ def on_end_document
35
+ @result << "end_document"
36
+ end
37
+
38
+ def on_end_element(name)
39
+ @result << "end_element: #{name}"
40
+ end
41
+
42
+ def on_end_element_ns(name, prefix, uri)
43
+ @result << "end_element_ns #{name}, prefix: #{prefix}, uri: #{uri}"
44
+ end
45
+
46
+ # Called for parser errors.
47
+ def on_error(error)
48
+ @result << "error: #{error}"
49
+ end
50
+
51
+ def on_processing_instruction(target, data)
52
+ @result << "pi: #{target} #{data}"
53
+ end
54
+
55
+ def on_start_document
56
+ @result << "startdoc"
57
+ end
58
+
59
+ def on_start_element(name, attributes)
60
+ attributes ||= Hash.new
61
+ @result << "start_element: #{name}, attr: #{attributes.inspect}"
62
+ end
63
+
64
+ def on_start_element_ns(name, attributes, prefix, uri, namespaces)
65
+ attributes ||= Hash.new
66
+ namespaces ||= Hash.new
67
+ @result << "start_element_ns: #{name}, attr: #{attributes.inspect}, prefix: #{prefix}, uri: #{uri}, ns: #{namespaces.inspect}"
68
+ end
69
+ end
70
+
71
+ class TestSaxParser < Test::Unit::TestCase
72
+ def saxtest_file
73
+ File.join(File.dirname(__FILE__), 'model/atom.xml')
74
+ end
75
+
76
+ def verify(parser)
77
+ result = parser.callbacks.result
78
+
79
+ i = -1
80
+ assert_equal("startdoc", result[i+=1])
81
+ assert_equal("pi: xml-stylesheet type=\"text/xsl\" href=\"my_stylesheet.xsl\"", result[i+=1])
82
+ assert_equal("start_element: feed, attr: {}", result[i+=1])
83
+ assert_equal("start_element_ns: feed, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {nil=>\"http://www.w3.org/2005/Atom\"}", result[i+=1])
84
+ assert_equal("characters: \n ", result[i+=1])
85
+ assert_equal("comment: Not a valid atom entry ", result[i+=1])
86
+ assert_equal("characters: \n ", result[i+=1])
87
+ assert_equal("start_element: entry, attr: {}", result[i+=1])
88
+ assert_equal("start_element_ns: entry, attr: {}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
89
+ assert_equal("characters: \n ", result[i+=1])
90
+ assert_equal("start_element: title, attr: {\"type\"=>\"html\"}", result[i+=1])
91
+ assert_equal("start_element_ns: title, attr: {\"type\"=>\"html\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
92
+ assert_equal("cdata: <<strong>>", result[i+=1])
93
+ assert_equal("end_element: title", result[i+=1])
94
+ assert_equal("end_element_ns title, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
95
+ assert_equal("characters: \n ", result[i+=1])
96
+ assert_equal("start_element: content, attr: {\"type\"=>\"xhtml\"}", result[i+=1])
97
+ assert_equal("start_element_ns: content, attr: {\"type\"=>\"xhtml\"}, prefix: , uri: http://www.w3.org/2005/Atom, ns: {}", result[i+=1])
98
+ assert_equal("characters: \n ", result[i+=1])
99
+ assert_equal("start_element: xhtml:div, attr: {}", result[i+=1])
100
+ assert_equal("start_element_ns: div, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {\"xhtml\"=>\"http://www.w3.org/1999/xhtml\"}", result[i+=1])
101
+ assert_equal("characters: \n ", result[i+=1])
102
+ assert_equal("start_element: xhtml:p, attr: {}", result[i+=1])
103
+ assert_equal("start_element_ns: p, attr: {}, prefix: xhtml, uri: http://www.w3.org/1999/xhtml, ns: {}", result[i+=1])
104
+ assert_equal("characters: hi there", result[i+=1])
105
+ assert_equal("end_element: xhtml:p", result[i+=1])
106
+ assert_equal("end_element_ns p, prefix: xhtml, uri: http://www.w3.org/1999/xhtml", result[i+=1])
107
+ assert_equal("characters: \n ", result[i+=1])
108
+ assert_equal("end_element: xhtml:div", result[i+=1])
109
+ assert_equal("end_element_ns div, prefix: xhtml, uri: http://www.w3.org/1999/xhtml", result[i+=1])
110
+ assert_equal("characters: \n ", result[i+=1])
111
+ assert_equal("end_element: content", result[i+=1])
112
+ assert_equal("end_element_ns content, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
113
+ assert_equal("characters: \n ", result[i+=1])
114
+ assert_equal("end_element: entry", result[i+=1])
115
+ assert_equal("end_element_ns entry, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
116
+ assert_equal("characters: \n", result[i+=1])
117
+ assert_equal("end_element: feed", result[i+=1])
118
+ assert_equal("end_element_ns feed, prefix: , uri: http://www.w3.org/2005/Atom", result[i+=1])
119
+ assert_equal("end_document", result[i+=1])
120
+ end
121
+
122
+ def test_file
123
+ parser = XML::SaxParser.file(saxtest_file)
124
+ parser.callbacks = TestCaseCallbacks.new
125
+ parser.parse
126
+ verify(parser)
127
+ end
128
+
129
+ def test_file_no_callbacks
130
+ parser = XML::SaxParser.file(saxtest_file)
131
+ assert_equal true, parser.parse
132
+ end
133
+
134
+ def test_noexistent_file
135
+ error = assert_raise(XML::Error) do
136
+ XML::SaxParser.file('i_dont_exist.xml')
137
+ end
138
+
139
+ assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.to_s)
140
+ end
141
+
142
+ def test_nil_file
143
+ error = assert_raise(TypeError) do
144
+ XML::SaxParser.file(nil)
145
+ end
146
+
147
+ assert_equal("can't convert nil into String", error.to_s)
148
+ end
149
+
150
+ def test_io
151
+ File.open(saxtest_file) do |file|
152
+ parser = XML::SaxParser.io(file)
153
+ parser.callbacks = TestCaseCallbacks.new
154
+ parser.parse
155
+ verify(parser)
156
+ end
157
+ end
158
+
159
+ def test_nil_io
160
+ error = assert_raise(TypeError) do
161
+ XML::HTMLParser.io(nil)
162
+ end
163
+
164
+ assert_equal("Must pass in an IO object", error.to_s)
165
+ end
166
+
167
+ def test_string_no_callbacks
168
+ xml = File.read(saxtest_file)
169
+ parser = XML::SaxParser.string(xml)
170
+ assert_equal true, parser.parse
171
+ end
172
+
173
+ def test_string
174
+ xml = File.read(saxtest_file)
175
+ parser = XML::SaxParser.string(xml)
176
+ parser.callbacks = TestCaseCallbacks.new
177
+ parser.parse
178
+ verify(parser)
179
+ end
180
+
181
+ def test_string_io
182
+ xml = File.read(saxtest_file)
183
+ io = StringIO.new(xml)
184
+ parser = XML::SaxParser.io(io)
185
+
186
+ parser.callbacks = TestCaseCallbacks.new
187
+ parser.parse
188
+ verify(parser)
189
+ end
190
+
191
+ def test_nil_string
192
+ error = assert_raise(TypeError) do
193
+ XML::SaxParser.string(nil)
194
+ end
195
+
196
+ assert_equal("wrong argument type nil (expected String)", error.to_s)
197
+ end
198
+
199
+ def test_doctype
200
+ xml = <<-EOS
201
+ <?xml version="1.0" encoding="UTF-8"?>
202
+ <!DOCTYPE Results SYSTEM "results.dtd">
203
+ <Results>
204
+ <a>a1</a>
205
+ </Results>
206
+ EOS
207
+ parser = XML::SaxParser.string(xml)
208
+ parser.callbacks = DocTypeCallback.new
209
+ doc = parser.parse
210
+ assert_not_nil(doc)
211
+ end
212
+
213
+ def test_parse_warning
214
+ # Two xml PIs is a warning
215
+ xml = <<-EOS
216
+ <?xml version="1.0" encoding="utf-8"?>
217
+ <?xml-invalid?>
218
+ <Test/>
219
+ EOS
220
+
221
+ parser = XML::SaxParser.string(xml)
222
+ parser.callbacks = TestCaseCallbacks.new
223
+
224
+ parser.parse
225
+
226
+ # Check callbacks
227
+ result = parser.callbacks.result
228
+ i = -1
229
+ assert_equal("startdoc", result[i+=1])
230
+ assert_equal("error: Warning: xmlParsePITarget: invalid name prefix 'xml' at :2.", result[i+=1])
231
+ assert_equal("pi: xml-invalid ", result[i+=1])
232
+ assert_equal("start_element: Test, attr: {}", result[i+=1])
233
+ assert_equal("start_element_ns: Test, attr: {}, prefix: , uri: , ns: {}", result[i+=1])
234
+ assert_equal("end_element: Test", result[i+=1])
235
+ assert_equal("end_element_ns Test, prefix: , uri: ", result[i+=1])
236
+ assert_equal("end_document", result[i+=1])
237
+ end
238
+
239
+ def test_parse_error
240
+ xml = <<-EOS
241
+ <Results>
242
+ EOS
243
+ parser = XML::SaxParser.string(xml)
244
+ parser.callbacks = TestCaseCallbacks.new
245
+
246
+ error = assert_raise(XML::Error) do
247
+ doc = parser.parse
248
+ end
249
+
250
+ # Check callbacks
251
+ result = parser.callbacks.result
252
+
253
+ i = -1
254
+ assert_equal("startdoc", result[i+=1])
255
+ assert_equal("start_element: Results, attr: {}", result[i+=1])
256
+ assert_equal("start_element_ns: Results, attr: {}, prefix: , uri: , ns: {}", result[i+=1])
257
+ assert_equal("characters: \n", result[i+=1])
258
+ assert_equal("error: Fatal error: Premature end of data in tag Results line 1 at :2.", result[i+=1])
259
+ assert_equal("end_document", result[i+=1])
260
+
261
+ assert_not_nil(error)
262
+ assert_kind_of(XML::Error, error)
263
+ assert_equal("Fatal error: Premature end of data in tag Results line 1 at :2.", error.message)
264
+ assert_equal(XML::Error::PARSER, error.domain)
265
+ assert_equal(XML::Error::TAG_NOT_FINISHED, error.code)
266
+ assert_equal(XML::Error::FATAL, error.level)
267
+ assert_nil(error.file)
268
+ assert_equal(2, error.line)
269
+ assert_equal('Results', error.str1)
270
+ assert_nil(error.str2)
271
+ assert_nil(error.str3)
272
+ assert_equal(1, error.int1)
273
+ assert_equal(1, error.int2)
274
+ assert_nil(error.node)
275
+ end
276
+ end