oj 3.12.3 → 3.13.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,27 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: utf-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+ $oj_dir = File.dirname(File.expand_path(File.dirname(__FILE__)))
6
+ %w(lib ext).each do |dir|
7
+ $: << File.join($oj_dir, dir)
8
+ end
9
+
10
+ require 'minitest'
11
+ require 'minitest/autorun'
12
+ require 'stringio'
13
+ require 'date'
14
+ require 'bigdecimal'
15
+ require 'oj'
16
+
17
+ class ParserJuice < Minitest::Test
18
+
19
+ def test_array
20
+ p = Oj::Parser.new(:debug)
21
+ out = p.parse(%|[true, false, null, 123, -1.23, "abc"]|)
22
+ puts out
23
+ out = p.parse(%|{"abc": []}|)
24
+ puts out
25
+ end
26
+
27
+ end
@@ -0,0 +1,245 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ require 'helper'
7
+
8
+ $json = %{{
9
+ "array": [
10
+ {
11
+ "num" : 3,
12
+ "string": "message",
13
+ "hash" : {
14
+ "h2" : {
15
+ "a" : [ 1, 2, 3 ]
16
+ }
17
+ }
18
+ }
19
+ ],
20
+ "boolean" : true
21
+ }}
22
+
23
+ class AllSaj < Oj::Saj
24
+ attr_accessor :calls
25
+
26
+ def initialize()
27
+ @calls = []
28
+ end
29
+
30
+ def hash_start(key)
31
+ @calls << [:hash_start, key]
32
+ end
33
+
34
+ def hash_end(key)
35
+ @calls << [:hash_end, key]
36
+ end
37
+
38
+ def array_start(key)
39
+ @calls << [:array_start, key]
40
+ end
41
+
42
+ def array_end(key)
43
+ @calls << [:array_end, key]
44
+ end
45
+
46
+ def add_value(value, key)
47
+ @calls << [:add_value, value, key]
48
+ end
49
+
50
+ def error(message, line, column)
51
+ @calls << [:error, message, line, column]
52
+ end
53
+
54
+ end # AllSaj
55
+
56
+ class SajTest < Minitest::Test
57
+
58
+ def test_nil
59
+ handler = AllSaj.new()
60
+ json = %{null}
61
+ p = Oj::Parser.new(:saj)
62
+ p.handler = handler
63
+ p.parse(json)
64
+ assert_equal([[:add_value, nil, nil]], handler.calls)
65
+ end
66
+
67
+ def test_true
68
+ handler = AllSaj.new()
69
+ json = %{true}
70
+ p = Oj::Parser.new(:saj)
71
+ p.handler = handler
72
+ p.parse(json)
73
+ assert_equal([[:add_value, true, nil]], handler.calls)
74
+ end
75
+
76
+ def test_false
77
+ handler = AllSaj.new()
78
+ json = %{false}
79
+ p = Oj::Parser.new(:saj)
80
+ p.handler = handler
81
+ p.parse(json)
82
+ assert_equal([[:add_value, false, nil]], handler.calls)
83
+ end
84
+
85
+ def test_string
86
+ handler = AllSaj.new()
87
+ json = %{"a string"}
88
+ p = Oj::Parser.new(:saj)
89
+ p.handler = handler
90
+ p.parse(json)
91
+ assert_equal([[:add_value, 'a string', nil]], handler.calls)
92
+ end
93
+
94
+ def test_fixnum
95
+ handler = AllSaj.new()
96
+ json = %{12345}
97
+ p = Oj::Parser.new(:saj)
98
+ p.handler = handler
99
+ p.parse(json)
100
+ assert_equal([[:add_value, 12345, nil]], handler.calls)
101
+ end
102
+
103
+ def test_float
104
+ handler = AllSaj.new()
105
+ json = %{12345.6789}
106
+ p = Oj::Parser.new(:saj)
107
+ p.handler = handler
108
+ p.parse(json)
109
+ assert_equal([[:add_value, 12345.6789, nil]], handler.calls)
110
+ end
111
+
112
+ def test_float_exp
113
+ handler = AllSaj.new()
114
+ json = %{12345.6789e7}
115
+ p = Oj::Parser.new(:saj)
116
+ p.handler = handler
117
+ p.parse(json)
118
+ assert_equal(1, handler.calls.size)
119
+ assert_equal(:add_value, handler.calls[0][0])
120
+ assert_equal((12345.6789e7 * 10000).to_i, (handler.calls[0][1] * 10000).to_i)
121
+ end
122
+
123
+ def test_array_empty
124
+ handler = AllSaj.new()
125
+ json = %{[]}
126
+ p = Oj::Parser.new(:saj)
127
+ p.handler = handler
128
+ p.parse(json)
129
+ assert_equal([[:array_start, nil],
130
+ [:array_end, nil]], handler.calls)
131
+ end
132
+
133
+ def test_array
134
+ handler = AllSaj.new()
135
+ json = %{[true,false]}
136
+ p = Oj::Parser.new(:saj)
137
+ p.handler = handler
138
+ p.parse(json)
139
+ assert_equal([[:array_start, nil],
140
+ [:add_value, true, nil],
141
+ [:add_value, false, nil],
142
+ [:array_end, nil]], handler.calls)
143
+ end
144
+
145
+ def test_hash_empty
146
+ handler = AllSaj.new()
147
+ json = %{{}}
148
+ p = Oj::Parser.new(:saj)
149
+ p.handler = handler
150
+ p.parse(json)
151
+ assert_equal([[:hash_start, nil],
152
+ [:hash_end, nil]], handler.calls)
153
+ end
154
+
155
+ def test_hash
156
+ handler = AllSaj.new()
157
+ json = %{{"one":true,"two":false}}
158
+ p = Oj::Parser.new(:saj)
159
+ p.handler = handler
160
+ p.parse(json)
161
+ assert_equal([[:hash_start, nil],
162
+ [:add_value, true, 'one'],
163
+ [:add_value, false, 'two'],
164
+ [:hash_end, nil]], handler.calls)
165
+ end
166
+
167
+ def test_full
168
+ handler = AllSaj.new()
169
+ Oj.saj_parse(handler, $json)
170
+ assert_equal([[:hash_start, nil],
171
+ [:array_start, 'array'],
172
+ [:hash_start, nil],
173
+ [:add_value, 3, 'num'],
174
+ [:add_value, 'message', 'string'],
175
+ [:hash_start, 'hash'],
176
+ [:hash_start, 'h2'],
177
+ [:array_start, 'a'],
178
+ [:add_value, 1, nil],
179
+ [:add_value, 2, nil],
180
+ [:add_value, 3, nil],
181
+ [:array_end, 'a'],
182
+ [:hash_end, 'h2'],
183
+ [:hash_end, 'hash'],
184
+ [:hash_end, nil],
185
+ [:array_end, 'array'],
186
+ [:add_value, true, 'boolean'],
187
+ [:hash_end, nil]], handler.calls)
188
+ end
189
+
190
+ def test_multiple
191
+ handler = AllSaj.new()
192
+ json = %|[true][false]|
193
+ p = Oj::Parser.new(:saj)
194
+ p.handler = handler
195
+ p.parse(json)
196
+ assert_equal([
197
+ [:array_start, nil],
198
+ [:add_value, true, nil],
199
+ [:array_end, nil],
200
+ [:array_start, nil],
201
+ [:add_value, false, nil],
202
+ [:array_end, nil],
203
+ ], handler.calls)
204
+ end
205
+
206
+ def test_io
207
+ handler = AllSaj.new()
208
+ json = %| [true,false] |
209
+ p = Oj::Parser.new(:saj)
210
+ p.handler = handler
211
+ p.load(StringIO.new(json))
212
+ assert_equal([
213
+ [:array_start, nil],
214
+ [:add_value, true, nil],
215
+ [:add_value, false, nil],
216
+ [:array_end, nil],
217
+ ], handler.calls)
218
+ end
219
+
220
+ def test_file
221
+ handler = AllSaj.new()
222
+ p = Oj::Parser.new(:saj)
223
+ p.handler = handler
224
+ p.file('saj_test.json')
225
+ assert_equal([
226
+ [:array_start, nil],
227
+ [:add_value, true, nil],
228
+ [:add_value, false, nil],
229
+ [:array_end, nil],
230
+ ], handler.calls)
231
+ end
232
+
233
+ def test_default
234
+ handler = AllSaj.new()
235
+ json = %|[true]|
236
+ Oj::Parser.saj.handler = handler
237
+ Oj::Parser.saj.parse(json)
238
+ assert_equal([
239
+ [:array_start, nil],
240
+ [:add_value, true, nil],
241
+ [:array_end, nil],
242
+ ], handler.calls)
243
+ end
244
+
245
+ end
@@ -0,0 +1,213 @@
1
+ #!/usr/bin/env ruby
2
+ # encoding: UTF-8
3
+
4
+ $: << File.dirname(__FILE__)
5
+
6
+ require 'helper'
7
+
8
+ class UsualTest < Minitest::Test
9
+
10
+ def test_nil
11
+ p = Oj::Parser.new(:usual)
12
+ doc = p.parse('nil')
13
+ assert_nil(doc)
14
+ end
15
+
16
+ def test_primitive
17
+ p = Oj::Parser.new(:usual)
18
+ [
19
+ ['true', true],
20
+ ['false', false],
21
+ ['123', 123],
22
+ ['1.25', 1.25],
23
+ ['"abc"', 'abc'],
24
+ ].each { |x|
25
+ doc = p.parse(x[0])
26
+ assert_equal(x[1], doc)
27
+ }
28
+ end
29
+
30
+ def test_big
31
+ p = Oj::Parser.new(:usual)
32
+ doc = p.parse('12345678901234567890123456789')
33
+ assert_equal(BigDecimal, doc.class)
34
+ doc = p.parse('1234567890.1234567890123456789')
35
+ assert_equal(BigDecimal, doc.class)
36
+ end
37
+
38
+ def test_array
39
+ p = Oj::Parser.new(:usual)
40
+ [
41
+ ['[]', []],
42
+ ['[false]', [false]],
43
+ ['[true,false]', [true,false]],
44
+ ['[[]]', [[]]],
45
+ ['[true,[],false]', [true,[],false]],
46
+ ['[true,[true],false]', [true,[true],false]],
47
+ ].each { |x|
48
+ doc = p.parse(x[0])
49
+ assert_equal(x[1], doc)
50
+ }
51
+ end
52
+
53
+ def test_hash
54
+ p = Oj::Parser.new(:usual)
55
+ [
56
+ ['{}', {}],
57
+ ['{"a": null}', {'a' => nil}],
58
+ ['{"t": true, "f": false, "s": "abc"}', {'t' => true, 'f' => false, 's' => 'abc'}],
59
+ ['{"a": {}}', {'a' => {}}],
60
+ ['{"a": {"b": 2}}', {'a' => {'b' => 2}}],
61
+ ['{"a": [true]}', {'a' => [true]}],
62
+ ].each { |x|
63
+ doc = p.parse(x[0])
64
+ assert_equal(x[1], doc)
65
+ }
66
+ end
67
+
68
+ def test_symbol_keys
69
+ p = Oj::Parser.new(:usual)
70
+ assert_equal(false, p.symbol_keys)
71
+ p.symbol_keys = true
72
+ doc = p.parse('{"a": true, "b": false}')
73
+ assert_equal({a: true, b: false}, doc)
74
+ end
75
+
76
+ def test_capacity
77
+ p = Oj::Parser.new(:usual)
78
+ p.capacity = 1000
79
+ assert_equal(4096, p.capacity)
80
+ p.capacity = 5000
81
+ assert_equal(5000, p.capacity)
82
+ end
83
+
84
+ def test_decimal
85
+ p = Oj::Parser.new(:usual)
86
+ assert_equal(:auto, p.decimal)
87
+ doc = p.parse('1.234567890123456789')
88
+ assert_equal(BigDecimal, doc.class)
89
+ assert_equal('0.1234567890123456789e1', doc.to_s)
90
+ doc = p.parse('1.25')
91
+ assert_equal(Float, doc.class)
92
+
93
+ p.decimal = :float
94
+ assert_equal(:float, p.decimal)
95
+ doc = p.parse('1.234567890123456789')
96
+ assert_equal(Float, doc.class)
97
+
98
+ p.decimal = :bigdecimal
99
+ assert_equal(:bigdecimal, p.decimal)
100
+ doc = p.parse('1.234567890123456789')
101
+ assert_equal(BigDecimal, doc.class)
102
+ doc = p.parse('1.25')
103
+ assert_equal(BigDecimal, doc.class)
104
+ assert_equal('0.125e1', doc.to_s)
105
+
106
+ p.decimal = :ruby
107
+ assert_equal(:ruby, p.decimal)
108
+ doc = p.parse('1.234567890123456789')
109
+ assert_equal(Float, doc.class)
110
+ end
111
+
112
+ def test_omit_null
113
+ p = Oj::Parser.new(:usual)
114
+ p.omit_null = true
115
+ doc = p.parse('{"a":true,"b":null}')
116
+ assert_equal({'a'=>true}, doc)
117
+
118
+ p.omit_null = false
119
+ doc = p.parse('{"a":true,"b":null}')
120
+ assert_equal({'a'=>true, 'b'=>nil}, doc)
121
+ end
122
+
123
+ class MyArray < Array
124
+ end
125
+
126
+ def test_array_class
127
+ p = Oj::Parser.new(:usual)
128
+ p.array_class = MyArray
129
+ assert_equal(MyArray, p.array_class)
130
+ doc = p.parse('[true]')
131
+ assert_equal(MyArray, doc.class)
132
+ end
133
+
134
+ class MyHash < Hash
135
+ end
136
+
137
+ def test_hash_class
138
+ p = Oj::Parser.new(:usual)
139
+ p.hash_class = MyHash
140
+ assert_equal(MyHash, p.hash_class)
141
+ doc = p.parse('{"a":true}')
142
+ assert_equal(MyHash, doc.class)
143
+ end
144
+
145
+ class MyClass
146
+ attr_accessor :a
147
+ attr_accessor :b
148
+
149
+ def to_s
150
+ "#{self.class}{a: #{@a} b: #{b}}"
151
+ end
152
+ end
153
+
154
+ class MyClass2 < MyClass
155
+ def self.json_create(arg)
156
+ obj = new
157
+ obj.a = arg['a']
158
+ obj.b = arg['b']
159
+ obj
160
+ end
161
+ end
162
+
163
+ def test_create_id
164
+ p = Oj::Parser.new(:usual)
165
+ p.create_id = '^'
166
+ doc = p.parse('{"a":true}')
167
+ assert_equal(Hash, doc.class)
168
+ doc = p.parse('{"a":true,"^":"UsualTest::MyClass","b":false}')
169
+ assert_equal('UsualTest::MyClass{a: true b: false}', doc.to_s)
170
+
171
+ doc = p.parse('{"a":true,"^":"UsualTest::MyClass2","b":false}')
172
+ assert_equal('UsualTest::MyClass2{a: true b: false}', doc.to_s)
173
+
174
+ p.hash_class = MyHash
175
+ assert_equal(MyHash, p.hash_class)
176
+ doc = p.parse('{"a":true}')
177
+ assert_equal(MyHash, doc.class)
178
+
179
+ doc = p.parse('{"a":true,"^":"UsualTest::MyClass","b":false}')
180
+ assert_equal('UsualTest::MyClass{a: true b: false}', doc.to_s)
181
+ end
182
+
183
+ def test_missing_class
184
+ p = Oj::Parser.new(:usual)
185
+ p.create_id = '^'
186
+ json = '{"a":true,"^":"Auto","b":false}'
187
+ doc = p.parse(json)
188
+ assert_equal(Hash, doc.class)
189
+
190
+ p.missing_class = :auto
191
+ doc = p.parse(json)
192
+ # Auto should be defined after parsing
193
+ assert_equal(Auto, doc.class)
194
+ end
195
+
196
+ def test_class_cache
197
+ p = Oj::Parser.new(:usual)
198
+ p.create_id = '^'
199
+ p.class_cache = true
200
+ p.missing_class = :auto
201
+ json = '{"a":true,"^":"Auto2","b":false}'
202
+ doc = p.parse(json)
203
+ assert_equal(Auto2, doc.class)
204
+
205
+ doc = p.parse(json)
206
+ assert_equal(Auto2, doc.class)
207
+ end
208
+
209
+ def test_default_parser
210
+ doc = Oj::Parser.usual.parse('{"a":true,"b":null}')
211
+ assert_equal({'a'=>true, 'b'=>nil}, doc)
212
+ end
213
+ end