From 4d4439d6d0adfcbd211ea295779315f1baa7dadd Mon Sep 17 00:00:00 2001 From: Greg Houle Date: Mon, 23 Apr 2018 13:53:30 -0400 Subject: unifying interface of Psych --- test/psych/test_exception.rb | 26 ++++++++++++++--- test/psych/test_safe_load.rb | 69 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 83 insertions(+), 12 deletions(-) (limited to 'test') diff --git a/test/psych/test_exception.rb b/test/psych/test_exception.rb index 3040bfb..3c72f4a 100644 --- a/test/psych/test_exception.rb +++ b/test/psych/test_exception.rb @@ -30,9 +30,15 @@ module Psych assert_nil ex.file ex = assert_raises(Psych::SyntaxError) do - Psych.load '--- `', 'meow' + Psych.load '--- `', filename: 'meow' end assert_equal 'meow', ex.file + + # deprecated interface + ex = assert_raises(Psych::SyntaxError) do + Psych.load '--- `', 'deprecated' + end + assert_equal 'deprecated', ex.file end def test_psych_parse_stream_takes_file @@ -43,7 +49,7 @@ module Psych assert_match '()', ex.message ex = assert_raises(Psych::SyntaxError) do - Psych.parse_stream '--- `', 'omg!' + Psych.parse_stream '--- `', filename: 'omg!' end assert_equal 'omg!', ex.file assert_match 'omg!', ex.message @@ -57,9 +63,15 @@ module Psych assert_match '()', ex.message ex = assert_raises(Psych::SyntaxError) do - Psych.load_stream '--- `', 'omg!' + Psych.load_stream '--- `', filename: 'omg!' end assert_equal 'omg!', ex.file + + # deprecated interface + ex = assert_raises(Psych::SyntaxError) do + Psych.load_stream '--- `', 'deprecated' + end + assert_equal 'deprecated', ex.file end def test_parse_file_exception @@ -94,9 +106,15 @@ module Psych assert_nil ex.file ex = assert_raises(Psych::SyntaxError) do - Psych.parse '--- `', 'omg!' + Psych.parse '--- `', filename: 'omg!' end assert_match 'omg!', ex.message + + # deprecated interface + ex = assert_raises(Psych::SyntaxError) do + Psych.parse '--- `', 'deprecated' + end + assert_match 'deprecated', ex.message end def test_attributes diff --git a/test/psych/test_safe_load.rb b/test/psych/test_safe_load.rb index cf8abeb..82a5f19 100644 --- a/test/psych/test_safe_load.rb +++ b/test/psych/test_safe_load.rb @@ -22,6 +22,8 @@ module Psych def test_explicit_recursion x = [] x << x + assert_equal(x, Psych.safe_load(Psych.dump(x), whitelist_classes: [], whitelist_symbols: [], aliases: true)) + # deprecated interface assert_equal(x, Psych.safe_load(Psych.dump(x), [], [], true)) end @@ -30,6 +32,16 @@ module Psych assert_raises(Psych::DisallowedClass) do Psych.safe_load yml end + assert_equal( + :foo, + Psych.safe_load( + yml, + whitelist_classes: [Symbol], + whitelist_symbols: [:foo] + ) + ) + + # deprecated interface assert_equal(:foo, Psych.safe_load(yml, [Symbol], [:foo])) end @@ -37,33 +49,72 @@ module Psych assert_raises(Psych::DisallowedClass) do assert_safe_cycle :foo end + assert_raises(Psych::DisallowedClass) do + Psych.safe_load '--- !ruby/symbol foo', whitelist_classes: [] + end + + # deprecated interface assert_raises(Psych::DisallowedClass) do Psych.safe_load '--- !ruby/symbol foo', [] end - assert_safe_cycle :foo, [Symbol] - assert_safe_cycle :foo, %w{ Symbol } + + assert_safe_cycle :foo, whitelist_classes: [Symbol] + assert_safe_cycle :foo, whitelist_classes: %w{ Symbol } + assert_equal :foo, Psych.safe_load('--- !ruby/symbol foo', whitelist_classes: [Symbol]) + + # deprecated interface assert_equal :foo, Psych.safe_load('--- !ruby/symbol foo', [Symbol]) end def test_foo + assert_raises(Psych::DisallowedClass) do + Psych.safe_load '--- !ruby/object:Foo {}', whitelist_classes: [Foo] + end + + # deprecated interface assert_raises(Psych::DisallowedClass) do Psych.safe_load '--- !ruby/object:Foo {}', [Foo] end + assert_raises(Psych::DisallowedClass) do assert_safe_cycle Foo.new end + assert_kind_of(Foo, Psych.safe_load(Psych.dump(Foo.new), whitelist_classes: [Foo])) + + # deprecated interface assert_kind_of(Foo, Psych.safe_load(Psych.dump(Foo.new), [Foo])) end X = Struct.new(:x) def test_struct_depends_on_sym - assert_safe_cycle(X.new, [X, Symbol]) + assert_safe_cycle(X.new, whitelist_classes: [X, Symbol]) assert_raises(Psych::DisallowedClass) do - cycle X.new, [X] + cycle X.new, whitelist_classes: [X] end end def test_anon_struct + assert Psych.safe_load(<<-eoyml, whitelist_classes: [Struct, Symbol]) +--- !ruby/struct + foo: bar + eoyml + + assert_raises(Psych::DisallowedClass) do + Psych.safe_load(<<-eoyml, whitelist_classes: [Struct]) +--- !ruby/struct + foo: bar + eoyml + end + + assert_raises(Psych::DisallowedClass) do + Psych.safe_load(<<-eoyml, whitelist_classes: [Symbol]) +--- !ruby/struct + foo: bar + eoyml + end + end + + def test_deprecated_anon_struct assert Psych.safe_load(<<-eoyml, [Struct, Symbol]) --- !ruby/struct foo: bar @@ -98,12 +149,14 @@ module Psych private - def cycle object, whitelist = [] - Psych.safe_load(Psych.dump(object), whitelist) + def cycle object, whitelist_classes: [] + Psych.safe_load(Psych.dump(object), whitelist_classes: whitelist_classes) + # deprecated interface test + Psych.safe_load(Psych.dump(object), whitelist_classes) end - def assert_safe_cycle object, whitelist = [] - other = cycle object, whitelist + def assert_safe_cycle object, whitelist_classes: [] + other = cycle object, whitelist_classes: whitelist_classes assert_equal object, other end end -- cgit v1.2.3