summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rwxr-xr-xtest/json/json_generator_test.rb77
1 files changed, 64 insertions, 13 deletions
diff --git a/test/json/json_generator_test.rb b/test/json/json_generator_test.rb
index e623e05409..9f8b35de09 100755
--- a/test/json/json_generator_test.rb
+++ b/test/json/json_generator_test.rb
@@ -321,7 +321,8 @@ class JSONGeneratorTest < Test::Unit::TestCase
end
end
- def test_depth_bad_to_json
+ # An object that changes state.depth when it receives to_json(state)
+ def bad_to_json
obj = Object.new
def obj.to_json(state)
state.depth += 1
@@ -329,21 +330,44 @@ class JSONGeneratorTest < Test::Unit::TestCase
"#{state.indent * state.depth}\"foo\":#{state.space}1#{state.object_nl}"\
"#{state.indent * (state.depth - 1)}}"
end
- assert_equal <<~JSON.chomp, JSON.pretty_generate([obj] * 2)
+ obj
+ end
+
+ def test_depth_restored_bad_to_json
+ state = JSON::State.new
+ state.generate(bad_to_json)
+ assert_equal 0, state.depth
+ end
+
+ def test_depth_restored_bad_to_json_in_Array
+ assert_equal <<~JSON.chomp, JSON.pretty_generate([bad_to_json] * 2)
[
{
"foo": 1
},
{
- "foo": 1
- }
+ "foo": 1
+ }
]
JSON
- state = JSON::State.new(object_nl: "\n", array_nl: "\n", space: " ", indent: " ")
- state.generate(obj)
- assert_equal 1, state.depth # FIXME
- state.depth = 0
- state.generate([obj])
+ state = JSON::State.new
+ state.generate([bad_to_json])
+ assert_equal 0, state.depth
+ end
+
+ def test_depth_restored_bad_to_json_in_Hash
+ assert_equal <<~JSON.chomp, JSON.pretty_generate(a: bad_to_json, b: bad_to_json)
+ {
+ "a": {
+ "foo": 1
+ },
+ "b": {
+ "foo": 1
+ }
+ }
+ JSON
+ state = JSON::State.new
+ state.generate(a: bad_to_json)
assert_equal 0, state.depth
end
@@ -361,10 +385,36 @@ class JSONGeneratorTest < Test::Unit::TestCase
ary = []; ary << ary
assert_raise(JSON::NestingError) { generate(ary) }
assert_raise(JSON::NestingError) { JSON.pretty_generate(ary) }
- s = JSON.state.new
- assert_equal 0, s.depth
+ end
+
+ def test_depth_nesting_error_to_json
+ ary = []; ary << ary
+ s = JSON.state.new(depth: 1)
assert_raise(JSON::NestingError) { ary.to_json(s) }
- assert_equal 100, s.depth
+ assert_equal 1, s.depth
+ end
+
+ def test_depth_nesting_error_Hash_to_json
+ hash = {}; hash[:a] = hash
+ s = JSON.state.new(depth: 1)
+ assert_raise(JSON::NestingError) { hash.to_json(s) }
+ assert_equal 1, s.depth
+ end
+
+ def test_depth_nesting_error_generate
+ ary = []; ary << ary
+ s = JSON.state.new(depth: 1)
+ assert_raise(JSON::NestingError) { s.generate(ary) }
+ assert_equal 1, s.depth
+ end
+
+ def test_depth_exception_calling_to_json
+ def (obj = Object.new).to_json(*)
+ raise
+ end
+ s = JSON.state.new(depth: 1).freeze
+ assert_raise(RuntimeError) { s.generate([{ hash: obj }]) }
+ assert_equal 1, s.depth
end
def test_buffer_initial_length
@@ -1006,7 +1056,8 @@ class JSONGeneratorTest < Test::Unit::TestCase
state = JSON::State.new
ary = []
ary << ary
- assert_raise(JSON::NestingError) { state.generate_new(ary) }
+ assert_raise(JSON::NestingError) { state.generate(ary) }
+ assert_equal 0, state.depth
assert_equal '{"a":1}', state.generate({ a: 1 })
end
end