json 2.4.0-java → 2.6.0-java
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/json/common.rb +34 -13
- data/lib/json/ext/generator.jar +0 -0
- data/lib/json/ext/parser.jar +0 -0
- data/lib/json/pure/parser.rb +1 -1
- data/lib/json/version.rb +1 -1
- data/lib/json.rb +1 -1
- metadata +6 -86
- data/tests/fixtures/fail10.json +0 -1
- data/tests/fixtures/fail11.json +0 -1
- data/tests/fixtures/fail12.json +0 -1
- data/tests/fixtures/fail13.json +0 -1
- data/tests/fixtures/fail14.json +0 -1
- data/tests/fixtures/fail18.json +0 -1
- data/tests/fixtures/fail19.json +0 -1
- data/tests/fixtures/fail2.json +0 -1
- data/tests/fixtures/fail20.json +0 -1
- data/tests/fixtures/fail21.json +0 -1
- data/tests/fixtures/fail22.json +0 -1
- data/tests/fixtures/fail23.json +0 -1
- data/tests/fixtures/fail24.json +0 -1
- data/tests/fixtures/fail25.json +0 -1
- data/tests/fixtures/fail27.json +0 -2
- data/tests/fixtures/fail28.json +0 -2
- data/tests/fixtures/fail29.json +0 -1
- data/tests/fixtures/fail3.json +0 -1
- data/tests/fixtures/fail30.json +0 -1
- data/tests/fixtures/fail31.json +0 -1
- data/tests/fixtures/fail32.json +0 -1
- data/tests/fixtures/fail4.json +0 -1
- data/tests/fixtures/fail5.json +0 -1
- data/tests/fixtures/fail6.json +0 -1
- data/tests/fixtures/fail7.json +0 -1
- data/tests/fixtures/fail8.json +0 -1
- data/tests/fixtures/fail9.json +0 -1
- data/tests/fixtures/obsolete_fail1.json +0 -1
- data/tests/fixtures/pass1.json +0 -56
- data/tests/fixtures/pass15.json +0 -1
- data/tests/fixtures/pass16.json +0 -1
- data/tests/fixtures/pass17.json +0 -1
- data/tests/fixtures/pass2.json +0 -1
- data/tests/fixtures/pass26.json +0 -1
- data/tests/fixtures/pass3.json +0 -6
- data/tests/json_addition_test.rb +0 -199
- data/tests/json_common_interface_test.rb +0 -169
- data/tests/json_encoding_test.rb +0 -107
- data/tests/json_ext_parser_test.rb +0 -15
- data/tests/json_fixtures_test.rb +0 -40
- data/tests/json_generator_test.rb +0 -432
- data/tests/json_generic_object_test.rb +0 -82
- data/tests/json_parser_test.rb +0 -497
- data/tests/json_string_matching_test.rb +0 -38
- data/tests/test_helper.rb +0 -17
@@ -1,432 +0,0 @@
|
|
1
|
-
#!/usr/bin/env ruby
|
2
|
-
# encoding: utf-8
|
3
|
-
# frozen_string_literal: false
|
4
|
-
|
5
|
-
require 'test_helper'
|
6
|
-
|
7
|
-
class JSONGeneratorTest < Test::Unit::TestCase
|
8
|
-
include JSON
|
9
|
-
|
10
|
-
def setup
|
11
|
-
@hash = {
|
12
|
-
'a' => 2,
|
13
|
-
'b' => 3.141,
|
14
|
-
'c' => 'c',
|
15
|
-
'd' => [ 1, "b", 3.14 ],
|
16
|
-
'e' => { 'foo' => 'bar' },
|
17
|
-
'g' => "\"\0\037",
|
18
|
-
'h' => 1000.0,
|
19
|
-
'i' => 0.001
|
20
|
-
}
|
21
|
-
@json2 = '{"a":2,"b":3.141,"c":"c","d":[1,"b",3.14],"e":{"foo":"bar"},' +
|
22
|
-
'"g":"\\"\\u0000\\u001f","h":1000.0,"i":0.001}'
|
23
|
-
@json3 = <<'EOT'.chomp
|
24
|
-
{
|
25
|
-
"a": 2,
|
26
|
-
"b": 3.141,
|
27
|
-
"c": "c",
|
28
|
-
"d": [
|
29
|
-
1,
|
30
|
-
"b",
|
31
|
-
3.14
|
32
|
-
],
|
33
|
-
"e": {
|
34
|
-
"foo": "bar"
|
35
|
-
},
|
36
|
-
"g": "\"\u0000\u001f",
|
37
|
-
"h": 1000.0,
|
38
|
-
"i": 0.001
|
39
|
-
}
|
40
|
-
EOT
|
41
|
-
end
|
42
|
-
|
43
|
-
def silence
|
44
|
-
v = $VERBOSE
|
45
|
-
$VERBOSE = nil
|
46
|
-
yield
|
47
|
-
ensure
|
48
|
-
$VERBOSE = v
|
49
|
-
end
|
50
|
-
|
51
|
-
def test_remove_const_segv
|
52
|
-
stress = GC.stress
|
53
|
-
const = JSON::SAFE_STATE_PROTOTYPE.dup
|
54
|
-
|
55
|
-
bignum_too_long_to_embed_as_string = 1234567890123456789012345
|
56
|
-
expect = bignum_too_long_to_embed_as_string.to_s
|
57
|
-
GC.stress = true
|
58
|
-
|
59
|
-
10.times do |i|
|
60
|
-
tmp = bignum_too_long_to_embed_as_string.to_json
|
61
|
-
raise "'\#{expect}' is expected, but '\#{tmp}'" unless tmp == expect
|
62
|
-
end
|
63
|
-
|
64
|
-
silence do
|
65
|
-
JSON.const_set :SAFE_STATE_PROTOTYPE, nil
|
66
|
-
end
|
67
|
-
|
68
|
-
10.times do |i|
|
69
|
-
assert_raise TypeError do
|
70
|
-
bignum_too_long_to_embed_as_string.to_json
|
71
|
-
end
|
72
|
-
end
|
73
|
-
ensure
|
74
|
-
GC.stress = stress
|
75
|
-
silence do
|
76
|
-
JSON.const_set :SAFE_STATE_PROTOTYPE, const
|
77
|
-
end
|
78
|
-
end if JSON.const_defined?("Ext") && RUBY_ENGINE != 'jruby'
|
79
|
-
|
80
|
-
def test_generate
|
81
|
-
json = generate(@hash)
|
82
|
-
assert_equal(parse(@json2), parse(json))
|
83
|
-
json = JSON[@hash]
|
84
|
-
assert_equal(parse(@json2), parse(json))
|
85
|
-
parsed_json = parse(json)
|
86
|
-
assert_equal(@hash, parsed_json)
|
87
|
-
json = generate({1=>2})
|
88
|
-
assert_equal('{"1":2}', json)
|
89
|
-
parsed_json = parse(json)
|
90
|
-
assert_equal({"1"=>2}, parsed_json)
|
91
|
-
assert_equal '666', generate(666)
|
92
|
-
end
|
93
|
-
|
94
|
-
def test_generate_pretty
|
95
|
-
json = pretty_generate({})
|
96
|
-
assert_equal(<<'EOT'.chomp, json)
|
97
|
-
{
|
98
|
-
}
|
99
|
-
EOT
|
100
|
-
json = pretty_generate(@hash)
|
101
|
-
# hashes aren't (insertion) ordered on every ruby implementation
|
102
|
-
# assert_equal(@json3, json)
|
103
|
-
assert_equal(parse(@json3), parse(json))
|
104
|
-
parsed_json = parse(json)
|
105
|
-
assert_equal(@hash, parsed_json)
|
106
|
-
json = pretty_generate({1=>2})
|
107
|
-
assert_equal(<<'EOT'.chomp, json)
|
108
|
-
{
|
109
|
-
"1": 2
|
110
|
-
}
|
111
|
-
EOT
|
112
|
-
parsed_json = parse(json)
|
113
|
-
assert_equal({"1"=>2}, parsed_json)
|
114
|
-
assert_equal '666', pretty_generate(666)
|
115
|
-
end
|
116
|
-
|
117
|
-
def test_generate_custom
|
118
|
-
state = State.new(:space_before => " ", :space => " ", :indent => "<i>", :object_nl => "\n", :array_nl => "<a_nl>")
|
119
|
-
json = generate({1=>{2=>3,4=>[5,6]}}, state)
|
120
|
-
assert_equal(<<'EOT'.chomp, json)
|
121
|
-
{
|
122
|
-
<i>"1" : {
|
123
|
-
<i><i>"2" : 3,
|
124
|
-
<i><i>"4" : [<a_nl><i><i><i>5,<a_nl><i><i><i>6<a_nl><i><i>]
|
125
|
-
<i>}
|
126
|
-
}
|
127
|
-
EOT
|
128
|
-
end
|
129
|
-
|
130
|
-
def test_fast_generate
|
131
|
-
json = fast_generate(@hash)
|
132
|
-
assert_equal(parse(@json2), parse(json))
|
133
|
-
parsed_json = parse(json)
|
134
|
-
assert_equal(@hash, parsed_json)
|
135
|
-
json = fast_generate({1=>2})
|
136
|
-
assert_equal('{"1":2}', json)
|
137
|
-
parsed_json = parse(json)
|
138
|
-
assert_equal({"1"=>2}, parsed_json)
|
139
|
-
assert_equal '666', fast_generate(666)
|
140
|
-
end
|
141
|
-
|
142
|
-
def test_own_state
|
143
|
-
state = State.new
|
144
|
-
json = generate(@hash, state)
|
145
|
-
assert_equal(parse(@json2), parse(json))
|
146
|
-
parsed_json = parse(json)
|
147
|
-
assert_equal(@hash, parsed_json)
|
148
|
-
json = generate({1=>2}, state)
|
149
|
-
assert_equal('{"1":2}', json)
|
150
|
-
parsed_json = parse(json)
|
151
|
-
assert_equal({"1"=>2}, parsed_json)
|
152
|
-
assert_equal '666', generate(666, state)
|
153
|
-
end
|
154
|
-
|
155
|
-
def test_states
|
156
|
-
json = generate({1=>2}, nil)
|
157
|
-
assert_equal('{"1":2}', json)
|
158
|
-
s = JSON.state.new
|
159
|
-
assert s.check_circular?
|
160
|
-
assert s[:check_circular?]
|
161
|
-
h = { 1=>2 }
|
162
|
-
h[3] = h
|
163
|
-
assert_raise(JSON::NestingError) { generate(h) }
|
164
|
-
assert_raise(JSON::NestingError) { generate(h, s) }
|
165
|
-
s = JSON.state.new
|
166
|
-
a = [ 1, 2 ]
|
167
|
-
a << a
|
168
|
-
assert_raise(JSON::NestingError) { generate(a, s) }
|
169
|
-
assert s.check_circular?
|
170
|
-
assert s[:check_circular?]
|
171
|
-
end
|
172
|
-
|
173
|
-
def test_pretty_state
|
174
|
-
state = PRETTY_STATE_PROTOTYPE.dup
|
175
|
-
assert_equal({
|
176
|
-
:allow_nan => false,
|
177
|
-
:array_nl => "\n",
|
178
|
-
:ascii_only => false,
|
179
|
-
:buffer_initial_length => 1024,
|
180
|
-
:depth => 0,
|
181
|
-
:escape_slash => false,
|
182
|
-
:indent => " ",
|
183
|
-
:max_nesting => 100,
|
184
|
-
:object_nl => "\n",
|
185
|
-
:space => " ",
|
186
|
-
:space_before => "",
|
187
|
-
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
|
188
|
-
end
|
189
|
-
|
190
|
-
def test_safe_state
|
191
|
-
state = SAFE_STATE_PROTOTYPE.dup
|
192
|
-
assert_equal({
|
193
|
-
:allow_nan => false,
|
194
|
-
:array_nl => "",
|
195
|
-
:ascii_only => false,
|
196
|
-
:buffer_initial_length => 1024,
|
197
|
-
:depth => 0,
|
198
|
-
:escape_slash => false,
|
199
|
-
:indent => "",
|
200
|
-
:max_nesting => 100,
|
201
|
-
:object_nl => "",
|
202
|
-
:space => "",
|
203
|
-
:space_before => "",
|
204
|
-
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
|
205
|
-
end
|
206
|
-
|
207
|
-
def test_fast_state
|
208
|
-
state = FAST_STATE_PROTOTYPE.dup
|
209
|
-
assert_equal({
|
210
|
-
:allow_nan => false,
|
211
|
-
:array_nl => "",
|
212
|
-
:ascii_only => false,
|
213
|
-
:buffer_initial_length => 1024,
|
214
|
-
:depth => 0,
|
215
|
-
:escape_slash => false,
|
216
|
-
:indent => "",
|
217
|
-
:max_nesting => 0,
|
218
|
-
:object_nl => "",
|
219
|
-
:space => "",
|
220
|
-
:space_before => "",
|
221
|
-
}.sort_by { |n,| n.to_s }, state.to_h.sort_by { |n,| n.to_s })
|
222
|
-
end
|
223
|
-
|
224
|
-
def test_allow_nan
|
225
|
-
assert_raise(GeneratorError) { generate([JSON::NaN]) }
|
226
|
-
assert_equal '[NaN]', generate([JSON::NaN], :allow_nan => true)
|
227
|
-
assert_raise(GeneratorError) { fast_generate([JSON::NaN]) }
|
228
|
-
assert_raise(GeneratorError) { pretty_generate([JSON::NaN]) }
|
229
|
-
assert_equal "[\n NaN\n]", pretty_generate([JSON::NaN], :allow_nan => true)
|
230
|
-
assert_raise(GeneratorError) { generate([JSON::Infinity]) }
|
231
|
-
assert_equal '[Infinity]', generate([JSON::Infinity], :allow_nan => true)
|
232
|
-
assert_raise(GeneratorError) { fast_generate([JSON::Infinity]) }
|
233
|
-
assert_raise(GeneratorError) { pretty_generate([JSON::Infinity]) }
|
234
|
-
assert_equal "[\n Infinity\n]", pretty_generate([JSON::Infinity], :allow_nan => true)
|
235
|
-
assert_raise(GeneratorError) { generate([JSON::MinusInfinity]) }
|
236
|
-
assert_equal '[-Infinity]', generate([JSON::MinusInfinity], :allow_nan => true)
|
237
|
-
assert_raise(GeneratorError) { fast_generate([JSON::MinusInfinity]) }
|
238
|
-
assert_raise(GeneratorError) { pretty_generate([JSON::MinusInfinity]) }
|
239
|
-
assert_equal "[\n -Infinity\n]", pretty_generate([JSON::MinusInfinity], :allow_nan => true)
|
240
|
-
end
|
241
|
-
|
242
|
-
def test_depth
|
243
|
-
ary = []; ary << ary
|
244
|
-
assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
|
245
|
-
assert_raise(JSON::NestingError) { generate(ary) }
|
246
|
-
assert_equal 0, JSON::SAFE_STATE_PROTOTYPE.depth
|
247
|
-
assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
|
248
|
-
assert_raise(JSON::NestingError) { JSON.pretty_generate(ary) }
|
249
|
-
assert_equal 0, JSON::PRETTY_STATE_PROTOTYPE.depth
|
250
|
-
s = JSON.state.new
|
251
|
-
assert_equal 0, s.depth
|
252
|
-
assert_raise(JSON::NestingError) { ary.to_json(s) }
|
253
|
-
assert_equal 100, s.depth
|
254
|
-
end
|
255
|
-
|
256
|
-
def test_buffer_initial_length
|
257
|
-
s = JSON.state.new
|
258
|
-
assert_equal 1024, s.buffer_initial_length
|
259
|
-
s.buffer_initial_length = 0
|
260
|
-
assert_equal 1024, s.buffer_initial_length
|
261
|
-
s.buffer_initial_length = -1
|
262
|
-
assert_equal 1024, s.buffer_initial_length
|
263
|
-
s.buffer_initial_length = 128
|
264
|
-
assert_equal 128, s.buffer_initial_length
|
265
|
-
end
|
266
|
-
|
267
|
-
def test_gc
|
268
|
-
if respond_to?(:assert_in_out_err)
|
269
|
-
assert_in_out_err(%w[-rjson --disable-gems], <<-EOS, [], [])
|
270
|
-
bignum_too_long_to_embed_as_string = 1234567890123456789012345
|
271
|
-
expect = bignum_too_long_to_embed_as_string.to_s
|
272
|
-
GC.stress = true
|
273
|
-
|
274
|
-
10.times do |i|
|
275
|
-
tmp = bignum_too_long_to_embed_as_string.to_json
|
276
|
-
raise "'\#{expect}' is expected, but '\#{tmp}'" unless tmp == expect
|
277
|
-
end
|
278
|
-
EOS
|
279
|
-
end
|
280
|
-
end if GC.respond_to?(:stress=)
|
281
|
-
|
282
|
-
def test_configure_using_configure_and_merge
|
283
|
-
numbered_state = {
|
284
|
-
:indent => "1",
|
285
|
-
:space => '2',
|
286
|
-
:space_before => '3',
|
287
|
-
:object_nl => '4',
|
288
|
-
:array_nl => '5'
|
289
|
-
}
|
290
|
-
state1 = JSON.state.new
|
291
|
-
state1.merge(numbered_state)
|
292
|
-
assert_equal '1', state1.indent
|
293
|
-
assert_equal '2', state1.space
|
294
|
-
assert_equal '3', state1.space_before
|
295
|
-
assert_equal '4', state1.object_nl
|
296
|
-
assert_equal '5', state1.array_nl
|
297
|
-
state2 = JSON.state.new
|
298
|
-
state2.configure(numbered_state)
|
299
|
-
assert_equal '1', state2.indent
|
300
|
-
assert_equal '2', state2.space
|
301
|
-
assert_equal '3', state2.space_before
|
302
|
-
assert_equal '4', state2.object_nl
|
303
|
-
assert_equal '5', state2.array_nl
|
304
|
-
end
|
305
|
-
|
306
|
-
def test_configure_hash_conversion
|
307
|
-
state = JSON.state.new
|
308
|
-
state.configure(:indent => '1')
|
309
|
-
assert_equal '1', state.indent
|
310
|
-
state = JSON.state.new
|
311
|
-
foo = 'foo'
|
312
|
-
assert_raise(TypeError) do
|
313
|
-
state.configure(foo)
|
314
|
-
end
|
315
|
-
def foo.to_h
|
316
|
-
{ :indent => '2' }
|
317
|
-
end
|
318
|
-
state.configure(foo)
|
319
|
-
assert_equal '2', state.indent
|
320
|
-
end
|
321
|
-
|
322
|
-
if defined?(JSON::Ext::Generator)
|
323
|
-
def test_broken_bignum # [ruby-core:38867]
|
324
|
-
pid = fork do
|
325
|
-
x = 1 << 64
|
326
|
-
x.class.class_eval do
|
327
|
-
def to_s
|
328
|
-
end
|
329
|
-
end
|
330
|
-
begin
|
331
|
-
JSON::Ext::Generator::State.new.generate(x)
|
332
|
-
exit 1
|
333
|
-
rescue TypeError
|
334
|
-
exit 0
|
335
|
-
end
|
336
|
-
end
|
337
|
-
_, status = Process.waitpid2(pid)
|
338
|
-
assert status.success?
|
339
|
-
rescue NotImplementedError
|
340
|
-
# forking to avoid modifying core class of a parent process and
|
341
|
-
# introducing race conditions of tests are run in parallel
|
342
|
-
end
|
343
|
-
end
|
344
|
-
|
345
|
-
def test_hash_likeness_set_symbol
|
346
|
-
state = JSON.state.new
|
347
|
-
assert_equal nil, state[:foo]
|
348
|
-
assert_equal nil.class, state[:foo].class
|
349
|
-
assert_equal nil, state['foo']
|
350
|
-
state[:foo] = :bar
|
351
|
-
assert_equal :bar, state[:foo]
|
352
|
-
assert_equal :bar, state['foo']
|
353
|
-
state_hash = state.to_hash
|
354
|
-
assert_kind_of Hash, state_hash
|
355
|
-
assert_equal :bar, state_hash[:foo]
|
356
|
-
end
|
357
|
-
|
358
|
-
def test_hash_likeness_set_string
|
359
|
-
state = JSON.state.new
|
360
|
-
assert_equal nil, state[:foo]
|
361
|
-
assert_equal nil, state['foo']
|
362
|
-
state['foo'] = :bar
|
363
|
-
assert_equal :bar, state[:foo]
|
364
|
-
assert_equal :bar, state['foo']
|
365
|
-
state_hash = state.to_hash
|
366
|
-
assert_kind_of Hash, state_hash
|
367
|
-
assert_equal :bar, state_hash[:foo]
|
368
|
-
end
|
369
|
-
|
370
|
-
def test_json_generate
|
371
|
-
assert_raise JSON::GeneratorError do
|
372
|
-
assert_equal true, generate(["\xea"])
|
373
|
-
end
|
374
|
-
end
|
375
|
-
|
376
|
-
def test_nesting
|
377
|
-
too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
|
378
|
-
too_deep_ary = eval too_deep
|
379
|
-
assert_raise(JSON::NestingError) { generate too_deep_ary }
|
380
|
-
assert_raise(JSON::NestingError) { generate too_deep_ary, :max_nesting => 100 }
|
381
|
-
ok = generate too_deep_ary, :max_nesting => 101
|
382
|
-
assert_equal too_deep, ok
|
383
|
-
ok = generate too_deep_ary, :max_nesting => nil
|
384
|
-
assert_equal too_deep, ok
|
385
|
-
ok = generate too_deep_ary, :max_nesting => false
|
386
|
-
assert_equal too_deep, ok
|
387
|
-
ok = generate too_deep_ary, :max_nesting => 0
|
388
|
-
assert_equal too_deep, ok
|
389
|
-
end
|
390
|
-
|
391
|
-
def test_backslash
|
392
|
-
data = [ '\\.(?i:gif|jpe?g|png)$' ]
|
393
|
-
json = '["\\\\.(?i:gif|jpe?g|png)$"]'
|
394
|
-
assert_equal json, generate(data)
|
395
|
-
#
|
396
|
-
data = [ '\\"' ]
|
397
|
-
json = '["\\\\\""]'
|
398
|
-
assert_equal json, generate(data)
|
399
|
-
#
|
400
|
-
data = [ '/' ]
|
401
|
-
json = '["/"]'
|
402
|
-
assert_equal json, generate(data)
|
403
|
-
#
|
404
|
-
data = [ '/' ]
|
405
|
-
json = '["\/"]'
|
406
|
-
assert_equal json, generate(data, :escape_slash => true)
|
407
|
-
#
|
408
|
-
data = ['"']
|
409
|
-
json = '["\""]'
|
410
|
-
assert_equal json, generate(data)
|
411
|
-
#
|
412
|
-
data = ["'"]
|
413
|
-
json = '["\\\'"]'
|
414
|
-
assert_equal '["\'"]', generate(data)
|
415
|
-
end
|
416
|
-
|
417
|
-
def test_string_subclass
|
418
|
-
s = Class.new(String) do
|
419
|
-
def to_s; self; end
|
420
|
-
undef to_json
|
421
|
-
end
|
422
|
-
assert_nothing_raised(SystemStackError) do
|
423
|
-
assert_equal '["foo"]', JSON.generate([s.new('foo')])
|
424
|
-
end
|
425
|
-
end
|
426
|
-
|
427
|
-
if defined?(Encoding)
|
428
|
-
def test_nonutf8_encoding
|
429
|
-
assert_equal("\"5\u{b0}\"", "5\xb0".force_encoding("iso-8859-1").to_json)
|
430
|
-
end
|
431
|
-
end
|
432
|
-
end
|
@@ -1,82 +0,0 @@
|
|
1
|
-
#frozen_string_literal: false
|
2
|
-
require 'test_helper'
|
3
|
-
|
4
|
-
class JSONGenericObjectTest < Test::Unit::TestCase
|
5
|
-
include JSON
|
6
|
-
|
7
|
-
def setup
|
8
|
-
@go = GenericObject[ :a => 1, :b => 2 ]
|
9
|
-
end
|
10
|
-
|
11
|
-
def test_attributes
|
12
|
-
assert_equal 1, @go.a
|
13
|
-
assert_equal 1, @go[:a]
|
14
|
-
assert_equal 2, @go.b
|
15
|
-
assert_equal 2, @go[:b]
|
16
|
-
assert_nil @go.c
|
17
|
-
assert_nil @go[:c]
|
18
|
-
end
|
19
|
-
|
20
|
-
def test_generate_json
|
21
|
-
switch_json_creatable do
|
22
|
-
assert_equal @go, JSON(JSON(@go), :create_additions => true)
|
23
|
-
end
|
24
|
-
end
|
25
|
-
|
26
|
-
def test_parse_json
|
27
|
-
assert_kind_of Hash,
|
28
|
-
JSON(
|
29
|
-
'{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }',
|
30
|
-
:create_additions => true
|
31
|
-
)
|
32
|
-
switch_json_creatable do
|
33
|
-
assert_equal @go, l =
|
34
|
-
JSON(
|
35
|
-
'{ "json_class": "JSON::GenericObject", "a": 1, "b": 2 }',
|
36
|
-
:create_additions => true
|
37
|
-
)
|
38
|
-
assert_equal 1, l.a
|
39
|
-
assert_equal @go,
|
40
|
-
l = JSON('{ "a": 1, "b": 2 }', :object_class => GenericObject)
|
41
|
-
assert_equal 1, l.a
|
42
|
-
assert_equal GenericObject[:a => GenericObject[:b => 2]],
|
43
|
-
l = JSON('{ "a": { "b": 2 } }', :object_class => GenericObject)
|
44
|
-
assert_equal 2, l.a.b
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_from_hash
|
49
|
-
result = GenericObject.from_hash(
|
50
|
-
:foo => { :bar => { :baz => true }, :quux => [ { :foobar => true } ] })
|
51
|
-
assert_kind_of GenericObject, result.foo
|
52
|
-
assert_kind_of GenericObject, result.foo.bar
|
53
|
-
assert_equal true, result.foo.bar.baz
|
54
|
-
assert_kind_of GenericObject, result.foo.quux.first
|
55
|
-
assert_equal true, result.foo.quux.first.foobar
|
56
|
-
assert_equal true, GenericObject.from_hash(true)
|
57
|
-
end
|
58
|
-
|
59
|
-
def test_json_generic_object_load
|
60
|
-
empty = JSON::GenericObject.load(nil)
|
61
|
-
assert_kind_of JSON::GenericObject, empty
|
62
|
-
simple_json = '{"json_class":"JSON::GenericObject","hello":"world"}'
|
63
|
-
simple = JSON::GenericObject.load(simple_json)
|
64
|
-
assert_kind_of JSON::GenericObject, simple
|
65
|
-
assert_equal "world", simple.hello
|
66
|
-
converting = JSON::GenericObject.load('{ "hello": "world" }')
|
67
|
-
assert_kind_of JSON::GenericObject, converting
|
68
|
-
assert_equal "world", converting.hello
|
69
|
-
|
70
|
-
json = JSON::GenericObject.dump(JSON::GenericObject[:hello => 'world'])
|
71
|
-
assert_equal JSON(json), JSON('{"json_class":"JSON::GenericObject","hello":"world"}')
|
72
|
-
end
|
73
|
-
|
74
|
-
private
|
75
|
-
|
76
|
-
def switch_json_creatable
|
77
|
-
JSON::GenericObject.json_creatable = true
|
78
|
-
yield
|
79
|
-
ensure
|
80
|
-
JSON::GenericObject.json_creatable = false
|
81
|
-
end
|
82
|
-
end
|