diff options
author | Aaron Patterson <tenderlove@ruby-lang.org> | 2022-08-08 09:33:27 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-08 09:33:27 -0700 |
commit | 2366075e0fcb01ff92831f9179bc000ec4fbb8c4 (patch) | |
tree | a0c33f3581527a7a207a303e66eb4fd71081b5ee | |
parent | 89164dcca205773daf6f9aff6e4efc86793df3d7 (diff) | |
parent | 98fbd5247a1727230ec3c0d459d24fd07f5d2bd4 (diff) | |
download | psych-2366075e0fcb01ff92831f9179bc000ec4fbb8c4.tar.gz |
Merge pull request #569 from amomchilov/new-AnchorNotDefined-error
Raise specific error when an anchor isn't defined
-rw-r--r-- | lib/psych/exception.rb | 7 | ||||
-rw-r--r-- | lib/psych/visitors/to_ruby.rb | 2 | ||||
-rw-r--r-- | test/psych/test_hash.rb | 46 | ||||
-rw-r--r-- | test/psych/test_merge_keys.rb | 2 |
4 files changed, 43 insertions, 14 deletions
diff --git a/lib/psych/exception.rb b/lib/psych/exception.rb index 04a9a90..d7469a4 100644 --- a/lib/psych/exception.rb +++ b/lib/psych/exception.rb @@ -13,6 +13,13 @@ module Psych end end + # Subclasses `BadAlias` for backwards compatibility + class AnchorNotDefined < BadAlias + def initialize anchor_name + super "An alias referenced an unknown anchor: #{anchor_name}" + end + end + class DisallowedClass < Exception def initialize action, klass_name super "Tried to #{action} unspecified class: #{klass_name}" diff --git a/lib/psych/visitors/to_ruby.rb b/lib/psych/visitors/to_ruby.rb index 0bf5198..cce5daf 100644 --- a/lib/psych/visitors/to_ruby.rb +++ b/lib/psych/visitors/to_ruby.rb @@ -323,7 +323,7 @@ module Psych end def visit_Psych_Nodes_Alias o - @st.fetch(o.anchor) { raise BadAlias, "Unknown alias: #{o.anchor}" } + @st.fetch(o.anchor) { raise AnchorNotDefined, o.anchor } end private diff --git a/test/psych/test_hash.rb b/test/psych/test_hash.rb index 0efa211..31eba85 100644 --- a/test/psych/test_hash.rb +++ b/test/psych/test_hash.rb @@ -102,16 +102,38 @@ 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 @@ -135,11 +157,11 @@ eoyml 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 diff --git a/test/psych/test_merge_keys.rb b/test/psych/test_merge_keys.rb index 8d2fcea..2f55a1e 100644 --- a/test/psych/test_merge_keys.rb +++ b/test/psych/test_merge_keys.rb @@ -117,7 +117,7 @@ development: bar: << : *foo eoyml - exp = assert_raise(Psych::BadAlias) { Psych.load(yaml, aliases: true) } + exp = assert_raise(Psych::AnchorNotDefined) { Psych.load(yaml, aliases: true) } assert_match 'foo', exp.message end |