Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify tests about parsing aliases #568

Merged
merged 3 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/psych/test_array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,22 @@ def test_self_referential
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
Expand Down
18 changes: 18 additions & 0 deletions test/psych/test_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ def test_ref_append
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"
Expand Down
13 changes: 11 additions & 2 deletions test/psych/test_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,19 @@ def test_tag_round_trip
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
29 changes: 21 additions & 8 deletions test/psych/test_safe_load.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,31 @@ class Foo; end
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
Expand Down