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,542 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "securerandom"
4
- require_relative "abstract_unit"
5
- require "active_support/core_ext/string/inflections"
6
- require "active_support/json"
7
- require "active_support/time"
8
- require_relative "time_zone_test_helpers"
9
- require_relative "encoding_test_cases"
10
-
11
- require 'oj'
12
-
13
- # Sets the ActiveSupport encoder to be Oj and also wraps the setting of
14
- # globals.
15
- Oj::Rails.set_encoder()
16
- #Oj::Rails.optimize(Hash, Array, BigDecimal, Time, Range, Regexp, ActiveSupport::TimeWithZone)
17
- Oj::Rails.optimize()
18
-
19
- class TestJSONEncoding < ActiveSupport::TestCase
20
- include TimeZoneTestHelpers
21
-
22
- # Added for testing if Oj is used.
23
- test "oj is used as an encoder" do
24
- assert_equal ActiveSupport.json_encoder, Oj::Rails::Encoder
25
- end
26
-
27
- def sorted_json(json)
28
- if json.start_with?("{") && json.end_with?("}")
29
- "{" + json[1..-2].split(",").sort.join(",") + "}"
30
- else
31
- json
32
- end
33
- end
34
-
35
- JSONTest::EncodingTestCases.constants.each do |class_tests|
36
- define_method("test_#{class_tests[0..-6].underscore}") do
37
- prev = ActiveSupport.use_standard_json_time_format
38
-
39
- standard_class_tests = /Standard/.match?(class_tests)
40
-
41
- ActiveSupport.escape_html_entities_in_json = !standard_class_tests
42
- ActiveSupport.use_standard_json_time_format = standard_class_tests
43
- JSONTest::EncodingTestCases.const_get(class_tests).each do |pair|
44
- assert_equal pair.last, sorted_json(ActiveSupport::JSON.encode(pair.first))
45
- end
46
- ensure
47
- ActiveSupport.escape_html_entities_in_json = false
48
- ActiveSupport.use_standard_json_time_format = prev
49
- end
50
- end
51
-
52
- def test_process_status
53
- rubinius_skip "https://github.com/rubinius/rubinius/issues/3334"
54
-
55
- # There doesn't seem to be a good way to get a handle on a Process::Status object without actually
56
- # creating a child process, hence this to populate $?
57
- system("not_a_real_program_#{SecureRandom.hex}")
58
- assert_equal %({"exitstatus":#{$?.exitstatus},"pid":#{$?.pid}}), ActiveSupport::JSON.encode($?)
59
- end
60
-
61
- def test_hash_encoding
62
- assert_equal %({\"a\":\"b\"}), ActiveSupport::JSON.encode(a: :b)
63
- assert_equal %({\"a\":1}), ActiveSupport::JSON.encode("a" => 1)
64
- assert_equal %({\"a\":[1,2]}), ActiveSupport::JSON.encode("a" => [1, 2])
65
- assert_equal %({"1":2}), ActiveSupport::JSON.encode(1 => 2)
66
-
67
- assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json(ActiveSupport::JSON.encode(a: :b, c: :d))
68
- end
69
-
70
- def test_hash_keys_encoding
71
- ActiveSupport.escape_html_entities_in_json = true
72
- assert_equal "{\"\\u003c\\u003e\":\"\\u003c\\u003e\"}", ActiveSupport::JSON.encode("<>" => "<>")
73
- ensure
74
- ActiveSupport.escape_html_entities_in_json = false
75
- end
76
-
77
- def test_hash_keys_encoding_without_escaping
78
- assert_equal "{\"<>\":\"<>\"}", ActiveSupport::JSON.encode("<>" => "<>")
79
- end
80
-
81
- module UnicodeTests
82
- def test_utf8_string_encoded_properly
83
- result = ActiveSupport::JSON.encode("€2.99")
84
- assert_equal '"€2.99"', result
85
- assert_equal(Encoding::UTF_8, result.encoding)
86
-
87
- result = ActiveSupport::JSON.encode("✎☺")
88
- assert_equal '"✎☺"', result
89
- assert_equal(Encoding::UTF_8, result.encoding)
90
- end
91
-
92
- def test_non_utf8_string_transcodes
93
- s = "二".encode("Shift_JIS")
94
- result = ActiveSupport::JSON.encode(s)
95
- assert_equal '"二"', result
96
- assert_equal Encoding::UTF_8, result.encoding
97
- end
98
-
99
- def test_wide_utf8_chars
100
- w = "𠜎"
101
- result = ActiveSupport::JSON.encode(w)
102
- assert_equal '"𠜎"', result
103
- end
104
-
105
- def test_wide_utf8_roundtrip
106
- hash = { string: "𐒑" }
107
- json = ActiveSupport::JSON.encode(hash)
108
- decoded_hash = ActiveSupport::JSON.decode(json)
109
- assert_equal "𐒑", decoded_hash["string"]
110
- end
111
-
112
- def test_invalid_encoding_raises
113
- s = "\xAE\xFF\x9F"
114
- refute s.valid_encoding?
115
-
116
- # n.b. this raises EncodingError, because we didn't call Oj.mimic_JSON in the test setup; but,
117
- # if you do that (even indirectly through Oj.optimize_rails), then this raises a
118
- # JSON::GeneratorError instead of an EncodingError.
119
- assert_raises(EncodingError) do
120
- ActiveSupport::JSON.encode([s])
121
- end
122
- end
123
- end
124
-
125
- module UnicodeTestsWithEscapingOn
126
- def setup
127
- ActiveSupport.escape_html_entities_in_json = true
128
- end
129
-
130
- def teardown
131
- ActiveSupport.escape_html_entities_in_json = false
132
- end
133
-
134
- include UnicodeTests
135
- end
136
-
137
- module UnicodeTestsWithEscapingOff
138
- def setup
139
- ActiveSupport.escape_html_entities_in_json = false
140
- end
141
-
142
- include UnicodeTests
143
- end
144
-
145
- include UnicodeTestsWithEscapingOn
146
- include UnicodeTestsWithEscapingOff
147
-
148
- def test_hash_key_identifiers_are_always_quoted
149
- values = { 0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B" }
150
- assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values))
151
- end
152
-
153
- def test_hash_should_allow_key_filtering_with_only
154
- assert_equal %({"a":1}), ActiveSupport::JSON.encode({ "a" => 1, :b => 2, :c => 3 }, { only: "a" })
155
- end
156
-
157
- def test_hash_should_allow_key_filtering_with_except
158
- assert_equal %({"b":2}), ActiveSupport::JSON.encode({ "foo" => "bar", :b => 2, :c => 3 }, { except: ["foo", :c] })
159
- end
160
-
161
- def test_time_to_json_includes_local_offset
162
- with_standard_json_time_format(true) do
163
- with_env_tz "US/Eastern" do
164
- assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005, 2, 1, 15, 15, 10))
165
- end
166
- end
167
- end
168
-
169
- def test_hash_with_time_to_json
170
- with_standard_json_time_format(false) do
171
- assert_equal '{"time":"2009/01/01 00:00:00 +0000"}', { time: Time.utc(2009) }.to_json
172
- end
173
- end
174
-
175
- def test_nested_hash_with_float
176
- assert_nothing_raised do
177
- hash = {
178
- "CHI" => {
179
- display_name: "chicago",
180
- latitude: 123.234
181
- }
182
- }
183
- ActiveSupport::JSON.encode(hash)
184
- end
185
- end
186
-
187
- def test_hash_like_with_options
188
- h = JSONTest::Hashlike.new
189
- json = h.to_json only: [:foo]
190
-
191
- assert_equal({ "foo" => "hello" }, JSON.parse(json))
192
- end
193
-
194
- def test_object_to_json_with_options
195
- obj = Object.new
196
- obj.instance_variable_set :@foo, "hello"
197
- obj.instance_variable_set :@bar, "world"
198
- json = obj.to_json only: ["foo"]
199
-
200
- assert_equal({ "foo" => "hello" }, JSON.parse(json))
201
- end
202
-
203
- def test_struct_to_json_with_options
204
- struct = Struct.new(:foo, :bar).new
205
- struct.foo = "hello"
206
- struct.bar = "world"
207
- json = struct.to_json only: [:foo]
208
-
209
- assert_equal({ "foo" => "hello" }, JSON.parse(json))
210
- end
211
-
212
- def test_struct_to_json_with_options_nested
213
- klass = Struct.new(:foo, :bar)
214
- struct = klass.new "hello", "world"
215
- parent_struct = klass.new struct, "world"
216
- json = parent_struct.to_json only: [:foo]
217
-
218
- assert_equal({ "foo" => { "foo" => "hello" } }, JSON.parse(json))
219
- end
220
-
221
-
222
- def test_hash_should_pass_encoding_options_to_children_in_as_json
223
- person = {
224
- name: "John",
225
- address: {
226
- city: "London",
227
- country: "UK"
228
- }
229
- }
230
- json = person.as_json only: [:address, :city]
231
-
232
- assert_equal({ "address" => { "city" => "London" } }, json)
233
- end
234
-
235
- def test_hash_should_pass_encoding_options_to_children_in_to_json
236
- person = {
237
- name: "John",
238
- address: {
239
- city: "London",
240
- country: "UK"
241
- }
242
- }
243
- json = person.to_json only: [:address, :city]
244
-
245
- assert_equal(%({"address":{"city":"London"}}), json)
246
- end
247
-
248
- def test_array_should_pass_encoding_options_to_children_in_as_json
249
- people = [
250
- { name: "John", address: { city: "London", country: "UK" } },
251
- { name: "Jean", address: { city: "Paris", country: "France" } }
252
- ]
253
- json = people.as_json only: [:address, :city]
254
- expected = [
255
- { "address" => { "city" => "London" } },
256
- { "address" => { "city" => "Paris" } }
257
- ]
258
-
259
- assert_equal(expected, json)
260
- end
261
-
262
- def test_array_should_pass_encoding_options_to_children_in_to_json
263
- people = [
264
- { name: "John", address: { city: "London", country: "UK" } },
265
- { name: "Jean", address: { city: "Paris", country: "France" } }
266
- ]
267
- json = people.to_json only: [:address, :city]
268
-
269
- assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
270
- end
271
-
272
- People = Class.new(BasicObject) do
273
- include Enumerable
274
- def initialize
275
- @people = [
276
- { name: "John", address: { city: "London", country: "UK" } },
277
- { name: "Jean", address: { city: "Paris", country: "France" } }
278
- ]
279
- end
280
- def each(*, &blk)
281
- @people.each do |p|
282
- yield p if blk
283
- p
284
- end.each
285
- end
286
- end
287
-
288
- def test_enumerable_should_generate_json_with_as_json
289
- json = People.new.as_json only: [:address, :city]
290
- expected = [
291
- { "address" => { "city" => "London" } },
292
- { "address" => { "city" => "Paris" } }
293
- ]
294
-
295
- assert_equal(expected, json)
296
- end
297
-
298
- def test_enumerable_should_generate_json_with_to_json
299
- json = People.new.to_json only: [:address, :city]
300
- assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
301
- end
302
-
303
- def test_enumerable_should_pass_encoding_options_to_children_in_as_json
304
- json = People.new.each.as_json only: [:address, :city]
305
- expected = [
306
- { "address" => { "city" => "London" } },
307
- { "address" => { "city" => "Paris" } }
308
- ]
309
-
310
- assert_equal(expected, json)
311
- end
312
-
313
- def test_enumerable_should_pass_encoding_options_to_children_in_to_json
314
- json = People.new.each.to_json only: [:address, :city]
315
-
316
- assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
317
- end
318
-
319
- class CustomWithOptions
320
- attr_accessor :foo, :bar
321
-
322
- def as_json(options = {})
323
- options[:only] = %w(foo bar)
324
- super(options)
325
- end
326
- end
327
-
328
- def test_hash_to_json_should_not_keep_options_around
329
- f = CustomWithOptions.new
330
- f.foo = "hello"
331
- f.bar = "world"
332
-
333
- hash = { "foo" => f, "other_hash" => { "foo" => "other_foo", "test" => "other_test" } }
334
- assert_equal({ "foo" => { "foo" => "hello", "bar" => "world" },
335
- "other_hash" => { "foo" => "other_foo", "test" => "other_test" } }, ActiveSupport::JSON.decode(hash.to_json))
336
- end
337
-
338
- def test_array_to_json_should_not_keep_options_around
339
- f = CustomWithOptions.new
340
- f.foo = "hello"
341
- f.bar = "world"
342
-
343
- array = [f, { "foo" => "other_foo", "test" => "other_test" }]
344
- assert_equal([{ "foo" => "hello", "bar" => "world" },
345
- { "foo" => "other_foo", "test" => "other_test" }], ActiveSupport::JSON.decode(array.to_json))
346
- end
347
-
348
- class OptionsTest
349
- def as_json(options = :default)
350
- options
351
- end
352
- end
353
-
354
- def test_hash_as_json_without_options
355
- json = { foo: OptionsTest.new }.as_json
356
- assert_equal({ "foo" => :default }, json)
357
- end
358
-
359
- def test_array_as_json_without_options
360
- json = [ OptionsTest.new ].as_json
361
- assert_equal([:default], json)
362
- end
363
-
364
- def test_struct_encoding
365
- Struct.new("UserNameAndEmail", :name, :email)
366
- Struct.new("UserNameAndDate", :name, :date)
367
- Struct.new("Custom", :name, :sub)
368
- user_email = Struct::UserNameAndEmail.new "David", "sample@example.com"
369
- user_birthday = Struct::UserNameAndDate.new "David", Date.new(2010, 01, 01)
370
- custom = Struct::Custom.new "David", user_birthday
371
-
372
- json_strings = ""
373
- json_string_and_date = ""
374
- json_custom = ""
375
-
376
- assert_nothing_raised do
377
- json_strings = user_email.to_json
378
- json_string_and_date = user_birthday.to_json
379
- json_custom = custom.to_json
380
- end
381
-
382
- assert_equal({ "name" => "David",
383
- "sub" => {
384
- "name" => "David",
385
- "date" => "2010-01-01" } }, ActiveSupport::JSON.decode(json_custom))
386
-
387
- assert_equal({ "name" => "David", "email" => "sample@example.com" },
388
- ActiveSupport::JSON.decode(json_strings))
389
-
390
- assert_equal({ "name" => "David", "date" => "2010-01-01" },
391
- ActiveSupport::JSON.decode(json_string_and_date))
392
- end
393
-
394
- def test_nil_true_and_false_represented_as_themselves
395
- assert_nil nil.as_json
396
- assert_equal true, true.as_json
397
- assert_equal false, false.as_json
398
- end
399
-
400
- class HashWithAsJson < Hash
401
- attr_accessor :as_json_called
402
-
403
- def initialize(*)
404
- super
405
- end
406
-
407
- def as_json(options = {})
408
- @as_json_called = true
409
- super
410
- end
411
- end
412
-
413
- def test_json_gem_dump_by_passing_active_support_encoder
414
- h = HashWithAsJson.new
415
- h[:foo] = "hello"
416
- h[:bar] = "world"
417
-
418
- assert_equal %({"foo":"hello","bar":"world"}), JSON.dump(h)
419
- assert_nil h.as_json_called
420
- end
421
-
422
- def test_json_gem_generate_by_passing_active_support_encoder
423
- h = HashWithAsJson.new
424
- h[:foo] = "hello"
425
- h[:bar] = "world"
426
-
427
- assert_equal %({"foo":"hello","bar":"world"}), JSON.generate(h)
428
- assert_nil h.as_json_called
429
- end
430
-
431
- def test_json_gem_pretty_generate_by_passing_active_support_encoder
432
- h = HashWithAsJson.new
433
- h[:foo] = "hello"
434
- h[:bar] = "world"
435
-
436
- assert_equal <<EXPECTED.chomp, JSON.pretty_generate(h)
437
- {
438
- "foo": "hello",
439
- "bar": "world"
440
- }
441
- EXPECTED
442
- assert_nil h.as_json_called
443
- end
444
-
445
- def test_twz_to_json_with_use_standard_json_time_format_config_set_to_false
446
- with_standard_json_time_format(false) do
447
- zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
448
- time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
449
- assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(time)
450
- end
451
- end
452
-
453
- def test_twz_to_json_with_use_standard_json_time_format_config_set_to_true
454
- with_standard_json_time_format(true) do
455
- zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
456
- time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
457
- assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(time)
458
- end
459
- end
460
-
461
- def test_twz_to_json_with_custom_time_precision
462
- with_standard_json_time_format(true) do
463
- with_time_precision(0) do
464
- zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
465
- time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
466
- assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time)
467
- end
468
- end
469
- end
470
-
471
- def test_time_to_json_with_custom_time_precision
472
- with_standard_json_time_format(true) do
473
- with_time_precision(0) do
474
- assert_equal "\"2000-01-01T00:00:00Z\"", ActiveSupport::JSON.encode(Time.utc(2000))
475
- end
476
- end
477
- end
478
-
479
- def test_datetime_to_json_with_custom_time_precision
480
- with_standard_json_time_format(true) do
481
- with_time_precision(0) do
482
- assert_equal "\"2000-01-01T00:00:00+00:00\"", ActiveSupport::JSON.encode(DateTime.new(2000))
483
- end
484
- end
485
- end
486
-
487
- def test_twz_to_json_when_wrapping_a_date_time
488
- zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
489
- time = ActiveSupport::TimeWithZone.new(DateTime.new(2000), zone)
490
- assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(time)
491
- end
492
-
493
- def test_exception_to_json
494
- exception = Exception.new("foo")
495
- assert_equal '"foo"', ActiveSupport::JSON.encode(exception)
496
- end
497
-
498
- class InfiniteNumber
499
- def as_json(options = nil)
500
- { "number" => Float::INFINITY }
501
- end
502
- end
503
-
504
- def test_to_json_works_when_as_json_returns_infinite_number
505
- assert_equal '{"number":null}', InfiniteNumber.new.to_json
506
- end
507
-
508
- class NaNNumber
509
- def as_json(options = nil)
510
- { "number" => Float::NAN }
511
- end
512
- end
513
-
514
- def test_to_json_works_when_as_json_returns_NaN_number
515
- assert_equal '{"number":null}', NaNNumber.new.to_json
516
- end
517
-
518
- def test_to_json_works_on_io_objects
519
- assert_equal STDOUT.to_s.to_json, STDOUT.to_json
520
- end
521
-
522
- private
523
-
524
- def object_keys(json_object)
525
- json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
526
- end
527
-
528
- def with_standard_json_time_format(boolean = true)
529
- old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, boolean
530
- yield
531
- ensure
532
- ActiveSupport.use_standard_json_time_format = old
533
- end
534
-
535
- def with_time_precision(value)
536
- old_value = ActiveSupport::JSON::Encoding.time_precision
537
- ActiveSupport::JSON::Encoding.time_precision = value
538
- yield
539
- ensure
540
- ActiveSupport::JSON::Encoding.time_precision = old_value
541
- end
542
- end
@@ -1,98 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- require "bigdecimal"
4
- require "date"
5
- require "time"
6
- require "pathname"
7
- require "uri"
8
-
9
- module JSONTest
10
- class Foo
11
- def initialize(a, b)
12
- @a, @b = a, b
13
- end
14
- end
15
-
16
- class Hashlike
17
- def to_hash
18
- { foo: "hello", bar: "world" }
19
- end
20
- end
21
-
22
- class Custom
23
- def initialize(serialized)
24
- @serialized = serialized
25
- end
26
-
27
- def as_json(options = nil)
28
- @serialized
29
- end
30
- end
31
-
32
- MyStruct = Struct.new(:name, :value) do
33
- def initialize(*)
34
- @unused = "unused instance variable"
35
- super
36
- end
37
- end
38
-
39
- module EncodingTestCases
40
- TrueTests = [[ true, %(true) ]]
41
- FalseTests = [[ false, %(false) ]]
42
- NilTests = [[ nil, %(null) ]]
43
- NumericTests = [[ 1, %(1) ],
44
- [ 2.5, %(2.5) ],
45
- [ 0.0 / 0.0, %(null) ],
46
- [ 1.0 / 0.0, %(null) ],
47
- [ -1.0 / 0.0, %(null) ],
48
- [ BigDecimal("0.0") / BigDecimal("0.0"), %(null) ],
49
- [ BigDecimal("2.5"), %("#{BigDecimal('2.5')}") ]]
50
-
51
- StringTests = [[ "this is the <string>", %("this is the \\u003cstring\\u003e")],
52
- [ 'a "string" with quotes & an ampersand', %("a \\"string\\" with quotes \\u0026 an ampersand") ],
53
- [ "http://test.host/posts/1", %("http://test.host/posts/1")],
54
- [ "Control characters: \x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f\u2028\u2029",
55
- %("Control characters: \\u0000\\u0001\\u0002\\u0003\\u0004\\u0005\\u0006\\u0007\\b\\t\\n\\u000b\\f\\r\\u000e\\u000f\\u0010\\u0011\\u0012\\u0013\\u0014\\u0015\\u0016\\u0017\\u0018\\u0019\\u001a\\u001b\\u001c\\u001d\\u001e\\u001f\\u2028\\u2029") ]]
56
-
57
- ArrayTests = [[ ["a", "b", "c"], %([\"a\",\"b\",\"c\"]) ],
58
- [ [1, "a", :b, nil, false], %([1,\"a\",\"b\",null,false]) ]]
59
-
60
- HashTests = [[ { foo: "bar" }, %({\"foo\":\"bar\"}) ],
61
- [ { 1 => 1, 2 => "a", 3 => :b, 4 => nil, 5 => false }, %({\"1\":1,\"2\":\"a\",\"3\":\"b\",\"4\":null,\"5\":false}) ]]
62
-
63
- RangeTests = [[ 1..2, %("1..2")],
64
- [ 1...2, %("1...2")],
65
- [ 1.5..2.5, %("1.5..2.5")]]
66
-
67
- SymbolTests = [[ :a, %("a") ],
68
- [ :this, %("this") ],
69
- [ :"a b", %("a b") ]]
70
-
71
- ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
72
- HashlikeTests = [[ Hashlike.new, %({\"bar\":\"world\",\"foo\":\"hello\"}) ]]
73
- StructTests = [[ MyStruct.new(:foo, "bar"), %({\"name\":\"foo\",\"value\":\"bar\"}) ],
74
- [ MyStruct.new(nil, nil), %({\"name\":null,\"value\":null}) ]]
75
- CustomTests = [[ Custom.new("custom"), '"custom"' ],
76
- [ Custom.new(nil), "null" ],
77
- [ Custom.new(:a), '"a"' ],
78
- [ Custom.new([ :foo, "bar" ]), '["foo","bar"]' ],
79
- [ Custom.new(foo: "hello", bar: "world"), '{"bar":"world","foo":"hello"}' ],
80
- [ Custom.new(Hashlike.new), '{"bar":"world","foo":"hello"}' ],
81
- [ Custom.new(Custom.new(Custom.new(:a))), '"a"' ]]
82
-
83
- RegexpTests = [[ /^a/, '"(?-mix:^a)"' ], [/^\w{1,2}[a-z]+/ix, '"(?ix-m:^\\\\w{1,2}[a-z]+)"']]
84
-
85
- URITests = [[ URI.parse("http://example.com"), %("http://example.com") ]]
86
-
87
- PathnameTests = [[ Pathname.new("lib/index.rb"), %("lib/index.rb") ]]
88
-
89
- DateTests = [[ Date.new(2005, 2, 1), %("2005/02/01") ]]
90
- TimeTests = [[ Time.utc(2005, 2, 1, 15, 15, 10), %("2005/02/01 15:15:10 +0000") ]]
91
- DateTimeTests = [[ DateTime.civil(2005, 2, 1, 15, 15, 10), %("2005/02/01 15:15:10 +0000") ]]
92
-
93
- StandardDateTests = [[ Date.new(2005, 2, 1), %("2005-02-01") ]]
94
- StandardTimeTests = [[ Time.utc(2005, 2, 1, 15, 15, 10), %("2005-02-01T15:15:10.000Z") ]]
95
- StandardDateTimeTests = [[ DateTime.civil(2005, 2, 1, 15, 15, 10), %("2005-02-01T15:15:10.000+00:00") ]]
96
- StandardStringTests = [[ "this is the <string>", %("this is the <string>")]]
97
- end
98
- end
@@ -1,17 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- if ENV["BUILDKITE"]
4
- require "minitest/reporters"
5
- require "fileutils"
6
-
7
- module Minitest
8
- def self.plugin_rails_ci_junit_format_test_report_for_buildkite_init(*)
9
- dir = File.join(__dir__, "../test-reports/#{ENV['BUILDKITE_JOB_ID']}")
10
- reporter << Minitest::Reporters::JUnitReporter.new(dir, false)
11
- FileUtils.mkdir_p(dir)
12
- end
13
- end
14
-
15
- Minitest.load_plugins
16
- Minitest.extensions.unshift "rails_ci_junit_format_test_report_for_buildkite"
17
- end