Welcome to the "trac"-ing site of soap4r!
[soap4r] [httpclient] [openpgp4u] [pkcs1] [logger] [csv] [vtr]

root/trunk/test/xsd/test_xsd.rb

Revision 1993, 44.9 kB (checked in by nahi, 1 year ago)
  • (merged from branches/1_5: -r1989:1992)
    • simpleRestriction + minExclusive/maxExclusive caused NameError?. closes #438.
    • parsing empty element for enumeration type caused NameError?. closes #440.
    • fixed wrongly implemented lexical representations for gMonth, gMonthDay and gDay. closes #439.
  • Property svn:eol-style set to native
  • Property svn:keywords set to author date id revision
Line 
1 require 'test/unit'
2 require 'xsd/datatypes'
3
4
5 module XSD
6
7
8 class TestXSD < Test::Unit::TestCase
9   NegativeZero = (-1.0 / (1.0 / 0.0))
10
11   def setup
12   end
13
14   def teardown
15   end
16
17   def assert_parsed_result(klass, str)
18     o = klass.new(str)
19     assert_equal(str, o.to_s)
20   end
21
22   def test_NSDBase
23     o = XSD::NSDBase.new
24     assert_equal(nil, o.type)
25   end
26
27   def test_XSDBase
28     o = XSD::XSDAnySimpleType.new
29     assert_equal(nil, o.data)
30     assert_equal(true, o.is_nil)
31     assert_equal('', o.to_s)
32   end
33
34   def test_XSDNil
35     o = XSD::XSDNil.new
36     assert_equal(XSD::Namespace, o.type.namespace)
37     assert_equal(XSD::NilLiteral, o.type.name)
38     assert_equal(nil, o.data)
39     assert_equal(true, o.is_nil)
40
41     o = XSD::XSDNil.new(nil)
42     assert_equal(true, o.is_nil)
43     assert_equal(nil, o.data)
44     assert_equal("", o.to_s)
45     o = XSD::XSDNil.new('var')
46     assert_equal(false, o.is_nil)
47     assert_equal('var', o.data)
48     assert_equal('var', o.to_s)
49   end
50
51   def test_XSDString_UTF8
52     o = XSD::XSDString.new
53     assert_equal(XSD::Namespace, o.type.namespace)
54     assert_equal(XSD::StringLiteral, o.type.name)
55     assert_equal(nil, o.data)
56     assert_equal(true, o.is_nil)
57
58     str = "abc"
59     assert_equal(str, XSD::XSDString.new(str).data)
60     assert_equal(str, XSD::XSDString.new(str).to_s)
61     back = XSD::XSDString.strict_ces_validation
62     XSD::XSDString.strict_ces_validation = true
63     begin
64       assert_raises(XSD::ValueSpaceError) do
65         XSD::XSDString.new("\0")
66       end
67       assert_raises(XSD::ValueSpaceError) do
68         p XSD::XSDString.new("\xC0\xC0").to_s
69       end
70     ensure
71       XSD::XSDString.strict_ces_validation = back
72     end
73   end
74
75   def test_XSDString_NONE
76     XSD::Charset.module_eval { @encoding_backup = @internal_encoding; @internal_encoding = "NONE" }
77     begin
78       o = XSD::XSDString.new
79       assert_equal(XSD::Namespace, o.type.namespace)
80       assert_equal(XSD::StringLiteral, o.type.name)
81       assert_equal(nil, o.data)
82       assert_equal(true, o.is_nil)
83
84       str = "abc"
85       assert_equal(str, XSD::XSDString.new(str).data)
86       assert_equal(str, XSD::XSDString.new(str).to_s)
87       back = XSD::XSDString.strict_ces_validation
88       XSD::XSDString.strict_ces_validation = true
89       begin
90         assert_raises(XSD::ValueSpaceError) do
91           XSD::XSDString.new("\0")
92         end
93         assert_raises(XSD::ValueSpaceError) do
94           XSD::XSDString.new("\xC0\xC0").to_s
95         end
96       ensure
97         XSD::XSDString.strict_ces_validation = back
98       end
99     ensure
100       XSD::Charset.module_eval { @internal_encoding = @encoding_backup }
101     end
102   end
103
104   def test_XSDNormalizedString
105     XSD::Charset.module_eval { @encoding_backup = @internal_encoding; @internal_encoding = "NONE" }
106     begin
107       o = XSD::XSDNormalizedString.new
108       assert_equal(XSD::Namespace, o.type.namespace)
109       assert_equal(XSD::NormalizedStringLiteral, o.type.name)
110       assert_equal(nil, o.data)
111       assert_equal(true, o.is_nil)
112
113       str = "abc"
114       assert_equal(str, XSD::XSDNormalizedString.new(str).data)
115       assert_equal(str, XSD::XSDNormalizedString.new(str).to_s)
116       back = XSD::XSDString.strict_ces_validation
117       XSD::XSDString.strict_ces_validation = true
118       begin
119         assert_raises(XSD::ValueSpaceError) do
120           XSD::XSDNormalizedString.new("\0")
121         end
122         assert_raises(XSD::ValueSpaceError) do
123           XSD::XSDNormalizedString.new("\xC0\xC0").to_s
124         end
125         assert_raises(XSD::ValueSpaceError) do
126           XSD::XSDNormalizedString.new("a\tb").to_s
127         end
128         assert_raises(XSD::ValueSpaceError) do
129           XSD::XSDNormalizedString.new("a\r").to_s
130         end
131         assert_raises(XSD::ValueSpaceError) do
132           XSD::XSDNormalizedString.new("\nb").to_s
133         end
134       ensure
135         XSD::XSDString.strict_ces_validation = back
136       end
137     ensure
138       XSD::Charset.module_eval { @internal_encoding = @encoding_backup }
139     end
140   end
141
142   def test_XSDToken
143     XSD::Charset.module_eval { @encoding_backup = @internal_encoding; @internal_encoding = "NONE" }
144     begin
145       o = XSD::XSDToken.new
146       assert_equal(XSD::Namespace, o.type.namespace)
147       assert_equal(XSD::TokenLiteral, o.type.name)
148       assert_equal(nil, o.data)
149       assert_equal(true, o.is_nil)
150
151       str = "abc"
152       assert_equal(str, XSD::XSDToken.new(str).data)
153       assert_equal(str, XSD::XSDToken.new(str).to_s)
154       back = XSD::XSDString.strict_ces_validation
155       XSD::XSDString.strict_ces_validation = true
156       begin
157         assert_raises(XSD::ValueSpaceError) do
158           XSD::XSDToken.new("\0")
159         end
160         assert_raises(XSD::ValueSpaceError) do
161           XSD::XSDToken.new("\xC0\xC0").to_s
162         end
163         assert_raises(XSD::ValueSpaceError) do
164           XSD::XSDToken.new("a\tb").to_s
165         end
166         assert_raises(XSD::ValueSpaceError) do
167           XSD::XSDToken.new("a\r").to_s
168         end
169         assert_raises(XSD::ValueSpaceError) do
170           XSD::XSDToken.new("\nb").to_s
171         end
172         assert_raises(XSD::ValueSpaceError) do
173           XSD::XSDToken.new(" a").to_s
174         end
175         assert_raises(XSD::ValueSpaceError) do
176           XSD::XSDToken.new("b ").to_s
177         end
178         assert_raises(XSD::ValueSpaceError) do
179           XSD::XSDToken.new("a  b").to_s
180         end
181         assert_equal("a b", XSD::XSDToken.new("a b").data)
182       ensure
183         XSD::XSDString.strict_ces_validation = back
184       end
185     ensure
186       XSD::Charset.module_eval { @internal_encoding = @encoding_backup }
187     end
188   end
189
190   def test_XSDLanguage
191     o = XSD::XSDLanguage.new
192     assert_equal(XSD::Namespace, o.type.namespace)
193     assert_equal(XSD::LanguageLiteral, o.type.name)
194     assert_equal(nil, o.data)
195     assert_equal(true, o.is_nil)
196
197     str = "ja"
198     assert_equal(str, XSD::XSDLanguage.new(str).data)
199     assert_equal(str, XSD::XSDLanguage.new(str).to_s)
200     str = "ja-jp"
201     assert_equal(str, XSD::XSDLanguage.new(str).data)
202     assert_equal(str, XSD::XSDLanguage.new(str).to_s)
203     assert_raises(XSD::ValueSpaceError) do
204       XSD::XSDLanguage.new("ja-jp-")
205     end
206     assert_raises(XSD::ValueSpaceError) do
207       XSD::XSDLanguage.new("-ja-")
208     end
209     assert_raises(XSD::ValueSpaceError) do
210       XSD::XSDLanguage.new("ja-")
211     end
212     assert_raises(XSD::ValueSpaceError) do
213       XSD::XSDLanguage.new("a1-01")
214     end
215     assert_equal("aA-01", XSD::XSDLanguage.new("aA-01").to_s)
216   end
217
218   def test_XSDBoolean
219     o = XSD::XSDBoolean.new
220     assert_equal(XSD::Namespace, o.type.namespace)
221     assert_equal(XSD::BooleanLiteral, o.type.name)
222     assert_equal(nil, o.data)
223     assert_equal(true, o.is_nil)
224
225     targets = [
226       ["true", true],
227       ["1", true],
228       ["false", false],
229       ["0", false],
230     ]
231     targets.each do |data, expected|
232       assert_equal(expected, XSD::XSDBoolean.new(data).data)
233       assert_equal(expected.to_s, XSD::XSDBoolean.new(data).to_s)
234     end
235
236     assert_raises(XSD::ValueSpaceError) do
237       XSD::XSDBoolean.new("nil").to_s
238     end
239   end
240
241   def test_XSDDecimal
242     o = XSD::XSDDecimal.new
243     assert_equal(XSD::Namespace, o.type.namespace)
244     assert_equal(XSD::DecimalLiteral, o.type.name)
245     assert_equal(nil, o.data)
246     assert_equal(true, o.is_nil)
247
248     targets = [
249       0,
250       1000000000,
251       -9999999999,
252       12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890,
253       12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890,
254       -1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789,
255     ]
256     targets.each do |dec|
257       assert_equal(dec.to_s, XSD::XSDDecimal.new(dec).data)
258     end
259
260     targets = [
261       "0",
262       "0.00000001",
263       "1000000000",
264       "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
265       "-12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123.45678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",
266     ]
267     targets.each do |str|
268       assert_equal(str, XSD::XSDDecimal.new(str).to_s)
269     end
270
271     targets = [
272       ["-0", "0"],
273       ["+0", "0"],
274       ["0.0", "0"],
275       ["-0.0", "0"],
276       ["+0.0", "0"],
277       ["0.", "0"],
278       [".0", "0"],
279       [
280         "+0.12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
281         "0.1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
282      ],
283       [
284         ".0000012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
285         "0.000001234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789"
286      ],
287       [
288         "-12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890.",
289         "-12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
290      ],
291     ]
292     targets.each do |data, expected|
293       assert_equal(expected, XSD::XSDDecimal.new(data).to_s)
294     end
295
296     targets = [
297       "0.000000000000a",
298       "00a.0000000000001",
299       "+-5",
300     ]
301     targets.each do |d|
302       assert_raises(XSD::ValueSpaceError) do
303         XSD::XSDDecimal.new(d)
304       end
305     end
306   end
307
308   def test_XSDFloat
309     o = XSD::XSDFloat.new
310     assert_equal(XSD::Namespace, o.type.namespace)
311     assert_equal(XSD::FloatLiteral, o.type.name)
312     assert_equal(nil, o.data)
313     assert_equal(true, o.is_nil)
314
315     targets = [
316       3.14159265358979,
317       12.34e36,
318       1.402e-45,
319       -1.402e-45,
320     ]
321     targets.each do |f|
322       assert_equal(f, XSD::XSDFloat.new(f).data)
323     end
324
325     targets = [
326       "+3.141592654",
327       "+1.234e+37",
328       "+1.402e-45",
329       "-1.402e-45",
330     ]
331     targets.each do |f|
332       assert_equal(f, XSD::XSDFloat.new(f).to_s)
333     end
334
335     targets = [
336       [3, "+3"],        # should be 3.0?
337       [-2, "-2"],       # ditto
338       [3.14159265358979, "+3.141592654"],
339       [12.34e36, "+1.234e+37"],
340       [1.402e-45, "+1.402e-45"],
341       [-1.402e-45, "-1.402e-45"],
342       ["1.402e", "+1.402"],
343       ["12.34E36", "+1.234e+37"],
344       ["1.402E-45", "+1.402e-45"],
345       ["-1.402E-45", "-1.402e-45"],
346       ["1.402E", "+1.402"],
347     ]
348     targets.each do |f, str|
349       assert_equal(str, XSD::XSDFloat.new(f).to_s)
350     end
351
352     assert_equal("+0", XSD::XSDFloat.new(+0.0).to_s)
353     assert_equal("-0", XSD::XSDFloat.new(NegativeZero).to_s)
354     assert(XSD::XSDFloat.new(0.0/0.0).data.nan?)
355     assert_equal("INF", XSD::XSDFloat.new(1.0/0.0).to_s)
356     assert_equal(1, XSD::XSDFloat.new(1.0/0.0).data.infinite?)
357     assert_equal("-INF", XSD::XSDFloat.new(-1.0/0.0).to_s)
358     assert_equal(-1, XSD::XSDFloat.new(-1.0/0.0).data.infinite?)
359
360     targets = [
361       "0.000000000000a",
362       "00a.0000000000001",
363       "+-5",
364       "5_0",
365     ]
366     targets.each do |d|
367       assert_raises(XSD::ValueSpaceError) do
368         XSD::XSDFloat.new(d)
369       end
370     end
371   end
372
373   def test_XSDDouble
374     o = XSD::XSDDouble.new
375     assert_equal(XSD::Namespace, o.type.namespace)
376     assert_equal(XSD::DoubleLiteral, o.type.name)
377     assert_equal(nil, o.data)
378     assert_equal(true, o.is_nil)
379
380     targets = [
381       3.14159265358979,
382       12.34e36,
383       1.402e-45,
384       -1.402e-45,
385     ]
386     targets.each do |f|
387       assert_equal(f, XSD::XSDDouble.new(f).data)
388     end
389
390     targets = [
391       "+3.14159265358979",
392       "+1.234e+37",
393       "+1.402e-45",
394       "-1.402e-45",
395     ]
396     targets.each do |f|
397       assert_equal(f, XSD::XSDDouble.new(f).to_s)
398     end
399
400     targets = [
401       [3, "+3"],        # should be 3.0?
402       [-2, "-2"],       # ditto.
403       [3.14159265358979, "+3.14159265358979"],
404       [12.34e36, "+1.234e+37"],
405       [1.402e-45, "+1.402e-45"],
406       [-1.402e-45, "-1.402e-45"],
407       ["1.402e", "+1.402"],
408       ["12.34E36", "+1.234e+37"],
409       ["1.402E-45", "+1.402e-45"],
410       ["-1.402E-45", "-1.402e-45"],
411       ["1.402E", "+1.402"],
412     ]
413     targets.each do |f, str|
414       assert_equal(str, XSD::XSDDouble.new(f).to_s)
415     end
416
417     assert_equal("+0", XSD::XSDFloat.new(+0.0).to_s)
418     assert_equal("-0", XSD::XSDFloat.new(NegativeZero).to_s)
419     assert_equal("NaN", XSD::XSDDouble.new(0.0/0.0).to_s)
420     assert(XSD::XSDDouble.new(0.0/0.0).data.nan?)
421     assert_equal("INF", XSD::XSDDouble.new(1.0/0.0).to_s)
422     assert_equal(1, XSD::XSDDouble.new(1.0/0.0).data.infinite?)
423     assert_equal("-INF", XSD::XSDDouble.new(-1.0/0.0).to_s)
424     assert_equal(-1, XSD::XSDDouble.new(-1.0/0.0).data.infinite?)
425
426     targets = [
427       "0.000000000000a",
428       "00a.0000000000001",
429       "+-5",
430     ]
431     targets.each do |d|
432       assert_raises(XSD::ValueSpaceError) do
433         XSD::XSDDouble.new(d)
434       end
435     end
436   end
437
438   def test_XSDDuration
439     o = XSD::XSDDuration.new
440     assert_equal(XSD::Namespace, o.type.namespace)
441     assert_equal(XSD::DurationLiteral, o.type.name)
442     assert_equal(nil, o.data)
443     assert_equal(true, o.is_nil)
444
445     targets = [
446       "P1Y2M3DT4H5M6S",
447       "P1234Y5678M9012DT3456H7890M1234.5678S",
448       "PT3456H7890M1234.5678S",
449       "P1234Y5678M9012D",
450       "-P1234Y5678M9012DT3456H7890M1234.5678S",
451       "P5678M9012DT3456H7890M1234.5678S",
452       "-P1234Y9012DT3456H7890M1234.5678S",
453       "+P1234Y5678MT3456H7890M1234.5678S",
454       "P1234Y5678M9012DT7890M1234.5678S",
455       "-P1234Y5678M9012DT3456H1234.5678S",
456       "+P1234Y5678M9012DT3456H7890M",
457       "P123400000000000Y",
458       "-P567800000000000M",
459       "+P901200000000000D",
460       "PT345600000000000H",
461       "-PT789000000000000M",
462       "+PT123400000000000.000000000005678S",
463       "P1234YT1234.5678S",
464       "-P5678MT7890M",
465       "+P9012DT3456H",
466       "PT5S",
467     ]
468     targets.each do |str|
469       assert_parsed_result(XSD::XSDDuration, str)
470     end
471
472     targets = [
473       ["P0Y0M0DT0H0M0S",
474         "P0D"],
475       ["-P0DT0S",
476         "-P0D"],
477       ["P01234Y5678M9012DT3456H7890M1234.5678S",
478         "P1234Y5678M9012DT3456H7890M1234.5678S"],
479       ["P1234Y005678M9012DT3456H7890M1234.5678S",
480         "P1234Y5678M9012DT3456H7890M1234.5678S"],
481       ["P1234Y5678M0009012DT3456H7890M1234.5678S",
482         "P1234Y5678M9012DT3456H7890M1234.5678S"],
483       ["P1234Y5678M9012DT00003456H7890M1234.5678S",
484         "P1234Y5678M9012DT3456H7890M1234.5678S"],
485       ["P1234Y5678M9012DT3456H000007890M1234.5678S",
486         "P1234Y5678M9012DT3456H7890M1234.5678S"],
487       ["P1234Y5678M9012DT3456H7890M0000001234.5678S",
488         "P1234Y5678M9012DT3456H7890M1234.5678S"],
489     ]
490     targets.each do |data, expected|
491       assert_equal(expected, XSD::XSDDuration.new(data).to_s)
492     end
493   end
494
495   def test_XSDDateTime
496     o = XSD::XSDDateTime.new
497     assert_equal(XSD::Namespace, o.type.namespace)
498     assert_equal(XSD::DateTimeLiteral, o.type.name)
499     assert_equal(nil, o.data)
500     assert_equal(true, o.is_nil)
501
502     targets = [
503       "2002-05-18T16:52:20Z",
504       "0001-01-01T00:00:00Z",
505       "9999-12-31T23:59:59Z",
506       "19999-12-31T23:59:59Z",
507       "2002-12-31T23:59:59.999Z",
508       "2002-12-31T23:59:59.001Z",
509       "2002-12-31T23:59:59.99999999999999999999Z",
510       "2002-12-31T23:59:59.00000000000000000001Z",
511       "2002-12-31T23:59:59+09:00",
512       "2002-12-31T23:59:59+00:01",
513       "2002-12-31T23:59:59-00:01",
514       "2002-12-31T23:59:59-23:59",
515       "2002-12-31T23:59:59.00000000000000000001+13:30",
516       "2002-12-31T23:59:59.5137Z",
517       "2002-12-31T23:59:59.51375Z",     # 411/800
518       "2002-12-31T23:59:59.51375+12:34",
519       "-2002-05-18T16:52:20Z",
520       "-4713-01-01T12:00:00Z",
521       "-2002-12-31T23:59:59+00:01",
522       "-0001-12-31T23:59:59.00000000000000000001+13:30",
523     ]
524     targets.each do |str|
525       assert_parsed_result(XSD::XSDDateTime, str)
526     end
527
528     targets = [
529       ["2002-12-31T23:59:59.00",
530         "2002-12-31T23:59:59Z"],
531       ["2002-12-31T23:59:59+00:00",
532         "2002-12-31T23:59:59Z"],
533       ["2002-12-31T23:59:59-00:00",
534         "2002-12-31T23:59:59Z"],
535       ["-2002-12-31T23:59:59.00",
536         "-2002-12-31T23:59:59Z"],
537       ["-2002-12-31T23:59:59+00:00",
538         "-2002-12-31T23:59:59Z"],
539       ["-2002-12-31T23:59:59-00:00",
540         "-2002-12-31T23:59:59Z"],
541     ]
542     targets.each do |data, expected|
543       assert_equal(expected, XSD::XSDDateTime.new(data).to_s)
544       d = DateTime.parse(data)
545       d >>= 12 if d.year < 0    # XSDDateTime.year(-1) == DateTime.year(0)
546       assert_equal(expected, XSD::XSDDateTime.new(d).to_s)
547     end
548
549     targets = [
550       "0000-05-18T16:52:20Z",
551       "05-18T16:52:20Z",
552       "2002-05T16:52:20Z",
553       "2002-05-18T16:52Z",
554       "",
555     ]
556     targets.each do |d|
557       assert_raises(XSD::ValueSpaceError, d.to_s) do
558         XSD::XSDDateTime.new(d)
559       end
560     end
561   end
562
563   def test_XSDTime
564     o = XSD::XSDTime.new
565     assert_equal(XSD::Namespace, o.type.namespace)
566     assert_equal(XSD::TimeLiteral, o.type.name)
567     assert_equal(nil, o.data)
568     assert_equal(true, o.is_nil)
569
570     targets = [
571       "16:52:20Z",
572       "00:00:00Z",
573       "23:59:59Z",
574       "23:59:59.999Z",
575       "23:59:59.001Z",
576       "23:59:59.99999999999999999999Z",
577       "23:59:59.00000000000000000001Z",
578       "23:59:59+09:00",
579       "23:59:59+00:01",
580       "23:59:59-00:01",
581       "23:59:59-23:59",
582       "23:59:59.00000000000000000001+13:30",
583       "23:59:59.51345Z",
584       "23:59:59.51345+12:34",
585       "23:59:59+00:01",
586     ]
587     targets.each do |str|
588       assert_parsed_result(XSD::XSDTime, str)
589     end
590
591     targets = [
592       ["23:59:59.00",
593         "23:59:59Z"],
594       ["23:59:59+00:00",
595         "23:59:59Z"],
596       ["23:59:59-00:00",
597         "23:59:59Z"],
598     ]
599     targets.each do |data, expected|
600       assert_equal(expected, XSD::XSDTime.new(data).to_s)
601     end
602   end
603
604   def test_XSDDate
605     o = XSD::XSDDate.new
606     assert_equal(XSD::Namespace, o.type.namespace)
607     assert_equal(XSD::DateLiteral, o.type.name)
608     assert_equal(nil, o.data)
609     assert_equal(true, o.is_nil)
610
611     targets = [
612       "2002-05-18Z",
613       "0001-01-01Z",
614       "9999-12-31Z",
615       "19999-12-31Z",
616       "2002-12-31+09:00",
617       "2002-12-31+00:01",
618       "2002-12-31-00:01",
619       "2002-12-31-23:59",
620       "2002-12-31+13:30",
621       "-2002-05-18Z",
622       "-19999-12-31Z",
623       "-2002-12-31+00:01",
624       "-0001-12-31+13:30",
625     ]
626     targets.each do |str|
627       assert_parsed_result(XSD::XSDDate, str)
628     end
629
630     targets = [
631       ["2002-12-31",
632         "2002-12-31Z"],
633       ["2002-12-31+00:00",
634         "2002-12-31Z"],
635       ["2002-12-31-00:00",
636         "2002-12-31Z"],
637       ["-2002-12-31",
638         "-2002-12-31Z"],
639       ["-2002-12-31+00:00",
640         "-2002-12-31Z"],
641       ["-2002-12-31-00:00",
642         "-2002-12-31Z"],
643     ]
644     targets.each do |data, expected|
645       assert_equal(expected, XSD::XSDDate.new(data).to_s)
646       d = Date.parse(data)
647       d >>= 12 if d.year < 0    # XSDDate.year(-1) == Date.year(0)
648       assert_equal(expected, XSD::XSDDate.new(d).to_s)
649     end
650   end
651 end
652
653 class TestXSD2 < Test::Unit::TestCase
654   def setup
655     # Nothing to do.
656   end
657
658   def teardown
659     # Nothing to do.
660   end
661
662   def assert_parsed_result(klass, str)
663     o = klass.new(str)
664     assert_equal(str, o.to_s)
665   end
666
667   def test_XSDGYearMonth
668     o = XSD::XSDGYearMonth.new
669     assert_equal(XSD::Namespace, o.type.namespace)
670     assert_equal(XSD::GYearMonthLiteral, o.type.name)
671     assert_equal(nil, o.data)
672     assert_equal(true, o.is_nil)
673
674     targets = [
675       "2002-05Z",
676       "0001-01Z",
677       "9999-12Z",
678       "19999-12Z",
679       "2002-12+09:00",
680       "2002-12+00:01",
681       "2002-12-00:01",
682       "2002-12-23:59",
683       "2002-12+13:30",
684       "-2002-05Z",
685       "-19999-12Z",
686       "-2002-12+00:01",
687       "-0001-12+13:30",
688     ]
689     targets.each do |str|
690       assert_parsed_result(XSD::XSDGYearMonth, str)
691     end
692
693     targets = [
694       ["2002-12",
695         "2002-12Z"],
696       ["2002-12+00:00",
697         "2002-12Z"],
698       ["2002-12-00:00",
699         "2002-12Z"],
700       ["-2002-12",
701         "-2002-12Z"],
702       ["-2002-12+00:00",
703         "-2002-12Z"],
704       ["-2002-12-00:00",
705         "-2002-12Z"],
706     ]
707     targets.each do |data, expected|
708       assert_equal(expected, XSD::XSDGYearMonth.new(data).to_s)
709     end
710   end
711
712   def test_XSDGYear
713     o = XSD::XSDGYear.new
714     assert_equal(XSD::Namespace, o.type.namespace)
715     assert_equal(XSD::GYearLiteral, o.type.name)
716     assert_equal(nil, o.data)
717     assert_equal(true, o.is_nil)
718
719     targets = [
720       "2002Z",
721       "0001Z",
722       "9999Z",
723       "19999Z",
724       "2002+09:00",
725       "2002+00:01",
726       "2002-00:01",
727       "2002-23:59",
728       "2002+13:30",
729       "-2002Z",
730       "-19999Z",
731       "-2002+00:01",
732       "-0001+13:30",
733     ]
734     targets.each do |str|
735       assert_parsed_result(XSD::XSDGYear, str)
736     end
737
738     targets = [
739       ["2002",
740         "2002Z"],
741       ["2002+00:00",
742         "2002Z"],
743       ["2002-00:00",
744         "2002Z"],
745       ["-2002",
746         "-2002Z"],
747       ["-2002+00:00",
748         "-2002Z"],
749       ["-2002-00:00",
750         "-2002Z"],
751     ]
752     targets.each do |data, expected|
753       assert_equal(expected, XSD::XSDGYear.new(data).to_s)
754     end
755   end
756
757   def test_XSDGMonthDay
758     o = XSD::XSDGMonthDay.new
759     assert_equal(XSD::Namespace, o.type.namespace)
760     assert_equal(XSD::GMonthDayLiteral, o.type.name)
761     assert_equal(nil, o.data)
762     assert_equal(true, o.is_nil)
763
764     targets = [
765       "--05-18Z",
766       "--01-01Z",
767       "--12-31Z",
768       "--12-31+09:00",
769       "--12-31+00:01",
770       "--12-31-00:01",
771       "--12-31-23:59",
772       "--12-31+13:30",
773     ]
774     targets.each do |str|
775       assert_parsed_result(XSD::XSDGMonthDay, str)
776     end
777
778     targets = [
779       ["--12-31",
780         "--12-31Z"],
781       ["--12-31+00:00",
782         "--12-31Z"],
783       ["--12-31-00:00",
784         "--12-31Z"],
785     ]
786     targets.each do |data, expected|
787       assert_equal(expected, XSD::XSDGMonthDay.new(data).to_s)
788     end
789   end
790
791   def test_XSDGDay
792     o = XSD::XSDGDay.new
793     assert_equal(XSD::Namespace, o.type.namespace)
794     assert_equal(XSD::GDayLiteral, o.type.name)
795     assert_equal(nil, o.data)
796     assert_equal(true, o.is_nil)
797
798     targets = [
799       "---18Z",
800       "---01Z",
801       "---31Z",
802       "---31+09:00",
803       "---31+00:01",
804       "---31-00:01",
805       "---31-23:59",
806       "---31+13:30",
807     ]
808     targets.each do |str|
809       assert_parsed_result(XSD::XSDGDay, str)
810     end
811
812     targets = [
813       ["---31",
814         "---31Z"],
815       ["---31+00:00",
816         "---31Z"],
817       ["---31-00:00",
818         "---31Z"],
819     ]
820     targets.each do |data, expected|
821       assert_equal(expected, XSD::XSDGDay.new(data).to_s)
822     end
823   end
824
825   def test_XSDGMonth
826     o = XSD::XSDGMonth.new
827     assert_equal(XSD::Namespace, o.type.namespace)
828     assert_equal(XSD::GMonthLiteral, o.type.name)
829     assert_equal(nil, o.data)
830     assert_equal(true, o.is_nil)
831
832     targets = [
833       "--05Z",
834       "--01Z",
835       "--12Z",
836       "--12+09:00",
837       "--12+00:01",
838       "--12-00:01",
839       "--12-23:59",
840       "--12+13:30",
841     ]
842     targets.each do |str|
843       assert_parsed_result(XSD::XSDGMonth, str)
844     end
845
846     targets = [
847       ["--12",
848         "--12Z"],
849       ["--12+00:00",
850         "--12Z"],
851       ["--12-00:00",
852         "--12Z"],
853     ]
854     targets.each do |data, expected|
855       assert_equal(expected, XSD::XSDGMonth.new(data).to_s)
856     end
857   end
858
859   def test_XSDHexBinary
860     o = XSD::XSDHexBinary.new
861     assert_equal(XSD::Namespace, o.type.namespace)
862     assert_equal(XSD::HexBinaryLiteral, o.type.name)
863     assert_equal(nil, o.data)
864     assert_equal(true, o.is_nil)
865
866     targets = [
867       "abcdef",
868       "\xe3\x81\xaa\xe3\x81\xb2",
869       %Q(\0),
870       "",
871     ]
872     targets.each do |str|
873       assert_equal(str, XSD::XSDHexBinary.new(str).string)
874       assert_equal(str.unpack("H*")[0 ].tr('a-f', 'A-F'),
875         XSD::XSDHexBinary.new(str).data)
876       o = XSD::XSDHexBinary.new
877       o.set_encoded(str.unpack("H*")[0 ].tr('a-f', 'A-F'))
878       assert_equal(str, o.string)
879       o.set_encoded(str.unpack("H*")[0 ].tr('A-F', 'a-f'))
880       assert_equal(str, o.string)
881     end
882
883     targets = [
884       "0FG7",
885       "0fg7",
886     ]
887     targets.each do |d|
888       assert_raises(XSD::ValueSpaceError, d.to_s) do
889         o = XSD::XSDHexBinary.new
890         o.set_encoded(d)
891         p o.string
892       end
893     end
894   end
895
896   def test_XSDBase64Binary
897     o = XSD::XSDBase64Binary.new
898     assert_equal(XSD::Namespace, o.type.namespace)
899     assert_equal(XSD::Base64BinaryLiteral, o.type.name)
900     assert_equal(nil, o.data)
901     assert_equal(true, o.is_nil)
902
903     targets = [
904       "abcdef",
905       "\xe3\x81\xaa\xe3\x81\xb2",
906       %Q(\0),
907       "",
908     ]
909     targets.each do |str|
910       assert_equal(str, XSD::XSDBase64Binary.new(str).string)
911       assert_equal([str ].pack("m").chomp, XSD::XSDBase64Binary.new(str).data)
912       o = XSD::XSDBase64Binary.new
913       o.set_encoded([str ].pack("m").chomp)
914       assert_equal(str, o.string)
915     end
916
917     targets = [
918       "-",
919       "*",
920     ]
921     targets.each do |d|
922       assert_raises(XSD::ValueSpaceError, d.to_s) do
923         o = XSD::XSDBase64Binary.new
924         o.set_encoded(d)
925         p o.string
926       end
927     end
928   end
929
930   def test_XSDAnyURI
931     o = XSD::XSDAnyURI.new
932     assert_equal(XSD::Namespace, o.type.namespace)
933     assert_equal(XSD::AnyURILiteral, o.type.name)
934     assert_equal(nil, o.data)
935     assert_equal(true, o.is_nil)
936
937     # Too few tests here I know.  Believe uri module. :)
938     targets = [
939       "foo",
940       "http://foo",
941       "http://foo/bar/baz",
942       "http://foo/bar#baz",
943       "http://foo/bar%20%20?a+b",
944       "HTTP://FOO/BAR%20%20?A+B",
945     ]
946     targets.each do |str|
947       assert_parsed_result(XSD::XSDAnyURI, str)
948     end
949   end
950
951   def test_XSDQName
952     o = XSD::XSDQName.new
953     assert_equal(XSD::Namespace, o.type.namespace)
954     assert_equal(XSD::QNameLiteral, o.type.name)
955     assert_equal(nil, o.data)
956     assert_equal(true, o.is_nil)
957
958     # More strict test is needed but current implementation allows all non-':'
959     # chars like ' ', C0 or C1...
960     targets = [
961       "foo",
962       "foo:bar",
963       "a:b",
964     ]
965     targets.each do |str|
966       assert_parsed_result(XSD::XSDQName, str)
967     end
968   end
969
970
971   ###
972   ## Derived types
973   #
974
975   def test_XSDInteger
976     o = XSD::XSDInteger.new
977     assert_equal(XSD::Namespace, o.type.namespace)
978     assert_equal(XSD::IntegerLiteral, o.type.name)
979     assert_equal(nil, o.data)
980     assert_equal(true, o.is_nil)
981
982     targets = [
983       0,
984       1000000000,
985       -9999999999,
986       12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890,
987       12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890,
988       -1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789,
989     ]
990     targets.each do |int|
991       assert_equal(int, XSD::XSDInteger.new(int).data)
992     end
993
994     targets = [
995       "0",
996       "1000000000",
997       "-9999999999",
998       "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
999       "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
1000       "-1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",
1001     ]
1002     targets.each do |str|
1003       assert_equal(str, XSD::XSDInteger.new(str).to_s)
1004     end
1005
1006     targets = [
1007       ["-0", "0"],
1008       ["+0", "0"],
1009       ["000123", "123"],
1010       ["-000123", "-123"],
1011       [
1012         "+12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890",
1013         "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890"
1014      ],
1015     ]
1016     targets.each do |data, expected|
1017       assert_equal(expected, XSD::XSDInteger.new(data).to_s)
1018     end
1019
1020     targets = [
1021       "0.0",
1022       "-5.2",
1023       "0.000000000000a",
1024       "+-5",
1025       "12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890."
1026     ]
1027     targets.each do |d|
1028       assert_raises(XSD::ValueSpaceError) do
1029         XSD::XSDInteger.new(d)
1030       end
1031     end
1032   end
1033
1034   def test_XSDNonPositiveInteger
1035     o = XSD::XSDNonPositiveInteger.new
1036     assert_equal(XSD::Namespace, o.type.namespace)
1037     assert_equal(XSD::NonPositiveIntegerLiteral, o.type.name)
1038     assert_equal(nil, o.data)
1039     assert_equal(true, o.is_nil)
1040
1041     targets = [
1042       0,
1043       -9999999999,
1044       -1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789,
1045     ]
1046     targets.each do |int|
1047       assert_equal(int, XSD::XSDNonPositiveInteger.new(int).data)
1048     end
1049
1050     targets = [
1051       "0",
1052       "-9999999999",
1053       "-1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",
1054     ]
1055     targets.each do |str|
1056       assert_equal(str, XSD::XSDNonPositiveInteger.new(str).to_s)
1057     end
1058
1059     targets = [
1060       ["-0", "0"],
1061       ["-000123", "-123"],
1062     ]
1063     targets.each do |data, expected|
1064       assert_equal(expected, XSD::XSDNonPositiveInteger.new(data).to_s)
1065     end
1066
1067     targets = [
1068       "0.0",
1069       "-5.2",
1070       "0.000000000000a",
1071       "+-5",
1072       "-12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890."
1073     ]
1074     targets.each do |d|
1075       assert_raises(XSD::ValueSpaceError) do
1076         XSD::XSDNonPositiveInteger.new(d)
1077       end
1078     end
1079   end
1080
1081   def test_XSDNegativeInteger
1082     o = XSD::XSDNegativeInteger.new
1083     assert_equal(XSD::Namespace, o.type.namespace)
1084     assert_equal(XSD::NegativeIntegerLiteral, o.type.name)
1085     assert_equal(nil, o.data)
1086     assert_equal(true, o.is_nil)
1087
1088     targets = [
1089       -1,
1090       -9999999999,
1091       -1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789,
1092     ]
1093     targets.each do |int|
1094       assert_equal(int, XSD::XSDNegativeInteger.new(int).data)
1095     end
1096
1097     targets = [
1098       "-1",
1099       "-9999999999",
1100       "-1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789",
1101     ]
1102     targets.each do |str|
1103       assert_equal(str, XSD::XSDNegativeInteger.new(str).to_s)
1104     end
1105
1106     targets = [
1107       ["-000123", "-123"],
1108     ]
1109     targets.each do |data, expected|
1110       assert_equal(expected, XSD::XSDNegativeInteger.new(data).to_s)
1111     end
1112
1113     targets = [
1114       "-0.0",
1115       "-5.2",
1116       "-0.000000000000a",
1117       "+-5",
1118       "-12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890."
1119     ]
1120     targets.each do |d|
1121       assert_raises(XSD::ValueSpaceError) do
1122         XSD::XSDNegativeInteger.new(d)
1123       end
1124     end
1125   end
1126
1127   def test_XSDLong
1128     o = XSD::XSDLong.new
1129     assert_equal(XSD::Namespace, o.type.namespace)
1130     assert_equal(XSD::LongLiteral, o.type.name)
1131     assert_equal(nil, o.data)
1132     assert_equal(true, o.is_nil)
1133
1134     targets = [
1135       0,
1136       123,
1137       -123,
1138       9223372036854775807,
1139       -9223372036854775808,
1140     ]
1141     targets.each do |lng|
1142       assert_equal(lng, XSD::XSDLong.new(lng).data)
1143     end
1144
1145     targets = [
1146       "0",
1147       "123",
1148       "-123",
1149       "9223372036854775807",
1150       "-9223372036854775808",
1151     ]
1152     targets.each do |str|
1153       assert_equal(str, XSD::XSDLong.new(str).to_s)
1154     end
1155
1156     targets = [
1157       ["-0", "0"],
1158       ["+0", "0"],
1159       ["000123", "123"],
1160       ["-000123", "-123"],
1161     ]
1162     targets.each do |data, expected|
1163       assert_equal(expected, XSD::XSDLong.new(data).to_s)
1164     end
1165
1166     targets = [
1167       9223372036854775808,
1168       -9223372036854775809,
1169       "0.0",
1170       "-5.2",
1171       "0.000000000000a",