libxml-ruby 4.0.0-x64-mingw-ucrt → 4.1.2-x64-mingw-ucrt

Sign up to get free protection for your applications and to get access to all the features.
data/lib/libxml/error.rb CHANGED
@@ -3,24 +3,24 @@
3
3
  module LibXML
4
4
  module XML
5
5
  class Error
6
- # Create mapping from domain constant value to keys
6
+ # Create mapping from domain constant values to keys
7
7
  DOMAIN_CODE_MAP = [:NO_ERROR, :PARSER, :TREE, :NAMESPACE, :DTD, :HTML, :MEMORY,
8
8
  :OUTPUT, :IO, :FTP, :HTTP, :XINCLUDE, :XPATH, :XPOINTER, :REGEXP,
9
9
  :DATATYPE, :SCHEMASP, :SCHEMASV, :RELAXNGP, :RELAXNGV, :CATALOG,
10
10
  :C14N, :XSLT, :VALID, :CHECK, :WRITER, :MODULE, :I18N, :SCHEMATRONV].inject(Hash.new) do |hash, code|
11
11
  if const_defined?(code)
12
- hash[const_get(code)] = code.to_s
12
+ hash[const_get(code)] = code
13
13
  end
14
14
  hash
15
15
  end
16
16
 
17
- # Create mapping from domain constant value to keys
17
+ # Create mapping from error constant values (so need to remove domain_codes) to keys
18
18
  ERROR_CODE_MAP = Hash.new.tap do |map|
19
19
  (constants -
20
20
  DOMAIN_CODE_MAP.values - #Domains
21
21
  [:NONE, :WARNING, :ERROR, :FATAL] # Levels
22
22
  ).each do |code|
23
- map[const_get(code)] = code.to_s
23
+ map[const_get(code)] = code
24
24
  end
25
25
  end
26
26
 
@@ -70,11 +70,11 @@ module LibXML
70
70
  end
71
71
 
72
72
  def domain_to_s
73
- DOMAIN_CODE_MAP[self.domain]
73
+ DOMAIN_CODE_MAP[self.domain].to_s
74
74
  end
75
75
 
76
76
  def code_to_s
77
- ERROR_CODE_MAP[self.code]
77
+ ERROR_CODE_MAP[self.code].to_s
78
78
  end
79
79
 
80
80
  def to_s
@@ -92,4 +92,4 @@ module LibXML
92
92
  end
93
93
  end
94
94
 
95
- LibXML::XML::Error.set_handler(&LibXML::XML::Error::VERBOSE_HANDLER)
95
+ LibXML::XML::Error.set_handler(&LibXML::XML::Error::VERBOSE_HANDLER)
@@ -1,19 +1,27 @@
1
- # encoding: UTF-8
2
-
3
- module LibXML
4
- module XML
5
- class Schema::Element
6
- def required?
7
- !min_occurs.zero?
8
- end
9
-
10
- def array?
11
- max_occurs > 1
12
- end
13
-
14
- def elements
15
- type.elements
16
- end
17
- end
18
- end
19
- end
1
+ # encoding: UTF-8
2
+
3
+ module LibXML
4
+ module XML
5
+ class Schema::Element
6
+ def min_occurs
7
+ @min
8
+ end
9
+
10
+ def max_occurs
11
+ @max
12
+ end
13
+
14
+ def required?
15
+ !min_occurs.zero?
16
+ end
17
+
18
+ def array?
19
+ max_occurs > 1
20
+ end
21
+
22
+ def elements
23
+ type.elements
24
+ end
25
+ end
26
+ end
27
+ end
data/test/test_error.rb CHANGED
@@ -4,135 +4,153 @@ require_relative './test_helper'
4
4
  require 'stringio'
5
5
 
6
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
- # LibXML::XML::Error.set_handler(&saved_handler)
70
- # assert_equal(LibXML::XML::Error.get_handler, saved_handler)
71
- # end
72
- #
73
- # def test_verbose_handler
74
- # LibXML::XML::Error.set_handler(&LibXML::XML::Error::VERBOSE_HANDLER)
75
- # output = StringIO.new
76
- # original_stderr = Object::STDERR
77
- #
78
- # Object.const_set(:STDERR, output)
79
- # begin
80
- # assert_raises(LibXML::XML::Error) do
81
- # LibXML::XML::Parser.string('<foo><bar/></foz>').parse
82
- # end
83
- # ensure
84
- # Object.const_set(:STDERR, original_stderr)
85
- # end
86
- # assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.\n", output.string)
87
- # end
88
- #
89
- # def test_no_hanlder
90
- # LibXML::XML::Error.reset_handler
91
- # output = StringIO.new
92
- # original_stderr = Object::STDERR
93
- #
94
- # Object.const_set(:STDERR, output)
95
- # begin
96
- # assert_raises(LibXML::XML::Error) do
97
- # LibXML::XML::Parser.string('<foo><bar/></foz>').parse
98
- # end
99
- # ensure
100
- # Object.const_set(:STDERR, original_stderr)
101
- # end
102
- # assert_equal('', output.string)
103
- # end
104
- #
105
- # def test_parse_error
106
- # exception = assert_raises(LibXML::XML::Error) do
107
- # LibXML::XML::Parser.string('<foo><bar/></foz>').parse
108
- # end
109
- #
110
- # assert_instance_of(LibXML::XML::Error, exception)
111
- # assert_equal("Fatal error: Opening and ending tag mismatch: foo line 1 and foz at :1.", exception.message)
112
- # assert_equal(LibXML::XML::Error::PARSER, exception.domain)
113
- # assert_equal(LibXML::XML::Error::TAG_NAME_MISMATCH, exception.code)
114
- # assert_equal(LibXML::XML::Error::FATAL, exception.level)
115
- # assert_nil(exception.file)
116
- # assert_equal(1, exception.line)
117
- # end
118
- #
119
- # def test_xpath_error
120
- # doc = LibXML::XML::Document.file(File.join(File.dirname(__FILE__), 'model/soap.xml'))
121
- #
122
- # exception = assert_raises(LibXML::XML::Error) do
123
- # doc.find('/foo[bar=test')
124
- # end
125
- #
126
- # assert_instance_of(LibXML::XML::Error, exception)
127
- # assert_equal("Error: Invalid predicate.", exception.message)
128
- # assert_equal(LibXML::XML::Error::XPATH, exception.domain)
129
- # assert_equal(LibXML::XML::Error::XPATH_INVALID_PREDICATE_ERROR, exception.code)
130
- # assert_equal(LibXML::XML::Error::ERROR, exception.level)
131
- # assert_nil(exception.file)
132
- # assert_nil(nil)
133
- # end
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
134
139
 
135
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
136
154
  LibXML::XML::Parser.register_error_handler(lambda {|msg| nil })
137
155
  parser = LibXML::XML::Parser.string("<test>something</test>")
138
156
  parser.parse
@@ -145,34 +163,32 @@ class TestError < Minitest::Test
145
163
  assert_equal(" LibXML::XML::Error.", error.to_s)
146
164
  end
147
165
 
148
- # def test_libxml_parser_empty_string
149
- # xp = LibXML::XML::Parser.new
150
- #
151
- # error = assert_raises(TypeError) do
152
- # xp.string = nil
153
- # end
154
- # assert_equal('wrong argument type nil (expected String)', error.to_s)
155
- #
156
- # error = assert_raises(ArgumentError) do
157
- # xp.string = ''
158
- # end
159
- # assert_equal('Must specify a string with one or more characters', error.to_s)
160
- # end
161
- #
162
- # def test_error_domain_to_s
163
- # exception = assert_raises(LibXML::XML::Error) do
164
- # LibXML::XML::Parser.string('<foo href="http://example.org/cgi?k1=v1&k2=v2"></foo>').parse
165
- # end
166
- #
167
- # assert_equal(LibXML::XML::Error::PARSER, exception.domain)
168
- # assert_equal("PARSER",exception.domain_to_s)
169
- # end
170
- #
171
- # def test_error_code_to_s
172
- # exception = assert_raises(LibXML::XML::Error) do
173
- # LibXML::XML::Parser.string('<foo href="http://example.org/cgi?k1=v1&k2=v2"></foo>').parse
174
- # end
175
- # assert_equal(LibXML::XML::Error::ENTITYREF_SEMICOL_MISSING, exception.code)
176
- # assert_equal("ENTITYREF_SEMICOL_MISSING",exception.code_to_s)
177
- # end
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
178
194
  end