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,39 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+
6
+ class NodeCommentTest < Minitest::Test
7
+ def setup
8
+ xp = XML::Parser.string('<root></root>')
9
+ @doc = xp.parse
10
+ assert_instance_of(XML::Document, @doc)
11
+ @root = @doc.root
12
+ end
13
+
14
+ def test_libxml_node_add_pi_01
15
+ @root << XML::Node.new_pi('mypi')
16
+ assert_equal '<root><?mypi?></root>',
17
+ @root.to_s.gsub(/\n\s*/,'')
18
+ end
19
+
20
+ def test_libxml_node_add_pi_02
21
+ @root << XML::Node.new_pi('mypi')
22
+ assert_equal 'pi',
23
+ @root.child.node_type_name
24
+ end
25
+
26
+ def test_libxml_node_add_pi_03
27
+ @root << el = XML::Node.new_pi('mypi')
28
+ el << "_this_is_added"
29
+ assert_equal '<root><?mypi _this_is_added?></root>',
30
+ @root.to_s.gsub(/\n\s*/,'')
31
+ end
32
+
33
+ def test_libxml_node_add_pi_04
34
+ @root << XML::Node.new_pi('mypi','mycontent')
35
+ assert_equal '<root><?mypi mycontent?></root>',
36
+ @root.to_s.gsub(/\n\s*/,'')
37
+ end
38
+
39
+ end
@@ -0,0 +1,70 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+ class TestTextNode < Minitest::Test
6
+ def test_content
7
+ node = XML::Node.new_text('testdata')
8
+ assert_instance_of(XML::Node, node)
9
+ assert_equal('testdata', node.content)
10
+ end
11
+
12
+ def test_invalid_content
13
+ error = assert_raises(TypeError) do
14
+ node = XML::Node.new_text(nil)
15
+ end
16
+ assert_equal('wrong argument type nil (expected String)', error.to_s)
17
+ end
18
+
19
+ # We use the same facility that libXSLT does here to disable output escaping.
20
+ # This lets you specify that the node's content should be rendered unaltered
21
+ # whenever it is being output. This is useful for things like <script> and
22
+ # <style> nodes in HTML documents if you don't want to be forced to wrap them
23
+ # in CDATA nodes. Or if you are sanitizing existing HTML documents and want
24
+ # to preserve the content of any of the text nodes.
25
+ #
26
+ def test_output_escaping
27
+ textnoenc = 'if (a < b || c > d) return "e";'
28
+ text = "if (a &lt; b || c &gt; d) return \"e\";"
29
+
30
+ node = XML::Node.new_text(textnoenc)
31
+ assert node.output_escaping?
32
+ assert_equal text, node.to_s
33
+
34
+ node.output_escaping = false
35
+ assert_equal textnoenc, node.to_s
36
+
37
+ node.output_escaping = true
38
+ assert_equal text, node.to_s
39
+
40
+ node.output_escaping = nil
41
+ assert_equal textnoenc, node.to_s
42
+
43
+ node.output_escaping = true
44
+ assert_equal text, node.to_s
45
+ end
46
+
47
+ # Just a sanity check for output escaping.
48
+ def test_output_escaping_sanity
49
+ node = XML::Node.new_text('testdata')
50
+ assert_equal 'text', node.name
51
+ assert node.output_escaping?
52
+
53
+ node.output_escaping = false
54
+ assert_equal 'textnoenc', node.name
55
+ assert ! node.output_escaping?
56
+
57
+ node.output_escaping = true
58
+ assert_equal 'text', node.name
59
+ assert node.output_escaping?
60
+
61
+ node.output_escaping = nil
62
+ assert_equal 'textnoenc', node.name
63
+ assert ! node.output_escaping?
64
+
65
+ node.output_escaping = true
66
+ assert_equal 'text', node.name
67
+ assert node.output_escaping?
68
+ end
69
+
70
+ end
@@ -0,0 +1,107 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+ class TestNodeWrite < Minitest::Test
6
+ def setup
7
+ load_encoding("utf-8")
8
+ end
9
+
10
+ def teardown
11
+ XML.default_keep_blanks = true
12
+ @doc = nil
13
+ end
14
+
15
+ def load_encoding(name)
16
+ @encoding = Encoding.find(name) if defined?(Encoding)
17
+ @file_name = "model/bands.#{name.downcase}.xml"
18
+
19
+ # Strip spaces to make testing easier
20
+ XML.default_keep_blanks = false
21
+ file = File.join(File.dirname(__FILE__), @file_name)
22
+ @doc = XML::Document.file(file)
23
+ end
24
+
25
+ def test_to_s_default
26
+ # Default to_s has indentation
27
+ node = @doc.root
28
+ assert_equal(Encoding::UTF_8, node.to_s.encoding) if defined?(Encoding)
29
+ assert_equal("<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
30
+ node.to_s)
31
+ end
32
+
33
+ def test_to_s_no_global_indentation
34
+ # No indentation due to global setting
35
+ node = @doc.root
36
+ XML.indent_tree_output = false
37
+ assert_equal("<bands genre=\"metal\">\n<m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n<iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
38
+ node.to_s)
39
+ ensure
40
+ XML.indent_tree_output = true
41
+ end
42
+
43
+ def test_to_s_no_indentation
44
+ # No indentation due to local setting
45
+ node = @doc.root
46
+ assert_equal("<bands genre=\"metal\"><m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e><iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden></bands>",
47
+ node.to_s(:indent => false))
48
+ end
49
+
50
+ def test_to_s_level
51
+ # No indentation due to local setting
52
+ node = @doc.root
53
+ assert_equal("<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n </bands>",
54
+ node.to_s(:level => 1))
55
+ end
56
+
57
+ def test_to_s_encoding
58
+ # Test encodings
59
+ node = @doc.root
60
+
61
+ # UTF8:
62
+ # ö - c3 b6 in hex, \303\266 in octal
63
+ # ü - c3 bc in hex, \303\274 in octal
64
+ value = node.to_s(:encoding => XML::Encoding::UTF_8)
65
+ assert_equal(Encoding::UTF_8, node.to_s.encoding) if defined?(Encoding)
66
+ assert_equal("<bands genre=\"metal\">\n <m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
67
+ value)
68
+
69
+ # ISO_8859_1:
70
+ # ö - f6 in hex, \366 in octal
71
+ # ü - fc in hex, \374 in octal
72
+ value = node.to_s(:encoding => XML::Encoding::ISO_8859_1)
73
+ if defined?(Encoding)
74
+ assert_equal(Encoding::ISO8859_1, value.encoding)
75
+ assert_equal("<bands genre=\"metal\">\n <m\xF6tley_cr\xFCe country=\"us\">M\xF6tley Cr\xFCe is an American heavy metal band formed in Los Angeles, California in 1981.</m\xF6tley_cr\xFCe>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>".force_encoding(Encoding::ISO8859_1),
76
+ value)
77
+ else
78
+ assert_equal("<bands genre=\"metal\">\n <m\xF6tley_cr\xFCe country=\"us\">M\xF6tley Cr\xFCe is an American heavy metal band formed in Los Angeles, California in 1981.</m\xF6tley_cr\xFCe>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>",
79
+ value)
80
+ end
81
+
82
+ # Invalid encoding
83
+ error = assert_raises(ArgumentError) do
84
+ node.to_s(:encoding => -9999)
85
+ end
86
+ assert_equal('Unknown encoding value: -9999', error.to_s)
87
+ end
88
+
89
+ def test_inner_xml
90
+ # Default to_s has indentation
91
+ node = @doc.root
92
+
93
+ if defined?(Encoding)
94
+ assert_equal(Encoding::UTF_8, node.inner_xml.encoding)
95
+ assert_equal("<m\u00F6tley_cr\u00FCe country=\"us\">M\u00F6tley Cr\u00FCe is an American heavy metal band formed in Los Angeles, California in 1981.</m\u00F6tley_cr\u00FCe><iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>",
96
+ node.inner_xml)
97
+ else
98
+ assert_equal("<m\303\266tley_cr\303\274e country=\"us\">M\303\266tley Cr\303\274e is an American heavy metal band formed in Los Angeles, California in 1981.</m\303\266tley_cr\303\274e><iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>",
99
+ node.inner_xml)
100
+ end
101
+ end
102
+
103
+ # --- Debug ---
104
+ def test_debug
105
+ assert(@doc.root.debug)
106
+ end
107
+ end
@@ -0,0 +1,28 @@
1
+ # encoding: UTF-8
2
+
3
+ # $Id$
4
+ require './test_helper'
5
+
6
+
7
+ class TC_XML_Node_XLink < Minitest::Test
8
+ def setup()
9
+ xp = XML::Parser.string('<ruby_array xmlns:xlink="http://www.w3.org/1999/xlink/namespace/"><fixnum xlink:type="simple">one</fixnum></ruby_array>')
10
+ doc = xp.parse
11
+ assert_instance_of(XML::Document, doc)
12
+ @root = doc.root
13
+ assert_instance_of(XML::Node, @root)
14
+ end
15
+
16
+ def teardown()
17
+ @root = nil
18
+ end
19
+
20
+ def test_xml_node_xlink()
21
+ for elem in @root.find('fixnum')
22
+ assert_instance_of(XML::Node, elem)
23
+ assert_instance_of(TrueClass, elem.xlink?)
24
+ assert_equal("simple", elem.xlink_type_name)
25
+ assert_equal(XML::Node::XLINK_TYPE_SIMPLE, elem.xlink_type)
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,375 @@
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'stringio'
5
+
6
+ class TestParser < Minitest::Test
7
+ def setup
8
+ XML::Error.set_handler(&XML::Error::QUIET_HANDLER)
9
+ end
10
+
11
+ # ----- Sources -------
12
+ def test_document
13
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
14
+ parser = XML::Parser.file(file)
15
+ doc = parser.parse
16
+
17
+ parser = XML::Parser.document(doc)
18
+
19
+ doc = parser.parse
20
+
21
+ assert_instance_of(XML::Document, doc)
22
+ assert_instance_of(XML::Parser::Context, parser.context)
23
+ end
24
+
25
+ def test_nil_document
26
+ error = assert_raises(TypeError) do
27
+ XML::Parser.document(nil)
28
+ end
29
+
30
+ assert_equal("Must pass an XML::Document object", error.to_s)
31
+ end
32
+
33
+ def test_file
34
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
35
+
36
+ parser = XML::Parser.file(file)
37
+ doc = parser.parse
38
+ assert_instance_of(XML::Document, doc)
39
+ assert_instance_of(XML::Parser::Context, parser.context)
40
+ end
41
+
42
+ def test_noexistent_file
43
+ error = assert_raises(XML::Error) do
44
+ XML::Parser.file('i_dont_exist.xml')
45
+ end
46
+
47
+ assert_equal('Warning: failed to load external entity "i_dont_exist.xml".', error.to_s)
48
+ end
49
+
50
+ def test_nil_file
51
+ error = assert_raises(TypeError) do
52
+ XML::Parser.file(nil)
53
+ end
54
+
55
+ assert_match(/nil into String/, error.to_s)
56
+ end
57
+
58
+ def test_file_encoding
59
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
60
+ parser = XML::Parser.file(file, :encoding => XML::Encoding::ISO_8859_1)
61
+
62
+ error = assert_raises(XML::Error) do
63
+ doc = parser.parse
64
+ end
65
+
66
+ assert(error.to_s.match(/Fatal error: Extra content at the end of the document/))
67
+
68
+ parser = XML::Parser.file(file, :encoding => XML::Encoding::UTF_8)
69
+ doc = parser.parse
70
+ refute_nil(doc)
71
+ end
72
+
73
+ def test_file_base_uri
74
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
75
+
76
+ parser = XML::Parser.file(file)
77
+ doc = parser.parse
78
+ assert(doc.child.base_uri.match(/test\/model\/bands.utf-8.xml/))
79
+
80
+ parser = XML::Parser.file(file, :base_uri => "http://libxml.org")
81
+ doc = parser.parse
82
+ assert(doc.child.base_uri.match(/test\/model\/bands.utf-8.xml/))
83
+ end
84
+
85
+ def test_io
86
+ File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
87
+ parser = XML::Parser.io(io)
88
+ assert_instance_of(XML::Parser, parser)
89
+
90
+ doc = parser.parse
91
+ assert_instance_of(XML::Document, doc)
92
+ assert_instance_of(XML::Parser::Context, parser.context)
93
+ end
94
+ end
95
+
96
+ def test_io_gc
97
+ # Test that the reader keeps a reference
98
+ # to the io object
99
+ file = File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
100
+ parser = XML::Parser.io(file)
101
+ file = nil
102
+ GC.start
103
+ assert(parser.parse)
104
+ end
105
+
106
+ def test_nil_io
107
+ error = assert_raises(TypeError) do
108
+ XML::Parser.io(nil)
109
+ end
110
+
111
+ assert_equal("Must pass in an IO object", error.to_s)
112
+ end
113
+
114
+ def test_string_io
115
+ data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
116
+ string_io = StringIO.new(data)
117
+ parser = XML::Parser.io(string_io)
118
+
119
+ doc = parser.parse
120
+ assert_instance_of(XML::Document, doc)
121
+ assert_instance_of(XML::Parser::Context, parser.context)
122
+ end
123
+
124
+ def test_string_io_thread
125
+ thread = Thread.new do
126
+ data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
127
+ string_io = StringIO.new(data)
128
+ parser = XML::Parser.io(string_io)
129
+
130
+ doc = parser.parse
131
+ assert_instance_of(XML::Document, doc)
132
+ assert_instance_of(XML::Parser::Context, parser.context)
133
+ end
134
+
135
+ thread.join
136
+ assert(true)
137
+ puts 'Thread completed'
138
+ end
139
+
140
+ def test_string
141
+ str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
142
+
143
+ parser = XML::Parser.string(str)
144
+ assert_instance_of(XML::Parser, parser)
145
+
146
+ doc = parser.parse
147
+ assert_instance_of(XML::Document, doc)
148
+ assert_instance_of(XML::Parser::Context, parser.context)
149
+ end
150
+
151
+ def test_nil_string
152
+ error = assert_raises(TypeError) do
153
+ XML::Parser.string(nil)
154
+ end
155
+
156
+ assert_equal("wrong argument type nil (expected String)", error.to_s)
157
+ end
158
+
159
+ def test_string_options
160
+ xml = <<-EOS
161
+ <!DOCTYPE foo [<!ENTITY foo 'bar'>]>
162
+ <test>
163
+ <cdata><![CDATA[something]]></cdata>
164
+ <entity>&foo;</entity>
165
+ </test>
166
+ EOS
167
+
168
+ XML::default_substitute_entities = false
169
+
170
+ # Parse normally
171
+ parser = XML::Parser.string(xml)
172
+ doc = parser.parse
173
+ assert_nil(doc.child.base_uri)
174
+
175
+ # Cdata section should be cdata nodes
176
+ node = doc.find_first('/test/cdata').child
177
+ assert_equal(XML::Node::CDATA_SECTION_NODE, node.node_type)
178
+
179
+ # Entities should not be subtituted
180
+ node = doc.find_first('/test/entity')
181
+ assert_equal('&foo;', node.child.to_s)
182
+
183
+ # Parse with options
184
+ parser = XML::Parser.string(xml, :base_uri => 'http://libxml.rubyforge.org',
185
+ :options => XML::Parser::Options::NOCDATA | XML::Parser::Options::NOENT)
186
+ doc = parser.parse
187
+ assert_equal(doc.child.base_uri, 'http://libxml.rubyforge.org')
188
+
189
+ # Cdata section should be text nodes
190
+ node = doc.find_first('/test/cdata').child
191
+ assert_equal(XML::Node::TEXT_NODE, node.node_type)
192
+
193
+ # Entities should be subtituted
194
+ node = doc.find_first('/test/entity')
195
+ assert_equal('bar', node.child.to_s)
196
+ end
197
+
198
+ def test_string_encoding
199
+ # ISO_8859_1:
200
+ # ö - f6 in hex, \366 in octal
201
+ # ü - fc in hex, \374 in octal
202
+
203
+ xml = <<-EOS
204
+ <bands>
205
+ <metal>m\366tley_cr\374e</metal>
206
+ </bands>
207
+ EOS
208
+
209
+ # Parse as UTF_8
210
+ parser = XML::Parser.string(xml, :encoding => XML::Encoding::UTF_8)
211
+
212
+ error = assert_raises(XML::Error) do
213
+ doc = parser.parse
214
+ end
215
+
216
+ assert_equal("Fatal error: Input is not proper UTF-8, indicate encoding !\nBytes: 0xF6 0x74 0x6C 0x65 at :2.",
217
+ error.to_s)
218
+
219
+ # Parse as ISO_8859_1:
220
+ parser = XML::Parser.string(xml, :encoding => XML::Encoding::ISO_8859_1)
221
+ doc = parser.parse
222
+ node = doc.find_first('//metal')
223
+ if defined?(Encoding)
224
+ assert_equal(Encoding::UTF_8, node.content.encoding)
225
+ assert_equal("m\303\266tley_cr\303\274e", node.content)
226
+ else
227
+ assert_equal("m\303\266tley_cr\303\274e", node.content)
228
+ end
229
+ end
230
+
231
+ def test_fd_gc
232
+ # Test opening # of documents up to the file limit for the OS.
233
+ # Ideally it should run until libxml emits a warning,
234
+ # thereby knowing we've done a GC sweep. For the time being,
235
+ # re-open the same doc `limit descriptors` times.
236
+ # If we make it to the end, then we've succeeded,
237
+ # otherwise an exception will be thrown.
238
+ XML::Error.set_handler {|error|}
239
+
240
+ max_fd = if RUBY_PLATFORM.match(/mswin32|mingw/i)
241
+ 500
242
+ else
243
+ Process.getrlimit(Process::RLIMIT_NOFILE)[0] + 1
244
+ end
245
+
246
+ file = File.join(File.dirname(__FILE__), 'model/rubynet.xml')
247
+ max_fd.times do
248
+ XML::Parser.file(file).parse
249
+ end
250
+ XML::Error.reset_handler {|error|}
251
+ end
252
+
253
+ def test_open_many_files
254
+ 1000.times do
255
+ doc = XML::Parser.file('model/atom.xml').parse
256
+ end
257
+ end
258
+
259
+ # ----- Errors ------
260
+ def test_error
261
+ error = assert_raises(XML::Error) do
262
+ XML::Parser.string('<foo><bar/></foz>').parse
263
+ end
264
+
265
+ refute_nil(error)
266
+ assert_kind_of(XML::Error, error)
267
+ assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", error.message)
268
+ assert_equal(XML::Error::PARSER, error.domain)
269
+ assert_equal(XML::Error::TAG_NAME_MISMATCH, error.code)
270
+ assert_equal(XML::Error::FATAL, error.level)
271
+ assert_nil(error.file)
272
+ assert_equal(1, error.line)
273
+ assert_equal('foo', error.str1)
274
+ assert_equal('foz', error.str2)
275
+ assert_nil(error.str3)
276
+ assert_equal(1, error.int1)
277
+ assert_equal(20, error.int2)
278
+ assert_nil(error.node)
279
+ end
280
+
281
+ def test_bad_xml
282
+ parser = XML::Parser.string('<ruby_array uga="booga" foo="bar"<fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>')
283
+ error = assert_raises(XML::Error) do
284
+ refute_nil(parser.parse)
285
+ end
286
+
287
+ refute_nil(error)
288
+ assert_kind_of(XML::Error, error)
289
+ assert_equal("Fatal error: Extra content at the end of the document at :1.", error.message)
290
+ assert_equal(XML::Error::PARSER, error.domain)
291
+ assert_equal(XML::Error::DOCUMENT_END, error.code)
292
+ assert_equal(XML::Error::FATAL, error.level)
293
+ assert_nil(error.file)
294
+ assert_equal(1, error.line)
295
+ assert_nil(error.str1)
296
+ assert_nil(error.str2)
297
+ assert_nil(error.str3)
298
+ assert_equal(0, error.int1)
299
+ assert_equal(34, error.int2)
300
+ assert_nil(error.node)
301
+ end
302
+
303
+ # Deprecated methods
304
+ def test_document_deprecated
305
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.utf-8.xml'))
306
+ parser = XML::Parser.file(file)
307
+ doc = parser.parse
308
+
309
+ parser = XML::Parser.new
310
+ parser.document = doc
311
+ doc = parser.parse
312
+
313
+ assert_instance_of(XML::Document, doc)
314
+ assert_instance_of(XML::Parser::Context, parser.context)
315
+ end
316
+
317
+ def test_file_deprecated
318
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
319
+
320
+ parser = XML::Parser.new
321
+ parser.file = file
322
+ doc = parser.parse
323
+ assert_instance_of(XML::Document, doc)
324
+ assert_instance_of(XML::Parser::Context, parser.context)
325
+ end
326
+
327
+ def test_io_deprecated
328
+ File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
329
+ parser = XML::Parser.new
330
+ assert_instance_of(XML::Parser, parser)
331
+ parser.io = io
332
+
333
+ doc = parser.parse
334
+ assert_instance_of(XML::Document, doc)
335
+ assert_instance_of(XML::Parser::Context, parser.context)
336
+ end
337
+ end
338
+
339
+ def test_string_deprecated
340
+ str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
341
+
342
+ parser = XML::Parser.new
343
+ parser.string = str
344
+ assert_instance_of(XML::Parser, parser)
345
+
346
+ doc = parser.parse
347
+ assert_instance_of(XML::Document, doc)
348
+ assert_instance_of(XML::Parser::Context, parser.context)
349
+ end
350
+
351
+ def test_errors_from_background_thread
352
+ errors = []
353
+ background_errors = []
354
+
355
+ begin
356
+ XML::Error.set_handler do |error|
357
+ errors << error
358
+ end
359
+ parser = XML::Parser.string("<moo>")
360
+
361
+ thread = Thread.new do
362
+ XML::Error.set_handler do |error|
363
+ background_errors << error
364
+ end
365
+ parser.parse rescue nil
366
+ end
367
+ thread.join
368
+ ensure
369
+ XML::Error.set_handler(&XML::Error::QUIET_HANDLER)
370
+ end
371
+
372
+ assert_equal(errors.size, 0)
373
+ assert_equal(background_errors.size, 1)
374
+ end
375
+ end