libxml-ruby 1.1.4 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (146) hide show
  1. data/{CHANGES → HISTORY} +35 -0
  2. data/LICENSE +1 -0
  3. data/MANIFEST +165 -0
  4. data/{README → README.rdoc} +2 -2
  5. data/Rakefile +47 -147
  6. data/ext/libxml/libxml.c +17 -1
  7. data/ext/libxml/ruby_libxml.h +9 -1
  8. data/ext/libxml/ruby_xml_attr.c +3 -3
  9. data/ext/libxml/ruby_xml_attr_decl.c +32 -32
  10. data/ext/libxml/ruby_xml_attributes.c +1 -1
  11. data/ext/libxml/ruby_xml_cbg.c +1 -2
  12. data/ext/libxml/ruby_xml_document.c +26 -4
  13. data/ext/libxml/ruby_xml_dtd.c +3 -3
  14. data/ext/libxml/ruby_xml_encoding.c +92 -5
  15. data/ext/libxml/ruby_xml_encoding.h +4 -0
  16. data/ext/libxml/ruby_xml_namespace.c +2 -2
  17. data/ext/libxml/ruby_xml_node.c +14 -14
  18. data/ext/libxml/ruby_xml_parser_context.c +8 -8
  19. data/ext/libxml/ruby_xml_reader.c +98 -43
  20. data/ext/libxml/ruby_xml_sax2_handler.c +13 -1
  21. data/ext/libxml/ruby_xml_sax_parser.c +4 -5
  22. data/ext/libxml/ruby_xml_version.h +5 -5
  23. data/ext/libxml/ruby_xml_xpath_context.c +9 -6
  24. data/ext/libxml/ruby_xml_xpath_object.c +1 -1
  25. data/ext/vc/libxml_ruby.sln +4 -4
  26. data/lib/libxml.rb +29 -29
  27. data/lib/libxml/attr.rb +112 -110
  28. data/lib/libxml/attr_decl.rb +2 -0
  29. data/lib/libxml/attributes.rb +13 -11
  30. data/lib/libxml/document.rb +192 -190
  31. data/lib/libxml/error.rb +89 -87
  32. data/lib/libxml/hpricot.rb +77 -75
  33. data/lib/libxml/html_parser.rb +96 -94
  34. data/lib/libxml/namespace.rb +61 -59
  35. data/lib/libxml/namespaces.rb +37 -35
  36. data/lib/libxml/node.rb +398 -384
  37. data/lib/libxml/ns.rb +21 -19
  38. data/lib/libxml/parser.rb +366 -364
  39. data/lib/libxml/properties.rb +22 -20
  40. data/lib/libxml/reader.rb +2 -0
  41. data/lib/libxml/sax_callbacks.rb +179 -177
  42. data/lib/libxml/sax_parser.rb +57 -55
  43. data/lib/libxml/tree.rb +28 -26
  44. data/lib/libxml/xpath_object.rb +15 -13
  45. data/lib/xml.rb +16 -14
  46. data/lib/xml/libxml.rb +10 -8
  47. data/libxml-ruby.gemspec +50 -0
  48. data/script/benchmark/depixelate +634 -0
  49. data/script/benchmark/hamlet.xml +9055 -0
  50. data/script/benchmark/parsecount +170 -0
  51. data/script/benchmark/sock_entries.xml +507 -0
  52. data/script/benchmark/throughput +41 -0
  53. data/script/test +6 -0
  54. data/test/etc_doc_to_s.rb +21 -19
  55. data/test/ets_doc_file.rb +17 -15
  56. data/test/ets_doc_to_s.rb +23 -21
  57. data/test/ets_gpx.rb +28 -26
  58. data/test/ets_node_gc.rb +23 -21
  59. data/test/ets_tsr.rb +11 -9
  60. data/test/model/bands.iso-8859-1.xml +5 -0
  61. data/test/model/bands.utf-8.xml +5 -0
  62. data/test/rb-magic-comment.rb +33 -0
  63. data/test/tc_attr.rb +181 -170
  64. data/test/tc_attr_decl.rb +3 -1
  65. data/test/tc_attributes.rb +134 -132
  66. data/test/tc_deprecated_require.rb +13 -11
  67. data/test/tc_document.rb +119 -113
  68. data/test/tc_document_write.rb +186 -117
  69. data/test/tc_dtd.rb +125 -123
  70. data/test/tc_error.rb +3 -1
  71. data/test/tc_html_parser.rb +139 -137
  72. data/test/tc_namespace.rb +61 -58
  73. data/test/tc_namespaces.rb +176 -173
  74. data/test/tc_node.rb +257 -180
  75. data/test/tc_node_cdata.rb +51 -49
  76. data/test/tc_node_comment.rb +33 -30
  77. data/test/tc_node_copy.rb +42 -40
  78. data/test/tc_node_edit.rb +159 -157
  79. data/test/tc_node_text.rb +71 -69
  80. data/test/tc_node_write.rb +41 -16
  81. data/test/tc_node_xlink.rb +29 -26
  82. data/test/tc_parser.rb +335 -329
  83. data/test/tc_parser_context.rb +188 -185
  84. data/test/tc_properties.rb +39 -36
  85. data/test/tc_reader.rb +297 -283
  86. data/test/tc_relaxng.rb +54 -51
  87. data/test/tc_sax_parser.rb +275 -273
  88. data/test/tc_schema.rb +53 -51
  89. data/test/tc_traversal.rb +222 -220
  90. data/test/tc_xinclude.rb +21 -19
  91. data/test/tc_xml.rb +3 -1
  92. data/test/tc_xpath.rb +195 -193
  93. data/test/tc_xpath_context.rb +80 -78
  94. data/test/tc_xpath_expression.rb +38 -35
  95. data/test/tc_xpointer.rb +74 -72
  96. data/test/test_helper.rb +14 -0
  97. data/test/test_suite.rb +39 -33
  98. metadata +65 -105
  99. data/doc/css/normal.css +0 -182
  100. data/doc/img/raze-tiny.png +0 -0
  101. data/doc/img/red-cube.jpg +0 -0
  102. data/doc/img/xml-ruby.png +0 -0
  103. data/doc/index.xml +0 -43
  104. data/doc/install.xml +0 -77
  105. data/doc/layout.rhtml +0 -38
  106. data/doc/layout.xsl +0 -67
  107. data/doc/license.xml +0 -32
  108. data/doc/log/changelog.xml +0 -1324
  109. data/doc/log/changelog.xsl +0 -42
  110. data/ext/libxml/Makefile +0 -156
  111. data/ext/libxml/extconf.h +0 -5
  112. data/ext/libxml/libxml-ruby.so.a +0 -0
  113. data/ext/libxml/libxml.o +0 -0
  114. data/ext/libxml/libxml_ruby.so +0 -0
  115. data/ext/libxml/mkmf.log +0 -129
  116. data/ext/libxml/ruby_xml.o +0 -0
  117. data/ext/libxml/ruby_xml_attr.o +0 -0
  118. data/ext/libxml/ruby_xml_attr_decl.o +0 -0
  119. data/ext/libxml/ruby_xml_attributes.o +0 -0
  120. data/ext/libxml/ruby_xml_cbg.o +0 -0
  121. data/ext/libxml/ruby_xml_document.o +0 -0
  122. data/ext/libxml/ruby_xml_dtd.o +0 -0
  123. data/ext/libxml/ruby_xml_encoding.o +0 -0
  124. data/ext/libxml/ruby_xml_error.o +0 -0
  125. data/ext/libxml/ruby_xml_html_parser.o +0 -0
  126. data/ext/libxml/ruby_xml_html_parser_context.o +0 -0
  127. data/ext/libxml/ruby_xml_html_parser_options.o +0 -0
  128. data/ext/libxml/ruby_xml_input_cbg.o +0 -0
  129. data/ext/libxml/ruby_xml_io.o +0 -0
  130. data/ext/libxml/ruby_xml_namespace.o +0 -0
  131. data/ext/libxml/ruby_xml_namespaces.o +0 -0
  132. data/ext/libxml/ruby_xml_node.o +0 -0
  133. data/ext/libxml/ruby_xml_parser.o +0 -0
  134. data/ext/libxml/ruby_xml_parser_context.o +0 -0
  135. data/ext/libxml/ruby_xml_parser_options.o +0 -0
  136. data/ext/libxml/ruby_xml_reader.o +0 -0
  137. data/ext/libxml/ruby_xml_relaxng.o +0 -0
  138. data/ext/libxml/ruby_xml_sax2_handler.o +0 -0
  139. data/ext/libxml/ruby_xml_sax_parser.o +0 -0
  140. data/ext/libxml/ruby_xml_schema.o +0 -0
  141. data/ext/libxml/ruby_xml_xinclude.o +0 -0
  142. data/ext/libxml/ruby_xml_xpath.o +0 -0
  143. data/ext/libxml/ruby_xml_xpath_context.o +0 -0
  144. data/ext/libxml/ruby_xml_xpath_expression.o +0 -0
  145. data/ext/libxml/ruby_xml_xpath_object.o +0 -0
  146. data/ext/libxml/ruby_xml_xpointer.o +0 -0
@@ -1,118 +1,187 @@
1
- require 'xml'
2
- require 'tmpdir'
3
- require 'test/unit'
4
-
5
- class TestDocumentWrite < Test::Unit::TestCase
6
- def setup
7
- # Strip spaces to make testing easier
8
- XML.default_keep_blanks = false
9
- file = File.join(File.dirname(__FILE__), 'model/bands.xml')
10
- @doc = XML::Document.file(file)
11
- end
12
-
13
- def teardown
14
- XML.default_keep_blanks = true
15
- @doc = nil
16
- end
17
-
18
- # --- to_s tests ---
19
- def test_to_s_default
20
- # Default to_s has indentation
21
- assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
22
- @doc.to_s)
23
- end
24
-
25
- def test_to_s_no_global_indentation
26
- # No indentation due to global setting
27
- XML.indent_tree_output = false
28
- assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
29
- @doc.to_s)
30
- ensure
31
- XML.indent_tree_output = true
32
- end
33
-
34
- def test_to_s_no_indentation
35
- # No indentation due to local setting
36
- assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bands genre=\"metal\"><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><iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden></bands>\n",
37
- @doc.to_s(:indent => false))
38
- end
39
-
40
- def test_to_s_encoding
41
- # Test encodings
42
-
43
- # UTF8:
44
- # ö - c3 b6 in hex, \303\266 in octal
45
- # ü - c3 bc in hex, \303\274 in octal
46
- assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
47
- @doc.to_s(:encoding => XML::Encoding::UTF_8))
48
-
49
- # ISO_8859_1:
50
- # ö - f6 in hex, \366 in octal
51
- # ü - fc in hex, \374 in octal
52
- assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<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>\n",
53
- @doc.to_s(:encoding => XML::Encoding::ISO_8859_1))
54
-
55
-
56
- # Invalid encoding
57
- error = assert_raise(ArgumentError) do
58
- @doc.to_s(:encoding => -9999)
59
- end
60
- assert_equal('Unknown encoding value: -9999', error.to_s)
61
- end
62
-
63
- # --- save tests -----
64
- def test_save_utf8
65
- temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_utf8.xml")
66
-
67
- bytes = @doc.save(temp_filename)
68
- assert_equal(271, bytes)
69
-
70
- contents = File.read(temp_filename)
71
- assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<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>\n",
72
- contents)
73
- ensure
74
- File.delete(temp_filename)
75
- end
76
-
77
- def test_save_utf8_no_indents
78
- temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_utf8_no_indents.xml")
79
-
80
- bytes = @doc.save(temp_filename, :indent => false)
81
- assert_equal(264, bytes)
82
-
83
- contents = File.read(temp_filename)
84
- assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<bands genre=\"metal\"><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><iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden></bands>\n",
85
- contents)
86
- ensure
87
- File.delete(temp_filename)
88
- end
89
-
90
- def test_save_iso_8859_1
91
- temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_iso_8859_1.xml")
92
- bytes = @doc.save(temp_filename, :encoding => XML::Encoding::ISO_8859_1)
93
- assert_equal(272, bytes)
94
-
95
- contents = File.read(temp_filename)
96
- assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<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>\n",
97
- contents)
98
- ensure
99
- File.delete(temp_filename)
100
- end
101
-
102
- def test_save_iso_8859_1_no_indent
103
- temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_iso_8859_1_no_indent.xml")
104
- bytes = @doc.save(temp_filename, :indent => false, :encoding => XML::Encoding::ISO_8859_1)
105
- assert_equal(265, bytes)
106
-
107
- contents = File.read(temp_filename)
108
- assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<bands genre=\"metal\"><m\366tley_cr\374e country=\"us\">An American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e><iron_maiden country=\"uk\">British heavy metal band formed in 1975.</iron_maiden></bands>\n",
109
- contents)
110
- ensure
111
- File.delete(temp_filename)
112
- end
113
-
114
- # --- Debug ---
115
- def test_debug
116
- assert(@doc.debug)
117
- end
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+ require 'tmpdir'
5
+ require 'test/unit'
6
+
7
+ class TestDocumentWrite < Test::Unit::TestCase
8
+ def setup
9
+ load_encoding("utf-8")
10
+ end
11
+
12
+ def teardown
13
+ XML.default_keep_blanks = true
14
+ @doc = nil
15
+ end
16
+
17
+ def load_encoding(name)
18
+ @encoding = Encoding.find(name) if defined?(Encoding)
19
+ @file_name = "model/bands.#{name.downcase}.xml"
20
+
21
+ # Strip spaces to make testing easier
22
+ XML.default_keep_blanks = false
23
+ file = File.join(File.dirname(__FILE__), @file_name)
24
+ @doc = XML::Document.file(file)
25
+ end
26
+
27
+ # --- to_s tests ---
28
+ def test_to_s_default
29
+ # Default to_s has indentation
30
+ if defined?(Encoding)
31
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bands genre=\"metal\">\n <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>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>\n",
32
+ @doc.to_s)
33
+ else
34
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
35
+ @doc.to_s)
36
+ end
37
+ end
38
+
39
+ def test_to_s_no_global_indentation
40
+ # No indentation due to global setting
41
+ XML.indent_tree_output = false
42
+ value = @doc.to_s
43
+
44
+ if defined?(Encoding)
45
+ assert_equal(Encoding::UTF_8, value.encoding)
46
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bands genre=\"metal\">\n<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>\n<iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>\n",
47
+ value)
48
+ else
49
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
50
+ value)
51
+ end
52
+ ensure
53
+ XML.indent_tree_output = true
54
+ end
55
+
56
+ def test_to_s_no_indentation
57
+ # No indentation due to local setting
58
+ value = @doc.to_s(:indent => false)
59
+ if defined?(Encoding)
60
+ assert_equal(Encoding::UTF_8, value.encoding)
61
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bands genre=\"metal\"><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></bands>\n",
62
+ value)
63
+ else
64
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
65
+ value)
66
+ end
67
+ end
68
+
69
+ def test_to_s_encoding
70
+ # Test encodings
71
+
72
+ # UTF8:
73
+ # ö - c3 b6 in hex, \303\266 in octal
74
+ # ü - c3 bc in hex, \303\274 in octal
75
+ value = @doc.to_s(:encoding => XML::Encoding::UTF_8)
76
+ if defined?(Encoding)
77
+ assert_equal(Encoding::UTF_8, value.encoding)
78
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<bands genre=\"metal\">\n <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>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>\n",
79
+ value)
80
+ else
81
+ assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<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>\n",
82
+ value)
83
+ end
84
+
85
+ # ISO_8859_1:
86
+ # ö - f6 in hex, \366 in octal
87
+ # ü - fc in hex, \374 in octal
88
+ value = @doc.to_s(:encoding => XML::Encoding::ISO_8859_1)
89
+ if defined?(Encoding)
90
+ assert_equal(Encoding::ISO8859_1, value.encoding)
91
+ assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<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>\n".force_encoding(Encoding::ISO8859_1),
92
+ @doc.to_s(:encoding => XML::Encoding::ISO_8859_1))
93
+ else
94
+ assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<bands genre=\"metal\">\n <m\366tley_cr\374e country=\"us\">M\366tley Cr\374e is an American heavy metal band formed in Los Angeles, California in 1981.</m\366tley_cr\374e>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>\n",
95
+ @doc.to_s(:encoding => XML::Encoding::ISO_8859_1))
96
+ end
97
+
98
+ # Invalid encoding
99
+ error = assert_raise(ArgumentError) do
100
+ @doc.to_s(:encoding => -9999)
101
+ end
102
+ assert_equal('Unknown encoding value: -9999', error.to_s)
103
+ end
104
+
105
+ # --- save tests -----
106
+ def test_save_utf8
107
+ temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_utf8.xml")
108
+
109
+ bytes = @doc.save(temp_filename)
110
+ assert_equal(305, bytes)
111
+
112
+ if defined?(Encoding)
113
+ contents = File.read(temp_filename, nil, nil, :encoding => Encoding::UTF_8)
114
+ assert_equal(Encoding::UTF_8, contents.encoding)
115
+ assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<bands genre=\"metal\">\n <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>\n <iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden>\n</bands>\n",
116
+ contents)
117
+ else
118
+ contents = File.read(temp_filename)
119
+ assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<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>\n",
120
+ contents)
121
+ end
122
+ ensure
123
+ File.delete(temp_filename)
124
+ end
125
+
126
+ def test_save_utf8_no_indents
127
+ temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_utf8_no_indents.xml")
128
+
129
+ bytes = @doc.save(temp_filename, :indent => false)
130
+ assert_equal(298, bytes)
131
+
132
+ if defined?(Encoding)
133
+ contents = File.read(temp_filename, nil, nil, :encoding => Encoding::UTF_8)
134
+ assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<bands genre=\"metal\"><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></bands>\n",
135
+ contents)
136
+ else
137
+ contents = File.read(temp_filename)
138
+ assert_equal("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<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>\n",
139
+ contents)
140
+ end
141
+ ensure
142
+ File.delete(temp_filename)
143
+ end
144
+
145
+ def test_save_iso_8859_1
146
+ temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_iso_8859_1.xml")
147
+ bytes = @doc.save(temp_filename, :encoding => XML::Encoding::ISO_8859_1)
148
+ assert_equal(304, bytes)
149
+
150
+ if defined?(Encoding)
151
+ contents = File.read(temp_filename, nil, nil, :encoding => Encoding::ISO8859_1)
152
+ assert_equal(Encoding::ISO8859_1, contents.encoding)
153
+ assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<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>\n".force_encoding(Encoding::ISO8859_1),
154
+ contents)
155
+ else
156
+ contents = File.read(temp_filename)
157
+ assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<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>\n",
158
+ contents)
159
+ end
160
+ ensure
161
+ File.delete(temp_filename)
162
+ end
163
+
164
+ def test_save_iso_8859_1_no_indent
165
+ temp_filename = File.join(Dir.tmpdir, "tc_document_write_test_save_iso_8859_1_no_indent.xml")
166
+ bytes = @doc.save(temp_filename, :indent => false, :encoding => XML::Encoding::ISO_8859_1)
167
+ assert_equal(297, bytes)
168
+
169
+ if defined?(Encoding)
170
+ contents = File.read(temp_filename, nil, nil, :encoding => Encoding::ISO8859_1)
171
+ assert_equal(Encoding::ISO8859_1, contents.encoding)
172
+ assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<bands genre=\"metal\"><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><iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden></bands>\n".force_encoding(Encoding::ISO8859_1),
173
+ contents)
174
+ else
175
+ contents = File.read(temp_filename)
176
+ assert_equal("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n<bands genre=\"metal\"><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><iron_maiden country=\"uk\">Iron Maiden is a British heavy metal band formed in 1975.</iron_maiden></bands>\n",
177
+ contents)
178
+ end
179
+ ensure
180
+ File.delete(temp_filename)
181
+ end
182
+
183
+ # --- Debug ---
184
+ def test_debug
185
+ assert(@doc.debug)
186
+ end
118
187
  end
data/test/tc_dtd.rb CHANGED
@@ -1,123 +1,125 @@
1
- require "xml"
2
- require 'test/unit'
3
-
4
- class TestDtd < Test::Unit::TestCase
5
- def setup
6
- xp = XML::Parser.string(<<-EOS)
7
- <root>
8
- <head a="ee" id="1">Colorado</head>
9
- <descr>Lots of nice mountains</descr>
10
- </root>
11
- EOS
12
- @doc = xp.parse
13
- end
14
-
15
- def teardown
16
- @doc = nil
17
- end
18
-
19
- def dtd
20
- XML::Dtd.new(<<-EOS)
21
- <!ELEMENT root (head, descr)>
22
- <!ELEMENT head (#PCDATA)>
23
- <!ATTLIST head
24
- id NMTOKEN #REQUIRED
25
- a CDATA #IMPLIED
26
- >
27
- <!ELEMENT descr (#PCDATA)>
28
- EOS
29
- end
30
-
31
- def test_internal_subset
32
- xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", nil, nil, true
33
- assert xhtml_dtd.name.nil?
34
- assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
35
- assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
36
- assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
37
-
38
- xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", "xhtml1", nil, true
39
- assert_equal "xhtml1", xhtml_dtd.name
40
- assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
41
- assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
42
- assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
43
- end
44
-
45
- def test_external_subset
46
- xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", nil
47
- assert xhtml_dtd.name.nil?
48
- assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
49
- assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
50
- assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
51
-
52
- xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", "xhtml1"
53
- assert_equal "xhtml1", xhtml_dtd.name
54
- assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
55
- assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
56
- assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
57
- end
58
-
59
- def test_valid
60
- assert(@doc.validate(dtd))
61
- end
62
-
63
- def test_invalid
64
- new_node = XML::Node.new('invalid', 'this will mess up validation')
65
- @doc.root.child_add(new_node)
66
-
67
- messages = Hash.new
68
- error = assert_raise(XML::Error) do
69
- @doc.validate(dtd)
70
- end
71
-
72
- # Check the error worked
73
- assert_not_nil(error)
74
- assert_kind_of(XML::Error, error)
75
- assert_equal("Error: No declaration for element invalid.", error.message)
76
- assert_equal(XML::Error::VALID, error.domain)
77
- assert_equal(XML::Error::DTD_UNKNOWN_ELEM, error.code)
78
- assert_equal(XML::Error::ERROR, error.level)
79
- assert_nil(error.file)
80
- assert_nil(error.line)
81
- assert_equal('invalid', error.str1)
82
- assert_equal('invalid', error.str2)
83
- assert_nil(error.str3)
84
- assert_equal(0, error.int1)
85
- assert_equal(0, error.int2)
86
- assert_not_nil(error.node)
87
- assert_equal('invalid', error.node.name)
88
- end
89
-
90
- def test_external_dtd
91
- xml = <<-EOS
92
- <!DOCTYPE test PUBLIC "-//TEST" "test.dtd" []>
93
- <test>
94
- <title>T1</title>
95
- </test>
96
- EOS
97
-
98
- errors = Array.new
99
- XML::Error.set_handler do |error|
100
- errors << error
101
- end
102
-
103
- XML.default_load_external_dtd = false
104
- doc = XML::Parser.string(xml).parse
105
- assert_equal(0, errors.length)
106
-
107
- errors.clear
108
- XML.default_load_external_dtd = true
109
- doc = XML::Parser.string(xml).parse
110
- assert_equal(1, errors.length)
111
- assert_equal("Warning: failed to load external entity \"test.dtd\" at :1.",
112
- errors[0].to_s)
113
-
114
- errors = Array.new
115
- doc = XML::Parser.string(xml, :options => XML::Parser::Options::DTDLOAD).parse
116
- assert_equal(1, errors.length)
117
- assert_equal("Warning: failed to load external entity \"test.dtd\" at :1.",
118
- errors[0].to_s)
119
- ensure
120
- XML.default_load_external_dtd = false
121
- XML::Error.reset_handler
122
- end
123
- end
1
+ # encoding: UTF-8
2
+
3
+ require './test_helper'
4
+
5
+ require 'test/unit'
6
+
7
+ class TestDtd < Test::Unit::TestCase
8
+ def setup
9
+ xp = XML::Parser.string(<<-EOS)
10
+ <root>
11
+ <head a="ee" id="1">Colorado</head>
12
+ <descr>Lots of nice mountains</descr>
13
+ </root>
14
+ EOS
15
+ @doc = xp.parse
16
+ end
17
+
18
+ def teardown
19
+ @doc = nil
20
+ end
21
+
22
+ def dtd
23
+ XML::Dtd.new(<<-EOS)
24
+ <!ELEMENT root (head, descr)>
25
+ <!ELEMENT head (#PCDATA)>
26
+ <!ATTLIST head
27
+ id NMTOKEN #REQUIRED
28
+ a CDATA #IMPLIED
29
+ >
30
+ <!ELEMENT descr (#PCDATA)>
31
+ EOS
32
+ end
33
+
34
+ def test_internal_subset
35
+ xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", nil, nil, true
36
+ assert xhtml_dtd.name.nil?
37
+ assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
38
+ assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
39
+ assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
40
+
41
+ xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", "xhtml1", nil, true
42
+ assert_equal "xhtml1", xhtml_dtd.name
43
+ assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
44
+ assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
45
+ assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
46
+ end
47
+
48
+ def test_external_subset
49
+ xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", nil
50
+ assert xhtml_dtd.name.nil?
51
+ assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
52
+ assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
53
+ assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
54
+
55
+ xhtml_dtd = XML::Dtd.new "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", "xhtml1"
56
+ assert_equal "xhtml1", xhtml_dtd.name
57
+ assert_equal "-//W3C//DTD XHTML 1.0 Transitional//EN", xhtml_dtd.external_id
58
+ assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.uri
59
+ assert_equal "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", xhtml_dtd.system_id
60
+ end
61
+
62
+ def test_valid
63
+ assert(@doc.validate(dtd))
64
+ end
65
+
66
+ def test_invalid
67
+ new_node = XML::Node.new('invalid', 'this will mess up validation')
68
+ @doc.root << new_node
69
+
70
+ error = assert_raise(XML::Error) do
71
+ @doc.validate(dtd)
72
+ end
73
+
74
+ # Check the error worked
75
+ assert_not_nil(error)
76
+ assert_kind_of(XML::Error, error)
77
+ assert_equal("Error: No declaration for element invalid.", error.message)
78
+ assert_equal(XML::Error::VALID, error.domain)
79
+ assert_equal(XML::Error::DTD_UNKNOWN_ELEM, error.code)
80
+ assert_equal(XML::Error::ERROR, error.level)
81
+ assert_nil(error.file)
82
+ assert_nil(error.line)
83
+ assert_equal('invalid', error.str1)
84
+ assert_equal('invalid', error.str2)
85
+ assert_nil(error.str3)
86
+ assert_equal(0, error.int1)
87
+ assert_equal(0, error.int2)
88
+ assert_not_nil(error.node)
89
+ assert_equal('invalid', error.node.name)
90
+ end
91
+
92
+ def test_external_dtd
93
+ xml = <<-EOS
94
+ <!DOCTYPE test PUBLIC "-//TEST" "test.dtd" []>
95
+ <test>
96
+ <title>T1</title>
97
+ </test>
98
+ EOS
99
+
100
+ errors = Array.new
101
+ XML::Error.set_handler do |error|
102
+ errors << error
103
+ end
104
+
105
+ XML.default_load_external_dtd = false
106
+ doc = XML::Parser.string(xml).parse
107
+ assert_equal(0, errors.length)
108
+
109
+ errors.clear
110
+ XML.default_load_external_dtd = true
111
+ doc = XML::Parser.string(xml).parse
112
+ assert_equal(1, errors.length)
113
+ assert_equal("Warning: failed to load external entity \"test.dtd\" at :1.",
114
+ errors[0].to_s)
115
+
116
+ errors = Array.new
117
+ doc = XML::Parser.string(xml, :options => XML::Parser::Options::DTDLOAD).parse
118
+ assert_equal(1, errors.length)
119
+ assert_equal("Warning: failed to load external entity \"test.dtd\" at :1.",
120
+ errors[0].to_s)
121
+ ensure
122
+ XML.default_load_external_dtd = false
123
+ XML::Error.reset_handler
124
+ end
125
+ end