Skip to content

Commit 696a19f

Browse files
committed
Expand the JSON test coverage for Struct and Hash (?!)
1 parent 2926d73 commit 696a19f

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

activesupport/test/json/encoding_test_cases.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,13 @@ def as_json(options = nil)
2323
end
2424
end
2525

26+
class MyStruct < Struct.new(:name, :value)
27+
def initialize(*)
28+
@unused = "unused instance variable"
29+
super
30+
end
31+
end
32+
2633
module EncodingTestCases
2734
TrueTests = [[ true, %(true) ]]
2835
FalseTests = [[ false, %(false) ]]
@@ -41,9 +48,12 @@ module EncodingTestCases
4148
[ "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",
4249
%("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") ]]
4350

44-
ArrayTests = [[ ['a', 'b', 'c'], %([\"a\",\"b\",\"c\"]) ],
51+
ArrayTests = [[ ['a', 'b', 'c'], %([\"a\",\"b\",\"c\"]) ],
4552
[ [1, 'a', :b, nil, false], %([1,\"a\",\"b\",null,false]) ]]
4653

54+
HashTests = [[ {foo: "bar"}, %({\"foo\":\"bar\"}) ],
55+
[ {1 => 1, 2 => 'a', 3 => :b, 4 => nil, 5 => false}, %({\"1\":1,\"2\":\"a\",\"3\":\"b\",\"4\":null,\"5\":false}) ]]
56+
4757
RangeTests = [[ 1..2, %("1..2")],
4858
[ 1...2, %("1...2")],
4959
[ 1.5..2.5, %("1.5..2.5")]]
@@ -54,6 +64,8 @@ module EncodingTestCases
5464

5565
ObjectTests = [[ Foo.new(1, 2), %({\"a\":1,\"b\":2}) ]]
5666
HashlikeTests = [[ Hashlike.new, %({\"bar\":\"world\",\"foo\":\"hello\"}) ]]
67+
StructTests = [[ MyStruct.new(:foo, "bar"), %({\"name\":\"foo\",\"value\":\"bar\"}) ],
68+
[ MyStruct.new(nil, nil), %({\"name\":null,\"value\":null}) ]]
5769
CustomTests = [[ Custom.new("custom"), '"custom"' ],
5870
[ Custom.new(nil), 'null' ],
5971
[ Custom.new(:a), '"a"' ],

0 commit comments

Comments
 (0)