44require 'active_support/json'
55require 'active_support/time'
66require 'time_zone_test_helpers'
7+ require 'json/encoding_test_cases'
78
89class TestJSONEncoding < ActiveSupport ::TestCase
910 include TimeZoneTestHelpers
1011
11- class Foo
12- def initialize ( a , b )
13- @a , @b = a , b
14- end
15- end
16-
17- class Hashlike
18- def to_hash
19- { :foo => "hello" , :bar => "world" }
20- end
21- end
22-
23- class Custom
24- def initialize ( serialized )
25- @serialized = serialized
26- end
27-
28- def as_json ( options = nil )
29- @serialized
30- end
31- end
32-
33- class CustomWithOptions
34- attr_accessor :foo , :bar
35-
36- def as_json ( options = { } )
37- options [ :only ] = %w( foo bar )
38- super ( options )
39- end
40- end
41-
42- class OptionsTest
43- def as_json ( options = :default )
44- options
45- end
46- end
47-
48- class HashWithAsJson < Hash
49- attr_accessor :as_json_called
50-
51- def initialize ( *)
52- super
53- end
54-
55- def as_json ( options = { } )
56- @as_json_called = true
57- super
58- end
59- end
60-
61- TrueTests = [ [ true , %(true) ] ]
62- FalseTests = [ [ false , %(false) ] ]
63- NilTests = [ [ nil , %(null) ] ]
64- NumericTests = [ [ 1 , %(1) ] ,
65- [ 2.5 , %(2.5) ] ,
66- [ 0.0 /0.0 , %(null) ] ,
67- [ 1.0 /0.0 , %(null) ] ,
68- [ -1.0 /0.0 , %(null) ] ,
69- [ BigDecimal ( '0.0' ) /BigDecimal ( '0.0' ) , %(null) ] ,
70- [ BigDecimal ( '2.5' ) , %("#{ BigDecimal ( '2.5' ) } ") ] ]
71-
72- StringTests = [ [ 'this is the <string>' , %("this is the \\ u003cstring\\ u003e") ] ,
73- [ 'a "string" with quotes & an ampersand' , %("a \\ "string\\ " with quotes \\ u0026 an ampersand") ] ,
74- [ 'http://test.host/posts/1' , %("http://test.host/posts/1") ] ,
75- [ "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 " ,
76- %("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") ] ]
77-
78- ArrayTests = [ [ [ 'a' , 'b' , 'c' ] , %([\" a\" ,\" b\" ,\" c\" ]) ] ,
79- [ [ 1 , 'a' , :b , nil , false ] , %([1,\" a\" ,\" b\" ,null,false]) ] ]
80-
81- RangeTests = [ [ 1 ..2 , %("1..2") ] ,
82- [ 1 ...2 , %("1...2") ] ,
83- [ 1.5 ..2.5 , %("1.5..2.5") ] ]
84-
85- SymbolTests = [ [ :a , %("a") ] ,
86- [ :this , %("this") ] ,
87- [ :"a b" , %("a b") ] ]
88-
89- ObjectTests = [ [ Foo . new ( 1 , 2 ) , %({\" a\" :1,\" b\" :2}) ] ]
90- HashlikeTests = [ [ Hashlike . new , %({\" bar\" :\" world\" ,\" foo\" :\" hello\" }) ] ]
91- CustomTests = [ [ Custom . new ( "custom" ) , '"custom"' ] ,
92- [ Custom . new ( nil ) , 'null' ] ,
93- [ Custom . new ( :a ) , '"a"' ] ,
94- [ Custom . new ( [ :foo , "bar" ] ) , '["foo","bar"]' ] ,
95- [ Custom . new ( { :foo => "hello" , :bar => "world" } ) , '{"bar":"world","foo":"hello"}' ] ,
96- [ Custom . new ( Hashlike . new ) , '{"bar":"world","foo":"hello"}' ] ,
97- [ Custom . new ( Custom . new ( Custom . new ( :a ) ) ) , '"a"' ] ]
98-
99- RegexpTests = [ [ /^a/ , '"(?-mix:^a)"' ] , [ /^\w {1,2}[a-z]+/ix , '"(?ix-m:^\\\\w{1,2}[a-z]+)"' ] ]
100-
101- DateTests = [ [ Date . new ( 2005 , 2 , 1 ) , %("2005/02/01") ] ]
102- TimeTests = [ [ Time . utc ( 2005 , 2 , 1 , 15 , 15 , 10 ) , %("2005/02/01 15:15:10 +0000") ] ]
103- DateTimeTests = [ [ DateTime . civil ( 2005 , 2 , 1 , 15 , 15 , 10 ) , %("2005/02/01 15:15:10 +0000") ] ]
104-
105- StandardDateTests = [ [ Date . new ( 2005 , 2 , 1 ) , %("2005-02-01") ] ]
106- StandardTimeTests = [ [ Time . utc ( 2005 , 2 , 1 , 15 , 15 , 10 ) , %("2005-02-01T15:15:10.000Z") ] ]
107- StandardDateTimeTests = [ [ DateTime . civil ( 2005 , 2 , 1 , 15 , 15 , 10 ) , %("2005-02-01T15:15:10.000+00:00") ] ]
108- StandardStringTests = [ [ 'this is the <string>' , %("this is the <string>") ] ]
109-
11012 def sorted_json ( json )
11113 return json unless json =~ /^\{ .*\} $/
11214 '{' + json [ 1 ..-2 ] . split ( ',' ) . sort . join ( ',' ) + '}'
11315 end
11416
115- constants . grep ( /Tests$/ ) . each do |class_tests |
17+ JSONTest :: EncodingTestCases . constants . each do |class_tests |
11618 define_method ( "test_#{ class_tests [ 0 ..-6 ] . underscore } " ) do
11719 begin
11820 prev = ActiveSupport . use_standard_json_time_format
11921
12022 ActiveSupport . escape_html_entities_in_json = class_tests !~ /^Standard/
12123 ActiveSupport . use_standard_json_time_format = class_tests =~ /^Standard/
122- self . class . const_get ( class_tests ) . each do |pair |
24+ JSONTest :: EncodingTestCases . const_get ( class_tests ) . each do |pair |
12325 assert_equal pair . last , sorted_json ( ActiveSupport ::JSON . encode ( pair . first ) )
12426 end
12527 ensure
@@ -224,7 +126,7 @@ def test_nested_hash_with_float
224126 end
225127
226128 def test_hash_like_with_options
227- h = Hashlike . new
129+ h = JSONTest :: Hashlike . new
228130 json = h . to_json :only => [ :foo ]
229131
230132 assert_equal ( { "foo" => "hello" } , JSON . parse ( json ) )
@@ -345,6 +247,15 @@ def test_enumerable_should_pass_encoding_options_to_children_in_to_json
345247 assert_equal ( %([{"address":{"city":"London"}},{"address":{"city":"Paris"}}]) , json )
346248 end
347249
250+ class CustomWithOptions
251+ attr_accessor :foo , :bar
252+
253+ def as_json ( options = { } )
254+ options [ :only ] = %w( foo bar )
255+ super ( options )
256+ end
257+ end
258+
348259 def test_hash_to_json_should_not_keep_options_around
349260 f = CustomWithOptions . new
350261 f . foo = "hello"
@@ -365,6 +276,12 @@ def test_array_to_json_should_not_keep_options_around
365276 { "foo" => "other_foo" , "test" => "other_test" } ] , ActiveSupport ::JSON . decode ( array . to_json ) )
366277 end
367278
279+ class OptionsTest
280+ def as_json ( options = :default )
281+ options
282+ end
283+ end
284+
368285 def test_hash_as_json_without_options
369286 json = { foo : OptionsTest . new } . as_json
370287 assert_equal ( { "foo" => :default } , json )
@@ -412,6 +329,19 @@ def test_nil_true_and_false_represented_as_themselves
412329 assert_equal false , false . as_json
413330 end
414331
332+ class HashWithAsJson < Hash
333+ attr_accessor :as_json_called
334+
335+ def initialize ( *)
336+ super
337+ end
338+
339+ def as_json ( options = { } )
340+ @as_json_called = true
341+ super
342+ end
343+ end
344+
415345 def test_json_gem_dump_by_passing_active_support_encoder
416346 h = HashWithAsJson . new
417347 h [ :foo ] = "hello"
0 commit comments