ox 1.5.4 → 1.5.5

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of ox might be problematic. Click here for more details.

Files changed (61) hide show
  1. data/README.md +21 -3
  2. data/ext/ox/dump.c +64 -42
  3. data/ext/ox/extconf.rb +34 -2
  4. data/ext/ox/gen_load.c +13 -13
  5. data/ext/ox/obj_load.c +31 -28
  6. data/ext/ox/ox.c +41 -31
  7. data/ext/ox/ox.h +20 -13
  8. data/ext/ox/parse.c +1 -0
  9. data/ext/ox/sax.c +323 -26
  10. data/lib/ox.rb +1 -2
  11. data/lib/ox/element.rb +1 -1
  12. data/lib/ox/sax.rb +24 -11
  13. data/lib/ox/version.rb +1 -1
  14. metadata +4 -50
  15. data/test/Sample.graffle +0 -2318
  16. data/test/bench.rb +0 -53
  17. data/test/bug1.rb +0 -24
  18. data/test/bug2.rb +0 -38
  19. data/test/bug3.rb +0 -21
  20. data/test/cache16_test.rb +0 -17
  21. data/test/cache8_test.rb +0 -17
  22. data/test/cache_test.rb +0 -17
  23. data/test/files.rb +0 -29
  24. data/test/func.rb +0 -652
  25. data/test/gen_sample.rb +0 -22
  26. data/test/obj_sample.rb +0 -19
  27. data/test/ox/change.rb +0 -16
  28. data/test/ox/dir.rb +0 -21
  29. data/test/ox/doc.rb +0 -39
  30. data/test/ox/file.rb +0 -33
  31. data/test/ox/group.rb +0 -18
  32. data/test/ox/hasprops.rb +0 -18
  33. data/test/ox/layer.rb +0 -14
  34. data/test/ox/line.rb +0 -22
  35. data/test/ox/oval.rb +0 -12
  36. data/test/ox/rect.rb +0 -12
  37. data/test/ox/shape.rb +0 -37
  38. data/test/ox/text.rb +0 -23
  39. data/test/parse_cmp.rb +0 -261
  40. data/test/perf.rb +0 -91
  41. data/test/perf_gen.rb +0 -237
  42. data/test/perf_mars.rb +0 -114
  43. data/test/perf_obj.rb +0 -124
  44. data/test/perf_pod.rb +0 -88
  45. data/test/perf_sax.rb +0 -233
  46. data/test/perf_write.rb +0 -80
  47. data/test/sample.rb +0 -55
  48. data/test/sample/change.rb +0 -14
  49. data/test/sample/dir.rb +0 -19
  50. data/test/sample/doc.rb +0 -36
  51. data/test/sample/file.rb +0 -48
  52. data/test/sample/group.rb +0 -16
  53. data/test/sample/hasprops.rb +0 -16
  54. data/test/sample/layer.rb +0 -12
  55. data/test/sample/line.rb +0 -20
  56. data/test/sample/oval.rb +0 -10
  57. data/test/sample/rect.rb +0 -10
  58. data/test/sample/shape.rb +0 -35
  59. data/test/sample/text.rb +0 -20
  60. data/test/sax_test.rb +0 -468
  61. data/test/test.rb +0 -70
data/test/bench.rb DELETED
@@ -1,53 +0,0 @@
1
- # Message Pack vs similar utilities
2
- # using ruby 1.9.2p290 (2011-07-09 revision 32553) [x86_64-darwin10.8.0]
3
- #
4
- # Packing
5
- # pack: message pack 0.142954
6
- # pack: marshall 0.636924
7
- # pack: json 3.001180
8
- # pack: ox 0.108832
9
- #
10
- # Unpacking
11
- # unpack: message pack 0.260064
12
- # unpack: marshal 0.616197
13
- # unpack: marshal 0.609927
14
- # unpack: ox 0.287053
15
-
16
- #require 'msgpack'
17
- require 'json'
18
- require 'ox'
19
-
20
- iter = 100000
21
-
22
- duck = { 'sound' => 'quack', 'name' => 'Daffy', 'feet' => 2, 'wings' => true }
23
- dude = { 'sound' => "I'm the dude!", 'name' => 'Lebowski', 'missing a rug' => true, 'missing money' => 1_000_000.00 }
24
- stuff = { :string => "A string", :array => [true, 2, 'yes'], :hash => { :string => "not very deep" }}
25
-
26
- def bench(title, iter, &blk)
27
- start = Time.now
28
- (1..iter).each { blk.call }
29
- time = Time.now - start
30
- puts "%-30s %10.6f" % [title, time]
31
- end
32
-
33
- def bench_all(title, iter, obj)
34
- puts "\n#{title} Packing"
35
- # bench('pack: message pack', iter) { MessagePack.pack(obj) }
36
- bench('pack: marshall', iter) { Marshal.dump(obj) }
37
- bench('pack: json', iter) { JSON.dump(obj) }
38
- bench('pack: ox', iter) { Ox.dump(obj) }
39
-
40
- puts "\n#{title} Unpacking"
41
- # mp_obj = MessagePack.pack(obj)
42
- # bench('unpack: message pack', iter) { MessagePack.unpack(mp_obj) }
43
- mars_obj = Marshal.dump(obj)
44
- bench('unpack: marshal', iter) { Marshal.load(mars_obj) }
45
- json_obj = JSON.dump(obj)
46
- bench('unpack: json', iter) { JSON.parse(json_obj) }
47
- ox_obj = Ox.dump(obj)
48
- bench('unpack: ox', iter) { Ox.parse_obj(ox_obj) }
49
- end
50
-
51
- bench_all('duck', iter, duck)
52
- bench_all('dude', iter, dude)
53
- bench_all('stuff', iter, stuff)
data/test/bug1.rb DELETED
@@ -1,24 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
-
3
- $: << '../lib'
4
- $: << '../ext'
5
-
6
- if __FILE__ == $0
7
- if (i = ARGV.index('-I'))
8
- x,path = ARGV.slice!(i, 2)
9
- $: << path
10
- end
11
- end
12
-
13
- require 'ox'
14
-
15
- def oxpoo(cnt = 100000)
16
- xml = "<?xml version=\"1.0\"?>\n<a>\n <m>inc</m>\n <i>1</i>\n</a>\n"
17
- cnt.times do |i|
18
- obj = Ox.load(xml, :mode => :object)
19
- #puts "#{obj} (#{obj.class})"
20
- raise "decode ##{i} not equal; #{obj.inspect} != '#{[:inc, 1] }'" unless [:inc, 1] == obj
21
- end
22
- end
23
-
24
- oxpoo()
data/test/bug2.rb DELETED
@@ -1,38 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
-
3
- $: << '../lib'
4
- $: << '../ext'
5
-
6
- if __FILE__ == $0
7
- if (i = ARGV.index('-I'))
8
- x,path = ARGV.slice!(i, 2)
9
- $: << path
10
- end
11
- end
12
-
13
- require 'ox'
14
-
15
- def bug2()
16
- s = File.read('long.pdf')
17
- puts "size before: #{s.size}"
18
-
19
- # s = "Hello\x00\x00\x00there."
20
- xml = Ox.dump(s, mode: :object)
21
- #puts "xml size: #{xml.size}"
22
- s2 = Ox.load(xml, mode: :object)
23
- puts "size after: #{s2.size}"
24
- #puts s2
25
- b1 = s.bytes
26
- b2 = s2.bytes
27
- (0..s2.bytesize).each do |i|
28
- puts "#{s.getbyte(i)} #{s2.getbyte(i)}"
29
- if s.getbyte(i) != s2.getbyte(i)
30
- puts "stopped at #{i}"
31
- break
32
- end
33
- end
34
- puts "byte sizes #{s.bytesize} #{s2.bytesize}"
35
- puts "char sizes #{s.size} #{s2.size}"
36
- end
37
-
38
- bug2()
data/test/bug3.rb DELETED
@@ -1,21 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
-
3
- $: << '../lib'
4
- $: << '../ext'
5
- $: << '../../pod/modules/pod/cfg/lib'
6
- $: << '../../pod/modules/pod/core/lib'
7
- $: << '../../pod/modules/pod/esb/lib'
8
-
9
- if __FILE__ == $0
10
- if (i = ARGV.index('-I'))
11
- x,path = ARGV.slice!(i, 2)
12
- $: << path
13
- end
14
- end
15
-
16
- require 'ox'
17
- require 'pod/cfg'
18
-
19
- x = Ox.load_file('fail.xml', :mode => :object, :effort => :strict)
20
-
21
- puts "return is a #{x.class}"
data/test/cache16_test.rb DELETED
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
-
3
- $: << '.'
4
- $: << '../lib'
5
- $: << '../ext'
6
-
7
- if __FILE__ == $0
8
- if (i = ARGV.index('-I'))
9
- x,path = ARGV.slice!(i, 2)
10
- $: << path
11
- end
12
- end
13
-
14
- require 'ox'
15
-
16
- Ox.cache16_test
17
-
data/test/cache8_test.rb DELETED
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
-
3
- $: << '.'
4
- $: << '../lib'
5
- $: << '../ext'
6
-
7
- if __FILE__ == $0
8
- if (i = ARGV.index('-I'))
9
- x,path = ARGV.slice!(i, 2)
10
- $: << path
11
- end
12
- end
13
-
14
- require 'ox'
15
-
16
- Ox.cache8_test
17
-
data/test/cache_test.rb DELETED
@@ -1,17 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
-
3
- $: << '.'
4
- $: << '../lib'
5
- $: << '../ext'
6
-
7
- if __FILE__ == $0
8
- if (i = ARGV.index('-I'))
9
- x,path = ARGV.slice!(i, 2)
10
- $: << path
11
- end
12
- end
13
-
14
- require 'ox'
15
-
16
- Ox.cache_test
17
-
data/test/files.rb DELETED
@@ -1,29 +0,0 @@
1
- #!/usr/bin/env ruby -wW2
2
-
3
- if $0 == __FILE__
4
- $: << '.'
5
- $: << '..'
6
- $: << '../lib'
7
- $: << '../ext'
8
- end
9
-
10
- require 'pp'
11
- require 'sample/file'
12
- require 'sample/dir'
13
-
14
- def files(dir)
15
- d = ::Sample::Dir.new(dir)
16
- Dir.new(dir).each do |fn|
17
- next if fn.start_with?('.')
18
- filename = File.join(dir, fn)
19
- #filename = '.' == dir ? fn : File.join(dir, fn)
20
- if File.directory?(filename)
21
- d << files(filename)
22
- else
23
- d << ::Sample::File.new(filename)
24
- end
25
- end
26
- #pp d
27
- d
28
- end
29
-
data/test/func.rb DELETED
@@ -1,652 +0,0 @@
1
- #!/usr/bin/env ruby -wW1
2
- # encoding: UTF-8
3
-
4
- $: << File.join(File.dirname(__FILE__), "../lib")
5
- $: << File.join(File.dirname(__FILE__), "../ext")
6
-
7
- require 'test/unit'
8
- require 'optparse'
9
- require 'date'
10
- require 'ox'
11
-
12
- $indent = 2
13
-
14
- opts = OptionParser.new
15
- # TBD add indent
16
- opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
17
- files = opts.parse(ARGV)
18
-
19
- class Func < ::Test::Unit::TestCase
20
-
21
- def test_get_options
22
- opts = Ox.default_options()
23
- assert_equal(opts, {
24
- :encoding=>nil,
25
- :indent=>2,
26
- :trace=>0,
27
- :with_dtd=>false,
28
- :with_xml=>false,
29
- :with_instructions=>false,
30
- :circular=>false,
31
- :xsd_date=>false,
32
- :mode=>nil,
33
- :effort=>:strict})
34
- end
35
-
36
- def test_set_options
37
- orig = {
38
- :encoding=>nil,
39
- :indent=>2,
40
- :trace=>0,
41
- :with_dtd=>false,
42
- :with_xml=>true,
43
- :with_instructions=>false,
44
- :circular=>false,
45
- :xsd_date=>false,
46
- :mode=>nil,
47
- :effort=>:strict}
48
- o2 = {
49
- :encoding=>"UTF-8",
50
- :indent=>4,
51
- :trace=>1,
52
- :with_dtd=>true,
53
- :with_xml=>false,
54
- :with_instructions=>true,
55
- :circular=>true,
56
- :xsd_date=>true,
57
- :mode=>:object,
58
- :effort=>:tolerant }
59
- o3 = { :xsd_date=>false }
60
- Ox.default_options = o2
61
- opts = Ox.default_options()
62
- assert_equal(opts, o2);
63
- Ox.default_options = o3 # see if it throws an exception
64
- Ox.default_options = orig # return to original
65
- end
66
-
67
- def test_nil
68
- dump_and_load(nil, false)
69
- end
70
-
71
- def test_true
72
- dump_and_load(true, false)
73
- end
74
-
75
- def test_false
76
- dump_and_load(false, false)
77
- end
78
-
79
- def test_fixnum
80
- dump_and_load(7, false)
81
- dump_and_load(-19, false)
82
- dump_and_load(0, false)
83
- end
84
-
85
- def test_float
86
- dump_and_load(7.7, false)
87
- dump_and_load(-1.9, false)
88
- dump_and_load(0.0, false)
89
- dump_and_load(-10.000005, false)
90
- dump_and_load(1.000000000005, false)
91
- end
92
-
93
- def test_string
94
- dump_and_load('a string', false)
95
- dump_and_load('', false)
96
- end
97
-
98
- def test_symbol
99
- dump_and_load(:a_symbol, false)
100
- dump_and_load(:<=, false)
101
- end
102
-
103
- def test_base64
104
- dump_and_load('a & x', false)
105
- end
106
-
107
- def test_time
108
- dump_and_load(Time.now, false)
109
- end
110
-
111
- def test_date
112
- dump_and_load(Date.new(2011, 1, 5), false)
113
- end
114
-
115
- def test_array
116
- dump_and_load([], false)
117
- dump_and_load([1, 'a'], false)
118
- end
119
-
120
- def test_hash
121
- dump_and_load({ }, false)
122
- dump_and_load({ 'a' => 1, 2 => 'b' }, false)
123
- end
124
-
125
- def test_range
126
- if RUBY_VERSION.start_with?('1.8')
127
- assert(true)
128
- else
129
- dump_and_load((0..3), false)
130
- dump_and_load((-2..3.7), false)
131
- dump_and_load(('a'...'f'), false)
132
- t = Time.now
133
- t2 = t + 20
134
- dump_and_load((t..t2), false)
135
- end
136
- end
137
-
138
- def test_regex
139
- if RUBY_VERSION.start_with?('1.8')
140
- assert(true)
141
- else
142
- dump_and_load(/^[0-9]/ix, false)
143
- dump_and_load(/^[&0-9]/ix, false) # with xml-unfriendly character
144
- end
145
- end
146
-
147
- def test_bignum
148
- dump_and_load(7 ** 55, false)
149
- dump_and_load(-7 ** 55, false)
150
- end
151
-
152
- def test_complex_number
153
- if RUBY_VERSION.start_with?('1.8')
154
- assert(true)
155
- else
156
- dump_and_load(Complex(1), false)
157
- dump_and_load(Complex(3, 2), false)
158
- end
159
- end
160
-
161
- def test_rational
162
- if RUBY_VERSION.start_with?('1.8')
163
- assert(true)
164
- else
165
- dump_and_load(Rational(1, 3), false)
166
- dump_and_load(Rational(0, 3), false)
167
- end
168
- end
169
-
170
- def test_object
171
- dump_and_load(Bag.new({ }), false)
172
- dump_and_load(Bag.new(:@x => 3), false)
173
- end
174
-
175
- def test_bad_object
176
- xml = %{<?xml version="1.0"?>
177
- <o c="Bad::Boy">
178
- <i a="@x">3</i>
179
- </o>
180
- }
181
- xml2 = %{<?xml version="1.0"?>
182
- <o c="Bad">
183
- <i a="@x">7</i>
184
- </o>
185
- }
186
- assert_raise(NameError) {
187
- Ox.load(xml, :mode => :object, :trace => 0)
188
- }
189
- loaded = Ox.load(xml, :mode => :object, :trace => 0, :effort => :tolerant)
190
- assert_equal(loaded, nil)
191
- loaded = Ox.load(xml, :mode => :object, :trace => 0, :effort => :auto_define)
192
- assert_equal(loaded.class.to_s, 'Bad::Boy')
193
- assert_equal(loaded.class.superclass.to_s, 'Ox::Bag')
194
- loaded = Ox.load(xml2, :mode => :object, :trace => 0, :effort => :auto_define)
195
- assert_equal(loaded.class.to_s, 'Bad')
196
- assert_equal(loaded.class.superclass.to_s, 'Ox::Bag')
197
- end
198
-
199
- def test_bad_class
200
- xml = %{<?xml version="1.0"?>
201
- <o c="Bad:Boy">
202
- <i a="@x">3</i>
203
- </o>
204
- }
205
- assert_raise(SyntaxError) {
206
- Ox.load(xml, :mode => :object, :trace => 0)
207
- }
208
- end
209
-
210
- def test_xml_instruction
211
- xml = Ox.dump("test", :mode => :object, :with_xml => false)
212
- assert_equal("<s>test</s>\n", xml)
213
- xml = Ox.dump("test", :mode => :object, :with_xml => true)
214
- assert_equal("<?xml version=\"1.0\"?>\n<s>test</s>\n", xml)
215
- xml = Ox.dump("test", :mode => :object, :with_xml => true, :encoding => 'UTF-8')
216
- assert_equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<s>test</s>\n", xml)
217
- end
218
-
219
- def test_ox_instruction
220
- xml = Ox.dump("test", :mode => :object, :with_xml => true, :with_instructions => true)
221
- assert_equal("<?xml version=\"1.0\"?>\n<?ox version=\"1.0\" mode=\"object\" circular=\"no\" xsd_date=\"no\"?>\n<s>test</s>\n", xml)
222
- xml = Ox.dump("test", :mode => :object, :with_instructions => true)
223
- assert_equal("<?ox version=\"1.0\" mode=\"object\" circular=\"no\" xsd_date=\"no\"?>\n<s>test</s>\n", xml)
224
- xml = Ox.dump("test", :mode => :object, :with_instructions => true, :circular => true, :xsd_date => true)
225
- assert_equal("<?ox version=\"1.0\" mode=\"object\" circular=\"yes\" xsd_date=\"yes\"?>\n<s i=\"1\">test</s>\n", xml)
226
- xml = Ox.dump("test", :mode => :object, :with_instructions => true, :circular => false, :xsd_date => false)
227
- assert_equal("<?ox version=\"1.0\" mode=\"object\" circular=\"no\" xsd_date=\"no\"?>\n<s>test</s>\n", xml)
228
- end
229
-
230
- def test_dtd
231
- xml = Ox.dump("test", :mode => :object, :with_dtd => true)
232
- assert_equal("<!DOCTYPE s SYSTEM \"ox.dtd\">\n<s>test</s>\n", xml)
233
- end
234
-
235
- def test_lone_dtd
236
- xml = "<!DOCTYPE html>" # not really a valid xml but should pass anyway
237
- doc = Ox.parse(xml)
238
- assert_equal('html', doc.nodes[0].value)
239
- end
240
-
241
- def test_quote_value
242
- xml = %{<top name="Pete"/>}
243
- doc = Ox.parse(xml)
244
- assert_equal('Pete', doc.attributes[:name])
245
- end
246
-
247
- def test_single_quote
248
- xml = %{<top name='Pete'/>}
249
- doc = Ox.load(xml, :effort => :tolerant)
250
- assert_equal('Pete', doc.attributes[:name])
251
- end
252
-
253
- def test_no_quote
254
- xml = %{<top name=Pete />}
255
- doc = Ox.load(xml, :effort => :tolerant)
256
- assert_equal('Pete', doc.attributes[:name])
257
- end
258
-
259
- def test_class
260
- dump_and_load(Bag, false)
261
- end
262
-
263
- def test_exception
264
- if RUBY_VERSION.start_with?('1.8')
265
- assert(true)
266
- else
267
- e = StandardError.new("Some Error")
268
- e.set_backtrace(["./func.rb:119: in test_exception",
269
- "./fake.rb:57: in fake_func"])
270
- dump_and_load(e, false)
271
- end
272
- end
273
-
274
- def test_exception_bag
275
- if RUBY_VERSION.start_with?('1.8')
276
- assert(true)
277
- else
278
- xml = %{
279
- <e c="FakeError">
280
- <s a="mesg">Some Error</s>
281
- <a a="bt">
282
- <s>./func.rb:119: in test_exception</s>
283
- <s>./fake.rb:57: in fake_func</s>
284
- </a>
285
- </e>
286
- }
287
- x = Ox.load(xml, :mode => :object, :effort => :auto_define)
288
- assert_equal('Some Error', x.message())
289
- assert(x.is_a?(Exception))
290
- end
291
- end
292
-
293
-
294
- def test_struct
295
- s = Struct.new('Box', :x, :y, :w, :h)
296
- dump_and_load(s.new(2, 4, 10, 20), false)
297
- end
298
-
299
- def test_bad_format
300
- xml = "<?xml version=\"1.0\"?>\n<tag>test</tagz>\n"
301
- assert_raise(SyntaxError) {
302
- Ox.load(xml, :mode => :generic, :trace => 0)
303
- }
304
- end
305
-
306
- def test_array_multi
307
- if RUBY_VERSION.start_with?('1.8')
308
- dump_and_load([nil, true, false, 3, 'z', 7.9, 'a&b', :xyz, Time.now], false)
309
- else
310
- dump_and_load([nil, true, false, 3, 'z', 7.9, 'a&b', :xyz, Time.now, (-1..7)], false)
311
- end
312
- end
313
-
314
- def test_hash_multi
315
- if RUBY_VERSION.start_with?('1.8')
316
- dump_and_load({ nil => nil, true => true, false => false, 3 => 3, 'z' => 'z', 7.9 => 7.9, 'a&b' => 'a&b', :xyz => :xyz, Time.now => Time.now }, false)
317
- else
318
- dump_and_load({ nil => nil, true => true, false => false, 3 => 3, 'z' => 'z', 7.9 => 7.9, 'a&b' => 'a&b', :xyz => :xyz, Time.now => Time.now, (-1..7) => (-1..7) }, false)
319
- end
320
- end
321
-
322
- def test_object_multi
323
- if RUBY_VERSION.start_with?('1.8')
324
- dump_and_load(Bag.new(:@a => nil, :@b => true, :@c => false, :@d => 3, :@e => 'z', :@f => 7.9, :@g => 'a&b', :@h => :xyz, :@i => Time.now), false)
325
- else
326
- dump_and_load(Bag.new(:@a => nil, :@b => true, :@c => false, :@d => 3, :@e => 'z', :@f => 7.9, :@g => 'a&b', :@h => :xyz, :@i => Time.now, :@j => (-1..7)), false)
327
- end
328
- end
329
-
330
- def test_complex
331
- dump_and_load(Bag.new(:@o => Bag.new(:@a => [2]), :@a => [1, {:b => 3, :a => [5], :c => Bag.new(:@x => 7)}]), false)
332
- end
333
-
334
- # Create an Object and an Array with the same Objects in them. Dump and load
335
- # and then change the ones in the loaded Object to verify that the ones in
336
- # the array change in the same way. They are the same objects so they should
337
- # change. Perform the operation on both the object before and the loaded so
338
- # the equal() method can be used.
339
- def test_circular
340
- if RUBY_VERSION.start_with?('1.8')
341
- # In 1.8.7 the eql? method behaves differently but the results are
342
- # correct when viewed by eye.
343
- assert(true)
344
- else
345
- a = [1]
346
- s = "a,b,c"
347
- h = { 1 => 2 }
348
- e = Ox::Element.new('Zoo')
349
- e[:cage] = 'bear'
350
- b = Bag.new(:@a => a, :@s => s, :@h => h, :@e => e)
351
- a << s
352
- a << h
353
- a << e
354
- a << b
355
- loaded = dump_and_load(b, false, true)
356
- # modify the string
357
- loaded.instance_variable_get(:@s).gsub!(',', '_')
358
- b.instance_variable_get(:@s).gsub!(',', '_')
359
- # modify hash
360
- loaded.instance_variable_get(:@h)[1] = 3
361
- b.instance_variable_get(:@h)[1] = 3
362
- # modify Element
363
- loaded.instance_variable_get(:@e)['pen'] = 'zebra'
364
- b.instance_variable_get(:@e)['pen'] = 'zebra'
365
- # pp loaded
366
- assert_equal(b, loaded)
367
- end
368
- end
369
-
370
- def test_raw
371
- raw = Ox::Element.new('raw')
372
- su = Ox::Element.new('sushi')
373
- su[:kind] = 'fish'
374
- raw << su
375
- ba = Ox::Element.new('basashi')
376
- ba[:kind] = 'animal'
377
- raw << ba
378
- dump_and_load(Bag.new(:@raw => raw), false)
379
- end
380
-
381
- def dump_and_load(obj, trace=false, circular=false)
382
- xml = Ox.dump(obj, :indent => $indent, :circular => circular)
383
- puts xml if trace
384
- loaded = Ox.load(xml, :mode => :object, :trace => (trace ? 2 : 0))
385
- assert_equal(obj, loaded)
386
- loaded
387
- end
388
-
389
- def test_nameerror
390
- begin
391
- "x".foo
392
- rescue Exception => e
393
- xml = Ox.dump(e, :effort => :tolerant)
394
- o = Ox.load(xml, :mode => :object)
395
- xml2 = Ox.dump(o, :effort => :tolerant)
396
- assert_equal(xml, xml2)
397
- end
398
- end
399
-
400
- def test_mutex
401
- if defined?(Mutex)
402
- # Mutex can not be serialize but it should not raise an exception.
403
- xml = Ox.dump(Mutex.new, :indent => 2, :effort => :tolerant)
404
- assert_equal(%{<z/>
405
- }, xml)
406
- xml = Ox.dump(Bag.new(:@x => Mutex.new), :indent => 2, :effort => :tolerant)
407
- assert_equal(%{<o c="Bag">
408
- <z a="@x"/>
409
- </o>
410
- }, xml)
411
- else
412
- assert(true)
413
- end
414
- end
415
-
416
- def test_encoding
417
- if RUBY_VERSION.start_with?('1.8')
418
- assert(true)
419
- else
420
- s = 'ピーター'
421
- xml = Ox.dump(s, :with_xml => true, :encoding => 'UTF-8')
422
- #puts xml
423
- #puts xml.encoding.to_s
424
- assert_equal('UTF-8', xml.encoding.to_s)
425
- obj = Ox.load(xml, :mode => :object)
426
- assert_equal(s, obj)
427
- end
428
- end
429
-
430
- def test_full_encoding
431
- if RUBY_VERSION.start_with?('1.8')
432
- assert(true)
433
- else
434
- xml = %{<?xml version="1.0" encoding="UTF-8"?>
435
- <いち name="ピーター" つま="まきえ">ピーター</いち>
436
- }
437
- obj = Ox.load(xml)
438
- dumped = Ox.dump(obj, :with_xml => true)
439
- assert_equal('UTF-8', dumped.encoding.to_s)
440
- assert_equal(xml, dumped)
441
- end
442
- end
443
-
444
- def test_obj_encoding
445
- if RUBY_VERSION.start_with?('1.8')
446
- assert(true)
447
- else
448
- orig = Ox.default_options
449
- opts = orig.clone
450
- opts[:encoding] = 'UTF-8'
451
- opts[:with_xml] = true
452
- Ox.default_options = opts
453
- begin
454
- dump_and_load(Bag.new(:@tsuma => :まきえ), false)
455
- dump_and_load(Bag.new(:@つま => :まきえ), false)
456
- ensure
457
- Ox.default_options = orig
458
- end
459
- end
460
- end
461
-
462
- def test_instructions
463
- xml = Ox.dump("test", :with_instructions => true)
464
- #puts xml
465
- obj = Ox.load(xml) # should convert it to an object
466
- assert_equal("test", obj)
467
- end
468
-
469
- def test_generic_string
470
- xml = %{<?xml?>
471
- <Str>A &lt;boo&gt;</Str>
472
- }
473
- doc = Ox.load(xml, :mode => :generic)
474
- xml2 = Ox.dump(doc, :with_xml => true)
475
- assert_equal(xml, xml2)
476
- end
477
-
478
- def test_generic_encoding
479
- if RUBY_VERSION.start_with?('1.8')
480
- assert(true)
481
- else
482
- xml = %{<?xml encoding="UTF-8"?>
483
- <Str>&lt;まきえ&gt;</Str>
484
- }
485
- doc = Ox.load(xml, :mode => :generic)
486
- xml2 = Ox.dump(doc, :with_xml => true)
487
- assert_equal(xml, xml2)
488
- end
489
- end
490
-
491
- def test_IO
492
- f = File.open(__FILE__, "r")
493
- assert_raise(NotImplementedError) {
494
- xml = Ox.dump(f, :effort => :strict)
495
- }
496
- xml = Ox.dump(f, :effort => :tolerant)
497
- obj = Ox.load(xml, :mode => :object) # should convert it to an object
498
- assert_equal(nil, obj)
499
- end
500
-
501
- def locate_xml()
502
- %{<?xml?>
503
- <Family real="false">
504
- <Pete age="57" type="male">
505
- <Kid1 age="32"/>
506
- <Kid2 age="31"/>
507
- </Pete>
508
- </Family>
509
- }
510
- end
511
-
512
- def test_locate_self
513
- doc = Ox.parse(locate_xml)
514
- nodes = doc.locate(nil)
515
- assert_equal(doc, nodes[0])
516
- end
517
-
518
- def test_locate_top
519
- doc = Ox.parse(locate_xml)
520
- nodes = doc.locate('Family')
521
- assert_equal([doc.nodes[0]], nodes)
522
- end
523
-
524
- def test_locate_top_wild
525
- doc = Ox.parse(locate_xml)
526
- nodes = doc.locate('?')
527
- assert_equal([doc.nodes[0]], nodes)
528
- end
529
-
530
- def test_locate_child
531
- doc = Ox.parse(locate_xml)
532
-
533
- nodes = doc.locate('Family/?')
534
- assert_equal(['Pete'], nodes.map { |n| n.name })
535
-
536
- nodes = doc.locate('Family/?/?')
537
- assert_equal(['Kid1', 'Kid2'], nodes.map { |n| n.name })
538
-
539
- nodes = doc.locate('Family/Pete/?')
540
- assert_equal(['Kid1', 'Kid2'], nodes.map { |n| n.name })
541
-
542
- nodes = doc.locate('Family/Makie/?')
543
- assert_equal([], nodes.map { |n| n.name })
544
- end
545
-
546
- def test_locate_child_wild_wild
547
- doc = Ox.parse(locate_xml)
548
- nodes = doc.locate('Family/?/?')
549
- assert_equal(['Kid1', 'Kid2'], nodes.map { |n| n.name })
550
- end
551
-
552
- def test_locate_child_wild
553
- doc = Ox.parse(locate_xml)
554
- nodes = doc.locate('Family/Pete/?')
555
- assert_equal(['Kid1', 'Kid2'], nodes.map { |n| n.name })
556
- end
557
-
558
- def test_locate_child_wild_missing
559
- doc = Ox.parse(locate_xml)
560
- nodes = doc.locate('Family/Makie/?')
561
- assert_equal([], nodes.map { |n| n.name })
562
- end
563
-
564
- def test_locate_attribute
565
- doc = Ox.parse(locate_xml)
566
-
567
- nodes = doc.locate('Family/@?')
568
- assert_equal(['false'], nodes)
569
-
570
- nodes = doc.locate('Family/@real')
571
- assert_equal(['false'], nodes)
572
-
573
- nodes = doc.locate('Family/Pete/@?')
574
- assert_equal(['57', 'male'], nodes.sort)
575
-
576
- nodes = doc.locate('Family/Pete/@age')
577
- assert_equal(['57'], nodes)
578
-
579
- nodes = doc.locate('Family/Makie/@?')
580
- assert_equal([], nodes)
581
-
582
- nodes = doc.locate('Family/Pete/?/@age')
583
- assert_equal(['31', '32'], nodes.sort)
584
-
585
- nodes = doc.locate('Family/*/@age')
586
- assert_equal(['31', '32', '57'], nodes.sort)
587
-
588
- nodes = doc.locate('*/@?')
589
- assert_equal(['31', '32', '57', 'false', 'male'], nodes.sort)
590
-
591
- assert_raise(::Ox::InvalidPath) {
592
- nodes = doc.locate('Family/@age/?')
593
- }
594
-
595
- assert_raise(::Ox::InvalidPath) {
596
- nodes = doc.locate('Family/?[/?')
597
- }
598
- end
599
-
600
- def test_locate_qual_index
601
- doc = Ox.parse(locate_xml)
602
- nodes = doc.locate('Family/Pete/?[0]')
603
- assert_equal(['Kid1'], nodes.map { |e| e.name } )
604
- nodes = doc.locate('Family/Pete/?[1]')
605
- assert_equal(['Kid2'], nodes.map { |e| e.name } )
606
- end
607
-
608
- def test_locate_qual_less
609
- doc = Ox.parse(locate_xml)
610
- nodes = doc.locate('Family/Pete/?[<1]')
611
- assert_equal(['Kid1'], nodes.map { |e| e.name } )
612
- end
613
-
614
- def test_locate_qual_great
615
- doc = Ox.parse(locate_xml)
616
- nodes = doc.locate('Family/Pete/?[>0]')
617
- assert_equal(['Kid2'], nodes.map { |e| e.name } )
618
- end
619
-
620
- def test_locate_qual_last
621
- doc = Ox.parse(locate_xml)
622
- nodes = doc.locate('Family/Pete/?[-1]')
623
- assert_equal(['Kid2'], nodes.map { |e| e.name } )
624
- end
625
-
626
- def test_locate_qual_last_attr
627
- doc = Ox.parse(locate_xml)
628
- nodes = doc.locate('Family/Pete/?[-1]/@age')
629
- assert_equal(['31'], nodes )
630
- end
631
- end
632
-
633
- class Bag
634
-
635
- def initialize(args)
636
- args.each do |k,v|
637
- self.instance_variable_set(k, v)
638
- end
639
- end
640
-
641
- def eql?(other)
642
- return false if (other.nil? or self.class != other.class)
643
- ova = other.instance_variables
644
- return false if ova.size != instance_variables.size
645
- instance_variables.each do |vid|
646
- return false if instance_variable_get(vid) != other.instance_variable_get(vid)
647
- end
648
- true
649
- end
650
- alias == eql?
651
-
652
- end