libxml-ruby 0.8.3 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. data/CHANGES +18 -0
  2. data/RAKEFILE +15 -39
  3. data/README +48 -47
  4. data/ext/libxml/libxml.c +847 -22
  5. data/ext/libxml/ruby_libxml.h +71 -95
  6. data/ext/libxml/ruby_xml_attr.c +500 -500
  7. data/ext/libxml/ruby_xml_attributes.c +1 -1
  8. data/ext/libxml/ruby_xml_document.c +1144 -1135
  9. data/ext/libxml/ruby_xml_document.h +4 -11
  10. data/ext/libxml/ruby_xml_dtd.c +27 -0
  11. data/ext/libxml/ruby_xml_encoding.c +164 -0
  12. data/ext/libxml/ruby_xml_encoding.h +13 -0
  13. data/ext/libxml/ruby_xml_error.c +941 -0
  14. data/ext/libxml/ruby_xml_error.h +13 -0
  15. data/ext/libxml/ruby_xml_html_parser.c +71 -387
  16. data/ext/libxml/ruby_xml_html_parser.h +1 -17
  17. data/ext/libxml/ruby_xml_input.c +179 -0
  18. data/ext/libxml/ruby_xml_input.h +18 -0
  19. data/ext/libxml/ruby_xml_input_cbg.c +17 -3
  20. data/ext/libxml/ruby_xml_node.c +1566 -1582
  21. data/ext/libxml/ruby_xml_node.h +1 -4
  22. data/ext/libxml/ruby_xml_ns.c +14 -3
  23. data/ext/libxml/ruby_xml_parser.c +164 -1398
  24. data/ext/libxml/ruby_xml_parser.h +5 -17
  25. data/ext/libxml/ruby_xml_parser_context.c +131 -169
  26. data/ext/libxml/ruby_xml_parser_context.h +2 -9
  27. data/ext/libxml/ruby_xml_reader.c +910 -945
  28. data/ext/libxml/ruby_xml_relaxng.c +32 -3
  29. data/ext/libxml/ruby_xml_sax_parser.c +106 -364
  30. data/ext/libxml/ruby_xml_sax_parser.h +1 -37
  31. data/ext/libxml/ruby_xml_schema.c +174 -145
  32. data/ext/libxml/ruby_xml_xinclude.c +9 -5
  33. data/ext/libxml/ruby_xml_xpath.c +25 -6
  34. data/ext/libxml/ruby_xml_xpath.h +1 -2
  35. data/ext/libxml/ruby_xml_xpath_context.c +17 -19
  36. data/ext/libxml/ruby_xml_xpath_object.c +60 -56
  37. data/ext/libxml/ruby_xml_xpointer.c +11 -5
  38. data/ext/libxml/sax_parser_callbacks.inc +42 -37
  39. data/ext/libxml/version.h +3 -3
  40. data/ext/mingw/Rakefile +20 -27
  41. data/ext/mingw/build.rake +41 -0
  42. data/ext/vc/libxml_ruby.vcproj +23 -15
  43. data/lib/libxml.rb +8 -2
  44. data/lib/libxml/document.rb +16 -4
  45. data/lib/libxml/error.rb +84 -0
  46. data/lib/libxml/hpricot.rb +76 -0
  47. data/lib/libxml/html_parser.rb +61 -0
  48. data/lib/libxml/node.rb +36 -25
  49. data/lib/libxml/parser.rb +312 -33
  50. data/lib/libxml/parser_context.rb +17 -0
  51. data/lib/libxml/properties.rb +15 -2
  52. data/lib/libxml/reader.rb +15 -0
  53. data/lib/libxml/sax_callbacks.rb +179 -0
  54. data/lib/libxml/sax_parser.rb +42 -0
  55. data/lib/libxml/tree.rb +1 -2
  56. data/lib/libxml/xpath_object.rb +12 -0
  57. data/test/model/atom.xml +4 -0
  58. data/test/tc_attributes.rb +43 -19
  59. data/test/tc_document.rb +1 -1
  60. data/test/tc_document_write.rb +15 -8
  61. data/test/tc_dtd.rb +36 -20
  62. data/test/tc_encoding.rb +13 -0
  63. data/test/tc_error.rb +136 -0
  64. data/test/tc_node.rb +2 -3
  65. data/test/tc_node_copy.rb +1 -1
  66. data/test/tc_node_edit.rb +6 -0
  67. data/test/tc_ns.rb +18 -0
  68. data/test/tc_parser.rb +113 -228
  69. data/test/tc_parser_context.rb +1 -2
  70. data/test/tc_reader.rb +24 -14
  71. data/test/tc_relaxng.rb +18 -6
  72. data/test/tc_sax_parser.rb +48 -13
  73. data/test/tc_schema.rb +20 -8
  74. data/test/tc_well_formed.rb +2 -1
  75. data/test/tc_xml.rb +212 -0
  76. data/test/tc_xpath.rb +60 -46
  77. data/test/tc_xpointer.rb +7 -11
  78. data/test/test_suite.rb +4 -3
  79. metadata +26 -109
  80. data/doc/rdoc/classes/LibXML.html +0 -241
  81. data/doc/rdoc/classes/LibXML/XML.html +0 -185
  82. data/doc/rdoc/classes/LibXML/XML/Attr.html +0 -1010
  83. data/doc/rdoc/classes/LibXML/XML/Attributes.html +0 -526
  84. data/doc/rdoc/classes/LibXML/XML/Document.html +0 -1489
  85. data/doc/rdoc/classes/LibXML/XML/Dtd.html +0 -213
  86. data/doc/rdoc/classes/LibXML/XML/Error.html +0 -117
  87. data/doc/rdoc/classes/LibXML/XML/HTMLParser.html +0 -348
  88. data/doc/rdoc/classes/LibXML/XML/InputCallbacks.html +0 -160
  89. data/doc/rdoc/classes/LibXML/XML/NS.html +0 -381
  90. data/doc/rdoc/classes/LibXML/XML/Node.html +0 -3396
  91. data/doc/rdoc/classes/LibXML/XML/Node/FailedModify.html +0 -123
  92. data/doc/rdoc/classes/LibXML/XML/Node/Set.html +0 -440
  93. data/doc/rdoc/classes/LibXML/XML/Node/SetNamespace.html +0 -123
  94. data/doc/rdoc/classes/LibXML/XML/Node/UnknownType.html +0 -123
  95. data/doc/rdoc/classes/LibXML/XML/Parser.html +0 -2239
  96. data/doc/rdoc/classes/LibXML/XML/Parser/Context.html +0 -1255
  97. data/doc/rdoc/classes/LibXML/XML/Parser/ParseError.html +0 -123
  98. data/doc/rdoc/classes/LibXML/XML/Reader.html +0 -2264
  99. data/doc/rdoc/classes/LibXML/XML/RelaxNG.html +0 -237
  100. data/doc/rdoc/classes/LibXML/XML/SaxParser.html +0 -415
  101. data/doc/rdoc/classes/LibXML/XML/Schema.html +0 -308
  102. data/doc/rdoc/classes/LibXML/XML/State.html +0 -124
  103. data/doc/rdoc/classes/LibXML/XML/Tree.html +0 -111
  104. data/doc/rdoc/classes/LibXML/XML/XInclude.html +0 -123
  105. data/doc/rdoc/classes/LibXML/XML/XInclude/Error.html +0 -117
  106. data/doc/rdoc/classes/LibXML/XML/XMLParserOptions.html +0 -198
  107. data/doc/rdoc/classes/LibXML/XML/XPath.html +0 -184
  108. data/doc/rdoc/classes/LibXML/XML/XPath/Context.html +0 -404
  109. data/doc/rdoc/classes/LibXML/XML/XPath/InvalidPath.html +0 -172
  110. data/doc/rdoc/classes/LibXML/XML/XPath/Object.html +0 -627
  111. data/doc/rdoc/classes/LibXML/XML/XPointer.html +0 -170
  112. data/doc/rdoc/classes/LibXML/XML/XPointer/Context.html +0 -123
  113. data/doc/rdoc/classes/LibXML/XML/XPointer/Context/InvalidPath.html +0 -117
  114. data/doc/rdoc/classes/LibXML/XML/XPointer/InvalidExpression.html +0 -124
  115. data/doc/rdoc/classes/singleton.html +0 -114
  116. data/doc/rdoc/created.rid +0 -1
  117. data/doc/rdoc/files/CHANGES.html +0 -442
  118. data/doc/rdoc/files/LICENSE.html +0 -133
  119. data/doc/rdoc/files/README.html +0 -388
  120. data/doc/rdoc/files/VERSION.html +0 -107
  121. data/doc/rdoc/files/ext/libxml/cbg_c.html +0 -101
  122. data/doc/rdoc/files/ext/libxml/libxml_c.html +0 -101
  123. data/doc/rdoc/files/ext/libxml/ruby_xml_attr_c.html +0 -101
  124. data/doc/rdoc/files/ext/libxml/ruby_xml_attributes_c.html +0 -101
  125. data/doc/rdoc/files/ext/libxml/ruby_xml_document_c.html +0 -101
  126. data/doc/rdoc/files/ext/libxml/ruby_xml_dtd_c.html +0 -101
  127. data/doc/rdoc/files/ext/libxml/ruby_xml_html_parser_c.html +0 -101
  128. data/doc/rdoc/files/ext/libxml/ruby_xml_input_cbg_c.html +0 -101
  129. data/doc/rdoc/files/ext/libxml/ruby_xml_node_c.html +0 -101
  130. data/doc/rdoc/files/ext/libxml/ruby_xml_node_set_c.html +0 -101
  131. data/doc/rdoc/files/ext/libxml/ruby_xml_ns_c.html +0 -101
  132. data/doc/rdoc/files/ext/libxml/ruby_xml_parser_c.html +0 -101
  133. data/doc/rdoc/files/ext/libxml/ruby_xml_parser_context_c.html +0 -101
  134. data/doc/rdoc/files/ext/libxml/ruby_xml_reader_c.html +0 -101
  135. data/doc/rdoc/files/ext/libxml/ruby_xml_relaxng_c.html +0 -101
  136. data/doc/rdoc/files/ext/libxml/ruby_xml_sax_parser_c.html +0 -101
  137. data/doc/rdoc/files/ext/libxml/ruby_xml_schema_c.html +0 -101
  138. data/doc/rdoc/files/ext/libxml/ruby_xml_state_c.html +0 -101
  139. data/doc/rdoc/files/ext/libxml/ruby_xml_xinclude_c.html +0 -101
  140. data/doc/rdoc/files/ext/libxml/ruby_xml_xpath_c.html +0 -101
  141. data/doc/rdoc/files/ext/libxml/ruby_xml_xpath_context_c.html +0 -101
  142. data/doc/rdoc/files/ext/libxml/ruby_xml_xpath_object_c.html +0 -101
  143. data/doc/rdoc/files/ext/libxml/ruby_xml_xpointer_c.html +0 -101
  144. data/doc/rdoc/files/ext/libxml/ruby_xml_xpointer_context_c.html +0 -101
  145. data/doc/rdoc/files/lib/libxml/attr_rb.html +0 -108
  146. data/doc/rdoc/files/lib/libxml/attributes_rb.html +0 -108
  147. data/doc/rdoc/files/lib/libxml/document_rb.html +0 -108
  148. data/doc/rdoc/files/lib/libxml/node_rb.html +0 -108
  149. data/doc/rdoc/files/lib/libxml/node_set_rb.html +0 -108
  150. data/doc/rdoc/files/lib/libxml/parser_options_rb.html +0 -107
  151. data/doc/rdoc/files/lib/libxml/parser_rb.html +0 -101
  152. data/doc/rdoc/files/lib/libxml/properties_rb.html +0 -108
  153. data/doc/rdoc/files/lib/libxml/tree_rb.html +0 -107
  154. data/doc/rdoc/files/lib/libxml_rb.html +0 -124
  155. data/doc/rdoc/files/lib/xml/libxml_rb.html +0 -124
  156. data/doc/rdoc/files/lib/xml_rb.html +0 -134
  157. data/doc/rdoc/fr_class_index.html +0 -62
  158. data/doc/rdoc/fr_file_index.html +0 -66
  159. data/doc/rdoc/fr_method_index.html +0 -392
  160. data/doc/rdoc/index.html +0 -24
  161. data/doc/rdoc/rdoc-style.css +0 -208
  162. data/ext/libxml/ruby_xml_node_set.c +0 -172
  163. data/ext/libxml/ruby_xml_node_set.h +0 -20
  164. data/ext/libxml/ruby_xml_xpointer_context.c +0 -22
  165. data/ext/libxml/ruby_xml_xpointer_context.h +0 -18
  166. data/lib/libxml/node_set.rb +0 -27
  167. data/test/tc_node_set.rb +0 -24
  168. data/test/tc_node_set2.rb +0 -37
@@ -1,7 +1,7 @@
1
1
  require "xml"
2
2
  require 'test/unit'
3
3
 
4
- class TC_XML_Document_Write < Test::Unit::TestCase
4
+ class TestDocumentWrite < Test::Unit::TestCase
5
5
  def setup
6
6
  @doc = XML::Document.new('1.0')
7
7
  end
@@ -35,13 +35,6 @@ class TC_XML_Document_Write < Test::Unit::TestCase
35
35
  assert_equal("<?xml version=\"1.0\"?>\n<rubynet/>\n", @doc.to_s(false))
36
36
  end
37
37
 
38
- def test_encoding
39
- @doc.root = XML::Node.new('rubynet')
40
- @doc.encoding = 'UTF-8'
41
- assert_instance_of(XML::Node, @doc.root)
42
- assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rubynet/>\n", @doc.to_s)
43
- end
44
-
45
38
  def test_child()
46
39
  xml = "<?xml version=\"1.0\"?>\n<rubynet>\n <pkg/>\n</rubynet>\n"
47
40
  @doc.root = XML::Node.new('rubynet')
@@ -136,4 +129,18 @@ class TC_XML_Document_Write < Test::Unit::TestCase
136
129
  pkgname = meta.child = XML::Node.new('pkgname', 'libxml')
137
130
  assert_equal(xml, @doc.to_s)
138
131
  end
132
+
133
+ def test_encoding_utf
134
+ @doc.root = XML::Node.new('rubynet')
135
+ @doc.encoding = XML::Encoding.encoding_to_s(XML::Encoding::UTF8)
136
+ assert_instance_of(XML::Node, @doc.root)
137
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<rubynet/>\n", @doc.to_s)
138
+ end
139
+
140
+ def test_encoding_latin1
141
+ @doc.root = XML::Node.new('rubynet')
142
+ @doc.encoding = XML::Encoding.encoding_to_s(XML::Encoding::ISO_8859_1)
143
+ assert_instance_of(XML::Node, @doc.root)
144
+ assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<rubynet/>\n", @doc.to_s)
145
+ end
139
146
  end
@@ -31,20 +31,32 @@ class TestDtd < Test::Unit::TestCase
31
31
  def test_valid
32
32
  assert(@doc.validate(dtd))
33
33
  end
34
-
34
+
35
35
  def test_invalid
36
36
  new_node = XML::Node.new('invalid', 'this will mess up validation')
37
37
  @doc.root.child_add(new_node)
38
-
38
+
39
39
  messages = Hash.new
40
- assert(!@doc.validate(dtd) do |message, error|
41
- messages[message] = error
42
- end)
40
+ error = assert_raise(XML::Error) do
41
+ @doc.validate(dtd)
42
+ end
43
43
 
44
- expected = {"No declaration for element invalid\n" => true,
45
- "Element root content does not follow the DTD, expecting (head , descr), got (head descr invalid)\n" => true}
46
-
47
- assert_equal(expected, messages)
44
+ # Check the error worked
45
+ assert_not_nil(error)
46
+ assert_kind_of(XML::Error, error)
47
+ assert_equal("Error: No declaration for element invalid at :0.", error.message)
48
+ assert_equal(XML::Error::VALID, error.domain)
49
+ assert_equal(XML::Error::DTD_UNKNOWN_ELEM, error.code)
50
+ assert_equal(XML::Error::ERROR, error.level)
51
+ assert_nil(error.file)
52
+ assert_nil(error.line)
53
+ assert_equal('invalid', error.str1)
54
+ assert_equal('invalid', error.str2)
55
+ assert_nil(error.str3)
56
+ assert_equal(0, error.int1)
57
+ assert_equal(0, error.int2)
58
+ assert_not_nil(error.node)
59
+ assert_equal('invalid', error.node.name)
48
60
  end
49
61
 
50
62
  def test_external_dtd
@@ -54,17 +66,21 @@ class TestDtd < Test::Unit::TestCase
54
66
  <title>T1</title>
55
67
  </test>
56
68
  EOS
57
-
58
- messages = Array.new
59
- XML::Parser.register_error_handler(lambda { |msg| messages << msg })
60
-
61
- XML::Parser.default_load_external_dtd = false
69
+
70
+ errors = Array.new
71
+ XML::Error.set_handler do |error|
72
+ errors << error
73
+ end
74
+
75
+ XML.default_load_external_dtd = false
62
76
  doc = XML::Parser.string(xml).parse
63
- assert_equal(Array.new, messages)
64
-
65
- XML::Parser.default_load_external_dtd = true
77
+ assert_equal(Array.new, errors)
78
+
79
+ XML.default_load_external_dtd = true
66
80
  doc = XML::Parser.string(xml).parse
67
- assert_equal('I/O warning : failed to load external entity "test.dtd" <!DOCTYPE test PUBLIC "-//TEST" "test.dtd" []> ^',
68
- messages.map{|msg| msg.strip}.join(' '))
81
+ assert_equal("Warning: failed to load external entity \"test.dtd\" at :1.",
82
+ errors.map do |error|
83
+ error.to_s
84
+ end.join(' '))
69
85
  end
70
- end
86
+ end
@@ -0,0 +1,13 @@
1
+ require 'xml'
2
+ require 'test/unit'
3
+
4
+ class TestEncoding < Test::Unit::TestCase
5
+ def test_latin1
6
+ assert_equal(XML::Encoding::ISO_8859_1, 10)
7
+ end
8
+
9
+ def test_latin1_to_s
10
+ encoding_str = XML::Encoding.encoding_to_s(XML::Encoding::ISO_8859_1)
11
+ assert_equal('ISO-8859-1', encoding_str)
12
+ end
13
+ end
@@ -0,0 +1,136 @@
1
+ require 'xml'
2
+ require 'test/unit'
3
+ require 'stringio'
4
+
5
+ class TestError < Test::Unit::TestCase
6
+ def test_invalid_handler
7
+ assert_raises(RuntimeError) do
8
+ XML::Error.set_handler
9
+ end
10
+ end
11
+
12
+ def test_handler
13
+ exception = nil
14
+ XML::Error.set_handler do |error|
15
+ exception = error
16
+ end
17
+
18
+ # Raise the error
19
+ XML::Reader.new('<foo').read
20
+
21
+ # Check the handler worked
22
+ assert_not_nil(exception)
23
+ assert_kind_of(XML::Error, exception)
24
+ assert_equal("Fatal error: Couldn't find end of Start Tag foo at :1.", exception.message)
25
+ assert_equal(XML::Error::XML_FROM_PARSER, exception.domain)
26
+ assert_equal(XML::Error::XML_ERR_GT_REQUIRED, exception.code)
27
+ assert_equal(XML::Error::XML_ERR_FATAL, exception.level)
28
+ assert_nil(exception.file)
29
+ assert_equal(1, exception.line)
30
+ assert_equal('foo', exception.str1)
31
+ assert_nil(exception.str2)
32
+ assert_nil(exception.str3)
33
+ assert_equal(0, exception.int1)
34
+ assert_equal(5, exception.int2)
35
+ assert_nil(exception.node)
36
+ end
37
+
38
+ def test_reset_handler
39
+ exception = nil
40
+ XML::Error.set_handler do |error|
41
+ exception = error
42
+ end
43
+
44
+ XML::Error.reset_handler
45
+ reader = XML::Reader.new('<foo')
46
+ assert_nil(exception)
47
+ end
48
+
49
+ def test_verbose_handler
50
+ XML::Error.set_handler(&XML::Error::VERBOSE_HANDLER)
51
+ output = StringIO.new
52
+ original_stderr = Object::STDERR
53
+
54
+ Object.const_set(:STDERR, output)
55
+ begin
56
+ assert_raise(XML::Error) do
57
+ XML::Parser.string('<foo><bar/></foz>').parse
58
+ end
59
+ ensure
60
+ Object.const_set(:STDERR, original_stderr)
61
+ end
62
+ assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.\n", output.string)
63
+ end
64
+
65
+ def test_no_hanlder
66
+ XML::Error.reset_handler
67
+ output = StringIO.new
68
+ original_stderr = Object::STDERR
69
+
70
+ Object.const_set(:STDERR, output)
71
+ begin
72
+ assert_raise(XML::Error) do
73
+ XML::Parser.string('<foo><bar/></foz>').parse
74
+ end
75
+ ensure
76
+ Object.const_set(:STDERR, original_stderr)
77
+ end
78
+ assert_equal('', output.string)
79
+ end
80
+
81
+ def test_parse_error
82
+ exception = assert_raise(XML::Error) do
83
+ XML::Parser.string('<foo><bar/></foz>').parse
84
+ end
85
+
86
+ assert_instance_of(XML::Error, exception)
87
+ assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", exception.message)
88
+ assert_equal(XML::Error::XML_FROM_PARSER, exception.domain)
89
+ assert_equal(XML::Error::XML_ERR_TAG_NAME_MISMATCH, exception.code)
90
+ assert_equal(XML::Error::XML_ERR_FATAL, exception.level)
91
+ assert_nil(exception.file)
92
+ assert_equal(1, exception.line)
93
+ end
94
+
95
+ def test_xpath_error
96
+ doc = XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
97
+
98
+ exception = assert_raise(XML::Error) do
99
+ elements = doc.find('/foo[bar=test')
100
+ end
101
+
102
+ assert_instance_of(XML::Error, exception)
103
+ assert_equal("Error: Invalid predicate at :0.", exception.message)
104
+ assert_equal(XML::Error::XML_FROM_XPATH, exception.domain)
105
+ assert_equal(XML::Error::XML_XPATH_INVALID_PREDICATE_ERROR, exception.code)
106
+ assert_equal(XML::Error::XML_ERR_ERROR, exception.level)
107
+ assert_nil(exception.file)
108
+ assert_nil(nil)
109
+ end
110
+
111
+ def test_double_parse
112
+ XML::Parser.register_error_handler(lambda {|msg| nil })
113
+ parser = XML::Parser.string("<test>something</test>")
114
+ doc = parser.parse
115
+
116
+ error = assert_raise(RuntimeError) do
117
+ # Try parsing a second time
118
+ parser.parse
119
+ end
120
+
121
+ assert_equal("You cannot parse a data source twice", error.to_s)
122
+ end
123
+
124
+ def test_libxml_parser_empty_string
125
+ xp = XML::Parser.new
126
+
127
+ assert_raise(TypeError) do
128
+ xp.string = nil
129
+ end
130
+
131
+ xp.string = ''
132
+ assert_raise(XML::Error) do
133
+ xp.parse
134
+ end
135
+ end
136
+ end
@@ -54,9 +54,8 @@ class TestNode < Test::Unit::TestCase
54
54
  end
55
55
 
56
56
  def test_node_find
57
- set = @doc.root.find('./fixnum').set
58
- assert_instance_of(XML::Node::Set, set)
59
- for node in set
57
+ nodes = @doc.root.find('./fixnum')
58
+ for node in nodes
60
59
  assert_instance_of(XML::Node, node)
61
60
  end
62
61
  end
@@ -1,4 +1,4 @@
1
- require "xml"
1
+ require 'xml'
2
2
  require 'test/unit'
3
3
 
4
4
  # see mailing list archive
@@ -88,4 +88,10 @@ class TestNodeEdit < Test::Unit::TestCase
88
88
  master_doc
89
89
  end
90
90
  end
91
+
92
+ def test_append_chain
93
+ node = XML::Node.new('foo') << XML::Node.new('bar') << "bars contents"
94
+ assert_equal('<foo><bar/>bars contents</foo>',
95
+ node.to_s)
96
+ end
91
97
  end
@@ -0,0 +1,18 @@
1
+ require "xml"
2
+ require 'test/unit'
3
+
4
+ class TestNS < Test::Unit::TestCase
5
+ def test_ns
6
+ node = XML::Node.new('foo')
7
+ ns = XML::NS.new(node, 'http://www.mynamespace.com', 'my_namepace')
8
+ assert_equal(ns.prefix, 'my_namepace')
9
+ assert_equal(ns.href, 'http://www.mynamespace.com')
10
+ end
11
+
12
+ def test_default_ns
13
+ node = XML::Node.new('foo')
14
+ ns = XML::NS.new(node, 'http://www.mynamespace.com', nil)
15
+ assert_equal(ns.prefix, nil)
16
+ assert_equal(ns.href, 'http://www.mynamespace.com')
17
+ end
18
+ end
@@ -1,294 +1,179 @@
1
- require "xml"
1
+ require 'xml'
2
2
  require 'test/unit'
3
+ require 'stringio'
3
4
 
4
5
  class TestParser < Test::Unit::TestCase
5
6
  def setup
6
- XML::Parser.register_error_handler(nil)
7
+ XML::Error.set_handler(&XML::Error::QUIET_HANDLER)
7
8
  @xp = XML::Parser.new
8
9
  end
9
10
 
10
11
  def teardown
11
12
  @xp = nil
12
- XML::Parser.register_error_handler(nil)
13
- # Clear out all the files we opened up in
14
- # the test_fd_gc test
13
+ GC.start
14
+ GC.start
15
15
  GC.start
16
16
  end
17
17
 
18
- # ----- Constants ------
19
- def test_lib_versions
20
- assert(XML::Parser::check_lib_versions)
21
- end
22
-
23
- def test_debug_entities
24
- XML::Parser::debug_entities = false
25
- assert(!XML::Parser::debug_entities)
26
-
27
- XML::Parser::debug_entities = true
28
- assert(XML::Parser::debug_entities)
29
-
30
- XML::Parser::debug_entities = false
31
- assert(!XML::Parser::debug_entities)
32
- end
33
-
34
- def test_default_compression
35
- return unless XML::Parser::default_compression
36
-
37
- 0.upto(9) do |i|
38
- XML::Parser::default_compression = i
39
- assert_equal(i, XML::Parser::default_compression)
40
- end
41
-
42
- 9.downto(0) do |i|
43
- assert_equal(i, XML::Parser::default_compression = i)
44
- assert_equal(i, XML::Parser::default_compression)
45
- end
46
-
47
- 0.downto(-10) do |i|
48
- assert_equal(i, XML::Parser::default_compression = i)
49
- assert_equal(0, XML::Parser::default_compression)
50
- end
51
-
52
- 10.upto(20) do |i|
53
- assert_equal(i, XML::Parser::default_compression = i)
54
- assert_equal(9, XML::Parser::default_compression)
55
- end
56
- end
57
-
58
- def test_default_keep_blanks
59
- XML::Parser::default_keep_blanks = false
60
- assert(!XML::Parser::default_keep_blanks)
61
-
62
- XML::Parser::default_keep_blanks = true
63
- assert(XML::Parser::default_keep_blanks)
64
-
65
- XML::Parser::default_keep_blanks = false
66
- assert(!XML::Parser::default_keep_blanks)
67
-
68
- # other tests depend on keeping blanks by default,
69
- # which is the default default behaviour anyway.
70
- XML::Parser::default_keep_blanks = true
71
- end
72
-
73
- def test_default_line_numbers
74
- XML::Parser::default_line_numbers = false
75
- assert(!XML::Parser::default_line_numbers)
76
-
77
- XML::Parser::default_line_numbers = true
78
- assert(XML::Parser::default_line_numbers)
79
-
80
- XML::Parser::default_line_numbers = false
81
- assert(!XML::Parser::default_line_numbers)
82
- end
18
+ # ----- Sources ------
19
+ def test_file
20
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
83
21
 
84
- def test_default_substitute_entities
85
- XML::Parser::default_substitute_entities = false
86
- assert(!XML::Parser::default_substitute_entities)
22
+ @xp.file = file
23
+ assert_equal(file, @xp.file)
24
+ assert_equal(file, @xp.input.file)
87
25
 
88
- XML::Parser::default_substitute_entities = true
89
- assert(XML::Parser::default_substitute_entities)
26
+ doc = @xp.parse
27
+ assert_instance_of(XML::Document, doc)
28
+ assert_instance_of(XML::Parser::Context, @xp.context)
29
+ GC.start
30
+ GC.start
31
+ GC.start
32
+ end
90
33
 
91
- XML::Parser::default_substitute_entities = false
92
- assert(!XML::Parser::default_substitute_entities)
93
- end
34
+ def test_file_class
35
+ file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
94
36
 
95
- def test_default_tree_indent_string
96
- s = XML::Parser::default_tree_indent_string
97
- assert_instance_of(String, s)
98
- assert_equal(' ', s)
99
- XML::Parser::default_tree_indent_string = 'uga'
100
- s = XML::Parser::default_tree_indent_string
101
- assert_instance_of(String, s)
102
- assert_equal('uga', s)
103
- XML::Parser::default_tree_indent_string = ' '
104
- s = XML::Parser::default_tree_indent_string
105
- assert_instance_of(String, s)
106
- assert_equal(' ', s)
37
+ xp = XML::Parser.file(file)
38
+ assert_instance_of(XML::Parser, xp)
39
+ assert_equal(file, xp.file)
40
+ assert_equal(file, xp.input.file)
107
41
  end
108
42
 
109
- def test_default_validity_checking
110
- XML::Parser::default_validity_checking = false
111
- assert(!XML::Parser::default_validity_checking)
43
+ def test_string
44
+ str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
112
45
 
113
- XML::Parser::default_validity_checking = true
114
- assert(XML::Parser::default_validity_checking)
46
+ @xp.string = str
47
+ assert_equal(str, @xp.string)
48
+ assert_equal(str, @xp.input.string)
115
49
 
116
- XML::Parser::default_validity_checking = false
117
- assert(!XML::Parser::default_validity_checking)
50
+ doc = @xp.parse
51
+ assert_instance_of(XML::Document, doc)
52
+ assert_instance_of(XML::Parser::Context, @xp.context)
118
53
  end
119
54
 
120
- def test_default_warnings
121
- XML::Parser::default_warnings = false
122
- assert(!XML::Parser::default_warnings)
123
-
124
- XML::Parser::default_warnings = true
125
- assert(XML::Parser::default_warnings)
55
+ def test_string_empty
56
+ assert_raise(XML::Error) do
57
+ @xp.string = ''
58
+ @xp.parse
59
+ end
126
60
 
127
- XML::Parser::default_warnings = false
128
- assert(!XML::Parser::default_warnings)
61
+ assert_raise(TypeError) do
62
+ @xp.string = nil
63
+ end
129
64
  end
130
65
 
131
- def test_intent_tree_output
132
- assert(TrueClass, XML::Parser::indent_tree_output)
133
-
134
- XML::Parser::indent_tree_output = false
135
- assert(FalseClass, XML::Parser::indent_tree_output)
66
+ def test_string_class
67
+ str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
136
68
 
137
- XML::Parser::indent_tree_output = true
138
- assert(TrueClass, XML::Parser::indent_tree_output)
69
+ xp = XML::Parser.string(str)
70
+ assert_instance_of(XML::Parser, xp)
71
+ assert_equal(str, xp.string)
72
+ assert_equal(str, xp.input.string)
139
73
  end
140
74
 
141
- def test_version
142
- assert_instance_of(String, XML::Parser::VERSION)
143
- end
75
+ def test_io
76
+ File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
77
+ @xp.io = io
78
+ assert_equal(io, @xp.io)
79
+ assert_equal(io, @xp.input.io)
144
80
 
145
- def test_vernum
146
- assert_instance_of(Fixnum, XML::Parser::VERNUM)
81
+ doc = @xp.parse
82
+ assert_instance_of(XML::Document, doc)
83
+ assert_instance_of(XML::Parser::Context, @xp.context)
84
+ end
147
85
  end
148
86
 
149
- # ----- IO ------
150
- def test_string
151
- str = '<ruby_array uga="booga" foo="bar"><fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
152
- assert_equal(str, @xp.string = str)
153
- assert_instance_of(XML::Document, @xp.parse)
154
- end
155
-
156
- def test_context
157
- @xp = XML::Parser.string('<rubynet><testing>uga</testing><uga>foo</uga></rubynet>')
158
- doc = @xp.parse
159
- assert_instance_of(XML::Document, doc)
160
- assert_instance_of(XML::Parser::Context, @xp.context)
87
+ def test_io_class
88
+ File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |io|
89
+ xp = XML::Parser.io(io)
90
+ assert_instance_of(XML::Parser, xp)
91
+ assert_equal(io, xp.io)
92
+ assert_equal(io, xp.input.io)
93
+ end
161
94
  end
162
95
 
163
- def test_file
164
- file = File.expand_path(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
165
-
166
- @xp.filename = file
167
- assert_equal(file, @xp.filename)
168
-
169
- doc = @xp.parse
170
- assert_instance_of(XML::Document, doc)
96
+ def test_string_io
97
+ data = File.read(File.join(File.dirname(__FILE__), 'model/rubynet.xml'))
98
+ string_io = StringIO.new(data)
99
+ assert_raises(TypeError) do
100
+ @xp.io = string_io
101
+ end
171
102
  end
172
103
 
173
- def test_io
174
- File.open(File.join(File.dirname(__FILE__), 'model/rubynet.xml')) do |f|
175
- assert_kind_of(IO, f)
176
- assert_kind_of(IO, @xp.io = f)
177
- assert_instance_of(XML::Document, @xp.parse)
178
- end # File.open
179
- end # def test_libxml_parser_io
180
-
181
104
  def test_fd_gc
182
105
  # Test opening # of documents up to the file limit for the OS.
183
- # Ideally it should run until libxml emits a warning,
106
+ # Ideally it should run until libxml emits a warning,
184
107
  # thereby knowing we've done a GC sweep. For the time being,
185
- # re-open the same doc `limit descriptors` times.
186
- # If we make it to the end, then we've succeeded,
108
+ # re-open the same doc `limit descriptors` times.
109
+ # If we make it to the end, then we've succeeded,
187
110
  # otherwise an exception will be thrown.
188
- XML::Parser.register_error_handler(lambda {|msg| nil })
189
-
111
+ XML::Error.set_handler {|error|}
112
+
190
113
  max_fd = if RUBY_PLATFORM.match(/mswin32/i)
191
114
  500
192
115
  else
193
116
  (`ulimit -n`.chomp.to_i) + 1
194
117
  end
195
-
196
- filename = File.join(File.dirname(__FILE__), 'model/rubynet.xml')
118
+
119
+ file = File.join(File.dirname(__FILE__), 'model/rubynet.xml')
197
120
  max_fd.times do
198
- XML::Document.file(filename)
121
+ XML::Parser.file(file).parse
199
122
  end
123
+ XML::Error.reset_handler {|error|}
200
124
  end
201
125
 
202
- def test_libxml_parser_features
203
- assert_instance_of(Array, XML::Parser::features)
204
- end
205
126
 
206
127
  # ----- Errors ------
207
- def test_error_handler
208
- assert_raise(XML::Parser::ParseError) do
128
+ def test_error
129
+ error = assert_raise(XML::Error) do
209
130
  XML::Parser.string('<foo><bar/></foz>').parse
210
131
  end
211
132
 
212
- ary = []
213
- XML::Parser.register_error_handler do |msg|
214
- ary << msg
215
- end
216
-
217
- # this will use our error handler
218
- assert_raise(XML::Parser::ParseError) do
219
- XML::Parser.string('<foo><bar/></foz>').parse
220
- end
221
-
222
- assert_equal(["Entity: line 1: ",
223
- "parser ",
224
- "error : ",
225
- "Opening and ending tag mismatch: foo line 1 and foz\n",
226
- "<foo><bar/></foz>\n",
227
- " ^\n"], ary)
228
-
229
- assert_instance_of(Proc, XML::Parser.register_error_handler(nil))
230
-
231
- # this will go to stderr again
232
- assert_raise(XML::Parser::ParseError) do
233
- d = XML::Parser.string('<foo><bar/></foz>').parse
234
- end
133
+ assert_not_nil(error)
134
+ assert_kind_of(XML::Error, error)
135
+ assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", error.message)
136
+ assert_equal(XML::Error::PARSER, error.domain)
137
+ assert_equal(XML::Error::TAG_NAME_MISMATCH, error.code)
138
+ assert_equal(XML::Error::FATAL, error.level)
139
+ assert_nil(error.file)
140
+ assert_equal(1, error.line)
141
+ assert_equal('foo', error.str1)
142
+ assert_equal('foz', error.str2)
143
+ assert_nil(error.str3)
144
+ assert_equal(1, error.int1)
145
+ assert_equal(20, error.int2)
146
+ assert_nil(error.node)
235
147
  end
236
-
237
- def test_error_handler_lambda
238
- XML::Parser.register_error_handler(nil)
239
- assert_raise(XML::Parser::ParseError) do
240
- XML::Parser.string('<foo><bar/></foz>').parse
241
- end
242
148
 
243
- ary = []
244
- assert_nil XML::Parser.register_error_handler(lambda { |msg| ary << msg })
245
-
246
- # this will use our error handler
247
- assert_raise(XML::Parser::ParseError) do
248
- XML::Parser.string('<foo><bar/></foz>').parse
249
- end
250
-
251
- assert_equal(["Entity: line 1: ",
252
- "parser ",
253
- "error : ",
254
- "Opening and ending tag mismatch: foo line 1 and foz\n",
255
- "<foo><bar/></foz>\n",
256
- " ^\n"], ary)
257
-
258
- assert_instance_of(Proc, XML::Parser.register_error_handler(nil))
259
-
260
- # this will go to stderr again
261
- assert_raise(XML::Parser::ParseError) do
262
- d = XML::Parser.string('<foo><bar/></foz>').parse
263
- end
264
- end
265
-
266
149
  def test_bad_xml
267
150
  @xp.string = '<ruby_array uga="booga" foo="bar"<fixnum>one</fixnum><fixnum>two</fixnum></ruby_array>'
268
- XML::Parser.register_error_handler(lambda {|msg| nil })
269
- assert_raise(XML::Parser::ParseError) do
151
+ error = assert_raise(XML::Error) do
270
152
  assert_not_nil(@xp.parse)
271
153
  end
154
+
155
+ assert_not_nil(error)
156
+ assert_kind_of(XML::Error, error)
157
+ assert_equal("Fatal error: Extra content at the end of the document at :1.", error.message)
158
+ assert_equal(XML::Error::PARSER, error.domain)
159
+ assert_equal(XML::Error::DOCUMENT_END, error.code)
160
+ assert_equal(XML::Error::FATAL, error.level)
161
+ assert_nil(error.file)
162
+ assert_equal(1, error.line)
163
+ assert_nil(error.str1)
164
+ assert_nil(error.str2)
165
+ assert_nil(error.str3)
166
+ assert_equal(0, error.int1)
167
+ assert_equal(20, error.int2)
168
+ assert_nil(error.node)
272
169
  end
273
-
170
+
274
171
  def test_double_parse
275
- XML::Parser.register_error_handler(lambda {|msg| nil })
276
172
  parser = XML::Parser.string("<test>something</test>")
277
173
  doc = parser.parse
278
-
279
- assert_raise(XML::Parser::ParseError) do
174
+
175
+ assert_raise(RuntimeError) do
280
176
  parser.parse
281
177
  end
282
178
  end
283
-
284
- def test_libxml_parser_empty_string
285
- assert_raise(XML::Parser::ParseError) do
286
- @xp.string = ''
287
- @xp.parse
288
- end
289
-
290
- assert_raise(TypeError) do
291
- @xp.string = nil
292
- end
293
- end
294
- end
179
+ end