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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/ext/oj/fast.c +3 -6
- data/ext/oj/mimic_json.c +3 -6
- data/ext/oj/oj.c +2 -4
- data/ext/oj/parse.c +15 -5
- data/ext/oj/parser.c +1 -1
- data/ext/oj/parser.h +2 -0
- data/ext/oj/rails.c +1 -2
- data/ext/oj/saj.c +3 -6
- data/ext/oj/scp.c +3 -6
- data/ext/oj/stream_writer.c +1 -7
- data/ext/oj/strict.c +2 -4
- data/ext/oj/string_writer.c +1 -3
- data/lib/oj/version.rb +1 -1
- metadata +3 -98
- data/test/_test_active.rb +0 -75
- data/test/_test_active_mimic.rb +0 -95
- data/test/_test_mimic_rails.rb +0 -123
- data/test/activerecord/result_test.rb +0 -31
- data/test/activesupport6/abstract_unit.rb +0 -44
- data/test/activesupport6/decoding_test.rb +0 -133
- data/test/activesupport6/encoding_test.rb +0 -542
- data/test/activesupport6/encoding_test_cases.rb +0 -98
- data/test/activesupport6/test_common.rb +0 -17
- data/test/activesupport6/test_helper.rb +0 -163
- data/test/activesupport6/time_zone_test_helpers.rb +0 -39
- data/test/activesupport7/abstract_unit.rb +0 -52
- data/test/activesupport7/decoding_test.rb +0 -125
- data/test/activesupport7/encoding_test.rb +0 -536
- data/test/activesupport7/encoding_test_cases.rb +0 -104
- data/test/activesupport7/time_zone_test_helpers.rb +0 -47
- data/test/files.rb +0 -29
- data/test/foo.rb +0 -26
- data/test/helper.rb +0 -39
- data/test/isolated/shared.rb +0 -309
- data/test/isolated/test_mimic_after.rb +0 -13
- data/test/isolated/test_mimic_alone.rb +0 -12
- data/test/isolated/test_mimic_as_json.rb +0 -45
- data/test/isolated/test_mimic_before.rb +0 -13
- data/test/isolated/test_mimic_define.rb +0 -28
- data/test/isolated/test_mimic_rails_after.rb +0 -22
- data/test/isolated/test_mimic_rails_before.rb +0 -21
- data/test/isolated/test_mimic_redefine.rb +0 -15
- data/test/json_gem/json_addition_test.rb +0 -216
- data/test/json_gem/json_common_interface_test.rb +0 -155
- data/test/json_gem/json_encoding_test.rb +0 -107
- data/test/json_gem/json_ext_parser_test.rb +0 -21
- data/test/json_gem/json_fixtures_test.rb +0 -36
- data/test/json_gem/json_generator_test.rb +0 -413
- data/test/json_gem/json_generic_object_test.rb +0 -90
- data/test/json_gem/json_parser_test.rb +0 -477
- data/test/json_gem/json_string_matching_test.rb +0 -42
- data/test/json_gem/test_helper.rb +0 -30
- data/test/mem.rb +0 -34
- data/test/perf.rb +0 -102
- data/test/perf_compat.rb +0 -128
- data/test/perf_dump.rb +0 -50
- data/test/perf_fast.rb +0 -162
- data/test/perf_file.rb +0 -62
- data/test/perf_object.rb +0 -134
- data/test/perf_once.rb +0 -59
- data/test/perf_parser.rb +0 -183
- data/test/perf_saj.rb +0 -101
- data/test/perf_scp.rb +0 -140
- data/test/perf_simple.rb +0 -289
- data/test/perf_strict.rb +0 -137
- data/test/perf_wab.rb +0 -129
- data/test/prec.rb +0 -23
- data/test/sample/change.rb +0 -13
- data/test/sample/dir.rb +0 -18
- data/test/sample/doc.rb +0 -35
- data/test/sample/file.rb +0 -47
- data/test/sample/group.rb +0 -15
- data/test/sample/hasprops.rb +0 -15
- data/test/sample/layer.rb +0 -11
- data/test/sample/line.rb +0 -20
- data/test/sample/oval.rb +0 -10
- data/test/sample/rect.rb +0 -9
- data/test/sample/shape.rb +0 -34
- data/test/sample/text.rb +0 -19
- data/test/sample.rb +0 -54
- data/test/sample_json.rb +0 -37
- data/test/test_compat.rb +0 -567
- data/test/test_custom.rb +0 -555
- data/test/test_debian.rb +0 -50
- data/test/test_fast.rb +0 -526
- data/test/test_file.rb +0 -250
- data/test/test_gc.rb +0 -60
- data/test/test_generate.rb +0 -21
- data/test/test_hash.rb +0 -39
- data/test/test_integer_range.rb +0 -72
- data/test/test_null.rb +0 -376
- data/test/test_object.rb +0 -1030
- data/test/test_parser.rb +0 -11
- data/test/test_parser_debug.rb +0 -27
- data/test/test_parser_saj.rb +0 -337
- data/test/test_parser_usual.rb +0 -255
- data/test/test_rails.rb +0 -35
- data/test/test_saj.rb +0 -188
- data/test/test_scp.rb +0 -431
- data/test/test_strict.rb +0 -441
- data/test/test_various.rb +0 -801
- data/test/test_wab.rb +0 -311
- data/test/test_writer.rb +0 -396
- data/test/tests.rb +0 -33
- data/test/tests_mimic.rb +0 -23
- data/test/tests_mimic_addition.rb +0 -16
@@ -1,536 +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
|
-
# Sets the ActiveSupport encoder to be Oj and also wraps the setting of globals.
|
13
|
-
Oj::Rails.set_encoder()
|
14
|
-
Oj::Rails.optimize()
|
15
|
-
|
16
|
-
class TestJSONEncoding < ActiveSupport::TestCase
|
17
|
-
include TimeZoneTestHelpers
|
18
|
-
|
19
|
-
def test_is_actually_oj
|
20
|
-
assert_equal Oj::Rails::Encoder, ActiveSupport.json_encoder
|
21
|
-
end
|
22
|
-
|
23
|
-
def sorted_json(json)
|
24
|
-
if json.start_with?("{") && json.end_with?("}")
|
25
|
-
"{" + json[1..-2].split(",").sort.join(",") + "}"
|
26
|
-
else
|
27
|
-
json
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
JSONTest::EncodingTestCases.constants.each do |class_tests|
|
32
|
-
define_method("test_#{class_tests[0..-6].underscore}") do
|
33
|
-
prev = ActiveSupport.use_standard_json_time_format
|
34
|
-
|
35
|
-
standard_class_tests = /Standard/.match?(class_tests)
|
36
|
-
|
37
|
-
ActiveSupport.escape_html_entities_in_json = !standard_class_tests
|
38
|
-
ActiveSupport.use_standard_json_time_format = standard_class_tests
|
39
|
-
JSONTest::EncodingTestCases.const_get(class_tests).each do |pair|
|
40
|
-
assert_equal pair.last, sorted_json(ActiveSupport::JSON.encode(pair.first))
|
41
|
-
end
|
42
|
-
ensure
|
43
|
-
ActiveSupport.escape_html_entities_in_json = false
|
44
|
-
ActiveSupport.use_standard_json_time_format = prev
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
def test_process_status
|
49
|
-
rubinius_skip "https://github.com/rubinius/rubinius/issues/3334"
|
50
|
-
|
51
|
-
# There doesn't seem to be a good way to get a handle on a Process::Status object without actually
|
52
|
-
# creating a child process, hence this to populate $?
|
53
|
-
system("not_a_real_program_#{SecureRandom.hex}")
|
54
|
-
assert_equal %({"exitstatus":#{$?.exitstatus},"pid":#{$?.pid}}), ActiveSupport::JSON.encode($?)
|
55
|
-
end
|
56
|
-
|
57
|
-
def test_hash_encoding
|
58
|
-
assert_equal %({\"a\":\"b\"}), ActiveSupport::JSON.encode(a: :b)
|
59
|
-
assert_equal %({\"a\":1}), ActiveSupport::JSON.encode("a" => 1)
|
60
|
-
assert_equal %({\"a\":[1,2]}), ActiveSupport::JSON.encode("a" => [1, 2])
|
61
|
-
assert_equal %({"1":2}), ActiveSupport::JSON.encode(1 => 2)
|
62
|
-
|
63
|
-
assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json(ActiveSupport::JSON.encode(a: :b, c: :d))
|
64
|
-
end
|
65
|
-
|
66
|
-
def test_hash_keys_encoding
|
67
|
-
ActiveSupport.escape_html_entities_in_json = true
|
68
|
-
assert_equal "{\"\\u003c\\u003e\":\"\\u003c\\u003e\"}", ActiveSupport::JSON.encode("<>" => "<>")
|
69
|
-
ensure
|
70
|
-
ActiveSupport.escape_html_entities_in_json = false
|
71
|
-
end
|
72
|
-
|
73
|
-
def test_hash_keys_encoding_without_escaping
|
74
|
-
assert_equal "{\"<>\":\"<>\"}", ActiveSupport::JSON.encode("<>" => "<>")
|
75
|
-
end
|
76
|
-
|
77
|
-
module UnicodeTests
|
78
|
-
def test_utf8_string_encoded_properly
|
79
|
-
result = ActiveSupport::JSON.encode("€2.99")
|
80
|
-
assert_equal '"€2.99"', result
|
81
|
-
assert_equal(Encoding::UTF_8, result.encoding)
|
82
|
-
|
83
|
-
result = ActiveSupport::JSON.encode("✎☺")
|
84
|
-
assert_equal '"✎☺"', result
|
85
|
-
assert_equal(Encoding::UTF_8, result.encoding)
|
86
|
-
end
|
87
|
-
|
88
|
-
def test_non_utf8_string_transcodes
|
89
|
-
s = "二".encode("Shift_JIS")
|
90
|
-
result = ActiveSupport::JSON.encode(s)
|
91
|
-
assert_equal '"二"', result
|
92
|
-
assert_equal Encoding::UTF_8, result.encoding
|
93
|
-
end
|
94
|
-
|
95
|
-
def test_wide_utf8_chars
|
96
|
-
w = "𠜎"
|
97
|
-
result = ActiveSupport::JSON.encode(w)
|
98
|
-
assert_equal '"𠜎"', result
|
99
|
-
end
|
100
|
-
|
101
|
-
def test_wide_utf8_roundtrip
|
102
|
-
hash = { string: "𐒑" }
|
103
|
-
json = ActiveSupport::JSON.encode(hash)
|
104
|
-
decoded_hash = ActiveSupport::JSON.decode(json)
|
105
|
-
assert_equal "𐒑", decoded_hash["string"]
|
106
|
-
end
|
107
|
-
|
108
|
-
def test_invalid_encoding_raises
|
109
|
-
s = "\xAE\xFF\x9F"
|
110
|
-
refute s.valid_encoding?
|
111
|
-
|
112
|
-
# n.b. this raises EncodingError, because we didn't call Oj.mimic_JSON in the test setup; but,
|
113
|
-
# if you do that (even indirectly through Oj.optimize_rails), then this raises a
|
114
|
-
# JSON::GeneratorError instead of an EncodingError.
|
115
|
-
assert_raises(EncodingError) do
|
116
|
-
ActiveSupport::JSON.encode([s])
|
117
|
-
end
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
module UnicodeTestsWithEscapingOn
|
122
|
-
def setup
|
123
|
-
ActiveSupport.escape_html_entities_in_json = true
|
124
|
-
end
|
125
|
-
|
126
|
-
def teardown
|
127
|
-
ActiveSupport.escape_html_entities_in_json = false
|
128
|
-
end
|
129
|
-
|
130
|
-
include UnicodeTests
|
131
|
-
end
|
132
|
-
|
133
|
-
module UnicodeTestsWithEscapingOff
|
134
|
-
def setup
|
135
|
-
ActiveSupport.escape_html_entities_in_json = false
|
136
|
-
end
|
137
|
-
|
138
|
-
include UnicodeTests
|
139
|
-
end
|
140
|
-
|
141
|
-
include UnicodeTestsWithEscapingOn
|
142
|
-
include UnicodeTestsWithEscapingOff
|
143
|
-
|
144
|
-
def test_hash_key_identifiers_are_always_quoted
|
145
|
-
values = { 0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B" }
|
146
|
-
assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values))
|
147
|
-
end
|
148
|
-
|
149
|
-
def test_hash_should_allow_key_filtering_with_only
|
150
|
-
assert_equal %({"a":1}), ActiveSupport::JSON.encode({ "a" => 1, :b => 2, :c => 3 }, { only: "a" })
|
151
|
-
end
|
152
|
-
|
153
|
-
def test_hash_should_allow_key_filtering_with_except
|
154
|
-
assert_equal %({"b":2}), ActiveSupport::JSON.encode({ "foo" => "bar", :b => 2, :c => 3 }, { except: ["foo", :c] })
|
155
|
-
end
|
156
|
-
|
157
|
-
def test_time_to_json_includes_local_offset
|
158
|
-
with_standard_json_time_format(true) do
|
159
|
-
with_env_tz "US/Eastern" do
|
160
|
-
assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005, 2, 1, 15, 15, 10))
|
161
|
-
end
|
162
|
-
end
|
163
|
-
end
|
164
|
-
|
165
|
-
def test_hash_with_time_to_json
|
166
|
-
with_standard_json_time_format(false) do
|
167
|
-
assert_equal '{"time":"2009/01/01 00:00:00 +0000"}', { time: Time.utc(2009) }.to_json
|
168
|
-
end
|
169
|
-
end
|
170
|
-
|
171
|
-
def test_nested_hash_with_float
|
172
|
-
assert_nothing_raised do
|
173
|
-
hash = {
|
174
|
-
"CHI" => {
|
175
|
-
display_name: "chicago",
|
176
|
-
latitude: 123.234
|
177
|
-
}
|
178
|
-
}
|
179
|
-
ActiveSupport::JSON.encode(hash)
|
180
|
-
end
|
181
|
-
end
|
182
|
-
|
183
|
-
def test_hash_like_with_options
|
184
|
-
h = JSONTest::Hashlike.new
|
185
|
-
json = h.to_json only: [:foo]
|
186
|
-
|
187
|
-
assert_equal({ "foo" => "hello" }, JSON.parse(json))
|
188
|
-
end
|
189
|
-
|
190
|
-
def test_object_to_json_with_options
|
191
|
-
obj = Object.new
|
192
|
-
obj.instance_variable_set :@foo, "hello"
|
193
|
-
obj.instance_variable_set :@bar, "world"
|
194
|
-
json = obj.to_json only: ["foo"]
|
195
|
-
|
196
|
-
assert_equal({ "foo" => "hello" }, JSON.parse(json))
|
197
|
-
end
|
198
|
-
|
199
|
-
def test_struct_to_json_with_options
|
200
|
-
struct = Struct.new(:foo, :bar).new
|
201
|
-
struct.foo = "hello"
|
202
|
-
struct.bar = "world"
|
203
|
-
json = struct.to_json only: [:foo]
|
204
|
-
|
205
|
-
assert_equal({ "foo" => "hello" }, JSON.parse(json))
|
206
|
-
end
|
207
|
-
|
208
|
-
def test_struct_to_json_with_options_nested
|
209
|
-
klass = Struct.new(:foo, :bar)
|
210
|
-
struct = klass.new "hello", "world"
|
211
|
-
parent_struct = klass.new struct, "world"
|
212
|
-
json = parent_struct.to_json only: [:foo]
|
213
|
-
|
214
|
-
assert_equal({ "foo" => { "foo" => "hello" } }, JSON.parse(json))
|
215
|
-
end
|
216
|
-
|
217
|
-
def test_hash_should_pass_encoding_options_to_children_in_as_json
|
218
|
-
person = {
|
219
|
-
name: "John",
|
220
|
-
address: {
|
221
|
-
city: "London",
|
222
|
-
country: "UK"
|
223
|
-
}
|
224
|
-
}
|
225
|
-
json = person.as_json only: [:address, :city]
|
226
|
-
|
227
|
-
assert_equal({ "address" => { "city" => "London" } }, json)
|
228
|
-
end
|
229
|
-
|
230
|
-
def test_hash_should_pass_encoding_options_to_children_in_to_json
|
231
|
-
person = {
|
232
|
-
name: "John",
|
233
|
-
address: {
|
234
|
-
city: "London",
|
235
|
-
country: "UK"
|
236
|
-
}
|
237
|
-
}
|
238
|
-
json = person.to_json only: [:address, :city]
|
239
|
-
|
240
|
-
assert_equal(%({"address":{"city":"London"}}), json)
|
241
|
-
end
|
242
|
-
|
243
|
-
def test_array_should_pass_encoding_options_to_children_in_as_json
|
244
|
-
people = [
|
245
|
-
{ name: "John", address: { city: "London", country: "UK" } },
|
246
|
-
{ name: "Jean", address: { city: "Paris", country: "France" } }
|
247
|
-
]
|
248
|
-
json = people.as_json only: [:address, :city]
|
249
|
-
expected = [
|
250
|
-
{ "address" => { "city" => "London" } },
|
251
|
-
{ "address" => { "city" => "Paris" } }
|
252
|
-
]
|
253
|
-
|
254
|
-
assert_equal(expected, json)
|
255
|
-
end
|
256
|
-
|
257
|
-
def test_array_should_pass_encoding_options_to_children_in_to_json
|
258
|
-
people = [
|
259
|
-
{ name: "John", address: { city: "London", country: "UK" } },
|
260
|
-
{ name: "Jean", address: { city: "Paris", country: "France" } }
|
261
|
-
]
|
262
|
-
json = people.to_json only: [:address, :city]
|
263
|
-
|
264
|
-
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
|
265
|
-
end
|
266
|
-
|
267
|
-
People = Class.new(BasicObject) do
|
268
|
-
include Enumerable
|
269
|
-
def initialize
|
270
|
-
@people = [
|
271
|
-
{ name: "John", address: { city: "London", country: "UK" } },
|
272
|
-
{ name: "Jean", address: { city: "Paris", country: "France" } }
|
273
|
-
]
|
274
|
-
end
|
275
|
-
def each(*, &blk)
|
276
|
-
@people.each do |p|
|
277
|
-
yield p if blk
|
278
|
-
p
|
279
|
-
end.each
|
280
|
-
end
|
281
|
-
end
|
282
|
-
|
283
|
-
def test_enumerable_should_generate_json_with_as_json
|
284
|
-
json = People.new.as_json only: [:address, :city]
|
285
|
-
expected = [
|
286
|
-
{ "address" => { "city" => "London" } },
|
287
|
-
{ "address" => { "city" => "Paris" } }
|
288
|
-
]
|
289
|
-
|
290
|
-
assert_equal(expected, json)
|
291
|
-
end
|
292
|
-
|
293
|
-
def test_enumerable_should_generate_json_with_to_json
|
294
|
-
json = People.new.to_json only: [:address, :city]
|
295
|
-
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
|
296
|
-
end
|
297
|
-
|
298
|
-
def test_enumerable_should_pass_encoding_options_to_children_in_as_json
|
299
|
-
json = People.new.each.as_json only: [:address, :city]
|
300
|
-
expected = [
|
301
|
-
{ "address" => { "city" => "London" } },
|
302
|
-
{ "address" => { "city" => "Paris" } }
|
303
|
-
]
|
304
|
-
|
305
|
-
assert_equal(expected, json)
|
306
|
-
end
|
307
|
-
|
308
|
-
def test_enumerable_should_pass_encoding_options_to_children_in_to_json
|
309
|
-
json = People.new.each.to_json only: [:address, :city]
|
310
|
-
|
311
|
-
assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
|
312
|
-
end
|
313
|
-
|
314
|
-
class CustomWithOptions
|
315
|
-
attr_accessor :foo, :bar
|
316
|
-
|
317
|
-
def as_json(options = {})
|
318
|
-
options[:only] = %w(foo bar)
|
319
|
-
super(options)
|
320
|
-
end
|
321
|
-
end
|
322
|
-
|
323
|
-
def test_hash_to_json_should_not_keep_options_around
|
324
|
-
f = CustomWithOptions.new
|
325
|
-
f.foo = "hello"
|
326
|
-
f.bar = "world"
|
327
|
-
|
328
|
-
hash = { "foo" => f, "other_hash" => { "foo" => "other_foo", "test" => "other_test" } }
|
329
|
-
assert_equal({ "foo" => { "foo" => "hello", "bar" => "world" },
|
330
|
-
"other_hash" => { "foo" => "other_foo", "test" => "other_test" } }, ActiveSupport::JSON.decode(hash.to_json))
|
331
|
-
end
|
332
|
-
|
333
|
-
def test_array_to_json_should_not_keep_options_around
|
334
|
-
f = CustomWithOptions.new
|
335
|
-
f.foo = "hello"
|
336
|
-
f.bar = "world"
|
337
|
-
|
338
|
-
array = [f, { "foo" => "other_foo", "test" => "other_test" }]
|
339
|
-
assert_equal([{ "foo" => "hello", "bar" => "world" },
|
340
|
-
{ "foo" => "other_foo", "test" => "other_test" }], ActiveSupport::JSON.decode(array.to_json))
|
341
|
-
end
|
342
|
-
|
343
|
-
class OptionsTest
|
344
|
-
def as_json(options = :default)
|
345
|
-
options
|
346
|
-
end
|
347
|
-
end
|
348
|
-
|
349
|
-
def test_hash_as_json_without_options
|
350
|
-
json = { foo: OptionsTest.new }.as_json
|
351
|
-
assert_equal({ "foo" => :default }, json)
|
352
|
-
end
|
353
|
-
|
354
|
-
def test_array_as_json_without_options
|
355
|
-
json = [ OptionsTest.new ].as_json
|
356
|
-
assert_equal([:default], json)
|
357
|
-
end
|
358
|
-
|
359
|
-
def test_struct_encoding
|
360
|
-
Struct.new("UserNameAndEmail", :name, :email)
|
361
|
-
Struct.new("UserNameAndDate", :name, :date)
|
362
|
-
Struct.new("Custom", :name, :sub)
|
363
|
-
user_email = Struct::UserNameAndEmail.new "David", "sample@example.com"
|
364
|
-
user_birthday = Struct::UserNameAndDate.new "David", Date.new(2010, 01, 01)
|
365
|
-
custom = Struct::Custom.new "David", user_birthday
|
366
|
-
|
367
|
-
json_strings = ""
|
368
|
-
json_string_and_date = ""
|
369
|
-
json_custom = ""
|
370
|
-
|
371
|
-
assert_nothing_raised do
|
372
|
-
json_strings = user_email.to_json
|
373
|
-
json_string_and_date = user_birthday.to_json
|
374
|
-
json_custom = custom.to_json
|
375
|
-
end
|
376
|
-
|
377
|
-
assert_equal({ "name" => "David",
|
378
|
-
"sub" => {
|
379
|
-
"name" => "David",
|
380
|
-
"date" => "2010-01-01" } }, ActiveSupport::JSON.decode(json_custom))
|
381
|
-
|
382
|
-
assert_equal({ "name" => "David", "email" => "sample@example.com" },
|
383
|
-
ActiveSupport::JSON.decode(json_strings))
|
384
|
-
|
385
|
-
assert_equal({ "name" => "David", "date" => "2010-01-01" },
|
386
|
-
ActiveSupport::JSON.decode(json_string_and_date))
|
387
|
-
end
|
388
|
-
|
389
|
-
def test_nil_true_and_false_represented_as_themselves
|
390
|
-
assert_nil nil.as_json
|
391
|
-
assert_equal true, true.as_json
|
392
|
-
assert_equal false, false.as_json
|
393
|
-
end
|
394
|
-
|
395
|
-
class HashWithAsJson < Hash
|
396
|
-
attr_accessor :as_json_called
|
397
|
-
|
398
|
-
def initialize(*)
|
399
|
-
super
|
400
|
-
end
|
401
|
-
|
402
|
-
def as_json(options = {})
|
403
|
-
@as_json_called = true
|
404
|
-
super
|
405
|
-
end
|
406
|
-
end
|
407
|
-
|
408
|
-
def test_json_gem_dump_by_passing_active_support_encoder
|
409
|
-
h = HashWithAsJson.new
|
410
|
-
h[:foo] = "hello"
|
411
|
-
h[:bar] = "world"
|
412
|
-
|
413
|
-
assert_equal %({"foo":"hello","bar":"world"}), JSON.dump(h)
|
414
|
-
assert_nil h.as_json_called
|
415
|
-
end
|
416
|
-
|
417
|
-
def test_json_gem_generate_by_passing_active_support_encoder
|
418
|
-
h = HashWithAsJson.new
|
419
|
-
h[:foo] = "hello"
|
420
|
-
h[:bar] = "world"
|
421
|
-
|
422
|
-
assert_equal %({"foo":"hello","bar":"world"}), JSON.generate(h)
|
423
|
-
assert_nil h.as_json_called
|
424
|
-
end
|
425
|
-
|
426
|
-
def test_json_gem_pretty_generate_by_passing_active_support_encoder
|
427
|
-
h = HashWithAsJson.new
|
428
|
-
h[:foo] = "hello"
|
429
|
-
h[:bar] = "world"
|
430
|
-
|
431
|
-
assert_equal <<EXPECTED.chomp, JSON.pretty_generate(h)
|
432
|
-
{
|
433
|
-
"foo": "hello",
|
434
|
-
"bar": "world"
|
435
|
-
}
|
436
|
-
EXPECTED
|
437
|
-
assert_nil h.as_json_called
|
438
|
-
end
|
439
|
-
|
440
|
-
def test_twz_to_json_with_use_standard_json_time_format_config_set_to_false
|
441
|
-
with_standard_json_time_format(false) do
|
442
|
-
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
443
|
-
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
|
444
|
-
assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(time)
|
445
|
-
end
|
446
|
-
end
|
447
|
-
|
448
|
-
def test_twz_to_json_with_use_standard_json_time_format_config_set_to_true
|
449
|
-
with_standard_json_time_format(true) do
|
450
|
-
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
451
|
-
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
|
452
|
-
assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(time)
|
453
|
-
end
|
454
|
-
end
|
455
|
-
|
456
|
-
def test_twz_to_json_with_custom_time_precision
|
457
|
-
with_standard_json_time_format(true) do
|
458
|
-
with_time_precision(0) do
|
459
|
-
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
460
|
-
time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
|
461
|
-
assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time)
|
462
|
-
end
|
463
|
-
end
|
464
|
-
end
|
465
|
-
|
466
|
-
def test_time_to_json_with_custom_time_precision
|
467
|
-
with_standard_json_time_format(true) do
|
468
|
-
with_time_precision(0) do
|
469
|
-
assert_equal "\"2000-01-01T00:00:00Z\"", ActiveSupport::JSON.encode(Time.utc(2000))
|
470
|
-
end
|
471
|
-
end
|
472
|
-
end
|
473
|
-
|
474
|
-
def test_datetime_to_json_with_custom_time_precision
|
475
|
-
with_standard_json_time_format(true) do
|
476
|
-
with_time_precision(0) do
|
477
|
-
assert_equal "\"2000-01-01T00:00:00+00:00\"", ActiveSupport::JSON.encode(DateTime.new(2000))
|
478
|
-
end
|
479
|
-
end
|
480
|
-
end
|
481
|
-
|
482
|
-
def test_twz_to_json_when_wrapping_a_date_time
|
483
|
-
zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
|
484
|
-
time = ActiveSupport::TimeWithZone.new(DateTime.new(2000), zone)
|
485
|
-
assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(time)
|
486
|
-
end
|
487
|
-
|
488
|
-
def test_exception_to_json
|
489
|
-
exception = Exception.new("foo")
|
490
|
-
assert_equal '"foo"', ActiveSupport::JSON.encode(exception)
|
491
|
-
end
|
492
|
-
|
493
|
-
class InfiniteNumber
|
494
|
-
def as_json(options = nil)
|
495
|
-
{ "number" => Float::INFINITY }
|
496
|
-
end
|
497
|
-
end
|
498
|
-
|
499
|
-
def test_to_json_works_when_as_json_returns_infinite_number
|
500
|
-
assert_equal '{"number":null}', InfiniteNumber.new.to_json
|
501
|
-
end
|
502
|
-
|
503
|
-
class NaNNumber
|
504
|
-
def as_json(options = nil)
|
505
|
-
{ "number" => Float::NAN }
|
506
|
-
end
|
507
|
-
end
|
508
|
-
|
509
|
-
def test_to_json_works_when_as_json_returns_NaN_number
|
510
|
-
assert_equal '{"number":null}', NaNNumber.new.to_json
|
511
|
-
end
|
512
|
-
|
513
|
-
def test_to_json_works_on_io_objects
|
514
|
-
assert_equal STDOUT.to_s.to_json, STDOUT.to_json
|
515
|
-
end
|
516
|
-
|
517
|
-
private
|
518
|
-
def object_keys(json_object)
|
519
|
-
json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
|
520
|
-
end
|
521
|
-
|
522
|
-
def with_standard_json_time_format(boolean = true)
|
523
|
-
old, ActiveSupport.use_standard_json_time_format = ActiveSupport.use_standard_json_time_format, boolean
|
524
|
-
yield
|
525
|
-
ensure
|
526
|
-
ActiveSupport.use_standard_json_time_format = old
|
527
|
-
end
|
528
|
-
|
529
|
-
def with_time_precision(value)
|
530
|
-
old_value = ActiveSupport::JSON::Encoding.time_precision
|
531
|
-
ActiveSupport::JSON::Encoding.time_precision = value
|
532
|
-
yield
|
533
|
-
ensure
|
534
|
-
ActiveSupport::JSON::Encoding.time_precision = old_value
|
535
|
-
end
|
536
|
-
end
|
@@ -1,104 +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
|
-
ModuleTests = [[ Module, %("Module") ],
|
72
|
-
[ Class, %("Class") ],
|
73
|
-
[ ActiveSupport, %("ActiveSupport") ],
|
74
|
-
[ ActiveSupport::MessageEncryptor, %("ActiveSupport::MessageEncryptor") ]]
|
75
|
-
ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
|
76
|
-
HashlikeTests = [[ Hashlike.new, %({\"bar\":\"world\",\"foo\":\"hello\"}) ]]
|
77
|
-
StructTests = [[ MyStruct.new(:foo, "bar"), %({\"name\":\"foo\",\"value\":\"bar\"}) ],
|
78
|
-
[ MyStruct.new(nil, nil), %({\"name\":null,\"value\":null}) ]]
|
79
|
-
CustomTests = [[ Custom.new("custom"), '"custom"' ],
|
80
|
-
[ Custom.new(nil), "null" ],
|
81
|
-
[ Custom.new(:a), '"a"' ],
|
82
|
-
[ Custom.new([ :foo, "bar" ]), '["foo","bar"]' ],
|
83
|
-
[ Custom.new(foo: "hello", bar: "world"), '{"bar":"world","foo":"hello"}' ],
|
84
|
-
[ Custom.new(Hashlike.new), '{"bar":"world","foo":"hello"}' ],
|
85
|
-
[ Custom.new(Custom.new(Custom.new(:a))), '"a"' ]]
|
86
|
-
|
87
|
-
RegexpTests = [[ /^a/, '"(?-mix:^a)"' ], [/^\w{1,2}[a-z]+/ix, '"(?ix-m:^\\\\w{1,2}[a-z]+)"']]
|
88
|
-
|
89
|
-
URITests = [[ URI.parse("http://example.com"), %("http://example.com") ]]
|
90
|
-
|
91
|
-
PathnameTests = [[ Pathname.new("lib/index.rb"), %("lib/index.rb") ]]
|
92
|
-
|
93
|
-
IPAddrTests = [[ IPAddr.new("127.0.0.1"), %("127.0.0.1") ]]
|
94
|
-
|
95
|
-
DateTests = [[ Date.new(2005, 2, 1), %("2005/02/01") ]]
|
96
|
-
TimeTests = [[ Time.utc(2005, 2, 1, 15, 15, 10), %("2005/02/01 15:15:10 +0000") ]]
|
97
|
-
DateTimeTests = [[ DateTime.civil(2005, 2, 1, 15, 15, 10), %("2005/02/01 15:15:10 +0000") ]]
|
98
|
-
|
99
|
-
StandardDateTests = [[ Date.new(2005, 2, 1), %("2005-02-01") ]]
|
100
|
-
StandardTimeTests = [[ Time.utc(2005, 2, 1, 15, 15, 10), %("2005-02-01T15:15:10.000Z") ]]
|
101
|
-
StandardDateTimeTests = [[ DateTime.civil(2005, 2, 1, 15, 15, 10), %("2005-02-01T15:15:10.000+00:00") ]]
|
102
|
-
StandardStringTests = [[ "this is the <string>", %("this is the <string>")]]
|
103
|
-
end
|
104
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module TimeZoneTestHelpers
|
4
|
-
def with_tz_default(tz = nil)
|
5
|
-
old_tz = Time.zone
|
6
|
-
Time.zone = tz
|
7
|
-
yield
|
8
|
-
ensure
|
9
|
-
Time.zone = old_tz
|
10
|
-
end
|
11
|
-
|
12
|
-
def with_env_tz(new_tz = "US/Eastern")
|
13
|
-
old_tz, ENV["TZ"] = ENV["TZ"], new_tz
|
14
|
-
yield
|
15
|
-
ensure
|
16
|
-
old_tz ? ENV["TZ"] = old_tz : ENV.delete("TZ")
|
17
|
-
end
|
18
|
-
|
19
|
-
def with_preserve_timezone(value)
|
20
|
-
old_preserve_tz = ActiveSupport.to_time_preserves_timezone
|
21
|
-
ActiveSupport.to_time_preserves_timezone = value
|
22
|
-
yield
|
23
|
-
ensure
|
24
|
-
ActiveSupport.to_time_preserves_timezone = old_preserve_tz
|
25
|
-
end
|
26
|
-
|
27
|
-
def with_tz_mappings(mappings)
|
28
|
-
old_mappings = ActiveSupport::TimeZone::MAPPING.dup
|
29
|
-
ActiveSupport::TimeZone.clear
|
30
|
-
ActiveSupport::TimeZone::MAPPING.clear
|
31
|
-
ActiveSupport::TimeZone::MAPPING.merge!(mappings)
|
32
|
-
|
33
|
-
yield
|
34
|
-
ensure
|
35
|
-
ActiveSupport::TimeZone.clear
|
36
|
-
ActiveSupport::TimeZone::MAPPING.clear
|
37
|
-
ActiveSupport::TimeZone::MAPPING.merge!(old_mappings)
|
38
|
-
end
|
39
|
-
|
40
|
-
def with_utc_to_local_returns_utc_offset_times(value)
|
41
|
-
old_tzinfo2_format = ActiveSupport.utc_to_local_returns_utc_offset_times
|
42
|
-
ActiveSupport.utc_to_local_returns_utc_offset_times = value
|
43
|
-
yield
|
44
|
-
ensure
|
45
|
-
ActiveSupport.utc_to_local_returns_utc_offset_times = old_tzinfo2_format
|
46
|
-
end
|
47
|
-
end
|