summaryrefslogtreecommitdiff
path: root/test/psych/test_hash.rb
diff options
context:
space:
mode:
Diffstat (limited to 'test/psych/test_hash.rb')
-rw-r--r--test/psych/test_hash.rb64
1 files changed, 52 insertions, 12 deletions
diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb
index 5374781339..31eba8580b 100644
--- a/test/psych/test_hash.rb
+++ b/test/psych/test_hash.rb
@@ -102,26 +102,66 @@ module Psych
end
def test_ref_append
- hash = Psych.unsafe_load(<<-eoyml)
----
-foo: &foo
- hello: world
-bar:
- <<: *foo
-eoyml
+ hash = Psych.unsafe_load(<<~eoyml)
+ ---
+ foo: &foo
+ hello: world
+ bar:
+ <<: *foo
+ eoyml
assert_equal({"foo"=>{"hello"=>"world"}, "bar"=>{"hello"=>"world"}}, hash)
end
+ def test_anchor_reuse
+ hash = Psych.unsafe_load(<<~eoyml)
+ ---
+ foo: &foo
+ hello: world
+ bar: *foo
+ eoyml
+ assert_equal({"foo"=>{"hello"=>"world"}, "bar"=>{"hello"=>"world"}}, hash)
+ assert_same(hash.fetch("foo"), hash.fetch("bar"))
+ end
+
+ def test_raises_if_anchor_not_defined
+ assert_raise(Psych::AnchorNotDefined) do
+ Psych.unsafe_load(<<~eoyml)
+ ---
+ foo: &foo
+ hello: world
+ bar: *not_foo
+ eoyml
+ end
+ 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(AliasesNotEnabled) 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"
end
- hashes = Psych.load(<<-eoyml)
----
-- unique_identifier: 1
-- unique_identifier: 2
-eoyml
+ hashes = Psych.load(<<~eoyml)
+ ---
+ - unique_identifier: 1
+ - unique_identifier: 2
+ eoyml
assert_same hashes[0].keys.first, hashes[1].keys.first
end