/
tests.rb
executable file
·759 lines (656 loc) · 20 KB
/
tests.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
#!/usr/bin/env ruby
# encoding: UTF-8
# Ubuntu does not accept arguments to ruby when called using env. To get warnings to show up the -w options is
# required. That can be set in the RUBYOPT environment variable.
# export RUBYOPT=-w
$VERBOSE = true
$: << File.join(File.dirname(__FILE__), "../lib")
$: << File.join(File.dirname(__FILE__), "../ext")
require 'test/unit'
require 'optparse'
require 'date'
require 'ox'
$ruby = RUBY_DESCRIPTION.split(' ')[0]
$ruby = 'ree' if 'ruby' == $ruby && RUBY_DESCRIPTION.include?('Ruby Enterprise Edition')
$indent = 2
opts = OptionParser.new
# TBD add indent
opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
files = opts.parse(ARGV)
class Func < ::Test::Unit::TestCase
def test_get_options
opts = Ox.default_options()
assert_equal(opts, {
:encoding=>nil,
:indent=>2,
:trace=>0,
:with_dtd=>false,
:with_xml=>false,
:with_instructions=>false,
:circular=>false,
:xsd_date=>false,
:mode=>nil,
:symbolize_keys=>true,
:effort=>:strict})
end
def test_set_options
orig = {
:encoding=>nil,
:indent=>2,
:trace=>0,
:with_dtd=>false,
:with_xml=>true,
:with_instructions=>false,
:circular=>false,
:xsd_date=>false,
:mode=>nil,
:symbolize_keys=>true,
:effort=>:strict}
o2 = {
:encoding=>"UTF-8",
:indent=>4,
:trace=>1,
:with_dtd=>true,
:with_xml=>false,
:with_instructions=>true,
:circular=>true,
:xsd_date=>true,
:mode=>:object,
:symbolize_keys=>true,
:effort=>:tolerant }
o3 = { :xsd_date=>false }
Ox.default_options = o2
opts = Ox.default_options()
assert_equal(opts, o2);
Ox.default_options = o3 # see if it throws an exception
Ox.default_options = orig # return to original
end
def test_nil
dump_and_load(nil, false)
end
def test_true
dump_and_load(true, false)
end
def test_false
dump_and_load(false, false)
end
def test_fixnum
dump_and_load(7, false)
dump_and_load(-19, false)
dump_and_load(0, false)
end
def test_float
dump_and_load(7.7, false)
dump_and_load(-1.9, false)
dump_and_load(0.0, false)
dump_and_load(-10.000005, false)
dump_and_load(1.000000000005, false)
end
def test_string
dump_and_load('a string', false)
dump_and_load('', false)
end
def test_symbol
dump_and_load(:a_symbol, false)
dump_and_load(:<=, false)
end
def test_base64
dump_and_load('a & x', false)
end
def test_time
t = Time.local(2012, 1, 5, 23, 58, 7)
dump_and_load(t, false)
end
def test_date
dump_and_load(Date.new(2011, 1, 5), false)
end
def test_array
dump_and_load([], false)
dump_and_load([1, 'a'], false)
end
def test_hash
dump_and_load({ }, false)
dump_and_load({ 'a' => 1, 2 => 'b' }, false)
end
def test_range
if RUBY_VERSION.start_with?('1.8')
assert(true)
else
dump_and_load((0..3), false)
dump_and_load((-2..3.7), false)
dump_and_load(('a'...'f'), false)
t = Time.local(2012, 1, 5, 23, 58, 7)
t2 = t + 20
dump_and_load((t..t2), false)
end
end
def test_regex
if RUBY_VERSION.start_with?('1.8') || 'rubinius' == $ruby
assert(true)
else
dump_and_load(/^[0-9]/ix, false)
dump_and_load(/^[&0-9]/ix, false) # with xml-unfriendly character
end
end
def test_bignum
dump_and_load(7 ** 55, false)
dump_and_load(-7 ** 55, false)
end
def test_complex_number
if RUBY_VERSION.start_with?('1.8') || 'rubinius' == $ruby
assert(true)
else
dump_and_load(Complex(1), false)
dump_and_load(Complex(3, 2), false)
end
end
def test_rational
if RUBY_VERSION.start_with?('1.8')
assert(true)
else
dump_and_load(Rational(1, 3), false)
dump_and_load(Rational(0, 3), false)
end
end
def test_object
dump_and_load(Bag.new({ }), false)
dump_and_load(Bag.new(:@x => 3), false)
end
def test_bad_object
xml = %{<?xml version="1.0"?>
<o c="Bad::Boy">
<i a="@x">3</i>
</o>
}
xml2 = %{<?xml version="1.0"?>
<o c="Bad">
<i a="@x">7</i>
</o>
}
assert_raise(NameError) {
Ox.load(xml, :mode => :object, :trace => 0)
}
loaded = Ox.load(xml, :mode => :object, :trace => 0, :effort => :tolerant)
assert_equal(loaded, nil)
loaded = Ox.load(xml, :mode => :object, :trace => 0, :effort => :auto_define)
assert_equal(loaded.class.to_s, 'Bad::Boy')
assert_equal(loaded.class.superclass.to_s, 'Ox::Bag')
loaded = Ox.load(xml2, :mode => :object, :trace => 0, :effort => :auto_define)
assert_equal(loaded.class.to_s, 'Bad')
assert_equal(loaded.class.superclass.to_s, 'Ox::Bag')
end
def test_bad_class
xml = %{<?xml version="1.0"?>
<o c="Bad:Boy">
<i a="@x">3</i>
</o>
}
assert_raise(SyntaxError) {
Ox.load(xml, :mode => :object, :trace => 0)
}
end
def test_xml_instruction_format
xml = %{<?xml version="1.0" ?>
<s>Test</s>
}
loaded = Ox.load(xml, :mode => :object)
assert_equal('Test', loaded);
end
def test_xml_instruction
xml = Ox.dump("test", :mode => :object, :with_xml => false)
assert_equal("<s>test</s>\n", xml)
xml = Ox.dump("test", :mode => :object, :with_xml => true)
assert_equal("<?xml version=\"1.0\"?>\n<s>test</s>\n", xml)
xml = Ox.dump("test", :mode => :object, :with_xml => true, :encoding => 'UTF-8')
assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<s>test</s>\n", xml)
end
def test_ox_instruction
xml = Ox.dump("test", :mode => :object, :with_xml => true, :with_instructions => true)
assert_equal("<?xml version=\"1.0\"?>\n<?ox version=\"1.0\" mode=\"object\" circular=\"no\" xsd_date=\"no\"?>\n<s>test</s>\n", xml)
xml = Ox.dump("test", :mode => :object, :with_instructions => true)
assert_equal("<?ox version=\"1.0\" mode=\"object\" circular=\"no\" xsd_date=\"no\"?>\n<s>test</s>\n", xml)
xml = Ox.dump("test", :mode => :object, :with_instructions => true, :circular => true, :xsd_date => true)
assert_equal("<?ox version=\"1.0\" mode=\"object\" circular=\"yes\" xsd_date=\"yes\"?>\n<s i=\"1\">test</s>\n", xml)
xml = Ox.dump("test", :mode => :object, :with_instructions => true, :circular => false, :xsd_date => false)
assert_equal("<?ox version=\"1.0\" mode=\"object\" circular=\"no\" xsd_date=\"no\"?>\n<s>test</s>\n", xml)
end
def test_dtd
xml = Ox.dump("test", :mode => :object, :with_dtd => true)
assert_equal("<!DOCTYPE s SYSTEM \"ox.dtd\">\n<s>test</s>\n", xml)
end
def test_lone_dtd
xml = "<!DOCTYPE html>" # not really a valid xml but should pass anyway
doc = Ox.parse(xml)
assert_equal('html', doc.nodes[0].value)
end
def test_quote_value
xml = %{<top name="Pete"/>}
doc = Ox.parse(xml)
assert_equal('Pete', doc.attributes[:name])
end
def test_escape_value
xml = %{\n<top name="<&test>"/>\n}
doc = Ox.parse(xml)
assert_equal('<&test>', doc.attributes[:name])
dumped_xml = Ox.dump(doc)
assert_equal(xml, dumped_xml)
end
def test_escape_hex_value
xml = %{\n<top name="<@test>"/>\n}
doc = Ox.parse(xml)
assert_equal('<@test>', doc.attributes[:name])
end
def test_escape_utf8_value
xml = %{<?xml encoding="UTF-8"?>\n<top name="ピー�">� test @</top>\n}
doc = Ox.parse(xml).root()
assert_equal('ピ test @', doc.nodes[0])
assert_equal('ピーター', doc.attributes[:name])
end
def test_escape_utf8_nil_encoding
xml = %{<?xml?>\n<top name="ピー�">ピ test @</top>\n}
doc = Ox.parse(xml).root()
assert_equal('ピ test @', doc.nodes[0])
assert_equal('ピーター', doc.attributes[:name])
end
def test_attr_as_string
xml = %{<top name="Pete"/>}
doc = Ox.load(xml, :mode => :generic, :symbolize_keys => false)
assert_equal(['name'], doc.attributes.keys)
end
def test_single_quote
xml = %{<top name='Pete'/>}
doc = Ox.load(xml, :effort => :tolerant)
assert_equal('Pete', doc.attributes[:name])
end
def test_no_quote
xml = %{<top name=Pete />}
doc = Ox.load(xml, :effort => :tolerant)
assert_equal('Pete', doc.attributes[:name])
end
def test_class
dump_and_load(Bag, false)
end
def test_exception
if RUBY_VERSION.start_with?('1.8') || 'rubinius' == $ruby
assert(true)
else
e = StandardError.new("Some Error")
e.set_backtrace(["./func.rb:119: in test_exception",
"./fake.rb:57: in fake_func"])
dump_and_load(e, false)
end
end
def test_exception_bag
if RUBY_VERSION.start_with?('1.8') || 'rubinius' == $ruby
assert(true)
else
xml = %{
<e c="FakeError">
<s a="mesg">Some Error</s>
<a a="bt">
<s>./func.rb:119: in test_exception</s>
<s>./fake.rb:57: in fake_func</s>
</a>
</e>
}
x = Ox.load(xml, :mode => :object, :effort => :auto_define)
assert_equal('Some Error', x.message())
assert(x.is_a?(Exception))
end
end
def test_struct
if 'ruby' == $ruby || 'ree' == $ruby
s = Struct.new('Box', :x, :y, :w, :h)
dump_and_load(s.new(2, 4, 10, 20), false)
else
assert(true)
end
end
def test_bad_format
xml = "<?xml version=\"1.0\"?>\n<tag>test</tagz>\n"
assert_raise(SyntaxError) {
Ox.load(xml, :mode => :generic, :trace => 0)
}
end
def test_array_multi
t = Time.local(2012, 1, 5, 23, 58, 7)
if RUBY_VERSION.start_with?('1.8')
dump_and_load([nil, true, false, 3, 'z', 7.9, 'a&b', :xyz, t], false)
else
dump_and_load([nil, true, false, 3, 'z', 7.9, 'a&b', :xyz, t, (-1..7)], false)
end
end
def test_hash_multi
t = Time.local(2012, 1, 5, 23, 58, 7)
if RUBY_VERSION.start_with?('1.8')
dump_and_load({ nil => nil, true => true, false => false, 3 => 3, 'z' => 'z', 7.9 => 7.9, 'a&b' => 'a&b', :xyz => :xyz, t => t }, false)
else
dump_and_load({ nil => nil, true => true, false => false, 3 => 3, 'z' => 'z', 7.9 => 7.9, 'a&b' => 'a&b', :xyz => :xyz, t => t, (-1..7) => (-1..7) }, false)
end
end
def test_object_multi
t = Time.local(2012, 1, 5, 23, 58, 7)
if RUBY_VERSION.start_with?('1.8')
dump_and_load(Bag.new(:@a => nil, :@b => true, :@c => false, :@d => 3, :@e => 'z', :@f => 7.9, :@g => 'a&b', :@h => :xyz, :@i => t), false)
else
dump_and_load(Bag.new(:@a => nil, :@b => true, :@c => false, :@d => 3, :@e => 'z', :@f => 7.9, :@g => 'a&b', :@h => :xyz, :@i => t, :@j => (-1..7)), false)
end
end
def test_complex
dump_and_load(Bag.new(:@o => Bag.new(:@a => [2]), :@a => [1, {:b => 3, :a => [5], :c => Bag.new(:@x => 7)}]), false)
end
# Create an Object and an Array with the same Objects in them. Dump and load
# and then change the ones in the loaded Object to verify that the ones in
# the array change in the same way. They are the same objects so they should
# change. Perform the operation on both the object before and the loaded so
# the equal() method can be used.
def test_circular
if RUBY_VERSION.start_with?('1.8')
# In 1.8.7 the eql? method behaves differently but the results are
# correct when viewed by eye.
assert(true)
else
a = [1]
s = "a,b,c"
h = { 1 => 2 }
e = Ox::Element.new('Zoo')
e[:cage] = 'bear'
b = Bag.new(:@a => a, :@s => s, :@h => h, :@e => e)
a << s
a << h
a << e
a << b
loaded = dump_and_load(b, false, true)
# modify the string
loaded.instance_variable_get(:@s).gsub!(',', '_')
b.instance_variable_get(:@s).gsub!(',', '_')
# modify hash
loaded.instance_variable_get(:@h)[1] = 3
b.instance_variable_get(:@h)[1] = 3
# modify Element
loaded.instance_variable_get(:@e)['pen'] = 'zebra'
b.instance_variable_get(:@e)['pen'] = 'zebra'
# pp loaded
assert_equal(b, loaded)
end
end
def test_raw
raw = Ox::Element.new('raw')
su = Ox::Element.new('sushi')
su[:kind] = 'fish'
raw << su
ba = Ox::Element.new('basashi')
ba[:kind] = 'animal'
raw << ba
dump_and_load(Bag.new(:@raw => raw), false)
end
def test_nameerror
begin
"x".foo
rescue Exception => e
xml = Ox.dump(e, :effort => :tolerant)
o = Ox.load(xml, :mode => :object)
xml2 = Ox.dump(o, :effort => :tolerant)
assert_equal(xml, xml2)
end
end
def test_mutex
if defined?(Mutex) && 'rubinius' != $ruby
# Mutex can not be serialize but it should not raise an exception.
xml = Ox.dump(Mutex.new, :indent => 2, :effort => :tolerant)
assert_equal(%{<z/>
}, xml)
xml = Ox.dump(Bag.new(:@x => Mutex.new), :indent => 2, :effort => :tolerant)
assert_equal(%{<o c="Bag">
<z a="@x"/>
</o>
}, xml)
else
assert(true)
end
end
def test_encoding
if RUBY_VERSION.start_with?('1.8')# || 'rubinius' == $ruby
assert(true)
else
s = 'ピーター'
xml = Ox.dump(s, :with_xml => true, :encoding => 'UTF-8')
#puts xml
#puts xml.encoding.to_s
assert_equal('UTF-8', xml.encoding.to_s)
obj = Ox.load(xml, :mode => :object)
assert_equal(s, obj)
end
end
def test_full_encoding
if RUBY_VERSION.start_with?('1.8') || 'rubinius' == $ruby
assert(true)
else
xml = %{<?xml version="1.0" encoding="UTF-8"?>
<いち name="ピーター" つま="まきえ">ピーター</いち>
}
obj = Ox.load(xml)
dumped = Ox.dump(obj, :with_xml => true)
assert_equal('UTF-8', dumped.encoding.to_s)
assert_equal(xml, dumped)
end
end
def test_obj_encoding
if RUBY_VERSION.start_with?('1.8')
assert(true)
else
orig = Ox.default_options
opts = orig.clone
opts[:encoding] = 'UTF-8'
opts[:with_xml] = true
Ox.default_options = opts
begin
# only 1.9.x rubies know how to parse a UTF-8 symbol
dump_and_load(Bag.new('@tsuma'.to_sym => 'まきえ'.to_sym), false)
dump_and_load(Bag.new('@つま'.to_sym => 'まきえ'.to_sym), false)
ensure
Ox.default_options = orig
end
end
end
def test_instructions
xml = Ox.dump("test", :with_instructions => true)
#puts xml
obj = Ox.load(xml) # should convert it to an object
assert_equal("test", obj)
end
def test_generic_string
xml = %{<?xml?>
<Str>A <boo></Str>
}
doc = Ox.load(xml, :mode => :generic)
xml2 = Ox.dump(doc, :with_xml => true)
assert_equal(xml, xml2)
end
def test_generic_encoding
if RUBY_VERSION.start_with?('1.8')
assert(true)
else
xml = %{<?xml encoding="UTF-8"?>
<Str><まきえ></Str>
}
doc = Ox.load(xml, :mode => :generic)
xml2 = Ox.dump(doc, :with_xml => true)
assert_equal(xml, xml2)
end
end
def test_IO
f = File.open(__FILE__, "r")
assert_raise(NotImplementedError) {
Ox.dump(f, :effort => :strict)
}
xml = Ox.dump(f, :effort => :tolerant)
obj = Ox.load(xml, :mode => :object) # should convert it to an object
assert_equal(nil, obj)
end
def locate_xml()
%{<?xml?>
<Family real="false">
<Pete age="57" type="male">
<Kid1 age="32"/>
<Kid2 age="31"/>
</Pete>
</Family>
}
end
def test_locate_self
doc = Ox.parse(locate_xml)
nodes = doc.locate(nil)
assert_equal(doc, nodes[0])
end
def test_locate_top
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family')
assert_equal([doc.nodes[0]], nodes)
end
def test_locate_top_wild
doc = Ox.parse(locate_xml)
nodes = doc.locate('?')
assert_equal([doc.nodes[0]], nodes)
end
def test_locate_child
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family/?')
assert_equal(['Pete'], nodes.map { |n| n.name })
nodes = doc.locate('Family/?/?')
assert_equal(['Kid1', 'Kid2'], nodes.map { |n| n.name })
nodes = doc.locate('Family/Pete/?')
assert_equal(['Kid1', 'Kid2'], nodes.map { |n| n.name })
nodes = doc.locate('Family/Makie/?')
assert_equal([], nodes.map { |n| n.name })
end
def test_locate_child_wild_wild
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family/?/?')
assert_equal(['Kid1', 'Kid2'], nodes.map { |n| n.name })
end
def test_locate_child_wild
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family/Pete/?')
assert_equal(['Kid1', 'Kid2'], nodes.map { |n| n.name })
end
def test_locate_child_wild_missing
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family/Makie/?')
assert_equal([], nodes.map { |n| n.name })
end
def test_locate_attribute
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family/@?')
assert_equal(['false'], nodes)
nodes = doc.locate('Family/@real')
assert_equal(['false'], nodes)
nodes = doc.locate('Family/Pete/@?')
assert_equal(['57', 'male'], nodes.sort)
nodes = doc.locate('Family/Pete/@age')
assert_equal(['57'], nodes)
nodes = doc.locate('Family/Makie/@?')
assert_equal([], nodes)
nodes = doc.locate('Family/Pete/?/@age')
assert_equal(['31', '32'], nodes.sort)
nodes = doc.locate('Family/*/@age')
assert_equal(['31', '32', '57'], nodes.sort)
nodes = doc.locate('*/@?')
assert_equal(['31', '32', '57', 'false', 'male'], nodes.sort)
assert_raise(::Ox::InvalidPath) {
nodes = doc.locate('Family/@age/?')
}
assert_raise(::Ox::InvalidPath) {
nodes = doc.locate('Family/?[/?')
}
end
def test_locate_qual_index
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family/Pete/?[0]')
assert_equal(['Kid1'], nodes.map { |e| e.name } )
nodes = doc.locate('Family/Pete/?[1]')
assert_equal(['Kid2'], nodes.map { |e| e.name } )
end
def test_locate_qual_less
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family/Pete/?[<1]')
assert_equal(['Kid1'], nodes.map { |e| e.name } )
end
def test_locate_qual_great
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family/Pete/?[>0]')
assert_equal(['Kid2'], nodes.map { |e| e.name } )
end
def test_locate_qual_last
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family/Pete/?[-1]')
assert_equal(['Kid2'], nodes.map { |e| e.name } )
end
def test_locate_qual_last_attr
doc = Ox.parse(locate_xml)
nodes = doc.locate('Family/Pete/?[-1]/@age')
assert_equal(['31'], nodes )
end
def easy_xml()
%{<?xml?>
<Family real="false">
<Pete age="58" type="male">
<Kid age="33"><!-- comment -->Nicole</Kid>
<Kid age="31">Pamela</Kid>
</Pete>
</Family>
}
end
def test_easy_attribute
doc = Ox.parse(easy_xml)
assert_equal('false', doc.Family.real)
assert_equal('58', doc.Family.Pete.age)
end
def test_easy_attribute_missing
doc = Ox.parse(easy_xml)
assert_raise(NoMethodError) {
doc.Family.fake
}
end
def test_easy_element
doc = Ox.parse(easy_xml)
assert_equal(::Ox::Element, doc.Family.Pete.class)
assert_equal('Pete', doc.Family.Pete.name)
assert_equal('Nicole', doc.Family.Pete.Kid.text)
assert_equal(nil, doc.Family.Pete.text)
end
def test_easy_element_index
doc = Ox.parse(easy_xml)
assert_equal('Pamela', doc.Family.Pete.Kid(1).text)
end
def test_easy_element_missing
doc = Ox.parse(easy_xml)
assert_raise(NoMethodError) {
doc.Family.Bob
}
end
def dump_and_load(obj, trace=false, circular=false)
xml = Ox.dump(obj, :indent => $indent, :circular => circular)
puts xml if trace
loaded = Ox.load(xml, :mode => :object, :trace => (trace ? 2 : 0))
assert_equal(obj, loaded)
loaded
end
end
class Bag
def initialize(args)
args.each do |k,v|
self.instance_variable_set(k, v)
end
end
def eql?(other)
return false if (other.nil? or self.class != other.class)
ova = other.instance_variables
return false if ova.size != instance_variables.size
instance_variables.each do |vid|
return false if instance_variable_get(vid) != other.instance_variable_get(vid)
end
true
end
alias == eql?
end