libxml-ruby 5.0.4 → 5.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/HISTORY +10 -6
- data/README.rdoc +1 -1
- data/ext/libxml/extconf.rb +5 -0
- data/ext/libxml/ruby_xml.c +556 -556
- data/ext/libxml/ruby_xml_attributes.h +17 -17
- data/ext/libxml/ruby_xml_document.c +1129 -1129
- data/ext/libxml/ruby_xml_dtd.c +257 -257
- data/ext/libxml/ruby_xml_encoding.c +250 -250
- data/ext/libxml/ruby_xml_error.c +1003 -1003
- data/ext/libxml/ruby_xml_error.h +14 -14
- data/ext/libxml/ruby_xml_html_parser_context.c +351 -351
- data/ext/libxml/ruby_xml_input_cbg.c +188 -188
- data/ext/libxml/ruby_xml_namespace.c +151 -151
- data/ext/libxml/ruby_xml_parser.h +10 -10
- data/ext/libxml/ruby_xml_parser_context.c +1009 -1009
- data/ext/libxml/ruby_xml_parser_options.c +74 -74
- data/ext/libxml/ruby_xml_parser_options.h +10 -10
- data/ext/libxml/ruby_xml_sax2_handler.c +326 -326
- data/ext/libxml/ruby_xml_sax_parser.c +108 -108
- data/ext/libxml/ruby_xml_version.h +9 -9
- data/lib/libxml/attr.rb +122 -122
- data/lib/libxml/attr_decl.rb +80 -80
- data/lib/libxml/attributes.rb +13 -13
- data/lib/libxml/document.rb +194 -194
- data/lib/libxml/error.rb +95 -95
- data/lib/libxml/hpricot.rb +77 -77
- data/lib/libxml/html_parser.rb +96 -96
- data/lib/libxml/namespace.rb +61 -61
- data/lib/libxml/namespaces.rb +37 -37
- data/lib/libxml/node.rb +323 -323
- data/lib/libxml/parser.rb +102 -102
- data/lib/libxml/sax_callbacks.rb +179 -179
- data/lib/libxml/sax_parser.rb +40 -40
- data/lib/libxml/tree.rb +28 -28
- data/lib/libxml.rb +4 -4
- data/lib/xml/libxml.rb +10 -10
- data/lib/xml.rb +13 -13
- data/libxml-ruby.gemspec +50 -49
- data/test/test_document.rb +140 -140
- data/test/test_document_write.rb +142 -142
- data/test/test_dtd.rb +126 -126
- data/test/test_encoding.rb +126 -126
- data/test/test_error.rb +194 -194
- data/test/test_helper.rb +20 -20
- data/test/test_namespace.rb +58 -58
- data/test/test_node.rb +235 -235
- data/test/test_node_write.rb +93 -93
- data/test/test_parser.rb +333 -333
- data/test/test_reader.rb +364 -364
- data/test/test_xml.rb +168 -168
- metadata +5 -4
data/test/test_encoding.rb
CHANGED
@@ -1,126 +1,126 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require_relative './test_helper'
|
4
|
-
|
5
|
-
# Code UTF8 Latin1 Hex
|
6
|
-
# m 109 109 6D
|
7
|
-
# ö 195 182 246 C3 B6 / F6
|
8
|
-
# t 116 116 74
|
9
|
-
# l 108 108 6C
|
10
|
-
# e 101 101 65
|
11
|
-
# y 121 121 79
|
12
|
-
# _ 95 95 5F
|
13
|
-
# c 99 99 63
|
14
|
-
# r 114 114 72
|
15
|
-
# ü 195 188 252 C3 BC / FC
|
16
|
-
# e 101 101 65
|
17
|
-
|
18
|
-
# See:
|
19
|
-
# http://en.wikipedia.org/wiki/ISO/IEC_8859-1
|
20
|
-
# http://en.wikipedia.org/wiki/List_of_Unicode_characters
|
21
|
-
|
22
|
-
class TestEncoding < Minitest::Test
|
23
|
-
def setup
|
24
|
-
@original_encoding = Encoding.default_internal
|
25
|
-
Encoding.default_internal = nil
|
26
|
-
end
|
27
|
-
|
28
|
-
def teardown
|
29
|
-
Encoding.default_internal = @original_encoding
|
30
|
-
end
|
31
|
-
|
32
|
-
def file_for_encoding(encoding)
|
33
|
-
file_name = "model/bands.#{encoding.name.downcase}.xml"
|
34
|
-
File.join(File.dirname(__FILE__), file_name)
|
35
|
-
end
|
36
|
-
|
37
|
-
def load_encoding(encoding)
|
38
|
-
@encoding = encoding
|
39
|
-
file = file_for_encoding(encoding)
|
40
|
-
|
41
|
-
@doc = LibXML::XML::Document.file(file, options: LibXML::XML::Parser::Options::NOBLANKS)
|
42
|
-
end
|
43
|
-
|
44
|
-
def test_encoding
|
45
|
-
doc = LibXML::XML::Document.new
|
46
|
-
assert_equal(LibXML::XML::Encoding::NONE, doc.encoding)
|
47
|
-
assert_equal(Encoding::ASCII_8BIT, doc.rb_encoding)
|
48
|
-
|
49
|
-
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
|
50
|
-
doc = LibXML::XML::Document.file(file)
|
51
|
-
assert_equal(LibXML::XML::Encoding::UTF_8, doc.encoding)
|
52
|
-
assert_equal(Encoding::UTF_8, doc.rb_encoding)
|
53
|
-
|
54
|
-
doc.encoding = LibXML::XML::Encoding::ISO_8859_1
|
55
|
-
assert_equal(LibXML::XML::Encoding::ISO_8859_1, doc.encoding)
|
56
|
-
assert_equal(Encoding::ISO8859_1, doc.rb_encoding)
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_no_internal_encoding_iso_8859_1
|
60
|
-
load_encoding(Encoding::ISO_8859_1)
|
61
|
-
node = @doc.root.children.first
|
62
|
-
|
63
|
-
name = node.name
|
64
|
-
assert_equal(Encoding::UTF_8, name.encoding)
|
65
|
-
assert_equal("m\u00F6tley_cr\u00FCe", name)
|
66
|
-
assert_equal("109 195 182 116 108 101 121 95 99 114 195 188 101",
|
67
|
-
name.bytes.to_a.join(" "))
|
68
|
-
assert_equal("M\u00F6tley Cr\u00FCe is an American heavy metal band formed in Los Angeles, California in 1981.",
|
69
|
-
node.content)
|
70
|
-
|
71
|
-
name = name.encode(Encoding::ISO_8859_1)
|
72
|
-
assert_equal(Encoding::ISO_8859_1, name.encoding)
|
73
|
-
assert_equal("m\xF6tley_cr\xFCe".force_encoding(Encoding::ISO_8859_1), name)
|
74
|
-
assert_equal("109 246 116 108 101 121 95 99 114 252 101",
|
75
|
-
name.bytes.to_a.join(" "))
|
76
|
-
assert_equal("M\xF6tley Cr\xFCe is an American heavy metal band formed in Los Angeles, California in 1981.".force_encoding(Encoding::ISO_8859_1),
|
77
|
-
node.content.encode(Encoding::ISO_8859_1))
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_internal_encoding_iso_8859_1
|
81
|
-
Encoding.default_internal = Encoding::ISO_8859_1
|
82
|
-
load_encoding(Encoding::ISO_8859_1)
|
83
|
-
node = @doc.root.children.first
|
84
|
-
|
85
|
-
name = node.name
|
86
|
-
assert_equal(Encoding::ISO_8859_1, name.encoding)
|
87
|
-
assert_equal("109 246 116 108 101 121 95 99 114 252 101",
|
88
|
-
name.bytes.to_a.join(" "))
|
89
|
-
assert_equal("m\xF6tley_cr\xFCe".force_encoding(Encoding::ISO_8859_1), name)
|
90
|
-
assert_equal("109 246 116 108 101 121 95 99 114 252 101",
|
91
|
-
name.bytes.to_a.join(" "))
|
92
|
-
assert_equal("M\xF6tley Cr\xFCe is an American heavy metal band formed in Los Angeles, California in 1981.".force_encoding(Encoding::ISO_8859_1),
|
93
|
-
node.content.encode(Encoding::ISO_8859_1))
|
94
|
-
end
|
95
|
-
|
96
|
-
def test_no_internal_encoding_utf_8
|
97
|
-
load_encoding(Encoding::UTF_8)
|
98
|
-
node = @doc.root.children.first
|
99
|
-
|
100
|
-
name = node.name
|
101
|
-
assert_equal(@encoding, name.encoding)
|
102
|
-
assert_equal("109 195 182 116 108 101 121 95 99 114 195 188 101",
|
103
|
-
name.bytes.to_a.join(" "))
|
104
|
-
|
105
|
-
name = name.encode(Encoding::ISO_8859_1)
|
106
|
-
assert_equal(Encoding::ISO_8859_1, name.encoding)
|
107
|
-
assert_equal("109 246 116 108 101 121 95 99 114 252 101",
|
108
|
-
name.bytes.to_a.join(" "))
|
109
|
-
end
|
110
|
-
|
111
|
-
def test_internal_encoding_utf_8
|
112
|
-
Encoding.default_internal = Encoding::ISO_8859_1
|
113
|
-
load_encoding(Encoding::UTF_8)
|
114
|
-
node = @doc.root.children.first
|
115
|
-
|
116
|
-
name = node.name
|
117
|
-
assert_equal(Encoding::ISO_8859_1, name.encoding)
|
118
|
-
assert_equal("109 246 116 108 101 121 95 99 114 252 101",
|
119
|
-
name.bytes.to_a.join(" "))
|
120
|
-
end
|
121
|
-
|
122
|
-
def test_encoding_conversions
|
123
|
-
assert_equal("UTF-8", LibXML::XML::Encoding.to_s(LibXML::XML::Encoding::UTF_8))
|
124
|
-
assert_equal(LibXML::XML::Encoding::UTF_8, LibXML::XML::Encoding.from_s("UTF-8"))
|
125
|
-
end
|
126
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative './test_helper'
|
4
|
+
|
5
|
+
# Code UTF8 Latin1 Hex
|
6
|
+
# m 109 109 6D
|
7
|
+
# ö 195 182 246 C3 B6 / F6
|
8
|
+
# t 116 116 74
|
9
|
+
# l 108 108 6C
|
10
|
+
# e 101 101 65
|
11
|
+
# y 121 121 79
|
12
|
+
# _ 95 95 5F
|
13
|
+
# c 99 99 63
|
14
|
+
# r 114 114 72
|
15
|
+
# ü 195 188 252 C3 BC / FC
|
16
|
+
# e 101 101 65
|
17
|
+
|
18
|
+
# See:
|
19
|
+
# http://en.wikipedia.org/wiki/ISO/IEC_8859-1
|
20
|
+
# http://en.wikipedia.org/wiki/List_of_Unicode_characters
|
21
|
+
|
22
|
+
class TestEncoding < Minitest::Test
|
23
|
+
def setup
|
24
|
+
@original_encoding = Encoding.default_internal
|
25
|
+
Encoding.default_internal = nil
|
26
|
+
end
|
27
|
+
|
28
|
+
def teardown
|
29
|
+
Encoding.default_internal = @original_encoding
|
30
|
+
end
|
31
|
+
|
32
|
+
def file_for_encoding(encoding)
|
33
|
+
file_name = "model/bands.#{encoding.name.downcase}.xml"
|
34
|
+
File.join(File.dirname(__FILE__), file_name)
|
35
|
+
end
|
36
|
+
|
37
|
+
def load_encoding(encoding)
|
38
|
+
@encoding = encoding
|
39
|
+
file = file_for_encoding(encoding)
|
40
|
+
|
41
|
+
@doc = LibXML::XML::Document.file(file, options: LibXML::XML::Parser::Options::NOBLANKS)
|
42
|
+
end
|
43
|
+
|
44
|
+
def test_encoding
|
45
|
+
doc = LibXML::XML::Document.new
|
46
|
+
assert_equal(LibXML::XML::Encoding::NONE, doc.encoding)
|
47
|
+
assert_equal(Encoding::ASCII_8BIT, doc.rb_encoding)
|
48
|
+
|
49
|
+
file = File.expand_path(File.join(File.dirname(__FILE__), 'model/bands.xml'))
|
50
|
+
doc = LibXML::XML::Document.file(file)
|
51
|
+
assert_equal(LibXML::XML::Encoding::UTF_8, doc.encoding)
|
52
|
+
assert_equal(Encoding::UTF_8, doc.rb_encoding)
|
53
|
+
|
54
|
+
doc.encoding = LibXML::XML::Encoding::ISO_8859_1
|
55
|
+
assert_equal(LibXML::XML::Encoding::ISO_8859_1, doc.encoding)
|
56
|
+
assert_equal(Encoding::ISO8859_1, doc.rb_encoding)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_no_internal_encoding_iso_8859_1
|
60
|
+
load_encoding(Encoding::ISO_8859_1)
|
61
|
+
node = @doc.root.children.first
|
62
|
+
|
63
|
+
name = node.name
|
64
|
+
assert_equal(Encoding::UTF_8, name.encoding)
|
65
|
+
assert_equal("m\u00F6tley_cr\u00FCe", name)
|
66
|
+
assert_equal("109 195 182 116 108 101 121 95 99 114 195 188 101",
|
67
|
+
name.bytes.to_a.join(" "))
|
68
|
+
assert_equal("M\u00F6tley Cr\u00FCe is an American heavy metal band formed in Los Angeles, California in 1981.",
|
69
|
+
node.content)
|
70
|
+
|
71
|
+
name = name.encode(Encoding::ISO_8859_1)
|
72
|
+
assert_equal(Encoding::ISO_8859_1, name.encoding)
|
73
|
+
assert_equal("m\xF6tley_cr\xFCe".force_encoding(Encoding::ISO_8859_1), name)
|
74
|
+
assert_equal("109 246 116 108 101 121 95 99 114 252 101",
|
75
|
+
name.bytes.to_a.join(" "))
|
76
|
+
assert_equal("M\xF6tley Cr\xFCe is an American heavy metal band formed in Los Angeles, California in 1981.".force_encoding(Encoding::ISO_8859_1),
|
77
|
+
node.content.encode(Encoding::ISO_8859_1))
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_internal_encoding_iso_8859_1
|
81
|
+
Encoding.default_internal = Encoding::ISO_8859_1
|
82
|
+
load_encoding(Encoding::ISO_8859_1)
|
83
|
+
node = @doc.root.children.first
|
84
|
+
|
85
|
+
name = node.name
|
86
|
+
assert_equal(Encoding::ISO_8859_1, name.encoding)
|
87
|
+
assert_equal("109 246 116 108 101 121 95 99 114 252 101",
|
88
|
+
name.bytes.to_a.join(" "))
|
89
|
+
assert_equal("m\xF6tley_cr\xFCe".force_encoding(Encoding::ISO_8859_1), name)
|
90
|
+
assert_equal("109 246 116 108 101 121 95 99 114 252 101",
|
91
|
+
name.bytes.to_a.join(" "))
|
92
|
+
assert_equal("M\xF6tley Cr\xFCe is an American heavy metal band formed in Los Angeles, California in 1981.".force_encoding(Encoding::ISO_8859_1),
|
93
|
+
node.content.encode(Encoding::ISO_8859_1))
|
94
|
+
end
|
95
|
+
|
96
|
+
def test_no_internal_encoding_utf_8
|
97
|
+
load_encoding(Encoding::UTF_8)
|
98
|
+
node = @doc.root.children.first
|
99
|
+
|
100
|
+
name = node.name
|
101
|
+
assert_equal(@encoding, name.encoding)
|
102
|
+
assert_equal("109 195 182 116 108 101 121 95 99 114 195 188 101",
|
103
|
+
name.bytes.to_a.join(" "))
|
104
|
+
|
105
|
+
name = name.encode(Encoding::ISO_8859_1)
|
106
|
+
assert_equal(Encoding::ISO_8859_1, name.encoding)
|
107
|
+
assert_equal("109 246 116 108 101 121 95 99 114 252 101",
|
108
|
+
name.bytes.to_a.join(" "))
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_internal_encoding_utf_8
|
112
|
+
Encoding.default_internal = Encoding::ISO_8859_1
|
113
|
+
load_encoding(Encoding::UTF_8)
|
114
|
+
node = @doc.root.children.first
|
115
|
+
|
116
|
+
name = node.name
|
117
|
+
assert_equal(Encoding::ISO_8859_1, name.encoding)
|
118
|
+
assert_equal("109 246 116 108 101 121 95 99 114 252 101",
|
119
|
+
name.bytes.to_a.join(" "))
|
120
|
+
end
|
121
|
+
|
122
|
+
def test_encoding_conversions
|
123
|
+
assert_equal("UTF-8", LibXML::XML::Encoding.to_s(LibXML::XML::Encoding::UTF_8))
|
124
|
+
assert_equal(LibXML::XML::Encoding::UTF_8, LibXML::XML::Encoding.from_s("UTF-8"))
|
125
|
+
end
|
126
|
+
end
|
data/test/test_error.rb
CHANGED
@@ -1,194 +1,194 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
|
-
require_relative './test_helper'
|
4
|
-
require 'stringio'
|
5
|
-
|
6
|
-
class TestError < Minitest::Test
|
7
|
-
def test_error_codes
|
8
|
-
assert_equal(4, LibXML::XML::Error::DTD)
|
9
|
-
assert_equal(4, LibXML::XML::Error.const_get('DTD'))
|
10
|
-
|
11
|
-
assert_equal(4, LibXML::XML::Error::DOCUMENT_EMPTY)
|
12
|
-
assert_equal(4, LibXML::XML::Error.const_get('DOCUMENT_EMPTY'))
|
13
|
-
end
|
14
|
-
|
15
|
-
def test_invalid_handler
|
16
|
-
assert_raises(RuntimeError) do
|
17
|
-
LibXML::XML::Error.set_handler
|
18
|
-
end
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_handler
|
22
|
-
exception = nil
|
23
|
-
LibXML::XML::Error.set_handler do |error|
|
24
|
-
exception = error
|
25
|
-
end
|
26
|
-
|
27
|
-
# Raise the error
|
28
|
-
error = assert_raises(LibXML::XML::Error) do
|
29
|
-
LibXML::XML::Reader.string('<foo').read
|
30
|
-
end
|
31
|
-
assert_equal(exception, error)
|
32
|
-
|
33
|
-
# Check the handler worked
|
34
|
-
refute_nil(exception)
|
35
|
-
assert_kind_of(LibXML::XML::Error, exception)
|
36
|
-
assert_equal("Fatal error: Couldn't find end of Start Tag foo at :1.", exception.message)
|
37
|
-
assert_equal(LibXML::XML::Error::PARSER, exception.domain)
|
38
|
-
assert_equal(LibXML::XML::Error::GT_REQUIRED, exception.code)
|
39
|
-
assert_equal(LibXML::XML::Error::FATAL, exception.level)
|
40
|
-
assert_nil(exception.file)
|
41
|
-
assert_equal(1, exception.line)
|
42
|
-
assert_equal('foo', exception.str1)
|
43
|
-
assert_nil(exception.str2)
|
44
|
-
assert_nil(exception.str3)
|
45
|
-
assert_equal(0, exception.int1)
|
46
|
-
assert_equal(5, exception.int2)
|
47
|
-
assert_nil(exception.node)
|
48
|
-
end
|
49
|
-
|
50
|
-
def test_reset_handler
|
51
|
-
exception = nil
|
52
|
-
LibXML::XML::Error.set_handler do |error|
|
53
|
-
exception = error
|
54
|
-
end
|
55
|
-
|
56
|
-
LibXML::XML::Error.reset_handler
|
57
|
-
LibXML::XML::Reader.string('<foo')
|
58
|
-
assert_nil(exception)
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_get_handler
|
62
|
-
assert_respond_to(LibXML::XML::Error, :get_handler)
|
63
|
-
assert_equal(0, LibXML::XML::Error.method(:get_handler).arity)
|
64
|
-
|
65
|
-
saved_handler = LibXML::XML::Error.get_handler
|
66
|
-
LibXML::XML::Error.set_handler { puts "New handler" }
|
67
|
-
refute_equal(LibXML::XML::Error.get_handler, saved_handler)
|
68
|
-
|
69
|
-
if saved_handler
|
70
|
-
LibXML::XML::Error.set_handler &saved_handler
|
71
|
-
else
|
72
|
-
LibXML::XML::Error.reset_handler
|
73
|
-
end
|
74
|
-
|
75
|
-
assert_equal(LibXML::XML::Error.get_handler, saved_handler)
|
76
|
-
end
|
77
|
-
|
78
|
-
def test_verbose_handler
|
79
|
-
LibXML::XML::Error.set_handler(&LibXML::XML::Error::VERBOSE_HANDLER)
|
80
|
-
output = StringIO.new
|
81
|
-
original_stderr = Object::STDERR
|
82
|
-
|
83
|
-
Object.const_set(:STDERR, output)
|
84
|
-
begin
|
85
|
-
assert_raises(LibXML::XML::Error) do
|
86
|
-
LibXML::XML::Parser.string('<foo><bar/></foz>').parse
|
87
|
-
end
|
88
|
-
ensure
|
89
|
-
Object.const_set(:STDERR, original_stderr)
|
90
|
-
end
|
91
|
-
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.\n", output.string)
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_no_handler
|
95
|
-
LibXML::XML::Error.reset_handler
|
96
|
-
output = StringIO.new
|
97
|
-
original_stderr = Object::STDERR
|
98
|
-
|
99
|
-
Object.const_set(:STDERR, output)
|
100
|
-
begin
|
101
|
-
assert_raises(LibXML::XML::Error) do
|
102
|
-
LibXML::XML::Parser.string('<foo><bar/></foz>').parse
|
103
|
-
end
|
104
|
-
ensure
|
105
|
-
Object.const_set(:STDERR, original_stderr)
|
106
|
-
end
|
107
|
-
assert_equal('', output.string)
|
108
|
-
end
|
109
|
-
|
110
|
-
def test_parse_error
|
111
|
-
exception = assert_raises(LibXML::XML::Error) do
|
112
|
-
LibXML::XML::Parser.string('<foo><bar/></foz>').parse
|
113
|
-
end
|
114
|
-
|
115
|
-
assert_instance_of(LibXML::XML::Error, exception)
|
116
|
-
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", exception.message)
|
117
|
-
assert_equal(LibXML::XML::Error::PARSER, exception.domain)
|
118
|
-
assert_equal(LibXML::XML::Error::TAG_NAME_MISMATCH, exception.code)
|
119
|
-
assert_equal(LibXML::XML::Error::FATAL, exception.level)
|
120
|
-
assert_nil(exception.file)
|
121
|
-
assert_equal(1, exception.line)
|
122
|
-
end
|
123
|
-
|
124
|
-
def test_xpath_error
|
125
|
-
doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
|
126
|
-
|
127
|
-
exception = assert_raises(LibXML::XML::Error) do
|
128
|
-
doc.find('/foo[bar=test')
|
129
|
-
end
|
130
|
-
|
131
|
-
assert_instance_of(LibXML::XML::Error, exception)
|
132
|
-
assert_equal("Error: Invalid predicate.", exception.message)
|
133
|
-
assert_equal(LibXML::XML::Error::XPATH, exception.domain)
|
134
|
-
assert_equal(LibXML::XML::Error::XPATH_INVALID_PREDICATE_ERROR, exception.code)
|
135
|
-
assert_equal(LibXML::XML::Error::ERROR, exception.level)
|
136
|
-
assert_nil(exception.file)
|
137
|
-
assert_nil(nil)
|
138
|
-
end
|
139
|
-
|
140
|
-
def test_double_parse
|
141
|
-
LibXML::XML::Error.set_handler {|msg| nil }
|
142
|
-
parser = LibXML::XML::Parser.string("<test>something</test>")
|
143
|
-
parser.parse
|
144
|
-
|
145
|
-
error = assert_raises(LibXML::XML::Error) do
|
146
|
-
# Try parsing a second time
|
147
|
-
parser.parse
|
148
|
-
end
|
149
|
-
|
150
|
-
assert_equal(" LibXML::XML::Error.", error.to_s)
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_double_parse_register_handler
|
154
|
-
LibXML::XML::Parser.register_error_handler(lambda {|msg| nil })
|
155
|
-
parser = LibXML::XML::Parser.string("<test>something</test>")
|
156
|
-
parser.parse
|
157
|
-
|
158
|
-
error = assert_raises(LibXML::XML::Error) do
|
159
|
-
# Try parsing a second time
|
160
|
-
parser.parse
|
161
|
-
end
|
162
|
-
|
163
|
-
assert_equal(" LibXML::XML::Error.", error.to_s)
|
164
|
-
end
|
165
|
-
|
166
|
-
def test_libxml_parser_empty_string
|
167
|
-
error = assert_raises(TypeError) do
|
168
|
-
LibXML::XML::Parser.string(nil)
|
169
|
-
end
|
170
|
-
assert_equal('wrong argument type nil (expected String)', error.to_s)
|
171
|
-
|
172
|
-
error = assert_raises(ArgumentError) do
|
173
|
-
LibXML::XML::Parser.string("")
|
174
|
-
end
|
175
|
-
assert_equal('Must specify a string with one or more characters', error.to_s)
|
176
|
-
end
|
177
|
-
|
178
|
-
def test_error_domain_to_s
|
179
|
-
exception = assert_raises(LibXML::XML::Error) do
|
180
|
-
LibXML::XML::Parser.string('<foo href="http://example.org/cgi?k1=v1&k2=v2"></foo>').parse
|
181
|
-
end
|
182
|
-
|
183
|
-
assert_equal(LibXML::XML::Error::PARSER, exception.domain)
|
184
|
-
assert_equal("PARSER", exception.domain_to_s)
|
185
|
-
end
|
186
|
-
|
187
|
-
def test_error_code_to_s
|
188
|
-
exception = assert_raises(LibXML::XML::Error) do
|
189
|
-
LibXML::XML::Parser.string('<foo href="http://example.org/cgi?k1=v1&k2=v2"></foo>').parse
|
190
|
-
end
|
191
|
-
assert_equal(LibXML::XML::Error::ENTITYREF_SEMICOL_MISSING, exception.code)
|
192
|
-
assert_equal("ENTITYREF_SEMICOL_MISSING", exception.code_to_s)
|
193
|
-
end
|
194
|
-
end
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
require_relative './test_helper'
|
4
|
+
require 'stringio'
|
5
|
+
|
6
|
+
class TestError < Minitest::Test
|
7
|
+
def test_error_codes
|
8
|
+
assert_equal(4, LibXML::XML::Error::DTD)
|
9
|
+
assert_equal(4, LibXML::XML::Error.const_get('DTD'))
|
10
|
+
|
11
|
+
assert_equal(4, LibXML::XML::Error::DOCUMENT_EMPTY)
|
12
|
+
assert_equal(4, LibXML::XML::Error.const_get('DOCUMENT_EMPTY'))
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_invalid_handler
|
16
|
+
assert_raises(RuntimeError) do
|
17
|
+
LibXML::XML::Error.set_handler
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_handler
|
22
|
+
exception = nil
|
23
|
+
LibXML::XML::Error.set_handler do |error|
|
24
|
+
exception = error
|
25
|
+
end
|
26
|
+
|
27
|
+
# Raise the error
|
28
|
+
error = assert_raises(LibXML::XML::Error) do
|
29
|
+
LibXML::XML::Reader.string('<foo').read
|
30
|
+
end
|
31
|
+
assert_equal(exception, error)
|
32
|
+
|
33
|
+
# Check the handler worked
|
34
|
+
refute_nil(exception)
|
35
|
+
assert_kind_of(LibXML::XML::Error, exception)
|
36
|
+
assert_equal("Fatal error: Couldn't find end of Start Tag foo at :1.", exception.message)
|
37
|
+
assert_equal(LibXML::XML::Error::PARSER, exception.domain)
|
38
|
+
assert_equal(LibXML::XML::Error::GT_REQUIRED, exception.code)
|
39
|
+
assert_equal(LibXML::XML::Error::FATAL, exception.level)
|
40
|
+
assert_nil(exception.file)
|
41
|
+
assert_equal(1, exception.line)
|
42
|
+
assert_equal('foo', exception.str1)
|
43
|
+
assert_nil(exception.str2)
|
44
|
+
assert_nil(exception.str3)
|
45
|
+
assert_equal(0, exception.int1)
|
46
|
+
assert_equal(5, exception.int2)
|
47
|
+
assert_nil(exception.node)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_reset_handler
|
51
|
+
exception = nil
|
52
|
+
LibXML::XML::Error.set_handler do |error|
|
53
|
+
exception = error
|
54
|
+
end
|
55
|
+
|
56
|
+
LibXML::XML::Error.reset_handler
|
57
|
+
LibXML::XML::Reader.string('<foo')
|
58
|
+
assert_nil(exception)
|
59
|
+
end
|
60
|
+
|
61
|
+
def test_get_handler
|
62
|
+
assert_respond_to(LibXML::XML::Error, :get_handler)
|
63
|
+
assert_equal(0, LibXML::XML::Error.method(:get_handler).arity)
|
64
|
+
|
65
|
+
saved_handler = LibXML::XML::Error.get_handler
|
66
|
+
LibXML::XML::Error.set_handler { puts "New handler" }
|
67
|
+
refute_equal(LibXML::XML::Error.get_handler, saved_handler)
|
68
|
+
|
69
|
+
if saved_handler
|
70
|
+
LibXML::XML::Error.set_handler &saved_handler
|
71
|
+
else
|
72
|
+
LibXML::XML::Error.reset_handler
|
73
|
+
end
|
74
|
+
|
75
|
+
assert_equal(LibXML::XML::Error.get_handler, saved_handler)
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_verbose_handler
|
79
|
+
LibXML::XML::Error.set_handler(&LibXML::XML::Error::VERBOSE_HANDLER)
|
80
|
+
output = StringIO.new
|
81
|
+
original_stderr = Object::STDERR
|
82
|
+
|
83
|
+
Object.const_set(:STDERR, output)
|
84
|
+
begin
|
85
|
+
assert_raises(LibXML::XML::Error) do
|
86
|
+
LibXML::XML::Parser.string('<foo><bar/></foz>').parse
|
87
|
+
end
|
88
|
+
ensure
|
89
|
+
Object.const_set(:STDERR, original_stderr)
|
90
|
+
end
|
91
|
+
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.\n", output.string)
|
92
|
+
end
|
93
|
+
|
94
|
+
def test_no_handler
|
95
|
+
LibXML::XML::Error.reset_handler
|
96
|
+
output = StringIO.new
|
97
|
+
original_stderr = Object::STDERR
|
98
|
+
|
99
|
+
Object.const_set(:STDERR, output)
|
100
|
+
begin
|
101
|
+
assert_raises(LibXML::XML::Error) do
|
102
|
+
LibXML::XML::Parser.string('<foo><bar/></foz>').parse
|
103
|
+
end
|
104
|
+
ensure
|
105
|
+
Object.const_set(:STDERR, original_stderr)
|
106
|
+
end
|
107
|
+
assert_equal('', output.string)
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_parse_error
|
111
|
+
exception = assert_raises(LibXML::XML::Error) do
|
112
|
+
LibXML::XML::Parser.string('<foo><bar/></foz>').parse
|
113
|
+
end
|
114
|
+
|
115
|
+
assert_instance_of(LibXML::XML::Error, exception)
|
116
|
+
assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", exception.message)
|
117
|
+
assert_equal(LibXML::XML::Error::PARSER, exception.domain)
|
118
|
+
assert_equal(LibXML::XML::Error::TAG_NAME_MISMATCH, exception.code)
|
119
|
+
assert_equal(LibXML::XML::Error::FATAL, exception.level)
|
120
|
+
assert_nil(exception.file)
|
121
|
+
assert_equal(1, exception.line)
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_xpath_error
|
125
|
+
doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
|
126
|
+
|
127
|
+
exception = assert_raises(LibXML::XML::Error) do
|
128
|
+
doc.find('/foo[bar=test')
|
129
|
+
end
|
130
|
+
|
131
|
+
assert_instance_of(LibXML::XML::Error, exception)
|
132
|
+
assert_equal("Error: Invalid predicate.", exception.message)
|
133
|
+
assert_equal(LibXML::XML::Error::XPATH, exception.domain)
|
134
|
+
assert_equal(LibXML::XML::Error::XPATH_INVALID_PREDICATE_ERROR, exception.code)
|
135
|
+
assert_equal(LibXML::XML::Error::ERROR, exception.level)
|
136
|
+
assert_nil(exception.file)
|
137
|
+
assert_nil(nil)
|
138
|
+
end
|
139
|
+
|
140
|
+
def test_double_parse
|
141
|
+
LibXML::XML::Error.set_handler {|msg| nil }
|
142
|
+
parser = LibXML::XML::Parser.string("<test>something</test>")
|
143
|
+
parser.parse
|
144
|
+
|
145
|
+
error = assert_raises(LibXML::XML::Error) do
|
146
|
+
# Try parsing a second time
|
147
|
+
parser.parse
|
148
|
+
end
|
149
|
+
|
150
|
+
assert_equal(" LibXML::XML::Error.", error.to_s)
|
151
|
+
end
|
152
|
+
|
153
|
+
def test_double_parse_register_handler
|
154
|
+
LibXML::XML::Parser.register_error_handler(lambda {|msg| nil })
|
155
|
+
parser = LibXML::XML::Parser.string("<test>something</test>")
|
156
|
+
parser.parse
|
157
|
+
|
158
|
+
error = assert_raises(LibXML::XML::Error) do
|
159
|
+
# Try parsing a second time
|
160
|
+
parser.parse
|
161
|
+
end
|
162
|
+
|
163
|
+
assert_equal(" LibXML::XML::Error.", error.to_s)
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_libxml_parser_empty_string
|
167
|
+
error = assert_raises(TypeError) do
|
168
|
+
LibXML::XML::Parser.string(nil)
|
169
|
+
end
|
170
|
+
assert_equal('wrong argument type nil (expected String)', error.to_s)
|
171
|
+
|
172
|
+
error = assert_raises(ArgumentError) do
|
173
|
+
LibXML::XML::Parser.string("")
|
174
|
+
end
|
175
|
+
assert_equal('Must specify a string with one or more characters', error.to_s)
|
176
|
+
end
|
177
|
+
|
178
|
+
def test_error_domain_to_s
|
179
|
+
exception = assert_raises(LibXML::XML::Error) do
|
180
|
+
LibXML::XML::Parser.string('<foo href="http://example.org/cgi?k1=v1&k2=v2"></foo>').parse
|
181
|
+
end
|
182
|
+
|
183
|
+
assert_equal(LibXML::XML::Error::PARSER, exception.domain)
|
184
|
+
assert_equal("PARSER", exception.domain_to_s)
|
185
|
+
end
|
186
|
+
|
187
|
+
def test_error_code_to_s
|
188
|
+
exception = assert_raises(LibXML::XML::Error) do
|
189
|
+
LibXML::XML::Parser.string('<foo href="http://example.org/cgi?k1=v1&k2=v2"></foo>').parse
|
190
|
+
end
|
191
|
+
assert_equal(LibXML::XML::Error::ENTITYREF_SEMICOL_MISSING, exception.code)
|
192
|
+
assert_equal("ENTITYREF_SEMICOL_MISSING", exception.code_to_s)
|
193
|
+
end
|
194
|
+
end
|