libxml-ruby 2.9.0-x64-mingw32

Sign up to get free protection for your applications and to get access to all the features.
Files changed (211) hide show
  1. checksums.yaml +7 -0
  2. data/HISTORY +790 -0
  3. data/LICENSE +21 -0
  4. data/MANIFEST +166 -0
  5. data/README.rdoc +184 -0
  6. data/Rakefile +81 -0
  7. data/ext/libxml/extconf.h +4 -0
  8. data/ext/libxml/extconf.rb +57 -0
  9. data/ext/libxml/libxml.c +80 -0
  10. data/ext/libxml/libxml_ruby.def +35 -0
  11. data/ext/libxml/ruby_libxml.h +75 -0
  12. data/ext/libxml/ruby_xml.c +977 -0
  13. data/ext/libxml/ruby_xml.h +20 -0
  14. data/ext/libxml/ruby_xml_attr.c +333 -0
  15. data/ext/libxml/ruby_xml_attr.h +12 -0
  16. data/ext/libxml/ruby_xml_attr_decl.c +153 -0
  17. data/ext/libxml/ruby_xml_attr_decl.h +11 -0
  18. data/ext/libxml/ruby_xml_attributes.c +275 -0
  19. data/ext/libxml/ruby_xml_attributes.h +15 -0
  20. data/ext/libxml/ruby_xml_cbg.c +85 -0
  21. data/ext/libxml/ruby_xml_document.c +1133 -0
  22. data/ext/libxml/ruby_xml_document.h +11 -0
  23. data/ext/libxml/ruby_xml_dtd.c +261 -0
  24. data/ext/libxml/ruby_xml_dtd.h +9 -0
  25. data/ext/libxml/ruby_xml_encoding.c +262 -0
  26. data/ext/libxml/ruby_xml_encoding.h +19 -0
  27. data/ext/libxml/ruby_xml_error.c +996 -0
  28. data/ext/libxml/ruby_xml_error.h +12 -0
  29. data/ext/libxml/ruby_xml_html_parser.c +92 -0
  30. data/ext/libxml/ruby_xml_html_parser.h +10 -0
  31. data/ext/libxml/ruby_xml_html_parser_context.c +337 -0
  32. data/ext/libxml/ruby_xml_html_parser_context.h +10 -0
  33. data/ext/libxml/ruby_xml_html_parser_options.c +46 -0
  34. data/ext/libxml/ruby_xml_html_parser_options.h +10 -0
  35. data/ext/libxml/ruby_xml_input_cbg.c +191 -0
  36. data/ext/libxml/ruby_xml_input_cbg.h +20 -0
  37. data/ext/libxml/ruby_xml_io.c +52 -0
  38. data/ext/libxml/ruby_xml_io.h +10 -0
  39. data/ext/libxml/ruby_xml_namespace.c +153 -0
  40. data/ext/libxml/ruby_xml_namespace.h +10 -0
  41. data/ext/libxml/ruby_xml_namespaces.c +293 -0
  42. data/ext/libxml/ruby_xml_namespaces.h +9 -0
  43. data/ext/libxml/ruby_xml_node.c +1446 -0
  44. data/ext/libxml/ruby_xml_node.h +11 -0
  45. data/ext/libxml/ruby_xml_parser.c +94 -0
  46. data/ext/libxml/ruby_xml_parser.h +12 -0
  47. data/ext/libxml/ruby_xml_parser_context.c +999 -0
  48. data/ext/libxml/ruby_xml_parser_context.h +10 -0
  49. data/ext/libxml/ruby_xml_parser_options.c +66 -0
  50. data/ext/libxml/ruby_xml_parser_options.h +12 -0
  51. data/ext/libxml/ruby_xml_reader.c +1226 -0
  52. data/ext/libxml/ruby_xml_reader.h +17 -0
  53. data/ext/libxml/ruby_xml_relaxng.c +110 -0
  54. data/ext/libxml/ruby_xml_relaxng.h +10 -0
  55. data/ext/libxml/ruby_xml_sax2_handler.c +326 -0
  56. data/ext/libxml/ruby_xml_sax2_handler.h +10 -0
  57. data/ext/libxml/ruby_xml_sax_parser.c +120 -0
  58. data/ext/libxml/ruby_xml_sax_parser.h +10 -0
  59. data/ext/libxml/ruby_xml_schema.c +300 -0
  60. data/ext/libxml/ruby_xml_schema.h +809 -0
  61. data/ext/libxml/ruby_xml_schema_attribute.c +109 -0
  62. data/ext/libxml/ruby_xml_schema_attribute.h +15 -0
  63. data/ext/libxml/ruby_xml_schema_element.c +94 -0
  64. data/ext/libxml/ruby_xml_schema_element.h +14 -0
  65. data/ext/libxml/ruby_xml_schema_facet.c +52 -0
  66. data/ext/libxml/ruby_xml_schema_facet.h +13 -0
  67. data/ext/libxml/ruby_xml_schema_type.c +259 -0
  68. data/ext/libxml/ruby_xml_schema_type.h +9 -0
  69. data/ext/libxml/ruby_xml_version.h +9 -0
  70. data/ext/libxml/ruby_xml_writer.c +1136 -0
  71. data/ext/libxml/ruby_xml_writer.h +10 -0
  72. data/ext/libxml/ruby_xml_xinclude.c +16 -0
  73. data/ext/libxml/ruby_xml_xinclude.h +11 -0
  74. data/ext/libxml/ruby_xml_xpath.c +188 -0
  75. data/ext/libxml/ruby_xml_xpath.h +13 -0
  76. data/ext/libxml/ruby_xml_xpath_context.c +360 -0
  77. data/ext/libxml/ruby_xml_xpath_context.h +9 -0
  78. data/ext/libxml/ruby_xml_xpath_expression.c +81 -0
  79. data/ext/libxml/ruby_xml_xpath_expression.h +10 -0
  80. data/ext/libxml/ruby_xml_xpath_object.c +335 -0
  81. data/ext/libxml/ruby_xml_xpath_object.h +17 -0
  82. data/ext/libxml/ruby_xml_xpointer.c +99 -0
  83. data/ext/libxml/ruby_xml_xpointer.h +11 -0
  84. data/ext/vc/libxml_ruby.sln +26 -0
  85. data/lib/2.3/libxml_ruby.so +0 -0
  86. data/lib/libs/libiconv-2.dll +0 -0
  87. data/lib/libs/libxml2-2.dll +0 -0
  88. data/lib/libs/zlib1.dll +0 -0
  89. data/lib/libxml.rb +35 -0
  90. data/lib/libxml/attr.rb +123 -0
  91. data/lib/libxml/attr_decl.rb +80 -0
  92. data/lib/libxml/attributes.rb +14 -0
  93. data/lib/libxml/document.rb +194 -0
  94. data/lib/libxml/error.rb +95 -0
  95. data/lib/libxml/hpricot.rb +78 -0
  96. data/lib/libxml/html_parser.rb +96 -0
  97. data/lib/libxml/namespace.rb +62 -0
  98. data/lib/libxml/namespaces.rb +38 -0
  99. data/lib/libxml/node.rb +399 -0
  100. data/lib/libxml/ns.rb +22 -0
  101. data/lib/libxml/parser.rb +367 -0
  102. data/lib/libxml/properties.rb +23 -0
  103. data/lib/libxml/reader.rb +29 -0
  104. data/lib/libxml/sax_callbacks.rb +180 -0
  105. data/lib/libxml/sax_parser.rb +58 -0
  106. data/lib/libxml/schema.rb +67 -0
  107. data/lib/libxml/schema/attribute.rb +19 -0
  108. data/lib/libxml/schema/element.rb +27 -0
  109. data/lib/libxml/schema/type.rb +29 -0
  110. data/lib/libxml/tree.rb +29 -0
  111. data/lib/libxml/xpath_object.rb +16 -0
  112. data/lib/xml.rb +14 -0
  113. data/lib/xml/libxml.rb +10 -0
  114. data/libxml-ruby.gemspec +47 -0
  115. data/script/benchmark/depixelate +634 -0
  116. data/script/benchmark/hamlet.xml +9055 -0
  117. data/script/benchmark/parsecount +170 -0
  118. data/script/benchmark/sock_entries.xml +507 -0
  119. data/script/benchmark/throughput +41 -0
  120. data/script/test +6 -0
  121. data/setup.rb +1585 -0
  122. data/test/c14n/given/doc.dtd +1 -0
  123. data/test/c14n/given/example-1.xml +14 -0
  124. data/test/c14n/given/example-2.xml +11 -0
  125. data/test/c14n/given/example-3.xml +18 -0
  126. data/test/c14n/given/example-4.xml +9 -0
  127. data/test/c14n/given/example-5.xml +12 -0
  128. data/test/c14n/given/example-6.xml +2 -0
  129. data/test/c14n/given/example-7.xml +11 -0
  130. data/test/c14n/given/example-8.xml +11 -0
  131. data/test/c14n/given/example-8.xpath +10 -0
  132. data/test/c14n/given/world.txt +1 -0
  133. data/test/c14n/result/1-1-without-comments/example-1 +4 -0
  134. data/test/c14n/result/1-1-without-comments/example-2 +11 -0
  135. data/test/c14n/result/1-1-without-comments/example-3 +14 -0
  136. data/test/c14n/result/1-1-without-comments/example-4 +9 -0
  137. data/test/c14n/result/1-1-without-comments/example-5 +3 -0
  138. data/test/c14n/result/1-1-without-comments/example-6 +1 -0
  139. data/test/c14n/result/1-1-without-comments/example-7 +1 -0
  140. data/test/c14n/result/1-1-without-comments/example-8 +1 -0
  141. data/test/c14n/result/with-comments/example-1 +6 -0
  142. data/test/c14n/result/with-comments/example-2 +11 -0
  143. data/test/c14n/result/with-comments/example-3 +14 -0
  144. data/test/c14n/result/with-comments/example-4 +9 -0
  145. data/test/c14n/result/with-comments/example-5 +4 -0
  146. data/test/c14n/result/with-comments/example-6 +1 -0
  147. data/test/c14n/result/with-comments/example-7 +1 -0
  148. data/test/c14n/result/without-comments/example-1 +4 -0
  149. data/test/c14n/result/without-comments/example-2 +11 -0
  150. data/test/c14n/result/without-comments/example-3 +14 -0
  151. data/test/c14n/result/without-comments/example-4 +9 -0
  152. data/test/c14n/result/without-comments/example-5 +3 -0
  153. data/test/c14n/result/without-comments/example-6 +1 -0
  154. data/test/c14n/result/without-comments/example-7 +1 -0
  155. data/test/model/atom.xml +13 -0
  156. data/test/model/bands.iso-8859-1.xml +5 -0
  157. data/test/model/bands.utf-8.xml +5 -0
  158. data/test/model/bands.xml +5 -0
  159. data/test/model/books.xml +154 -0
  160. data/test/model/merge_bug_data.xml +58 -0
  161. data/test/model/ruby-lang.html +238 -0
  162. data/test/model/rubynet.xml +79 -0
  163. data/test/model/rubynet_project +1 -0
  164. data/test/model/shiporder.rnc +28 -0
  165. data/test/model/shiporder.rng +86 -0
  166. data/test/model/shiporder.xml +23 -0
  167. data/test/model/shiporder.xsd +40 -0
  168. data/test/model/soap.xml +27 -0
  169. data/test/model/xinclude.xml +5 -0
  170. data/test/tc_attr.rb +181 -0
  171. data/test/tc_attr_decl.rb +132 -0
  172. data/test/tc_attributes.rb +142 -0
  173. data/test/tc_canonicalize.rb +124 -0
  174. data/test/tc_deprecated_require.rb +12 -0
  175. data/test/tc_document.rb +125 -0
  176. data/test/tc_document_write.rb +195 -0
  177. data/test/tc_dtd.rb +128 -0
  178. data/test/tc_encoding.rb +126 -0
  179. data/test/tc_encoding_sax.rb +115 -0
  180. data/test/tc_error.rb +179 -0
  181. data/test/tc_html_parser.rb +161 -0
  182. data/test/tc_html_parser_context.rb +23 -0
  183. data/test/tc_namespace.rb +61 -0
  184. data/test/tc_namespaces.rb +209 -0
  185. data/test/tc_node.rb +215 -0
  186. data/test/tc_node_cdata.rb +50 -0
  187. data/test/tc_node_comment.rb +32 -0
  188. data/test/tc_node_copy.rb +41 -0
  189. data/test/tc_node_edit.rb +174 -0
  190. data/test/tc_node_pi.rb +39 -0
  191. data/test/tc_node_text.rb +70 -0
  192. data/test/tc_node_write.rb +107 -0
  193. data/test/tc_node_xlink.rb +28 -0
  194. data/test/tc_parser.rb +375 -0
  195. data/test/tc_parser_context.rb +204 -0
  196. data/test/tc_properties.rb +38 -0
  197. data/test/tc_reader.rb +399 -0
  198. data/test/tc_relaxng.rb +53 -0
  199. data/test/tc_sax_parser.rb +319 -0
  200. data/test/tc_schema.rb +161 -0
  201. data/test/tc_traversal.rb +152 -0
  202. data/test/tc_writer.rb +447 -0
  203. data/test/tc_xinclude.rb +20 -0
  204. data/test/tc_xml.rb +225 -0
  205. data/test/tc_xpath.rb +244 -0
  206. data/test/tc_xpath_context.rb +88 -0
  207. data/test/tc_xpath_expression.rb +37 -0
  208. data/test/tc_xpointer.rb +72 -0
  209. data/test/test_helper.rb +16 -0
  210. data/test/test_suite.rb +49 -0
  211. metadata +344 -0
@@ -0,0 +1,204 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+
6
+ class TestParserContext < Minitest::Test
7
+ def test_string
8
+ # UTF8
9
+ xml = <<-EOS
10
+ <bands>
11
+ <metal>m\303\266tley_cr\303\274e</metal>
12
+ </bands>
13
+ EOS
14
+
15
+ context = XML::Parser::Context.string(xml)
16
+ assert_instance_of(XML::Parser::Context, context)
17
+ assert_equal(XML::Encoding::NONE, context.encoding)
18
+ assert_nil(context.base_uri)
19
+ end
20
+
21
+ def test_encoding
22
+ # ISO_8859_1:
23
+ xml = <<-EOS
24
+ <bands>
25
+ <metal>m\366tley_cr\374e</metal>
26
+ </bands>
27
+ EOS
28
+
29
+ context = XML::Parser::Context.string(xml)
30
+ assert_equal(XML::Encoding::NONE, context.encoding)
31
+
32
+ context.encoding = XML::Encoding::ISO_8859_1
33
+ assert_equal(XML::Encoding::ISO_8859_1, context.encoding)
34
+ end
35
+
36
+ def test_invalid_encoding
37
+ # UTF8
38
+ xml = <<-EOS
39
+ <bands>
40
+ <metal>m\303\266tley_cr\303\274e</metal>
41
+ </bands>
42
+ EOS
43
+
44
+ context = XML::Parser::Context.string(xml)
45
+
46
+ error = assert_raises(ArgumentError) do
47
+ context.encoding = -999
48
+ end
49
+ assert_equal("Unknown encoding: -999", error.to_s)
50
+ end
51
+
52
+ def test_base_uri
53
+ # UTF8
54
+ xml = <<-EOS
55
+ <bands>
56
+ <metal>m\303\266tley_cr\303\274e</metal>
57
+ </bands>
58
+ EOS
59
+
60
+ context = XML::Parser::Context.string(xml)
61
+ assert_nil(context.base_uri)
62
+
63
+ context.base_uri = 'http://libxml.rubyforge.org'
64
+ assert_equal('http://libxml.rubyforge.org', context.base_uri)
65
+ end
66
+
67
+ def test_string_empty
68
+ error = assert_raises(TypeError) do
69
+ XML::Parser::Context.string(nil)
70
+ end
71
+ assert_equal("wrong argument type nil (expected String)", error.to_s)
72
+
73
+ error = assert_raises(ArgumentError) do
74
+ XML::Parser::Context.string('')
75
+ end
76
+ assert_equal("Must specify a string with one or more characters", error.to_s)
77
+ end
78
+
79
+ def test_well_formed
80
+ parser = XML::Parser.string("<abc/>")
81
+ parser.parse
82
+ assert(parser.context.well_formed?)
83
+ end
84
+
85
+ def test_not_well_formed
86
+ parser = XML::Parser.string("<abc>")
87
+ assert_raises(XML::Error) do
88
+ parser.parse
89
+ end
90
+ assert(!parser.context.well_formed?)
91
+ end
92
+
93
+ def test_version_info
94
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
95
+ parser = XML::Parser.file(file)
96
+ assert_nil(parser.context.version)
97
+ parser.parse
98
+ assert_equal("1.0", parser.context.version)
99
+ end
100
+
101
+ def test_depth
102
+ context = XML::Parser::Context.new
103
+ assert_instance_of(Fixnum, context.depth)
104
+ end
105
+
106
+ def test_disable_sax
107
+ context = XML::Parser::Context.new
108
+ assert(!context.disable_sax?)
109
+ end
110
+
111
+ def test_docbook
112
+ context = XML::Parser::Context.new
113
+ assert(!context.docbook?)
114
+ end
115
+
116
+ def test_html
117
+ context = XML::Parser::Context.new
118
+ assert(!context.html?)
119
+ end
120
+
121
+ def test_keep_blanks
122
+ context = XML::Parser::Context.new
123
+ if context.keep_blanks?
124
+ assert_instance_of(TrueClass, context.keep_blanks?)
125
+ else
126
+ assert_instance_of(FalseClass, context.keep_blanks?)
127
+ end
128
+ end
129
+
130
+ if ENV['NOTWORKING']
131
+ def test_num_chars
132
+ assert_equal(17, context.num_chars)
133
+ end
134
+ end
135
+
136
+ def test_replace_entities
137
+ context = XML::Parser::Context.new
138
+ assert(!context.replace_entities?)
139
+
140
+ # context.options = 1
141
+ # assert(context.replace_entities?)
142
+
143
+ context.options = 0
144
+ assert(!context.replace_entities?)
145
+
146
+ context.replace_entities = true
147
+ assert(context.replace_entities?)
148
+ end
149
+
150
+ def test_space_depth
151
+ context = XML::Parser::Context.new
152
+ assert_equal(1, context.space_depth)
153
+ end
154
+
155
+ def test_subset_external
156
+ context = XML::Parser::Context.new
157
+ assert(!context.subset_external?)
158
+ end
159
+
160
+ def test_data_directory_get
161
+ context = XML::Parser::Context.new
162
+ assert_nil(context.data_directory)
163
+ end
164
+
165
+ def test_parse_error
166
+ xp = XML::Parser.string('<foo><bar/></foz>')
167
+
168
+ assert_raises(XML::Error) do
169
+ xp.parse
170
+ end
171
+
172
+ # Now check context
173
+ context = xp.context
174
+ assert_equal(nil, context.data_directory)
175
+ assert_equal(0, context.depth)
176
+ assert_equal(true, context.disable_sax?)
177
+ assert_equal(false, context.docbook?)
178
+ assert_equal(XML::Encoding::NONE, context.encoding)
179
+ assert_equal(76, context.errno)
180
+ assert_equal(false, context.html?)
181
+ assert_equal(5, context.io_max_num_streams)
182
+ assert_equal(1, context.io_num_streams)
183
+ assert_equal(true, context.keep_blanks?)
184
+ assert_equal(1, context.io_num_streams)
185
+ assert_equal(nil, context.name_node)
186
+ assert_equal(0, context.name_depth)
187
+ assert_equal(10, context.name_depth_max)
188
+ assert_equal(17, context.num_chars)
189
+ assert_equal(false, context.replace_entities?)
190
+ assert_equal(1, context.space_depth)
191
+ assert_equal(10, context.space_depth_max)
192
+ assert_equal(false, context.subset_external?)
193
+ assert_equal(nil, context.subset_external_system_id)
194
+ assert_equal(nil, context.subset_external_uri)
195
+ assert_equal(false, context.subset_internal?)
196
+ assert_equal(nil, context.subset_internal_name)
197
+ assert_equal(false, context.stats?)
198
+ assert_equal(true, context.standalone?)
199
+ assert_equal(false, context.valid)
200
+ assert_equal(false, context.validate?)
201
+ assert_equal('1.0', context.version)
202
+ assert_equal(false, context.well_formed?)
203
+ end
204
+ end
@@ -0,0 +1,38 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+
6
+ # attributes is deprecated - use attributes instead.
7
+ # Tests for backwards compatibility
8
+
9
+ class Testattributes < Minitest::Test
10
+ def setup()
11
+ xp = XML::Parser.string('<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
12
+ @doc = xp.parse
13
+ end
14
+
15
+ def teardown()
16
+ @doc = nil
17
+ end
18
+
19
+ def test_traversal
20
+ attributes = @doc.root.attributes
21
+
22
+ assert_instance_of(XML::Attributes, attributes)
23
+ attribute = attributes.first
24
+ assert_equal('uga', attribute.name)
25
+ assert_equal('booga', attribute.value)
26
+
27
+ attribute = attribute.next
28
+ assert_instance_of(XML::Attr, attribute)
29
+ assert_equal('foo', attribute.name)
30
+ assert_equal('bar', attribute.value)
31
+ end
32
+
33
+ def test_no_attributes
34
+ attributes = @doc.root.child.attributes
35
+ assert_instance_of(XML::Attributes, attributes)
36
+ assert_equal(0, attributes.length)
37
+ end
38
+ end
@@ -0,0 +1,399 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'stringio'
5
+
6
+ class TestReader < Minitest::Test
7
+ XML_FILE = File.join(File.dirname(__FILE__), 'model/atom.xml')
8
+
9
+ def verify_simple(reader)
10
+ node_types = []
11
+
12
+ # Read each node
13
+ 26.times do
14
+ assert(reader.read)
15
+ node_types << reader.node_type
16
+ end
17
+
18
+ # There are no more nodes
19
+ assert(!reader.read)
20
+
21
+ # Check what was read
22
+ expected = [XML::Reader::TYPE_PROCESSING_INSTRUCTION,
23
+ XML::Reader::TYPE_ELEMENT,
24
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
25
+ XML::Reader::TYPE_COMMENT,
26
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
27
+ XML::Reader::TYPE_ELEMENT,
28
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
29
+ XML::Reader::TYPE_ELEMENT,
30
+ XML::Reader::TYPE_CDATA,
31
+ XML::Reader::TYPE_END_ELEMENT,
32
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
33
+ XML::Reader::TYPE_ELEMENT,
34
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
35
+ XML::Reader::TYPE_ELEMENT,
36
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
37
+ XML::Reader::TYPE_ELEMENT,
38
+ XML::Reader::TYPE_TEXT,
39
+ XML::Reader::TYPE_END_ELEMENT,
40
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
41
+ XML::Reader::TYPE_END_ELEMENT,
42
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
43
+ XML::Reader::TYPE_END_ELEMENT,
44
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
45
+ XML::Reader::TYPE_END_ELEMENT,
46
+ XML::Reader::TYPE_SIGNIFICANT_WHITESPACE,
47
+ XML::Reader::TYPE_END_ELEMENT]
48
+
49
+ assert_equal(expected, node_types)
50
+ end
51
+
52
+ def test_document
53
+ reader = XML::Reader.document(XML::Document.file(XML_FILE))
54
+ verify_simple(reader)
55
+ end
56
+
57
+ def test_file
58
+ reader = XML::Reader.file(XML_FILE)
59
+ verify_simple(reader)
60
+ end
61
+
62
+ def test_invalid_file
63
+ assert_raises(XML::Error) do
64
+ XML::Reader.file('/does/not/exist')
65
+ end
66
+ end
67
+
68
+ def test_string
69
+ reader = XML::Reader.string(File.read(XML_FILE))
70
+ verify_simple(reader)
71
+ end
72
+
73
+ def test_io
74
+ File.open(XML_FILE, 'rb') do |io|
75
+ reader = XML::Reader.io(io)
76
+ verify_simple(reader)
77
+ end
78
+ end
79
+
80
+ def test_io_gc
81
+ # Test that the reader keeps a reference
82
+ # to the io object
83
+ file = File.open(XML_FILE, 'rb')
84
+ reader = XML::Reader.io(file)
85
+ file = nil
86
+ GC.start
87
+ assert(reader.read)
88
+ end
89
+
90
+ def test_string_io
91
+ data = File.read(XML_FILE)
92
+ string_io = StringIO.new(data)
93
+ reader = XML::Reader.io(string_io)
94
+ verify_simple(reader)
95
+ end
96
+
97
+ def test_error
98
+ reader = XML::Reader.string('<foo blah')
99
+
100
+ error = assert_raises(XML::Error) do
101
+ reader.read
102
+ end
103
+ assert_equal("Fatal error: Couldn't find end of Start Tag foo at :1.", error.to_s)
104
+ end
105
+
106
+ def test_deprecated_error_handler
107
+ called = false
108
+ reader = XML::Reader.string('<foo blah')
109
+ reader.set_error_handler do |error|
110
+ called = true
111
+ end
112
+
113
+ assert_raises(XML::Error) do
114
+ reader.read
115
+ end
116
+
117
+ assert(called)
118
+ end
119
+
120
+ def test_deprecated_reset_error_handler
121
+ called = false
122
+ reader = XML::Reader.string('<foo blah')
123
+ reader.set_error_handler do |error|
124
+ called = true
125
+ end
126
+ reader.reset_error_handler
127
+
128
+ assert_raises(XML::Error) do
129
+ reader.read
130
+ end
131
+
132
+ assert(!called)
133
+ end
134
+
135
+ def test_attr
136
+ parser = XML::Reader.string("<foo x='1' y='2'/>")
137
+ assert(parser.read)
138
+ assert_equal('foo', parser.name)
139
+ assert_equal('1', parser['x'])
140
+ assert_equal('1', parser[0])
141
+ assert_equal('2', parser['y'])
142
+ assert_equal('2', parser[1])
143
+ assert_equal(nil, parser['z'])
144
+ assert_equal(nil, parser[2])
145
+ end
146
+
147
+ def test_move_to_attribute_depreciation
148
+ previous_stderr, $stderr = $stderr, StringIO.new
149
+ reader = XML::Reader.string("<foo x='1' y='2'/>")
150
+ assert(reader.read)
151
+ assert(reader.move_to_attribute 1)
152
+ assert($stderr.string =~ /deprecated/)
153
+ ensure
154
+ $stderr = previous_stderr
155
+ end
156
+
157
+ def test_move_attr
158
+ reader = XML::Reader.string('<root xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xhtml="http://www.w3.org/1999/xhtml"><link xml:id="abc" xlink:href="def" xhtml:class="ghi" bar="jkl" /></root>')
159
+ assert(reader.read) # <root/>
160
+ assert(reader.read) # <link/>
161
+
162
+ assert(reader.move_to_attribute_no(1))
163
+ assert_equal(reader.value, 'def')
164
+ assert(reader.move_to_attribute_ns('id', 'http://www.w3.org/XML/1998/namespace'))
165
+ assert_equal(reader.value, 'abc')
166
+ assert(reader.move_to_attribute('bar'))
167
+ assert_equal(reader.value, 'jkl')
168
+
169
+ # 1 in case of success, -1 in case of error, 0 if not found
170
+ assert_equal(reader.move_to_attribute_no(12), 0)
171
+ assert_equal(reader.move_to_attribute('baz'), 0)
172
+ assert_equal(reader.move_to_attribute_ns('baz', 'http://ruby/namespace'), 0)
173
+ end
174
+
175
+ def test_get_attr
176
+ reader = XML::Reader.string('<root xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xhtml="http://www.w3.org/1999/xhtml"><link xml:id="abc" xlink:href="def" xhtml:class="ghi" bar="jkl" /></root>')
177
+ assert(reader.read) # <root/>
178
+ assert(reader.read) # <link/>
179
+
180
+ assert_equal(reader.get_attribute_no(1), 'def')
181
+ assert_equal(reader.get_attribute_ns('id', 'http://www.w3.org/XML/1998/namespace'), 'abc')
182
+ assert_equal(reader.get_attribute('bar'), 'jkl')
183
+
184
+ assert_equal(reader.get_attribute_no(12), nil)
185
+ assert_equal(reader.get_attribute('baz'), nil)
186
+ assert_equal(reader.get_attribute_ns('baz', 'http://ruby/namespace'), nil)
187
+ end
188
+
189
+ def test_value
190
+ parser = XML::Reader.string("<foo><bar>1</bar><bar>2</bar><bar>3</bar></foo>")
191
+ assert(parser.read)
192
+ assert_equal('foo', parser.name)
193
+ assert_equal(nil, parser.value)
194
+ 3.times do |i|
195
+ assert(parser.read)
196
+ assert_equal(XML::Reader::TYPE_ELEMENT, parser.node_type)
197
+ assert_equal('bar', parser.name)
198
+ assert(parser.read)
199
+ assert_equal(XML::Reader::TYPE_TEXT, parser.node_type)
200
+ assert_equal((i + 1).to_s, parser.value)
201
+ assert(parser.read)
202
+ assert_equal(XML::Reader::TYPE_END_ELEMENT, parser.node_type)
203
+ end
204
+ end
205
+
206
+ def test_expand
207
+ reader = XML::Reader.file(XML_FILE)
208
+ reader.read.to_s
209
+ reader.read
210
+
211
+ # Read a node
212
+ node = reader.expand
213
+ assert_equal('feed', node.name)
214
+ assert_equal(::Encoding::UTF_8, node.name.encoding) if defined?(::Encoding)
215
+ end
216
+
217
+ def test_expand_doc
218
+ reader = XML::Reader.file(XML_FILE)
219
+ reader.read.to_s
220
+ reader.read
221
+
222
+ # Read a node
223
+ node = reader.expand
224
+
225
+ # Try to access the document
226
+ refute_nil(node.doc)
227
+ end
228
+
229
+ def test_expand_find
230
+ reader = XML::Reader.file(XML_FILE)
231
+ reader.read.to_s
232
+ reader.read
233
+
234
+ # Read first node which
235
+ node = reader.expand
236
+ assert_equal('feed', node.name)
237
+
238
+ # Search for entries
239
+ entries = node.find('atom:entry', 'atom:http://www.w3.org/2005/Atom')
240
+
241
+ assert_equal(1, entries.length)
242
+ end
243
+
244
+ def test_expand_invalid
245
+ reader = XML::Reader.file(XML_FILE)
246
+
247
+ # Expand a node before one has been read
248
+ error = assert_raises(RuntimeError) do
249
+ reader.expand
250
+ end
251
+ assert_equal("The reader does not have a document. Did you forget to call read?", error.to_s)
252
+ end
253
+
254
+ def test_expand_invalid_access
255
+ reader = XML::Reader.file(XML_FILE)
256
+
257
+ # Read a node
258
+ reader.read
259
+ reader.read
260
+
261
+ # Expand the node
262
+ node = reader.expand
263
+ assert_equal('feed', node.name)
264
+
265
+ # Read another node, this makes the last node invalid
266
+ reader.next
267
+
268
+ # The previous node is now invalid
269
+ error = assert_raises(RuntimeError) do
270
+ assert_equal('feed', node.name)
271
+ end
272
+ assert_equal("This node has already been freed.", error.to_s)
273
+ end
274
+
275
+ def test_mode
276
+ reader = XML::Reader.string('<xml/>')
277
+ assert_equal(XML::Reader::MODE_INITIAL, reader.read_state)
278
+ reader.read
279
+ assert_equal(XML::Reader::MODE_EOF, reader.read_state)
280
+ end
281
+
282
+ def test_bytes_consumed
283
+ reader = XML::Reader.file(XML_FILE)
284
+ reader.read
285
+ assert_equal(416, reader.byte_consumed)
286
+ end
287
+
288
+ def test_node
289
+ XML.default_line_numbers = true
290
+ reader = XML::Reader.file(XML_FILE)
291
+
292
+ # first try to get a node
293
+ assert_nil(reader.node)
294
+
295
+ reader.read
296
+ assert_instance_of(XML::Node, reader.node)
297
+ end
298
+
299
+ def test_base_uri
300
+ # UTF8:
301
+ # ö - c3 b6 in hex, \303\266 in octal
302
+ # ü - c3 bc in hex, \303\274 in octal
303
+ 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>"
304
+ reader = XML::Reader.string(xml, :base_uri => "http://libxml.rubyforge.org")
305
+
306
+ reader.read
307
+ assert_equal(reader.base_uri, "http://libxml.rubyforge.org")
308
+ assert_equal(::Encoding::UTF_8, reader.base_uri.encoding) if defined?(::Encoding)
309
+ end
310
+
311
+ def test_options
312
+ xml = <<-EOS
313
+ <!DOCTYPE foo [<!ENTITY foo 'bar'>]>
314
+ <test>
315
+ <cdata><![CDATA[something]]></cdata>
316
+ <entity>&foo;</entity>
317
+ </test>
318
+ EOS
319
+
320
+ # Parse normally
321
+ reader = XML::Reader.string(xml)
322
+ reader.read # foo
323
+ reader.read # test
324
+ reader.read # text
325
+ reader.read # cdata
326
+ reader.read # cdata-section
327
+ assert_equal(XML::Node::CDATA_SECTION_NODE, reader.node_type)
328
+
329
+ # Convert cdata section to text
330
+ reader = XML::Reader.string(xml, :options => XML::Parser::Options::NOCDATA)
331
+ reader.read # foo
332
+ reader.read # test
333
+ reader.read # text
334
+ reader.read # cdata
335
+ reader.read # cdata-section
336
+ assert_equal(XML::Node::TEXT_NODE, reader.node_type)
337
+ end
338
+
339
+ def test_encoding
340
+ # ISO_8859_1:
341
+ # ö - f6 in hex, \366 in octal
342
+ # ü - fc in hex, \374 in octal
343
+ 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>"
344
+
345
+ reader = XML::Reader.string(xml, :encoding => XML::Encoding::ISO_8859_1)
346
+ reader.read
347
+
348
+ if defined?(Encoding)
349
+ assert_equal(Encoding::ISO8859_1, reader.read_outer_xml.encoding)
350
+ assert_equal(Encoding::ISO8859_1, reader.read_inner_xml.encoding)
351
+ assert_equal(Encoding::ISO8859_1, reader.read_string.encoding)
352
+
353
+ 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),
354
+ reader.read_outer_xml)
355
+ 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),
356
+ reader.read_inner_xml)
357
+ 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),
358
+ reader.read_string)
359
+ else
360
+ 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>",
361
+ reader.read_outer_xml)
362
+ end
363
+ end
364
+
365
+ def test_invalid_encoding
366
+ # ISO_8859_1:
367
+ # ö - f6 in hex, \366 in octal
368
+ # ü - fc in hex, \374 in octal
369
+ 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>"
370
+
371
+ reader = XML::Reader.string(xml)
372
+ error = assert_raises(XML::Error) do
373
+ node = reader.read
374
+ end
375
+
376
+ assert_equal("Fatal error: Input is not proper UTF-8, indicate encoding !\nBytes: 0xF6 0x74 0x6C 0x65 at :2.",
377
+ error.to_s)
378
+
379
+ end
380
+
381
+ def test_file_encoding
382
+ reader = XML::Reader.file(XML_FILE)
383
+ reader.read
384
+ assert_equal(XML::Encoding::UTF_8, reader.encoding)
385
+ assert_equal(Encoding::UTF_8, reader.value.encoding) if defined?(Encoding)
386
+ end
387
+
388
+ def test_string_encoding
389
+ # ISO_8859_1:
390
+ # ö - f6 in hex, \366 in octal
391
+ # ü - fc in hex, \374 in octal
392
+ 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>"
393
+ reader = XML::Reader.string(xml, :encoding => XML::Encoding::ISO_8859_1)
394
+ reader.read
395
+
396
+ # Encoding is always null for strings, very annoying!
397
+ assert_equal(reader.encoding, XML::Encoding::NONE)
398
+ end
399
+ end