oj 3.16.9 → 3.16.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +5 -0
  3. data/ext/oj/fast.c +3 -6
  4. data/ext/oj/mimic_json.c +3 -6
  5. data/ext/oj/oj.c +2 -4
  6. data/ext/oj/parse.c +15 -5
  7. data/ext/oj/parser.c +1 -1
  8. data/ext/oj/parser.h +2 -0
  9. data/ext/oj/rails.c +1 -2
  10. data/ext/oj/saj.c +3 -6
  11. data/ext/oj/scp.c +3 -6
  12. data/ext/oj/stream_writer.c +1 -7
  13. data/ext/oj/strict.c +2 -4
  14. data/ext/oj/string_writer.c +1 -3
  15. data/lib/oj/version.rb +1 -1
  16. metadata +3 -98
  17. data/test/_test_active.rb +0 -75
  18. data/test/_test_active_mimic.rb +0 -95
  19. data/test/_test_mimic_rails.rb +0 -123
  20. data/test/activerecord/result_test.rb +0 -31
  21. data/test/activesupport6/abstract_unit.rb +0 -44
  22. data/test/activesupport6/decoding_test.rb +0 -133
  23. data/test/activesupport6/encoding_test.rb +0 -542
  24. data/test/activesupport6/encoding_test_cases.rb +0 -98
  25. data/test/activesupport6/test_common.rb +0 -17
  26. data/test/activesupport6/test_helper.rb +0 -163
  27. data/test/activesupport6/time_zone_test_helpers.rb +0 -39
  28. data/test/activesupport7/abstract_unit.rb +0 -52
  29. data/test/activesupport7/decoding_test.rb +0 -125
  30. data/test/activesupport7/encoding_test.rb +0 -536
  31. data/test/activesupport7/encoding_test_cases.rb +0 -104
  32. data/test/activesupport7/time_zone_test_helpers.rb +0 -47
  33. data/test/files.rb +0 -29
  34. data/test/foo.rb +0 -26
  35. data/test/helper.rb +0 -39
  36. data/test/isolated/shared.rb +0 -309
  37. data/test/isolated/test_mimic_after.rb +0 -13
  38. data/test/isolated/test_mimic_alone.rb +0 -12
  39. data/test/isolated/test_mimic_as_json.rb +0 -45
  40. data/test/isolated/test_mimic_before.rb +0 -13
  41. data/test/isolated/test_mimic_define.rb +0 -28
  42. data/test/isolated/test_mimic_rails_after.rb +0 -22
  43. data/test/isolated/test_mimic_rails_before.rb +0 -21
  44. data/test/isolated/test_mimic_redefine.rb +0 -15
  45. data/test/json_gem/json_addition_test.rb +0 -216
  46. data/test/json_gem/json_common_interface_test.rb +0 -155
  47. data/test/json_gem/json_encoding_test.rb +0 -107
  48. data/test/json_gem/json_ext_parser_test.rb +0 -21
  49. data/test/json_gem/json_fixtures_test.rb +0 -36
  50. data/test/json_gem/json_generator_test.rb +0 -413
  51. data/test/json_gem/json_generic_object_test.rb +0 -90
  52. data/test/json_gem/json_parser_test.rb +0 -477
  53. data/test/json_gem/json_string_matching_test.rb +0 -42
  54. data/test/json_gem/test_helper.rb +0 -30
  55. data/test/mem.rb +0 -34
  56. data/test/perf.rb +0 -102
  57. data/test/perf_compat.rb +0 -128
  58. data/test/perf_dump.rb +0 -50
  59. data/test/perf_fast.rb +0 -162
  60. data/test/perf_file.rb +0 -62
  61. data/test/perf_object.rb +0 -134
  62. data/test/perf_once.rb +0 -59
  63. data/test/perf_parser.rb +0 -183
  64. data/test/perf_saj.rb +0 -101
  65. data/test/perf_scp.rb +0 -140
  66. data/test/perf_simple.rb +0 -289
  67. data/test/perf_strict.rb +0 -137
  68. data/test/perf_wab.rb +0 -129
  69. data/test/prec.rb +0 -23
  70. data/test/sample/change.rb +0 -13
  71. data/test/sample/dir.rb +0 -18
  72. data/test/sample/doc.rb +0 -35
  73. data/test/sample/file.rb +0 -47
  74. data/test/sample/group.rb +0 -15
  75. data/test/sample/hasprops.rb +0 -15
  76. data/test/sample/layer.rb +0 -11
  77. data/test/sample/line.rb +0 -20
  78. data/test/sample/oval.rb +0 -10
  79. data/test/sample/rect.rb +0 -9
  80. data/test/sample/shape.rb +0 -34
  81. data/test/sample/text.rb +0 -19
  82. data/test/sample.rb +0 -54
  83. data/test/sample_json.rb +0 -37
  84. data/test/test_compat.rb +0 -567
  85. data/test/test_custom.rb +0 -555
  86. data/test/test_debian.rb +0 -50
  87. data/test/test_fast.rb +0 -526
  88. data/test/test_file.rb +0 -250
  89. data/test/test_gc.rb +0 -60
  90. data/test/test_generate.rb +0 -21
  91. data/test/test_hash.rb +0 -39
  92. data/test/test_integer_range.rb +0 -72
  93. data/test/test_null.rb +0 -376
  94. data/test/test_object.rb +0 -1030
  95. data/test/test_parser.rb +0 -11
  96. data/test/test_parser_debug.rb +0 -27
  97. data/test/test_parser_saj.rb +0 -337
  98. data/test/test_parser_usual.rb +0 -255
  99. data/test/test_rails.rb +0 -35
  100. data/test/test_saj.rb +0 -188
  101. data/test/test_scp.rb +0 -431
  102. data/test/test_strict.rb +0 -441
  103. data/test/test_various.rb +0 -801
  104. data/test/test_wab.rb +0 -311
  105. data/test/test_writer.rb +0 -396
  106. data/test/tests.rb +0 -33
  107. data/test/tests_mimic.rb +0 -23
  108. data/test/tests_mimic_addition.rb +0 -16
@@ -1,477 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
-
4
- # frozen_string_literal: false
5
-
6
- require 'json_gem/test_helper'
7
-
8
- require 'stringio'
9
- require 'tempfile'
10
- require 'ostruct'
11
-
12
- class JSONParserTest < Test::Unit::TestCase
13
- include Test::Unit::TestCaseOmissionSupport
14
-
15
- def test_construction
16
- parser = JSON::Parser.new('test')
17
- assert_equal 'test', parser.source
18
- end
19
-
20
- def test_argument_encoding
21
- source = "{}".encode("UTF-16")
22
- JSON::Parser.new(source)
23
- assert_equal Encoding::UTF_16, source.encoding
24
- end if defined?(Encoding::UTF_16)
25
-
26
- def test_error_message_encoding
27
- bug10705 = '[ruby-core:67386] [Bug #10705]'
28
- json = ".\"\xE2\x88\x9A\"".force_encoding(Encoding::UTF_8)
29
- e = assert_raise(JSON::ParserError) {
30
- JSON::Ext::Parser.new(json).parse
31
- }
32
- assert_equal(Encoding::UTF_8, e.message.encoding, bug10705)
33
- assert_include(e.message, json, bug10705)
34
- end if defined?(Encoding::UTF_8) and defined?(JSON::Ext::Parser)
35
-
36
- def test_parsing
37
- parser = JSON::Parser.new('"test"')
38
- assert_equal 'test', parser.parse
39
- end
40
-
41
- def test_parser_reset
42
- parser = JSON::Parser.new('{"a":"b"}')
43
- assert_equal({ 'a' => 'b' }, parser.parse)
44
- assert_equal({ 'a' => 'b' }, parser.parse)
45
- end
46
-
47
- def test_parse_values
48
- assert_equal(nil, JSON.parse('null'))
49
- assert_equal(false, JSON.parse('false'))
50
- assert_equal(true, JSON.parse('true'))
51
- assert_equal(-23, JSON.parse('-23'))
52
- assert_equal(23, JSON.parse('23'))
53
- assert_in_delta(0.23, JSON.parse('0.23'), 1e-2)
54
- assert_in_delta(0.0, JSON.parse('0e0'), 1e-2)
55
- assert_equal("", JSON.parse('""'))
56
- assert_equal("foobar", JSON.parse('"foobar"'))
57
- end
58
-
59
- def test_parse_simple_arrays
60
- assert_equal([], JSON.parse('[]'))
61
- assert_equal([], JSON.parse(' [ ] '))
62
- assert_equal([ nil ], JSON.parse('[null]'))
63
- assert_equal([ false ], JSON.parse('[false]'))
64
- assert_equal([ true ], JSON.parse('[true]'))
65
- assert_equal([ -23 ], JSON.parse('[-23]'))
66
- assert_equal([ 23 ], JSON.parse('[23]'))
67
- assert_equal_float([ 0.23 ], JSON.parse('[0.23]'))
68
- assert_equal_float([ 0.0 ], JSON.parse('[0e0]'))
69
- assert_equal([""], JSON.parse('[""]'))
70
- assert_equal(["foobar"], JSON.parse('["foobar"]'))
71
- assert_equal([{}], JSON.parse('[{}]'))
72
- end
73
-
74
- def test_parse_simple_objects
75
- assert_equal({}, JSON.parse('{}'))
76
- assert_equal({}, JSON.parse(' { } '))
77
- assert_equal({ "a" => nil }, JSON.parse('{ "a" : null}'))
78
- assert_equal({ "a" => nil }, JSON.parse('{"a":null}'))
79
- assert_equal({ "a" => false }, JSON.parse('{ "a" : false } '))
80
- assert_equal({ "a" => false }, JSON.parse('{"a":false}'))
81
- assert_raise(JSON::ParserError) { JSON.parse('{false}') }
82
- assert_equal({ "a" => true }, JSON.parse('{"a":true}'))
83
- assert_equal({ "a" => true }, JSON.parse(' { "a" : true } '))
84
- assert_equal({ "a" => -23 }, JSON.parse(' { "a" : -23 } '))
85
- assert_equal({ "a" => -23 }, JSON.parse(' { "a" : -23 } '))
86
- assert_equal({ "a" => 23 }, JSON.parse('{"a":23 } '))
87
- assert_equal({ "a" => 23 }, JSON.parse(' { "a" : 23 } '))
88
- assert_equal({ "a" => 0.23 }, JSON.parse(' { "a" : 0.23 } '))
89
- assert_equal({ "a" => 0.23 }, JSON.parse(' { "a" : 0.23 } '))
90
- end
91
-
92
- def test_parse_numbers
93
- assert_raise(JSON::ParserError) { JSON.parse('+23.2') }
94
- assert_raise(JSON::ParserError) { JSON.parse('+23') }
95
- assert_raise(JSON::ParserError) { JSON.parse('.23') }
96
- assert_raise(JSON::ParserError) { JSON.parse('023') }
97
- assert_equal 23, JSON.parse('23')
98
- assert_equal(-23, JSON.parse('-23'))
99
- assert_equal_float 3.141, JSON.parse('3.141')
100
- assert_equal_float(-3.141, JSON.parse('-3.141'))
101
- assert_equal_float 3.141, JSON.parse('3141e-3')
102
- assert_equal_float 3.141, JSON.parse('3141.1e-3')
103
- assert_equal_float 3.141, JSON.parse('3141E-3')
104
- assert_equal_float 3.141, JSON.parse('3141.0E-3')
105
- assert_equal_float(-3.141, JSON.parse('-3141.0e-3'))
106
- assert_equal_float(-3.141, JSON.parse('-3141e-3'))
107
- assert_raise(JSON::ParserError) { JSON.parse('NaN') }
108
- assert JSON.parse('NaN', :allow_nan => true).nan?
109
- assert_raise(JSON::ParserError) { JSON.parse('Infinity') }
110
- assert_equal 1.0/0, JSON.parse('Infinity', :allow_nan => true)
111
- assert_raise(JSON::ParserError) { JSON.parse('-Infinity') }
112
- assert_equal(-1.0/0, JSON.parse('-Infinity', :allow_nan => true))
113
- end
114
-
115
- if Array.method_defined?(:permutation)
116
- def test_parse_more_complex_arrays
117
- a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
118
- a.permutation.each do |perm|
119
- json = JSON.pretty_generate(perm)
120
- assert_equal perm, JSON.parse(json)
121
- end
122
- end
123
-
124
- def test_parse_complex_objects
125
- a = [ nil, false, true, "foßbar", [ "n€st€d", true ], { "nested" => true, "n€ßt€ð2" => {} }]
126
- a.permutation.each do |perm|
127
- s = "a"
128
- orig_obj = perm.inject({}) { |h, x| h[s.dup] = x; s = s.succ; h }
129
- json = JSON.pretty_generate(orig_obj)
130
- assert_equal orig_obj, JSON.parse(json)
131
- end
132
- end
133
- end
134
-
135
- def test_parse_arrays
136
- assert_equal([1, 2, 3], JSON.parse('[1,2,3]'))
137
- assert_equal([1.2, 2, 3], JSON.parse('[1.2,2,3]'))
138
- assert_equal([[], [[], []]], JSON.parse('[[],[[],[]]]'))
139
- assert_equal([], JSON.parse('[]'))
140
- assert_equal([], JSON.parse(' [ ] '))
141
- assert_equal([1], JSON.parse('[1]'))
142
- assert_equal([1], JSON.parse(' [ 1 ] '))
143
- ary = [[1], ["foo"], [3.14], [4711.0], [2.718], [nil],
144
- [[1, -2, 3]], [false], [true]]
145
- assert_equal(ary,
146
- JSON.parse('[[1],["foo"],[3.14],[47.11e+2],[2718.0E-3],[null],[[1,-2,3]],[false],[true]]'))
147
- assert_equal(ary, JSON.parse(%Q{ [ [1] , ["foo"] , [3.14] \t , [47.11e+2]\s
148
- , [2718.0E-3 ],\r[ null] , [[1, -2, 3 ]], [false ],[ true]\n ] }))
149
- end
150
-
151
- def test_parse_json_primitive_values
152
- assert_raise(JSON::ParserError) { JSON.parse('') }
153
- assert_raise(TypeError) { JSON.parse(nil) }
154
- assert_raise(JSON::ParserError) { JSON.parse(' /* foo */ ') }
155
- assert_equal nil, JSON.parse('null')
156
- assert_equal false, JSON.parse('false')
157
- assert_equal true, JSON.parse('true')
158
- assert_equal 23, JSON.parse('23')
159
- assert_equal 1, JSON.parse('1')
160
- assert_equal_float 3.141, JSON.parse('3.141'), 1E-3
161
- assert_equal 2 ** 64, JSON.parse('18446744073709551616')
162
- assert_equal 'foo', JSON.parse('"foo"')
163
- assert JSON.parse('NaN', :allow_nan => true).nan?
164
- assert JSON.parse('Infinity', :allow_nan => true).infinite?
165
- assert JSON.parse('-Infinity', :allow_nan => true).infinite?
166
- assert_raise(JSON::ParserError) { JSON.parse('[ 1, ]') }
167
- end
168
-
169
- def test_parse_some_strings
170
- assert_equal([""], JSON.parse('[""]'))
171
- assert_equal(["\\"], JSON.parse('["\\\\"]'))
172
- assert_equal(['"'], JSON.parse('["\""]'))
173
- assert_equal(['\\"\\'], JSON.parse('["\\\\\\"\\\\"]'))
174
- assert_equal(
175
- ["\"\b\n\r\t\0\037"],
176
- JSON.parse('["\"\b\n\r\t\u0000\u001f"]')
177
- )
178
- end
179
-
180
- def test_parse_big_integers
181
- json1 = JSON(orig = (1 << 31) - 1)
182
- assert_equal orig, JSON.parse(json1)
183
- json2 = JSON(orig = 1 << 31)
184
- assert_equal orig, JSON.parse(json2)
185
- json3 = JSON(orig = (1 << 62) - 1)
186
- assert_equal orig, JSON.parse(json3)
187
- json4 = JSON(orig = 1 << 62)
188
- assert_equal orig, JSON.parse(json4)
189
- json5 = JSON(orig = 1 << 64)
190
- assert_equal orig, JSON.parse(json5)
191
- end
192
-
193
- def test_some_wrong_inputs
194
- assert_raise(JSON::ParserError) { JSON.parse('[] bla') }
195
- assert_raise(JSON::ParserError) { JSON.parse('[] 1') }
196
- assert_raise(JSON::ParserError) { JSON.parse('[] []') }
197
- assert_raise(JSON::ParserError) { JSON.parse('[] {}') }
198
- assert_raise(JSON::ParserError) { JSON.parse('{} []') }
199
- assert_raise(JSON::ParserError) { JSON.parse('{} {}') }
200
- assert_raise(JSON::ParserError) { JSON.parse('[NULL]') }
201
- assert_raise(JSON::ParserError) { JSON.parse('[FALSE]') }
202
- assert_raise(JSON::ParserError) { JSON.parse('[TRUE]') }
203
- assert_raise(JSON::ParserError) { JSON.parse('[07] ') }
204
- assert_raise(JSON::ParserError) { JSON.parse('[0a]') }
205
- assert_raise(JSON::ParserError) { JSON.parse('[1.]') }
206
- assert_raise(JSON::ParserError) { JSON.parse(' ') }
207
- end
208
-
209
- def test_symbolize_names
210
- assert_equal({ "foo" => "bar", "baz" => "quux" },
211
- JSON.parse('{"foo":"bar", "baz":"quux"}'))
212
- assert_equal({ :foo => "bar", :baz => "quux" },
213
- JSON.parse('{"foo":"bar", "baz":"quux"}', :symbolize_names => true))
214
- assert_raise(ArgumentError) do
215
- JSON.parse('{}', :symbolize_names => true, :create_additions => true)
216
- end
217
- end
218
-
219
- def test_parse_comments
220
- json = <<~EOT
221
- {
222
- "key1":"value1", // eol comment
223
- "key2":"value2" /* multi line
224
- * comment */,
225
- "key3":"value3" /* multi line
226
- // nested eol comment
227
- * comment */
228
- }
229
- EOT
230
- assert_equal(
231
- { "key1" => "value1", "key2" => "value2", "key3" => "value3" },
232
- JSON.parse(json))
233
- json = <<~EOT
234
- {
235
- "key1":"value1" /* multi line
236
- // nested eol comment
237
- /* illegal nested multi line comment */
238
- * comment */
239
- }
240
- EOT
241
- assert_raise(JSON::ParserError) { JSON.parse(json) }
242
- json = <<~EOT
243
- {
244
- "key1":"value1" /* multi line
245
- // nested eol comment
246
- closed multi comment */
247
- and again, throw an Error */
248
- }
249
- EOT
250
- assert_raise(JSON::ParserError) { JSON.parse(json) }
251
- json = <<~EOT
252
- {
253
- "key1":"value1" /*/*/
254
- }
255
- EOT
256
- assert_equal({ "key1" => "value1" }, JSON.parse(json))
257
- end
258
-
259
- def test_nesting
260
- too_deep = '[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]'
261
- too_deep_ary = eval too_deep
262
- assert_raise(JSON::NestingError) { JSON.parse too_deep }
263
- assert_raise(JSON::NestingError) { JSON.parse too_deep, :max_nesting => 100 }
264
- ok = JSON.parse too_deep, :max_nesting => 101
265
- assert_equal too_deep_ary, ok
266
- ok = JSON.parse too_deep, :max_nesting => nil
267
- assert_equal too_deep_ary, ok
268
- ok = JSON.parse too_deep, :max_nesting => false
269
- assert_equal too_deep_ary, ok
270
- ok = JSON.parse too_deep, :max_nesting => 0
271
- assert_equal too_deep_ary, ok
272
-
273
- unless ENV['REAL_JSON_GEM']
274
- # max_nesting should be reset to 0 if not included in options
275
- # This behavior is not compatible with Ruby standard JSON gem
276
- ok = JSON.parse too_deep, {}
277
- assert_equal too_deep_ary, ok
278
- end
279
- end
280
-
281
- def test_backslash
282
- data = [ '\\.(?i:gif|jpe?g|png)$' ]
283
- json = '["\\\\.(?i:gif|jpe?g|png)$"]'
284
- assert_equal data, JSON.parse(json)
285
- #
286
- data = [ '\\"' ]
287
- json = '["\\\\\""]'
288
- assert_equal data, JSON.parse(json)
289
- #
290
- json = '["/"]'
291
- data = [ '/' ]
292
- assert_equal data, JSON.parse(json)
293
- #
294
- json = '["\""]'
295
- data = ['"']
296
- assert_equal data, JSON.parse(json)
297
- #
298
- json = '["\\\'"]'
299
- data = ["'"]
300
- assert_equal data, JSON.parse(json)
301
- end
302
-
303
- class SubArray < Array
304
- def <<(v)
305
- @shifted = true
306
- super
307
- end
308
-
309
- def shifted?
310
- @shifted
311
- end
312
- end
313
-
314
- class SubArray2 < Array
315
- def to_json(*a)
316
- {
317
- JSON.create_id => self.class.name,
318
- 'ary' => to_a,
319
- }.to_json(*a)
320
- end
321
-
322
- def self.json_create(o)
323
- o.delete JSON.create_id
324
- o['ary']
325
- end
326
- end
327
-
328
- class SubArrayWrapper
329
- def initialize
330
- @data = []
331
- end
332
-
333
- attr_reader :data
334
-
335
- def [](index)
336
- @data[index]
337
- end
338
-
339
- def <<(value)
340
- @data << value
341
- @shifted = true
342
- end
343
-
344
- def shifted?
345
- @shifted
346
- end
347
- end
348
-
349
- def test_parse_array_custom_array_derived_class
350
- res = JSON.parse('[1,2]', :array_class => SubArray)
351
- assert_equal([1, 2], res)
352
- assert_equal(SubArray, res.class)
353
- assert res.shifted?
354
- end
355
-
356
- def test_parse_array_custom_non_array_derived_class
357
- res = JSON.parse('[1,2]', :array_class => SubArrayWrapper)
358
- assert_equal(SubArrayWrapper, res.class)
359
- assert_equal([1, 2], res.data)
360
- assert res.shifted?
361
- end
362
-
363
- def test_parse_object
364
- assert_equal({}, JSON.parse('{}'))
365
- assert_equal({}, JSON.parse(' { } '))
366
- assert_equal({'foo'=>'bar'}, JSON.parse('{"foo":"bar"}'))
367
- assert_equal({'foo'=>'bar'}, JSON.parse(' { "foo" : "bar" } '))
368
- end
369
-
370
- class SubHash < Hash
371
- def []=(k, v)
372
- @item_set = true
373
- super
374
- end
375
-
376
- def item_set?
377
- @item_set
378
- end
379
- end
380
-
381
- class SubHash2 < Hash
382
- def to_json(*a)
383
- {
384
- JSON.create_id => self.class.name,
385
- }.merge(self).to_json(*a)
386
- end
387
-
388
- def self.json_create(o)
389
- o.delete JSON.create_id
390
- self[o]
391
- end
392
- end
393
-
394
- class SubOpenStruct < OpenStruct
395
- def [](k)
396
- __send__(k)
397
- end
398
-
399
- def []=(k, v)
400
- @item_set = true
401
- __send__("#{k}=", v)
402
- end
403
-
404
- def item_set?
405
- @item_set
406
- end
407
- end
408
-
409
- def test_parse_object_custom_hash_derived_class
410
- res = JSON.parse('{"foo":"bar"}', :object_class => SubHash)
411
- assert_equal(SubHash, res.class)
412
- assert_equal({"foo" => "bar"}, res)
413
- assert res.item_set?
414
- end
415
-
416
- def test_parse_object_custom_non_hash_derived_class
417
- res = JSON.parse('{"foo":"bar"}', :object_class => SubOpenStruct)
418
- assert_equal(SubOpenStruct, res.class)
419
- assert_equal "bar", res.foo
420
- assert res.item_set?
421
- end
422
-
423
- def test_parse_generic_object
424
- res = JSON.parse(
425
- '{"foo":"bar", "baz":{}}',
426
- :object_class => JSON::GenericObject
427
- )
428
- assert_equal(JSON::GenericObject, res.class)
429
- assert_equal "bar", res.foo
430
- assert_equal "bar", res["foo"]
431
- assert_equal "bar", res[:foo]
432
- assert_equal "bar", res.to_hash[:foo]
433
- assert_equal(JSON::GenericObject, res.baz.class)
434
- end if defined?(JSON::GenericObject)
435
-
436
- def test_generate_core_subclasses_with_new_to_json
437
- obj = SubHash2["foo" => SubHash2["bar" => true]]
438
- obj_json = JSON(obj)
439
- obj_again = JSON.parse(obj_json, :create_additions => true)
440
- assert_kind_of SubHash2, obj_again
441
- assert_kind_of SubHash2, obj_again['foo']
442
- assert obj_again['foo']['bar']
443
- assert_equal obj, obj_again
444
- assert_equal ["foo"],
445
- JSON(JSON(SubArray2["foo"]), :create_additions => true)
446
- end
447
-
448
- def test_generate_core_subclasses_with_default_to_json
449
- assert_equal '{"foo":"bar"}', JSON(SubHash["foo" => "bar"])
450
- assert_equal '["foo"]', JSON(SubArray["foo"])
451
- end
452
-
453
- def test_generate_of_core_subclasses
454
- obj = SubHash["foo" => SubHash["bar" => true]]
455
- obj_json = JSON(obj)
456
- obj_again = JSON(obj_json)
457
- assert_kind_of Hash, obj_again
458
- assert_kind_of Hash, obj_again['foo']
459
- assert obj_again['foo']['bar']
460
- assert_equal obj, obj_again
461
- end
462
-
463
- def test_parsing_frozen_ascii8bit_string
464
- assert_equal(
465
- { 'foo' => 'bar' },
466
- JSON('{ "foo": "bar" }'.force_encoding(Encoding::ASCII_8BIT).freeze)
467
- )
468
- end
469
-
470
- private
471
-
472
- def assert_equal_float(expected, actual, delta = 1e-2)
473
- Array === expected and expected = expected.first
474
- Array === actual and actual = actual.first
475
- assert_in_delta(expected, actual, delta)
476
- end
477
- end
@@ -1,42 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # encoding: UTF-8
3
- #frozen_string_literal: false
4
-
5
- require 'json_gem/test_helper'
6
-
7
- require 'time'
8
-
9
- class JSONStringMatchingTest < Test::Unit::TestCase
10
- include Test::Unit::TestCaseOmissionSupport
11
-
12
- class TestTime < ::Time
13
- def self.json_create(string)
14
- Time.parse(string)
15
- end
16
-
17
- def to_json(*)
18
- %{"#{strftime('%FT%T%z')}"}
19
- end
20
-
21
- def ==(other)
22
- to_i == other.to_i
23
- end
24
- end
25
-
26
- def test_match_date
27
- t = TestTime.new
28
- t_json = [ t ].to_json
29
- time_regexp = /\A\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}[+-]\d{4}\z/
30
- assert_equal [ t ],
31
- JSON.parse(
32
- t_json,
33
- :create_additions => true,
34
- :match_string => { time_regexp => TestTime }
35
- )
36
- assert_equal [ t.strftime('%FT%T%z') ],
37
- JSON.parse(
38
- t_json,
39
- :match_string => { time_regexp => TestTime }
40
- )
41
- end
42
- end
@@ -1,30 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- $: << File.dirname(__FILE__)
4
- $oj_dir = File.dirname(File.dirname(File.expand_path(File.dirname(__FILE__))))
5
- %w(lib ext).each do |dir|
6
- $: << File.join($oj_dir, dir)
7
- end
8
-
9
- require 'test/unit'
10
- REAL_JSON_GEM = !!ENV['REAL_JSON_GEM']
11
-
12
- if ENV['REAL_JSON_GEM']
13
- require 'json'
14
- else
15
- require 'oj'
16
- Oj.mimic_JSON
17
-
18
- # This method was added in Ruby 3.0.0. Calling it this way asks the GC to
19
- # move objects around, helping to find object movement bugs.
20
- if defined?(GC.verify_compaction_references) == 'method'
21
- if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.2.0")
22
- GC.verify_compaction_references(expand_heap: true, toward: :empty)
23
- else
24
- GC.verify_compaction_references(double_heap: true, toward: :empty)
25
- end
26
- end
27
- end
28
-
29
- NaN = JSON::NaN if defined?(JSON::NaN)
30
- NaN = 0.0/0 unless defined?(NaN)
data/test/mem.rb DELETED
@@ -1,34 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # frozen_string_literal: true
3
-
4
- require 'English'
5
- $LOAD_PATH << '.'
6
- $LOAD_PATH << File.join(__dir__, '../lib')
7
- $LOAD_PATH << File.join(__dir__, '../ext')
8
-
9
- require 'oj'
10
-
11
- Oj.default_options = { mode: :rails, cache_keys: false, cache_str: -1 }
12
-
13
- def mem
14
- `ps -o rss= -p #{$PROCESS_ID}`.to_i
15
- end
16
-
17
- ('a'..'z').each { |a|
18
- ('a'..'z').each { |b|
19
- ('a'..'z').each { |c|
20
- ('a'..'z').each { |d|
21
- ('a'..'z').each { |e|
22
- ('a'..'z').each { |f|
23
- key = "#{a}#{b}#{c}#{d}#{e}#{f}"
24
- Oj.load(%|{ "#{key}": 101}|)
25
- # Oj.dump(x)
26
- }
27
- }
28
- }
29
- }
30
- puts "#{a}#{b} #{mem}"
31
- }
32
- }
33
-
34
- Oj::Parser.new(:usual)
data/test/perf.rb DELETED
@@ -1,102 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class Perf
4
-
5
- def initialize
6
- @items = []
7
- end
8
-
9
- def add(title, op, &blk)
10
- @items << Item.new(title, op, &blk)
11
- end
12
-
13
- def before(title, &blk)
14
- @items.each do |i|
15
- if title == i.title
16
- i.set_before(&blk)
17
- break
18
- end
19
- end
20
- end
21
-
22
- def run(iter)
23
- base = Item.new(nil, nil) {}
24
- base.run(iter, 0.0)
25
- @items.each do |i|
26
- i.run(iter, base.duration)
27
- if i.error.nil?
28
- puts "#{i.title}.#{i.op} #{iter} times in %0.3f seconds or %0.3f #{i.op}/sec." % [i.duration, iter / i.duration]
29
- else
30
- puts "***** #{i.title}.#{i.op} failed! #{i.error}"
31
- end
32
- end
33
- summary()
34
- end
35
-
36
- def summary
37
- width = 6
38
- @items.each do |i|
39
- next if i.duration.nil?
40
-
41
- width = i.title.size if width < i.title.size
42
- end
43
- iva = @items.clone
44
- iva.delete_if { |i| i.duration.nil? }
45
- iva = iva.sort_by { |i| i.duration }
46
- puts
47
- puts 'Summary:'
48
- puts '%*s time (secs) rate (ops/sec)' % [width, 'System']
49
- puts "#{'-' * width} ----------- --------------"
50
- iva.each do |i|
51
- puts '%*s %11.3f %14.3f' % [width, i.title, i.duration, i.rate ] unless i.duration.nil?
52
- end
53
- puts
54
- puts "Comparison Matrix\n(performance factor, 2.0 means row is twice as fast as column)"
55
- puts ([' ' * width] + iva.map { |i| '%*s' % [width, i.title] }).join(' ')
56
- puts (['-' * width] + iva.map { |_i| '-' * width }).join(' ')
57
- iva.each do |i|
58
- line = ['%*s' % [width, i.title]]
59
- iva.each do |o|
60
- line << ('%*.2f' % [width, o.duration / i.duration])
61
- end
62
- puts line.join(' ')
63
- end
64
- puts
65
- end
66
-
67
- class Item
68
- attr_accessor :title
69
- attr_accessor :op
70
- attr_accessor :blk
71
- attr_accessor :duration
72
- attr_accessor :rate
73
- attr_accessor :error
74
-
75
- def initialize(title, op, &blk)
76
- @title = title
77
- @blk = blk
78
- @op = op
79
- @duration = nil
80
- @rate = nil
81
- @error = nil
82
- @before = nil
83
- end
84
-
85
- def set_before(&blk)
86
- @before = blk
87
- end
88
-
89
- def run(iter, base)
90
- GC.start
91
- @before.call unless @before.nil?
92
- start = Time.now
93
- iter.times { @blk.call }
94
- @duration = Time.now - start - base
95
- @duration = 0.0 if @duration < 0.0
96
- @rate = iter / @duration
97
- rescue Exception => e
98
- @error = "#{e.class}: #{e.message}"
99
- end
100
-
101
- end # Item
102
- end # Perf