diff options
author | Aaron Patterson <tenderlove@ruby-lang.org> | 2022-08-08 09:31:08 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-08 09:31:08 -0700 |
commit | c12823abb03385cc155c01992a3a108d4c76c3d7 (patch) | |
tree | b4f34cb8cf1ebf59c6c513a84e5bb1eefab4dd5a | |
parent | 74fb0be6a07b0564ed58430efc3dc38a60411b63 (diff) | |
parent | b9ab19094f1534c38b724a2ef76c30eb9cf5262f (diff) | |
download | psych-c12823abb03385cc155c01992a3a108d4c76c3d7.tar.gz |
Merge pull request #568 from amomchilov/clarify-tests-about-alias-parsing
Clarify tests about parsing aliases
-rw-r--r-- | test/psych/test_array.rb | 16 | ||||
-rw-r--r-- | test/psych/test_hash.rb | 18 | ||||
-rw-r--r-- | test/psych/test_object.rb | 13 | ||||
-rw-r--r-- | test/psych/test_safe_load.rb | 29 |
4 files changed, 66 insertions, 10 deletions
diff --git a/test/psych/test_array.rb b/test/psych/test_array.rb index 28b76da..6a9931a 100644 --- a/test/psych/test_array.rb +++ b/test/psych/test_array.rb @@ -57,6 +57,22 @@ module Psych assert_cycle(@list) end + def test_recursive_array + @list << @list + + loaded = Psych.load(Psych.dump(@list), aliases: true) + + assert_same loaded, loaded.last + end + + def test_recursive_array_uses_alias + @list << @list + + assert_raise(BadAlias) do + Psych.load(Psych.dump(@list), aliases: false) + end + end + def test_cycle assert_cycle(@list) end diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb index 5374781..0555f6e 100644 --- a/test/psych/test_hash.rb +++ b/test/psych/test_hash.rb @@ -112,6 +112,24 @@ eoyml assert_equal({"foo"=>{"hello"=>"world"}, "bar"=>{"hello"=>"world"}}, hash) end + def test_recursive_hash + h = { } + h["recursive_reference"] = h + + loaded = Psych.load(Psych.dump(h), aliases: true) + + assert_same loaded, loaded.fetch("recursive_reference") + end + + def test_recursive_hash_uses_alias + h = { } + h["recursive_reference"] = h + + assert_raise(BadAlias) do + Psych.load(Psych.dump(h), aliases: false) + end + end + def test_key_deduplication unless String.method_defined?(:-@) && (-("a" * 20)).equal?((-("a" * 20))) pend "This Ruby implementation doesn't support string deduplication" diff --git a/test/psych/test_object.rb b/test/psych/test_object.rb index 0faf6b2..227a1d1 100644 --- a/test/psych/test_object.rb +++ b/test/psych/test_object.rb @@ -36,10 +36,19 @@ module Psych def test_cyclic_references foo = Foo.new(nil) foo.parent = foo - loaded = Psych.unsafe_load Psych.dump foo + loaded = Psych.load(Psych.dump(foo), permitted_classes: [Foo], aliases: true) assert_instance_of(Foo, loaded) - assert_equal loaded, loaded.parent + assert_same loaded, loaded.parent + end + + def test_cyclic_reference_uses_alias + foo = Foo.new(nil) + foo.parent = foo + + assert_raise(BadAlias) do + Psych.load(Psych.dump(foo), permitted_classes: [Foo], aliases: false) + end end end end diff --git a/test/psych/test_safe_load.rb b/test/psych/test_safe_load.rb index b52d604..e57dbcb 100644 --- a/test/psych/test_safe_load.rb +++ b/test/psych/test_safe_load.rb @@ -19,18 +19,31 @@ module Psych end end - def test_no_recursion - x = [] - x << x + def test_raises_when_alias_found_if_alias_parsing_not_enabled + yaml_with_aliases = <<~YAML + --- + a: &ABC + k1: v1 + k2: v2 + b: *ABC + YAML + assert_raise(Psych::BadAlias) do - Psych.safe_load Psych.dump(x) + Psych.safe_load(yaml_with_aliases) end end - def test_explicit_recursion - x = [] - x << x - assert_equal(x, Psych.safe_load(Psych.dump(x), permitted_classes: [], permitted_symbols: [], aliases: true)) + def test_aliases_are_parsed_when_alias_parsing_is_enabled + yaml_with_aliases = <<~YAML + --- + a: &ABC + k1: v1 + k2: v2 + b: *ABC + YAML + + result = Psych.safe_load(yaml_with_aliases, aliases: true) + assert_same result.fetch("a"), result.fetch("b") end def test_permitted_symbol |