ox 1.0.0

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 (52) hide show
  1. data/LICENSE +27 -0
  2. data/README +153 -0
  3. data/ext/ox/base64.c +123 -0
  4. data/ext/ox/base64.h +44 -0
  5. data/ext/ox/cache.c +148 -0
  6. data/ext/ox/cache.h +43 -0
  7. data/ext/ox/cache8.c +80 -0
  8. data/ext/ox/cache8.h +43 -0
  9. data/ext/ox/cache8_test.c +69 -0
  10. data/ext/ox/cache_test.c +69 -0
  11. data/ext/ox/dump.c +901 -0
  12. data/ext/ox/extconf.rb +7 -0
  13. data/ext/ox/gen_load.c +196 -0
  14. data/ext/ox/obj_load.c +802 -0
  15. data/ext/ox/ox.c +456 -0
  16. data/ext/ox/ox.h +190 -0
  17. data/ext/ox/parse.c +629 -0
  18. data/lib/ox.rb +97 -0
  19. data/lib/ox/cdata.rb +12 -0
  20. data/lib/ox/comment.rb +13 -0
  21. data/lib/ox/doctype.rb +13 -0
  22. data/lib/ox/document.rb +20 -0
  23. data/lib/ox/element.rb +67 -0
  24. data/lib/ox/node.rb +24 -0
  25. data/test/Sample.graffle +2318 -0
  26. data/test/cache16_test.rb +17 -0
  27. data/test/cache8_test.rb +17 -0
  28. data/test/cache_test.rb +17 -0
  29. data/test/files.rb +34 -0
  30. data/test/func.rb +228 -0
  31. data/test/gen_sample.rb +22 -0
  32. data/test/obj_sample.rb +19 -0
  33. data/test/ox/change.rb +16 -0
  34. data/test/ox/dir.rb +21 -0
  35. data/test/ox/doc.rb +39 -0
  36. data/test/ox/file.rb +33 -0
  37. data/test/ox/group.rb +18 -0
  38. data/test/ox/hasprops.rb +18 -0
  39. data/test/ox/layer.rb +14 -0
  40. data/test/ox/line.rb +22 -0
  41. data/test/ox/oval.rb +12 -0
  42. data/test/ox/rect.rb +12 -0
  43. data/test/ox/shape.rb +37 -0
  44. data/test/ox/text.rb +23 -0
  45. data/test/perf_gen.rb +193 -0
  46. data/test/perf_mars.rb +97 -0
  47. data/test/perf_obj.rb +201 -0
  48. data/test/perf_pod.rb +88 -0
  49. data/test/perf_write.rb +80 -0
  50. data/test/sample.rb +62 -0
  51. data/test/test.rb +70 -0
  52. metadata +106 -0
@@ -0,0 +1,17 @@
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
+
@@ -0,0 +1,17 @@
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
+
@@ -0,0 +1,17 @@
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 ADDED
@@ -0,0 +1,34 @@
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 'ox'
12
+ require 'test/ox/file'
13
+ require 'test/ox/dir'
14
+
15
+ def files(dir)
16
+ d = ::Test::Ox::Dir.new(dir)
17
+ Dir.new(dir).each do |fn|
18
+ next if fn.start_with?('.')
19
+ filename = File.join(dir, fn)
20
+ #filename = '.' == dir ? fn : File.join(dir, fn)
21
+ if File.directory?(filename)
22
+ d << files(filename)
23
+ else
24
+ d << Test::Ox::File.new(filename)
25
+ end
26
+ end
27
+ #pp d
28
+ d
29
+ end
30
+
31
+ if $0 == __FILE__
32
+ File.open('files.xml', "w") { |f| f.write(Ox.dump(files('.'), :indent => 2, :opt_format => true)) }
33
+ #Ox.to_file(files('.'), 'files2.xml', :indent => 2, :opt_format => true)
34
+ end
data/test/func.rb ADDED
@@ -0,0 +1,228 @@
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 'test/unit'
14
+ require 'optparse'
15
+ require 'ox'
16
+
17
+ $indent = 2
18
+
19
+ opts = OptionParser.new
20
+ # TBD add indent
21
+ opts.on("-h", "--help", "Show this display") { puts opts; Process.exit!(0) }
22
+ files = opts.parse(ARGV)
23
+
24
+ class Func < ::Test::Unit::TestCase
25
+
26
+ def test_nil
27
+ dump_and_load(nil, false)
28
+ end
29
+
30
+ def test_true
31
+ dump_and_load(true, false)
32
+ end
33
+
34
+ def test_false
35
+ dump_and_load(false, false)
36
+ end
37
+
38
+ def test_fixnum
39
+ dump_and_load(7, false)
40
+ dump_and_load(-19, false)
41
+ dump_and_load(0, false)
42
+ end
43
+
44
+ def test_float
45
+ dump_and_load(7.7, false)
46
+ dump_and_load(-1.9, false)
47
+ dump_and_load(0.0, false)
48
+ dump_and_load(-10.000005, false)
49
+ dump_and_load(1.000000000005, false)
50
+ end
51
+
52
+ def test_string
53
+ dump_and_load('a string', false)
54
+ dump_and_load('', false)
55
+ end
56
+
57
+ def test_symbol
58
+ dump_and_load(:a_symbol, false)
59
+ end
60
+
61
+ def test_base64
62
+ dump_and_load('a & x', false)
63
+ end
64
+
65
+ def test_time
66
+ dump_and_load(Time.now, false)
67
+ end
68
+
69
+ def test_array
70
+ dump_and_load([], false)
71
+ dump_and_load([1, 'a'], false)
72
+ end
73
+
74
+ def test_hash
75
+ dump_and_load({ }, false)
76
+ dump_and_load({ 'a' => 1, 2 => 'b' }, false)
77
+ end
78
+
79
+ def test_range
80
+ dump_and_load((0..3), false)
81
+ dump_and_load((-2..3.7), false)
82
+ dump_and_load(('a'...'f'), false)
83
+ t = Time.now
84
+ t2 = t + 20
85
+ dump_and_load((t..t2), false)
86
+ end
87
+
88
+ def test_regex
89
+ dump_and_load(/^[0-9]/ix, false)
90
+ dump_and_load(/^[&0-9]/ix, false) # with xml-unfriendly character
91
+ end
92
+
93
+ def test_bignum
94
+ dump_and_load(7 ** 55, false)
95
+ dump_and_load(-7 ** 55, false)
96
+ end
97
+
98
+ def test_complex_number
99
+ dump_and_load(Complex(1), false)
100
+ dump_and_load(Complex(3, 2), false)
101
+ end
102
+
103
+ def test_rational
104
+ dump_and_load(Rational(1, 3), false)
105
+ dump_and_load(Rational(0, 3), false)
106
+ end
107
+
108
+ def test_object
109
+ dump_and_load(Bag.new({ }), false)
110
+ dump_and_load(Bag.new(:@x => 3), false)
111
+ end
112
+
113
+ def test_bad_object
114
+ xml = %{<?xml version="1.0"?>
115
+ <o c="Bad">
116
+ <i a="@x">3</i>
117
+ </o>
118
+ }
119
+ assert_raise(NameError) {
120
+ Ox.load(xml, :mode => :object, :trace => 0)
121
+ }
122
+ loaded = Ox.load(xml, :mode => :object, :trace => 0, :best_effort => true)
123
+ assert_equal(loaded, nil)
124
+ end
125
+
126
+ def test_class
127
+ dump_and_load(Bag, false)
128
+ end
129
+
130
+ def test_exception
131
+ e = Exception.new("Some Error")
132
+ e.set_backtrace(["./func.rb:119: in test_exception",
133
+ "./fake.rb:57: in fake_func"])
134
+ dump_and_load(e, false)
135
+ end
136
+
137
+ def test_struct
138
+ s = Struct.new('Box', :x, :y, :w, :h)
139
+ dump_and_load(s.new(2, 4, 10, 20), false)
140
+ end
141
+
142
+ def test_array_multi
143
+ dump_and_load([nil, true, false, 3, 'z', 7.9, 'a&b', :xyz, Time.now, (-1..7)], false)
144
+ end
145
+
146
+ def test_hash_multi
147
+ 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)
148
+ end
149
+
150
+ def test_object_multi
151
+ 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)
152
+ end
153
+
154
+ def test_complex
155
+ dump_and_load(Bag.new(:@o => Bag.new(:@a => [2]), :@a => [1, {b: 3, a: [5]}, c: Bag.new(:@x => 7)]), false)
156
+ end
157
+
158
+ # Create an Object and an Array with the same Objects in them. Dump and load
159
+ # and then change the ones in the loaded Object to verify that the ones in
160
+ # the array change in the same way. They are the same objects so they should
161
+ # change. Perform the operation on both the object before and the loaded so
162
+ # the equal() method can be used.
163
+ def test_circular
164
+ a = [1]
165
+ s = "a,b,c"
166
+ h = { 1 => 2 }
167
+ e = Ox::Element.new('Zoo')
168
+ e[:cage] = 'bear'
169
+ b = Bag.new(:@a => a, :@s => s, :@h => h, :@e => e)
170
+ a << s
171
+ a << h
172
+ a << e
173
+ a << b
174
+ loaded = dump_and_load(b, false, true)
175
+ # modify the string
176
+ loaded.instance_variable_get(:@s).gsub!(',', '_')
177
+ b.instance_variable_get(:@s).gsub!(',', '_')
178
+ # modify hash
179
+ loaded.instance_variable_get(:@h)[1] = 3
180
+ b.instance_variable_get(:@h)[1] = 3
181
+ # modify Element
182
+ loaded.instance_variable_get(:@e)['pen'] = 'zebra'
183
+ b.instance_variable_get(:@e)['pen'] = 'zebra'
184
+ # pp loaded
185
+ assert_equal(b, loaded)
186
+ end
187
+
188
+ def test_raw
189
+ raw = Ox::Element.new('raw')
190
+ su = Ox::Element.new('sushi')
191
+ su[:kind] = 'fish'
192
+ raw << su
193
+ ba = Ox::Element.new('basashi')
194
+ ba[:kind] = 'animal'
195
+ raw << ba
196
+ dump_and_load(Bag.new(:@raw => raw), false)
197
+ end
198
+
199
+ def dump_and_load(obj, trace=false, circular=false)
200
+ xml = Ox.dump(obj, :indent => $indent, :circular => circular)
201
+ puts xml if trace
202
+ loaded = Ox.load(xml, :mode => :object, :trace => (trace ? 2 : 0))
203
+ assert_equal(obj, loaded)
204
+ loaded
205
+ end
206
+
207
+ end
208
+
209
+ class Bag
210
+
211
+ def initialize(args)
212
+ args.each do |k,v|
213
+ self.instance_variable_set(k, v)
214
+ end
215
+ end
216
+
217
+ def eql?(other)
218
+ return false if (other.nil? or self.class != other.class)
219
+ ova = other.instance_variables
220
+ return false if ova.size != instance_variables.size
221
+ instance_variables.each do |vid|
222
+ return false if instance_variable_get(vid) != other.instance_variable_get(vid)
223
+ end
224
+ true
225
+ end
226
+ alias == eql?
227
+
228
+ end
@@ -0,0 +1,22 @@
1
+
2
+ require 'ox'
3
+
4
+ doc = Ox::Document.new(:version => '1.0')
5
+
6
+ top = Ox::Element.new('top')
7
+ top[:name] = 'sample'
8
+ doc << top
9
+
10
+ mid = Ox::Element.new('middle')
11
+ mid[:name] = 'second'
12
+ top << mid
13
+
14
+ bot = Ox::Element.new('bottom')
15
+ bot[:name] = 'third'
16
+ mid << bot
17
+
18
+ xml = Ox.dump(doc)
19
+ puts xml
20
+
21
+ doc2 = Ox.parse(xml)
22
+ puts "Same? #{doc == doc2}"
@@ -0,0 +1,19 @@
1
+
2
+ require 'ox'
3
+
4
+ class Sample
5
+ attr_accessor :a, :b, :c
6
+
7
+ def initialize(a, b, c)
8
+ @a = a
9
+ @b = b
10
+ @c = c
11
+ end
12
+ end # File
13
+
14
+ # Create Object
15
+ obj = Sample.new(1, "bee", ['x', :y, 7.0])
16
+ # Now dump the Object to an XML String.
17
+ xml = Ox.dump(obj)
18
+ # Convert the object back into a Sample Object.
19
+ obj2 = Ox.parse_obj(xml)
data/test/ox/change.rb ADDED
@@ -0,0 +1,16 @@
1
+
2
+ module Test
3
+ module Ox
4
+ class Change
5
+ attr_accessor :time
6
+ attr_accessor :user
7
+ attr_accessor :comment
8
+
9
+ def initialize(comment=nil, time=nil, user=nil)
10
+ @user = user || ENV['USER']
11
+ @time = time || Time.now
12
+ @comment = comment
13
+ end
14
+ end # Change
15
+ end # Ox
16
+ end # Test
data/test/ox/dir.rb ADDED
@@ -0,0 +1,21 @@
1
+
2
+ require 'etc'
3
+
4
+ module Test
5
+ module Ox
6
+
7
+ class Dir < File
8
+ attr_accessor :files
9
+
10
+ def initialize(filename)
11
+ super
12
+ @files = []
13
+ end
14
+
15
+ def <<(f)
16
+ @files << f
17
+ end
18
+
19
+ end # Dir
20
+ end # Ox
21
+ end # Test
data/test/ox/doc.rb ADDED
@@ -0,0 +1,39 @@
1
+
2
+ require 'test/ox/hasprops'
3
+ require 'test/ox/group'
4
+ require 'test/ox/layer'
5
+ require 'test/ox/line'
6
+ require 'test/ox/shape'
7
+ require 'test/ox/oval'
8
+ require 'test/ox/rect'
9
+ require 'test/ox/text'
10
+ require 'test/ox/change'
11
+
12
+ module Test
13
+ module Ox
14
+ class Doc
15
+ include HasProps
16
+
17
+ attr_accessor :title
18
+ attr_accessor :create_time
19
+ attr_accessor :user
20
+ # Hash of layers in the document indexed by layer name.
21
+ attr_reader :layers
22
+ attr_reader :change_history
23
+
24
+ def initialize(title)
25
+ @title = title
26
+ @user = ENV['USER']
27
+ @create_time = Time.now
28
+ @layers = { }
29
+ @change_history = []
30
+ end
31
+
32
+ def add_change(comment, time=nil, user=nil)
33
+ @change_history << Change.new(comment, time, user)
34
+ end
35
+
36
+ end # Doc
37
+ end # Ox
38
+ end # Test
39
+