summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorAaron Patterson <tenderlove@ruby-lang.org>2021-05-21 11:38:55 -0700
committerGitHub <noreply@github.com>2021-05-21 11:38:55 -0700
commit5d8b7fb0f8393e8ce1aed7642f509818e4e5c211 (patch)
treeb879281965f51d4f2224a70b2b34f0887c1e199e /test
parent38e4b5115f170ae7d79debfc9219331a6d52babe (diff)
parent441958396f40d526a62e564761d2bad534465a5b (diff)
downloadpsych-master.zip
Merge pull request #495 from Shopify/safe-dumpHEADmaster
Implement YAML.safe_dump to make safe_load more usable.
Diffstat (limited to 'test')
-rw-r--r--test/psych/test_psych.rb57
1 files changed, 57 insertions, 0 deletions
diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb
index 256ed91..c9d39c5 100644
--- a/test/psych/test_psych.rb
+++ b/test/psych/test_psych.rb
@@ -381,4 +381,61 @@ hoge:
result = Psych.safe_load(yaml, symbolize_names: true)
assert_equal result, { foo: { bar: "baz", 1 => 2 }, hoge: [{ fuga: "piyo" }] }
end
+
+ def test_safe_dump_defaults
+ yaml = <<-eoyml
+---
+array:
+- 1
+float: 13.12
+booleans:
+- true
+- false
+eoyml
+
+ payload = YAML.safe_dump({
+ "array" => [1],
+ "float" => 13.12,
+ "booleans" => [true, false],
+ })
+ assert_equal yaml, payload
+ end
+
+ def test_safe_dump_unpermitted_class
+ error = assert_raises Psych::DisallowedClass do
+ YAML.safe_dump(Object.new)
+ end
+ assert_equal "Tried to dump unspecified class: Object", error.message
+
+ hash_subclass = Class.new(Hash)
+ error = assert_raises Psych::DisallowedClass do
+ YAML.safe_dump(hash_subclass.new)
+ end
+ assert_equal "Tried to dump unspecified class: #{hash_subclass.inspect}", error.message
+ end
+
+ def test_safe_dump_extra_permitted_classes
+ assert_equal "--- !ruby/object {}\n", YAML.safe_dump(Object.new, permitted_classes: [Object])
+ end
+
+ def test_safe_dump_symbols
+ error = assert_raises Psych::DisallowedClass do
+ YAML.safe_dump(:foo, permitted_classes: [Symbol])
+ end
+ assert_equal "Tried to dump unspecified class: Symbol(:foo)", error.message
+
+ assert_equal "--- :foo\n", YAML.safe_dump(:foo, permitted_classes: [Symbol], permitted_symbols: [:foo])
+ end
+
+ def test_safe_dump_aliases
+ x = []
+ x << x
+ error = assert_raises Psych::BadAlias do
+ YAML.safe_dump(x)
+ end
+ assert_equal "Tried to dump an aliased object", error.message
+
+ assert_equal "--- &1\n" + "- *1\n", YAML.safe_dump(x, aliases: true)
+ end
+
end