From 0bc30cb4cb03164fb60cab7f0d2eb27fd6e78193 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 22 Jul 2022 15:49:05 -0400 Subject: Clarify tests about parsing aliases --- test/psych/test_safe_load.rb | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) 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 -- cgit v1.2.1 From d9f7289190d424981555dec9f2c019f6a270f205 Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Fri, 22 Jul 2022 16:45:03 -0400 Subject: Test that recursive refs dump as aliases --- test/psych/test_array.rb | 13 +++++++++++++ test/psych/test_hash.rb | 12 ++++++++++++ test/psych/test_object.rb | 12 ++++++++++++ 3 files changed, 37 insertions(+) diff --git a/test/psych/test_array.rb b/test/psych/test_array.rb index 28b76da..a6be0ba 100644 --- a/test/psych/test_array.rb +++ b/test/psych/test_array.rb @@ -57,6 +57,19 @@ module Psych assert_cycle(@list) end + def test_recursive_array_uses_alias + @list << @list + + expected = <<~eoyaml + --- &1 + - :a: b + - foo + - *1 + eoyaml + + assert_equal expected, Psych.dump(@list) + end + def test_cycle assert_cycle(@list) end diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb index 5374781..43e4b8b 100644 --- a/test/psych/test_hash.rb +++ b/test/psych/test_hash.rb @@ -112,6 +112,18 @@ eoyml assert_equal({"foo"=>{"hello"=>"world"}, "bar"=>{"hello"=>"world"}}, hash) end + def test_recursive_hash_uses_alias + h = { } + h["recursive_reference"] = h + + expected = <<~eoyaml + --- &1 + recursive_reference: *1 + eoyaml + + assert_equal(expected, Psych.dump(h)) + 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..648a3ca 100644 --- a/test/psych/test_object.rb +++ b/test/psych/test_object.rb @@ -41,5 +41,17 @@ module Psych assert_instance_of(Foo, loaded) assert_equal loaded, loaded.parent end + + def test_cyclic_reference_uses_alias + foo = Foo.new(nil) + foo.parent = foo + + expected = <<~eoyaml + --- &1 !ruby/object:Psych::Foo + parent: *1 + eoyaml + + assert_equal expected, Psych.dump(foo) + end end end -- cgit v1.2.1 From b9ab19094f1534c38b724a2ef76c30eb9cf5262f Mon Sep 17 00:00:00 2001 From: Alexander Momchilov Date: Wed, 27 Jul 2022 10:19:37 -0400 Subject: Don't hardcode expected alias names --- test/psych/test_array.rb | 19 +++++++++++-------- test/psych/test_hash.rb | 18 ++++++++++++------ test/psych/test_object.rb | 13 +++++-------- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/test/psych/test_array.rb b/test/psych/test_array.rb index a6be0ba..6a9931a 100644 --- a/test/psych/test_array.rb +++ b/test/psych/test_array.rb @@ -57,17 +57,20 @@ module Psych assert_cycle(@list) end - def test_recursive_array_uses_alias + def test_recursive_array @list << @list - expected = <<~eoyaml - --- &1 - - :a: b - - foo - - *1 - eoyaml + loaded = Psych.load(Psych.dump(@list), aliases: true) + + assert_same loaded, loaded.last + end + + def test_recursive_array_uses_alias + @list << @list - assert_equal expected, Psych.dump(@list) + assert_raise(BadAlias) do + Psych.load(Psych.dump(@list), aliases: false) + end end def test_cycle diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb index 43e4b8b..0555f6e 100644 --- a/test/psych/test_hash.rb +++ b/test/psych/test_hash.rb @@ -112,16 +112,22 @@ eoyml assert_equal({"foo"=>{"hello"=>"world"}, "bar"=>{"hello"=>"world"}}, hash) end - def test_recursive_hash_uses_alias + def test_recursive_hash h = { } h["recursive_reference"] = h - expected = <<~eoyaml - --- &1 - recursive_reference: *1 - eoyaml + 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_equal(expected, Psych.dump(h)) + assert_raise(BadAlias) do + Psych.load(Psych.dump(h), aliases: false) + end end def test_key_deduplication diff --git a/test/psych/test_object.rb b/test/psych/test_object.rb index 648a3ca..227a1d1 100644 --- a/test/psych/test_object.rb +++ b/test/psych/test_object.rb @@ -36,22 +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 - expected = <<~eoyaml - --- &1 !ruby/object:Psych::Foo - parent: *1 - eoyaml - - assert_equal expected, Psych.dump(foo) + assert_raise(BadAlias) do + Psych.load(Psych.dump(foo), permitted_classes: [Foo], aliases: false) + end end end end -- cgit v1.2.1