oj 3.7.4 → 3.13.21

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +1352 -0
  3. data/README.md +29 -8
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +53 -72
  6. data/ext/oj/cache.c +326 -0
  7. data/ext/oj/cache.h +21 -0
  8. data/ext/oj/cache8.c +61 -64
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +37 -43
  11. data/ext/oj/circarray.h +16 -17
  12. data/ext/oj/code.c +165 -179
  13. data/ext/oj/code.h +27 -29
  14. data/ext/oj/compat.c +174 -194
  15. data/ext/oj/custom.c +809 -866
  16. data/ext/oj/debug.c +132 -0
  17. data/ext/oj/dump.c +848 -863
  18. data/ext/oj/dump.h +81 -67
  19. data/ext/oj/dump_compat.c +85 -123
  20. data/ext/oj/dump_leaf.c +100 -188
  21. data/ext/oj/dump_object.c +527 -656
  22. data/ext/oj/dump_strict.c +315 -338
  23. data/ext/oj/encode.h +7 -34
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +40 -29
  26. data/ext/oj/err.h +48 -48
  27. data/ext/oj/extconf.rb +17 -4
  28. data/ext/oj/fast.c +1070 -1087
  29. data/ext/oj/intern.c +301 -0
  30. data/ext/oj/intern.h +26 -0
  31. data/ext/oj/mimic_json.c +469 -436
  32. data/ext/oj/object.c +525 -593
  33. data/ext/oj/odd.c +154 -138
  34. data/ext/oj/odd.h +37 -38
  35. data/ext/oj/oj.c +1325 -986
  36. data/ext/oj/oj.h +333 -316
  37. data/ext/oj/parse.c +1002 -846
  38. data/ext/oj/parse.h +92 -87
  39. data/ext/oj/parser.c +1557 -0
  40. data/ext/oj/parser.h +91 -0
  41. data/ext/oj/rails.c +888 -878
  42. data/ext/oj/rails.h +11 -14
  43. data/ext/oj/reader.c +141 -147
  44. data/ext/oj/reader.h +73 -89
  45. data/ext/oj/resolve.c +41 -62
  46. data/ext/oj/resolve.h +7 -9
  47. data/ext/oj/rxclass.c +71 -75
  48. data/ext/oj/rxclass.h +18 -19
  49. data/ext/oj/saj.c +443 -486
  50. data/ext/oj/saj2.c +602 -0
  51. data/ext/oj/scp.c +88 -113
  52. data/ext/oj/sparse.c +787 -709
  53. data/ext/oj/stream_writer.c +133 -159
  54. data/ext/oj/strict.c +127 -118
  55. data/ext/oj/string_writer.c +230 -249
  56. data/ext/oj/trace.c +34 -41
  57. data/ext/oj/trace.h +19 -19
  58. data/ext/oj/usual.c +1254 -0
  59. data/ext/oj/util.c +136 -0
  60. data/ext/oj/util.h +20 -0
  61. data/ext/oj/val_stack.c +59 -67
  62. data/ext/oj/val_stack.h +91 -129
  63. data/ext/oj/validate.c +46 -0
  64. data/ext/oj/wab.c +342 -353
  65. data/lib/oj/bag.rb +1 -0
  66. data/lib/oj/easy_hash.rb +5 -4
  67. data/lib/oj/error.rb +1 -1
  68. data/lib/oj/json.rb +1 -1
  69. data/lib/oj/mimic.rb +48 -14
  70. data/lib/oj/saj.rb +20 -6
  71. data/lib/oj/state.rb +8 -7
  72. data/lib/oj/version.rb +2 -2
  73. data/lib/oj.rb +0 -8
  74. data/pages/Compatibility.md +1 -1
  75. data/pages/JsonGem.md +15 -0
  76. data/pages/Modes.md +53 -46
  77. data/pages/Options.md +72 -11
  78. data/pages/Parser.md +309 -0
  79. data/pages/Rails.md +73 -22
  80. data/pages/Security.md +1 -1
  81. data/test/activerecord/result_test.rb +7 -2
  82. data/test/activesupport5/abstract_unit.rb +45 -0
  83. data/test/activesupport5/decoding_test.rb +68 -60
  84. data/test/activesupport5/encoding_test.rb +111 -96
  85. data/test/activesupport5/encoding_test_cases.rb +33 -25
  86. data/test/activesupport5/test_helper.rb +43 -21
  87. data/test/activesupport5/time_zone_test_helpers.rb +18 -3
  88. data/test/activesupport6/abstract_unit.rb +44 -0
  89. data/test/activesupport6/decoding_test.rb +133 -0
  90. data/test/activesupport6/encoding_test.rb +507 -0
  91. data/test/activesupport6/encoding_test_cases.rb +98 -0
  92. data/test/activesupport6/test_common.rb +17 -0
  93. data/test/activesupport6/test_helper.rb +163 -0
  94. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  95. data/test/activesupport7/abstract_unit.rb +49 -0
  96. data/test/activesupport7/decoding_test.rb +125 -0
  97. data/test/activesupport7/encoding_test.rb +486 -0
  98. data/test/activesupport7/encoding_test_cases.rb +104 -0
  99. data/test/activesupport7/time_zone_test_helpers.rb +47 -0
  100. data/test/bar.rb +6 -12
  101. data/test/baz.rb +16 -0
  102. data/test/bug.rb +16 -0
  103. data/test/foo.rb +69 -75
  104. data/test/helper.rb +16 -0
  105. data/test/json_gem/json_common_interface_test.rb +8 -3
  106. data/test/json_gem/json_generator_test.rb +18 -4
  107. data/test/json_gem/json_parser_test.rb +9 -0
  108. data/test/json_gem/test_helper.rb +12 -0
  109. data/test/mem.rb +33 -0
  110. data/test/perf.rb +1 -1
  111. data/test/perf_dump.rb +50 -0
  112. data/test/perf_once.rb +58 -0
  113. data/test/perf_parser.rb +189 -0
  114. data/test/perf_scp.rb +11 -10
  115. data/test/perf_strict.rb +17 -23
  116. data/test/prec.rb +23 -0
  117. data/test/sample_json.rb +1 -1
  118. data/test/test_compat.rb +46 -10
  119. data/test/test_custom.rb +147 -8
  120. data/test/test_fast.rb +62 -2
  121. data/test/test_file.rb +25 -2
  122. data/test/test_gc.rb +13 -0
  123. data/test/test_generate.rb +21 -0
  124. data/test/test_hash.rb +11 -1
  125. data/test/test_integer_range.rb +7 -2
  126. data/test/test_object.rb +85 -9
  127. data/test/test_parser.rb +27 -0
  128. data/test/test_parser_saj.rb +335 -0
  129. data/test/test_parser_usual.rb +217 -0
  130. data/test/test_rails.rb +35 -0
  131. data/test/test_saj.rb +1 -1
  132. data/test/test_scp.rb +5 -5
  133. data/test/test_strict.rb +26 -1
  134. data/test/test_various.rb +87 -65
  135. data/test/test_wab.rb +2 -0
  136. data/test/test_writer.rb +19 -2
  137. data/test/tests.rb +1 -1
  138. data/test/zoo.rb +13 -0
  139. metadata +60 -110
  140. data/ext/oj/hash.c +0 -163
  141. data/ext/oj/hash.h +0 -46
  142. data/ext/oj/hash_test.c +0 -512
@@ -1,11 +1,13 @@
1
- # coding: utf-8
2
- require 'activesupport5/test_helper'
3
- require 'securerandom'
4
- require 'active_support/core_ext/string/inflections'
5
- require 'active_support/json'
6
- require 'active_support/time'
7
- require 'activesupport5/time_zone_test_helpers'
8
- require 'activesupport5/encoding_test_cases'
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/core_ext/regexp"
7
+ require "active_support/json"
8
+ require "active_support/time"
9
+ require_relative "time_zone_test_helpers"
10
+ require_relative "encoding_test_cases"
9
11
 
10
12
  require 'oj'
11
13
 
@@ -18,9 +20,17 @@ Oj::Rails.optimize()
18
20
  class TestJSONEncoding < ActiveSupport::TestCase
19
21
  include TimeZoneTestHelpers
20
22
 
23
+ # Added for testing if Oj is used.
24
+ test "oj is used as an encoder" do
25
+ assert_equal ActiveSupport.json_encoder, Oj::Rails::Encoder
26
+ end
27
+
21
28
  def sorted_json(json)
22
- return json unless json =~ /^\{.*\}$/
23
- '{' + json[1..-2].split(',').sort.join(',') + '}'
29
+ if json.start_with?("{") && json.end_with?("}")
30
+ "{" + json[1..-2].split(",").sort.join(",") + "}"
31
+ else
32
+ json
33
+ end
24
34
  end
25
35
 
26
36
  JSONTest::EncodingTestCases.constants.each do |class_tests|
@@ -28,8 +38,10 @@ class TestJSONEncoding < ActiveSupport::TestCase
28
38
  begin
29
39
  prev = ActiveSupport.use_standard_json_time_format
30
40
 
31
- ActiveSupport.escape_html_entities_in_json = class_tests !~ /^Standard/
32
- ActiveSupport.use_standard_json_time_format = class_tests =~ /^Standard/
41
+ standard_class_tests = /Standard/.match?(class_tests)
42
+
43
+ ActiveSupport.escape_html_entities_in_json = !standard_class_tests
44
+ ActiveSupport.use_standard_json_time_format = standard_class_tests
33
45
  JSONTest::EncodingTestCases.const_get(class_tests).each do |pair|
34
46
  assert_equal pair.last, sorted_json(ActiveSupport::JSON.encode(pair.first))
35
47
  end
@@ -50,12 +62,12 @@ class TestJSONEncoding < ActiveSupport::TestCase
50
62
  end
51
63
 
52
64
  def test_hash_encoding
53
- assert_equal %({\"a\":\"b\"}), ActiveSupport::JSON.encode(:a => :b)
54
- assert_equal %({\"a\":1}), ActiveSupport::JSON.encode('a' => 1)
55
- assert_equal %({\"a\":[1,2]}), ActiveSupport::JSON.encode('a' => [1,2])
65
+ assert_equal %({\"a\":\"b\"}), ActiveSupport::JSON.encode(a: :b)
66
+ assert_equal %({\"a\":1}), ActiveSupport::JSON.encode("a" => 1)
67
+ assert_equal %({\"a\":[1,2]}), ActiveSupport::JSON.encode("a" => [1, 2])
56
68
  assert_equal %({"1":2}), ActiveSupport::JSON.encode(1 => 2)
57
69
 
58
- assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json(ActiveSupport::JSON.encode(:a => :b, :c => :d))
70
+ assert_equal %({\"a\":\"b\",\"c\":\"d\"}), sorted_json(ActiveSupport::JSON.encode(a: :b, c: :d))
59
71
  end
60
72
 
61
73
  def test_hash_keys_encoding
@@ -68,28 +80,28 @@ class TestJSONEncoding < ActiveSupport::TestCase
68
80
  def test_utf8_string_encoded_properly
69
81
  # The original test seems to expect that
70
82
  # ActiveSupport.escape_html_entities_in_json reverts to true even after
71
- # being set to false. I haven't been able to figure that out so the value
72
- # is set to true, the default, before running the test. This might be
73
- # wrong but for now it will have to do.
74
- ActiveSupport.escape_html_entities_in_json = true
75
- result = ActiveSupport::JSON.encode('€2.99')
83
+ # being set to false. I haven't been able to figure that out so the value is
84
+ # set to true, the default, before running the test. This might be wrong but
85
+ # for now it will have to do.
86
+ ActiveSupport.escape_html_entities_in_json = true
87
+ result = ActiveSupport::JSON.encode("€2.99")
76
88
  assert_equal '"€2.99"', result
77
89
  assert_equal(Encoding::UTF_8, result.encoding)
78
90
 
79
- result = ActiveSupport::JSON.encode('✎☺')
91
+ result = ActiveSupport::JSON.encode("✎☺")
80
92
  assert_equal '"✎☺"', result
81
93
  assert_equal(Encoding::UTF_8, result.encoding)
82
94
  end
83
95
 
84
96
  def test_non_utf8_string_transcodes
85
- s = ''.encode('Shift_JIS')
97
+ s = "".encode("Shift_JIS")
86
98
  result = ActiveSupport::JSON.encode(s)
87
99
  assert_equal '"二"', result
88
100
  assert_equal Encoding::UTF_8, result.encoding
89
101
  end
90
102
 
91
103
  def test_wide_utf8_chars
92
- w = '𠜎'
104
+ w = "𠜎"
93
105
  result = ActiveSupport::JSON.encode(w)
94
106
  assert_equal '"𠜎"', result
95
107
  end
@@ -98,33 +110,33 @@ class TestJSONEncoding < ActiveSupport::TestCase
98
110
  hash = { string: "𐒑" }
99
111
  json = ActiveSupport::JSON.encode(hash)
100
112
  decoded_hash = ActiveSupport::JSON.decode(json)
101
- assert_equal "𐒑", decoded_hash['string']
113
+ assert_equal "𐒑", decoded_hash["string"]
102
114
  end
103
115
 
104
116
  def test_hash_key_identifiers_are_always_quoted
105
- values = {0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B"}
117
+ values = { 0 => 0, 1 => 1, :_ => :_, "$" => "$", "a" => "a", :A => :A, :A0 => :A0, "A0B" => "A0B" }
106
118
  assert_equal %w( "$" "A" "A0" "A0B" "_" "a" "0" "1" ).sort, object_keys(ActiveSupport::JSON.encode(values))
107
119
  end
108
120
 
109
121
  def test_hash_should_allow_key_filtering_with_only
110
- assert_equal %({"a":1}), ActiveSupport::JSON.encode({'a' => 1, :b => 2, :c => 3}, :only => 'a')
122
+ assert_equal %({"a":1}), ActiveSupport::JSON.encode({ "a" => 1, :b => 2, :c => 3 }, { only: "a" })
111
123
  end
112
124
 
113
125
  def test_hash_should_allow_key_filtering_with_except
114
- assert_equal %({"b":2}), ActiveSupport::JSON.encode({'foo' => 'bar', :b => 2, :c => 3}, :except => ['foo', :c])
126
+ assert_equal %({"b":2}), ActiveSupport::JSON.encode({ "foo" => "bar", :b => 2, :c => 3 }, { except: ["foo", :c] })
115
127
  end
116
128
 
117
129
  def test_time_to_json_includes_local_offset
118
130
  with_standard_json_time_format(true) do
119
- with_env_tz 'US/Eastern' do
120
- assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005,2,1,15,15,10))
131
+ with_env_tz "US/Eastern" do
132
+ assert_equal %("2005-02-01T15:15:10.000-05:00"), ActiveSupport::JSON.encode(Time.local(2005, 2, 1, 15, 15, 10))
121
133
  end
122
134
  end
123
135
  end
124
136
 
125
137
  def test_hash_with_time_to_json
126
138
  with_standard_json_time_format(false) do
127
- assert_equal '{"time":"2009/01/01 00:00:00 +0000"}', { :time => Time.utc(2009) }.to_json
139
+ assert_equal '{"time":"2009/01/01 00:00:00 +0000"}', { time: Time.utc(2009) }.to_json
128
140
  end
129
141
  end
130
142
 
@@ -132,8 +144,8 @@ class TestJSONEncoding < ActiveSupport::TestCase
132
144
  assert_nothing_raised do
133
145
  hash = {
134
146
  "CHI" => {
135
- :display_name => "chicago",
136
- :latitude => 123.234
147
+ display_name: "chicago",
148
+ latitude: 123.234
137
149
  }
138
150
  }
139
151
  ActiveSupport::JSON.encode(hash)
@@ -142,64 +154,64 @@ class TestJSONEncoding < ActiveSupport::TestCase
142
154
 
143
155
  def test_hash_like_with_options
144
156
  h = JSONTest::Hashlike.new
145
- json = h.to_json :only => [:foo]
157
+ json = h.to_json only: [:foo]
146
158
 
147
- assert_equal({"foo"=>"hello"}, JSON.parse(json))
159
+ assert_equal({ "foo" => "hello" }, JSON.parse(json))
148
160
  end
149
161
 
150
162
  def test_object_to_json_with_options
151
163
  obj = Object.new
152
164
  obj.instance_variable_set :@foo, "hello"
153
165
  obj.instance_variable_set :@bar, "world"
154
- json = obj.to_json :only => ["foo"]
166
+ json = obj.to_json only: ["foo"]
155
167
 
156
- assert_equal({"foo"=>"hello"}, JSON.parse(json))
168
+ assert_equal({ "foo" => "hello" }, JSON.parse(json))
157
169
  end
158
170
 
159
171
  def test_struct_to_json_with_options
160
172
  struct = Struct.new(:foo, :bar).new
161
173
  struct.foo = "hello"
162
174
  struct.bar = "world"
163
- json = struct.to_json :only => [:foo]
175
+ json = struct.to_json only: [:foo]
164
176
 
165
- assert_equal({"foo"=>"hello"}, JSON.parse(json))
177
+ assert_equal({ "foo" => "hello" }, JSON.parse(json))
166
178
  end
167
179
 
168
180
  def test_hash_should_pass_encoding_options_to_children_in_as_json
169
181
  person = {
170
- :name => 'John',
171
- :address => {
172
- :city => 'London',
173
- :country => 'UK'
182
+ name: "John",
183
+ address: {
184
+ city: "London",
185
+ country: "UK"
174
186
  }
175
187
  }
176
- json = person.as_json :only => [:address, :city]
188
+ json = person.as_json only: [:address, :city]
177
189
 
178
- assert_equal({ 'address' => { 'city' => 'London' }}, json)
190
+ assert_equal({ "address" => { "city" => "London" } }, json)
179
191
  end
180
192
 
181
193
  def test_hash_should_pass_encoding_options_to_children_in_to_json
182
194
  person = {
183
- :name => 'John',
184
- :address => {
185
- :city => 'London',
186
- :country => 'UK'
195
+ name: "John",
196
+ address: {
197
+ city: "London",
198
+ country: "UK"
187
199
  }
188
200
  }
189
- json = person.to_json :only => [:address, :city]
201
+ json = person.to_json only: [:address, :city]
190
202
 
191
203
  assert_equal(%({"address":{"city":"London"}}), json)
192
204
  end
193
205
 
194
206
  def test_array_should_pass_encoding_options_to_children_in_as_json
195
207
  people = [
196
- { :name => 'John', :address => { :city => 'London', :country => 'UK' }},
197
- { :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }}
208
+ { name: "John", address: { city: "London", country: "UK" } },
209
+ { name: "Jean", address: { city: "Paris", country: "France" } }
198
210
  ]
199
- json = people.as_json :only => [:address, :city]
211
+ json = people.as_json only: [:address, :city]
200
212
  expected = [
201
- { 'address' => { 'city' => 'London' }},
202
- { 'address' => { 'city' => 'Paris' }}
213
+ { "address" => { "city" => "London" } },
214
+ { "address" => { "city" => "Paris" } }
203
215
  ]
204
216
 
205
217
  assert_equal(expected, json)
@@ -207,20 +219,20 @@ class TestJSONEncoding < ActiveSupport::TestCase
207
219
 
208
220
  def test_array_should_pass_encoding_options_to_children_in_to_json
209
221
  people = [
210
- { :name => 'John', :address => { :city => 'London', :country => 'UK' }},
211
- { :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }}
222
+ { name: "John", address: { city: "London", country: "UK" } },
223
+ { name: "Jean", address: { city: "Paris", country: "France" } }
212
224
  ]
213
- json = people.to_json :only => [:address, :city]
225
+ json = people.to_json only: [:address, :city]
214
226
 
215
227
  assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
216
228
  end
217
229
 
218
230
  People = Class.new(BasicObject) do
219
231
  include Enumerable
220
- def initialize()
232
+ def initialize
221
233
  @people = [
222
- { :name => 'John', :address => { :city => 'London', :country => 'UK' }},
223
- { :name => 'Jean', :address => { :city => 'Paris' , :country => 'France' }}
234
+ { name: "John", address: { city: "London", country: "UK" } },
235
+ { name: "Jean", address: { city: "Paris", country: "France" } }
224
236
  ]
225
237
  end
226
238
  def each(*, &blk)
@@ -232,32 +244,32 @@ class TestJSONEncoding < ActiveSupport::TestCase
232
244
  end
233
245
 
234
246
  def test_enumerable_should_generate_json_with_as_json
235
- json = People.new.as_json :only => [:address, :city]
247
+ json = People.new.as_json only: [:address, :city]
236
248
  expected = [
237
- { 'address' => { 'city' => 'London' }},
238
- { 'address' => { 'city' => 'Paris' }}
249
+ { "address" => { "city" => "London" } },
250
+ { "address" => { "city" => "Paris" } }
239
251
  ]
240
252
 
241
253
  assert_equal(expected, json)
242
254
  end
243
255
 
244
256
  def test_enumerable_should_generate_json_with_to_json
245
- json = People.new.to_json :only => [:address, :city]
257
+ json = People.new.to_json only: [:address, :city]
246
258
  assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
247
259
  end
248
260
 
249
261
  def test_enumerable_should_pass_encoding_options_to_children_in_as_json
250
- json = People.new.each.as_json :only => [:address, :city]
262
+ json = People.new.each.as_json only: [:address, :city]
251
263
  expected = [
252
- { 'address' => { 'city' => 'London' }},
253
- { 'address' => { 'city' => 'Paris' }}
264
+ { "address" => { "city" => "London" } },
265
+ { "address" => { "city" => "Paris" } }
254
266
  ]
255
267
 
256
268
  assert_equal(expected, json)
257
269
  end
258
270
 
259
271
  def test_enumerable_should_pass_encoding_options_to_children_in_to_json
260
- json = People.new.each.to_json :only => [:address, :city]
272
+ json = People.new.each.to_json only: [:address, :city]
261
273
 
262
274
  assert_equal(%([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]), json)
263
275
  end
@@ -265,7 +277,7 @@ class TestJSONEncoding < ActiveSupport::TestCase
265
277
  class CustomWithOptions
266
278
  attr_accessor :foo, :bar
267
279
 
268
- def as_json(options={})
280
+ def as_json(options = {})
269
281
  options[:only] = %w(foo bar)
270
282
  super(options)
271
283
  end
@@ -276,9 +288,9 @@ class TestJSONEncoding < ActiveSupport::TestCase
276
288
  f.foo = "hello"
277
289
  f.bar = "world"
278
290
 
279
- hash = {"foo" => f, "other_hash" => {"foo" => "other_foo", "test" => "other_test"}}
280
- assert_equal({"foo"=>{"foo"=>"hello","bar"=>"world"},
281
- "other_hash" => {"foo"=>"other_foo","test"=>"other_test"}}, ActiveSupport::JSON.decode(hash.to_json))
291
+ hash = { "foo" => f, "other_hash" => { "foo" => "other_foo", "test" => "other_test" } }
292
+ assert_equal({ "foo" => { "foo" => "hello", "bar" => "world" },
293
+ "other_hash" => { "foo" => "other_foo", "test" => "other_test" } }, ActiveSupport::JSON.decode(hash.to_json))
282
294
  end
283
295
 
284
296
  def test_array_to_json_should_not_keep_options_around
@@ -286,9 +298,9 @@ class TestJSONEncoding < ActiveSupport::TestCase
286
298
  f.foo = "hello"
287
299
  f.bar = "world"
288
300
 
289
- array = [f, {"foo" => "other_foo", "test" => "other_test"}]
290
- assert_equal([{"foo"=>"hello","bar"=>"world"},
291
- {"foo"=>"other_foo","test"=>"other_test"}], ActiveSupport::JSON.decode(array.to_json))
301
+ array = [f, { "foo" => "other_foo", "test" => "other_test" }]
302
+ assert_equal([{ "foo" => "hello", "bar" => "world" },
303
+ { "foo" => "other_foo", "test" => "other_test" }], ActiveSupport::JSON.decode(array.to_json))
292
304
  end
293
305
 
294
306
  class OptionsTest
@@ -299,7 +311,7 @@ class TestJSONEncoding < ActiveSupport::TestCase
299
311
 
300
312
  def test_hash_as_json_without_options
301
313
  json = { foo: OptionsTest.new }.as_json
302
- assert_equal({"foo" => :default}, json)
314
+ assert_equal({ "foo" => :default }, json)
303
315
  end
304
316
 
305
317
  def test_array_as_json_without_options
@@ -308,13 +320,12 @@ class TestJSONEncoding < ActiveSupport::TestCase
308
320
  end
309
321
 
310
322
  def test_struct_encoding
311
- Struct.new('UserNameAndEmail', :name, :email)
312
- Struct.new('UserNameAndDate', :name, :date)
313
- Struct.new('Custom', :name, :sub)
314
- user_email = Struct::UserNameAndEmail.new 'David', 'sample@example.com'
315
- user_birthday = Struct::UserNameAndDate.new 'David', Date.new(2010, 01, 01)
316
- custom = Struct::Custom.new 'David', user_birthday
317
-
323
+ Struct.new("UserNameAndEmail", :name, :email)
324
+ Struct.new("UserNameAndDate", :name, :date)
325
+ Struct.new("Custom", :name, :sub)
326
+ user_email = Struct::UserNameAndEmail.new "David", "sample@example.com"
327
+ user_birthday = Struct::UserNameAndDate.new "David", Date.new(2010, 01, 01)
328
+ custom = Struct::Custom.new "David", user_birthday
318
329
 
319
330
  json_strings = ""
320
331
  json_string_and_date = ""
@@ -326,20 +337,20 @@ class TestJSONEncoding < ActiveSupport::TestCase
326
337
  json_custom = custom.to_json
327
338
  end
328
339
 
329
- assert_equal({"name" => "David",
340
+ assert_equal({ "name" => "David",
330
341
  "sub" => {
331
342
  "name" => "David",
332
- "date" => "2010-01-01" }}, ActiveSupport::JSON.decode(json_custom))
343
+ "date" => "2010-01-01" } }, ActiveSupport::JSON.decode(json_custom))
333
344
 
334
- assert_equal({"name" => "David", "email" => "sample@example.com"},
345
+ assert_equal({ "name" => "David", "email" => "sample@example.com" },
335
346
  ActiveSupport::JSON.decode(json_strings))
336
347
 
337
- assert_equal({"name" => "David", "date" => "2010-01-01"},
348
+ assert_equal({ "name" => "David", "date" => "2010-01-01" },
338
349
  ActiveSupport::JSON.decode(json_string_and_date))
339
350
  end
340
351
 
341
352
  def test_nil_true_and_false_represented_as_themselves
342
- assert_nil nil.as_json
353
+ assert_nil nil.as_json
343
354
  assert_equal true, true.as_json
344
355
  assert_equal false, false.as_json
345
356
  end
@@ -351,7 +362,7 @@ class TestJSONEncoding < ActiveSupport::TestCase
351
362
  super
352
363
  end
353
364
 
354
- def as_json(options={})
365
+ def as_json(options = {})
355
366
  @as_json_called = true
356
367
  super
357
368
  end
@@ -391,7 +402,7 @@ EXPECTED
391
402
 
392
403
  def test_twz_to_json_with_use_standard_json_time_format_config_set_to_false
393
404
  with_standard_json_time_format(false) do
394
- zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
405
+ zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
395
406
  time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
396
407
  assert_equal "\"1999/12/31 19:00:00 -0500\"", ActiveSupport::JSON.encode(time)
397
408
  end
@@ -399,7 +410,7 @@ EXPECTED
399
410
 
400
411
  def test_twz_to_json_with_use_standard_json_time_format_config_set_to_true
401
412
  with_standard_json_time_format(true) do
402
- zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
413
+ zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
403
414
  time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
404
415
  assert_equal "\"1999-12-31T19:00:00.000-05:00\"", ActiveSupport::JSON.encode(time)
405
416
  end
@@ -408,7 +419,7 @@ EXPECTED
408
419
  def test_twz_to_json_with_custom_time_precision
409
420
  with_standard_json_time_format(true) do
410
421
  with_time_precision(0) do
411
- zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
422
+ zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
412
423
  time = ActiveSupport::TimeWithZone.new(Time.utc(2000), zone)
413
424
  assert_equal "\"1999-12-31T19:00:00-05:00\"", ActiveSupport::JSON.encode(time)
414
425
  end
@@ -432,7 +443,7 @@ EXPECTED
432
443
  end
433
444
 
434
445
  def test_twz_to_json_when_wrapping_a_date_time
435
- zone = ActiveSupport::TimeZone['Eastern Time (US & Canada)']
446
+ zone = ActiveSupport::TimeZone["Eastern Time (US & Canada)"]
436
447
  time = ActiveSupport::TimeWithZone.new(DateTime.new(2000), zone)
437
448
  assert_equal '"1999-12-31T19:00:00.000-05:00"', ActiveSupport::JSON.encode(time)
438
449
  end
@@ -454,7 +465,7 @@ EXPECTED
454
465
 
455
466
  class NaNNumber
456
467
  def as_json(options = nil)
457
- { "number" => Float::INFINITY }
468
+ { "number" => Float::NAN }
458
469
  end
459
470
  end
460
471
 
@@ -462,7 +473,11 @@ EXPECTED
462
473
  assert_equal '{"number":null}', NaNNumber.new.to_json
463
474
  end
464
475
 
465
- protected
476
+ def test_to_json_works_on_io_objects
477
+ assert_equal STDOUT.to_s.to_json, STDOUT.to_json
478
+ end
479
+
480
+ private
466
481
 
467
482
  def object_keys(json_object)
468
483
  json_object[1..-2].scan(/([^{}:,\s]+):/).flatten.sort
@@ -1,6 +1,10 @@
1
- require 'bigdecimal'
2
- require 'date'
3
- require 'time'
1
+ # frozen_string_literal: true
2
+
3
+ require "bigdecimal"
4
+ require "date"
5
+ require "time"
6
+ require "pathname"
7
+ require "uri"
4
8
 
5
9
  module JSONTest
6
10
  class Foo
@@ -11,7 +15,7 @@ module JSONTest
11
15
 
12
16
  class Hashlike
13
17
  def to_hash
14
- { :foo => "hello", :bar => "world" }
18
+ { foo: "hello", bar: "world" }
15
19
  end
16
20
  end
17
21
 
@@ -25,7 +29,7 @@ module JSONTest
25
29
  end
26
30
  end
27
31
 
28
- class MyStruct < Struct.new(:name, :value)
32
+ MyStruct = Struct.new(:name, :value) do
29
33
  def initialize(*)
30
34
  @unused = "unused instance variable"
31
35
  super
@@ -38,23 +42,23 @@ module JSONTest
38
42
  NilTests = [[ nil, %(null) ]]
39
43
  NumericTests = [[ 1, %(1) ],
40
44
  [ 2.5, %(2.5) ],
41
- [ 0.0/0.0, %(null) ],
42
- [ 1.0/0.0, %(null) ],
43
- [ -1.0/0.0, %(null) ],
44
- [ BigDecimal('0.0')/BigDecimal('0.0'), %(null) ],
45
- [ BigDecimal('2.5'), %("#{BigDecimal('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')}") ]]
46
50
 
47
- StringTests = [[ 'this is the <string>', %("this is the \\u003cstring\\u003e")],
51
+ StringTests = [[ "this is the <string>", %("this is the \\u003cstring\\u003e")],
48
52
  [ 'a "string" with quotes & an ampersand', %("a \\"string\\" with quotes \\u0026 an ampersand") ],
49
- [ 'http://test.host/posts/1', %("http://test.host/posts/1")],
53
+ [ "http://test.host/posts/1", %("http://test.host/posts/1")],
50
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",
51
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") ]]
52
56
 
53
- ArrayTests = [[ ['a', 'b', 'c'], %([\"a\",\"b\",\"c\"]) ],
54
- [ [1, 'a', :b, nil, false], %([1,\"a\",\"b\",null,false]) ]]
57
+ ArrayTests = [[ ["a", "b", "c"], %([\"a\",\"b\",\"c\"]) ],
58
+ [ [1, "a", :b, nil, false], %([1,\"a\",\"b\",null,false]) ]]
55
59
 
56
- HashTests = [[ {foo: "bar"}, %({\"foo\":\"bar\"}) ],
57
- [ {1 => 1, 2 => 'a', 3 => :b, 4 => nil, 5 => false}, %({\"1\":1,\"2\":\"a\",\"3\":\"b\",\"4\":null,\"5\":false}) ]]
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}) ]]
58
62
 
59
63
  RangeTests = [[ 1..2, %("1..2")],
60
64
  [ 1...2, %("1...2")],
@@ -69,22 +73,26 @@ module JSONTest
69
73
  StructTests = [[ MyStruct.new(:foo, "bar"), %({\"name\":\"foo\",\"value\":\"bar\"}) ],
70
74
  [ MyStruct.new(nil, nil), %({\"name\":null,\"value\":null}) ]]
71
75
  CustomTests = [[ Custom.new("custom"), '"custom"' ],
72
- [ Custom.new(nil), 'null' ],
76
+ [ Custom.new(nil), "null" ],
73
77
  [ Custom.new(:a), '"a"' ],
74
78
  [ Custom.new([ :foo, "bar" ]), '["foo","bar"]' ],
75
- [ Custom.new({ :foo => "hello", :bar => "world" }), '{"bar":"world","foo":"hello"}' ],
79
+ [ Custom.new(foo: "hello", bar: "world"), '{"bar":"world","foo":"hello"}' ],
76
80
  [ Custom.new(Hashlike.new), '{"bar":"world","foo":"hello"}' ],
77
81
  [ Custom.new(Custom.new(Custom.new(:a))), '"a"' ]]
78
82
 
79
83
  RegexpTests = [[ /^a/, '"(?-mix:^a)"' ], [/^\w{1,2}[a-z]+/ix, '"(?ix-m:^\\\\w{1,2}[a-z]+)"']]
80
84
 
81
- DateTests = [[ Date.new(2005,2,1), %("2005/02/01") ]]
82
- TimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]]
83
- DateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005/02/01 15:15:10 +0000") ]]
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") ]]
84
92
 
85
- StandardDateTests = [[ Date.new(2005,2,1), %("2005-02-01") ]]
86
- StandardTimeTests = [[ Time.utc(2005,2,1,15,15,10), %("2005-02-01T15:15:10.000Z") ]]
87
- StandardDateTimeTests = [[ DateTime.civil(2005,2,1,15,15,10), %("2005-02-01T15:15:10.000+00:00") ]]
88
- StandardStringTests = [[ 'this is the <string>', %("this is the <string>")]]
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>")]]
89
97
  end
90
98
  end
@@ -1,25 +1,54 @@
1
- require 'active_support/testing/assertions'
2
- require 'active_support/testing/deprecation'
3
- require 'active_support/testing/declarative'
4
- require 'minitest/autorun'
1
+ # frozen_string_literal: true
2
+
3
+ gem "minitest" # make sure we get the gem, not stdlib
4
+ require "minitest"
5
+ require "active_support/testing/tagged_logging"
6
+ require "active_support/testing/setup_and_teardown"
7
+ require "active_support/testing/assertions"
8
+ require "active_support/testing/deprecation"
9
+ require "active_support/testing/declarative"
10
+ require "active_support/testing/isolation"
11
+ require "active_support/testing/constant_lookup"
12
+ require "active_support/testing/time_helpers"
13
+ require "active_support/testing/file_fixtures"
5
14
 
6
15
  module ActiveSupport
7
16
  class TestCase < ::Minitest::Test
8
- # Skips the current run on Rubinius using Minitest::Assertions#skip
9
- private def rubinius_skip(message = "")
10
- skip message if RUBY_ENGINE == "rbx"
11
- end
12
- # Skips the current run on JRuby using Minitest::Assertions#skip
13
- private def jruby_skip(message = "")
14
- skip message if defined?(JRUBY_VERSION)
15
- end
16
-
17
17
  Assertion = Minitest::Assertion
18
18
 
19
+ class << self
20
+ # Sets the order in which test cases are run.
21
+ #
22
+ # ActiveSupport::TestCase.test_order = :random # => :random
23
+ #
24
+ # Valid values are:
25
+ # * +:random+ (to run tests in random order)
26
+ # * +:parallel+ (to run tests in parallel)
27
+ # * +:sorted+ (to run tests alphabetically by method name)
28
+ # * +:alpha+ (equivalent to +:sorted+)
29
+ def test_order=(new_order)
30
+ ActiveSupport.test_order = new_order
31
+ end
32
+
33
+ # Returns the order in which test cases are run.
34
+ #
35
+ # ActiveSupport::TestCase.test_order # => :random
36
+ #
37
+ # Possible values are +:random+, +:parallel+, +:alpha+, +:sorted+.
38
+ # Defaults to +:random+.
39
+ def test_order
40
+ ActiveSupport.test_order ||= :random
41
+ end
42
+ end
43
+
19
44
  alias_method :method_name, :name
20
45
 
46
+ include ActiveSupport::Testing::TaggedLogging
47
+ prepend ActiveSupport::Testing::SetupAndTeardown
21
48
  include ActiveSupport::Testing::Assertions
22
49
  include ActiveSupport::Testing::Deprecation
50
+ include ActiveSupport::Testing::TimeHelpers
51
+ include ActiveSupport::Testing::FileFixtures
23
52
  extend ActiveSupport::Testing::Declarative
24
53
 
25
54
  # test/unit backwards compatibility methods
@@ -38,13 +67,6 @@ module ActiveSupport
38
67
  alias :assert_not_respond_to :refute_respond_to
39
68
  alias :assert_not_same :refute_same
40
69
 
41
- # Fails if the block raises an exception.
42
- #
43
- # assert_nothing_raised do
44
- # ...
45
- # end
46
- def assert_nothing_raised(*args)
47
- yield
48
- end
70
+ ActiveSupport.run_load_hooks(:active_support_test_case, self)
49
71
  end
50
72
  end