summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--test/psych/test_psych.rb63
-rw-r--r--test/psych/test_safe_load.rb12
-rw-r--r--test/psych/test_yaml_special_cases.rb130
3 files changed, 202 insertions, 3 deletions
diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb
index a67342f..3a04a3f 100644
--- a/test/psych/test_psych.rb
+++ b/test/psych/test_psych.rb
@@ -60,6 +60,22 @@ class TestPsych < Psych::TestCase
end
end
+ def test_parse
+ assert_equal %w[a b], Psych.parse("- a\n- b").to_ruby
+ end
+
+ def test_parse_default_fallback
+ assert_equal false, Psych.parse("")
+ end
+
+ def test_parse_raises_on_bad_input
+ assert_raises(Psych::SyntaxError) { Psych.parse("--- `") }
+ end
+
+ def test_parse_with_fallback
+ assert_equal 42, Psych.parse("", fallback: 42)
+ end
+
def test_non_existing_class_on_deserialize
e = assert_raises(ArgumentError) do
Psych.load("--- !ruby/object:NonExistent\nfoo: 1")
@@ -103,9 +119,44 @@ class TestPsych < Psych::TestCase
assert_equal %w{ foo bar }, docs
end
+ def test_load_stream_default_fallback
+ assert_equal [], Psych.load_stream("")
+ end
+
+ def test_load_stream_raises_on_bad_input
+ assert_raises(Psych::SyntaxError) { Psych.load_stream("--- `") }
+ end
+
def test_parse_stream
docs = Psych.parse_stream("--- foo\n...\n--- bar\n...")
- assert_equal %w{ foo bar }, docs.children.map { |x| x.transform }
+ assert_equal(%w[foo bar], docs.children.map(&:transform))
+ end
+
+ def test_parse_stream_with_block
+ docs = []
+ Psych.parse_stream("--- foo\n...\n--- bar\n...") do |node|
+ docs << node
+ end
+
+ assert_equal %w[foo bar], docs.map(&:to_ruby)
+ end
+
+ def test_parse_stream_default_fallback
+ docs = Psych.parse_stream("")
+ assert_equal [], docs.children.map(&:to_ruby)
+ end
+
+ def test_parse_stream_with_block_default_fallback
+ docs = []
+ Psych.parse_stream("") do |node|
+ docs << node
+ end
+
+ assert_equal [], docs.map(&:to_ruby)
+ end
+
+ def test_parse_stream_raises_on_bad_input
+ assert_raises(Psych::SyntaxError) { Psych.parse_stream("--- `") }
end
def test_add_builtin_type
@@ -135,7 +186,7 @@ class TestPsych < Psych::TestCase
assert_equal({ 'hello' => 'world' }, got)
end
- def test_load_default_return_value
+ def test_load_default_fallback
assert_equal false, Psych.load("")
end
@@ -169,7 +220,7 @@ class TestPsych < Psych::TestCase
}
end
- def test_load_file_default_return_value
+ def test_load_file_default_fallback
Tempfile.create(['empty', 'yml']) {|t|
assert_equal false, Psych.load_file(t.path)
}
@@ -221,6 +272,12 @@ class TestPsych < Psych::TestCase
}
end
+ def test_parse_file_default_fallback
+ Tempfile.create(['empty', 'yml']) do |t|
+ assert_equal false, Psych.parse_file(t.path)
+ end
+ end
+
def test_degenerate_strings
assert_equal false, Psych.load(' ')
assert_equal false, Psych.parse(' ')
diff --git a/test/psych/test_safe_load.rb b/test/psych/test_safe_load.rb
index f3fdb9b..cf8abeb 100644
--- a/test/psych/test_safe_load.rb
+++ b/test/psych/test_safe_load.rb
@@ -84,6 +84,18 @@ module Psych
end
end
+ def test_safe_load_default_fallback
+ assert_nil Psych.safe_load("")
+ end
+
+ def test_safe_load
+ assert_equal %w[a b], Psych.safe_load("- a\n- b")
+ end
+
+ def test_safe_load_raises_on_bad_input
+ assert_raises(Psych::SyntaxError) { Psych.safe_load("--- `") }
+ end
+
private
def cycle object, whitelist = []
diff --git a/test/psych/test_yaml_special_cases.rb b/test/psych/test_yaml_special_cases.rb
new file mode 100644
index 0000000..66448d4
--- /dev/null
+++ b/test/psych/test_yaml_special_cases.rb
@@ -0,0 +1,130 @@
+# frozen_string_literal: true
+
+require_relative 'helper'
+
+require 'stringio'
+require 'tempfile'
+
+module Psych
+ class TestYamlSpecialCases < TestCase
+ def setup
+ super
+ end
+
+ def test_empty_string
+ s = ""
+ assert_equal false, Psych.load(s)
+ assert_equal [], Psych.load_stream(s)
+ assert_equal false, Psych.parse(s)
+ assert_equal [], Psych.parse_stream(s).transform
+ assert_equal nil, Psych.safe_load(s)
+ end
+
+ def test_false
+ s = "false"
+ assert_equal false, Psych.load(s)
+ assert_equal [false], Psych.load_stream(s)
+ assert_equal false, Psych.parse(s).transform
+ assert_equal [false], Psych.parse_stream(s).transform
+ assert_equal false, Psych.safe_load(s)
+ end
+
+ def test_n
+ s = "n"
+ assert_equal "n", Psych.load(s)
+ assert_equal ["n"], Psych.load_stream(s)
+ assert_equal "n", Psych.parse(s).transform
+ assert_equal ["n"], Psych.parse_stream(s).transform
+ assert_equal "n", Psych.safe_load(s)
+ end
+
+ def test_off
+ s = "off"
+ assert_equal false, Psych.load(s)
+ assert_equal [false], Psych.load_stream(s)
+ assert_equal false, Psych.parse(s).transform
+ assert_equal [false], Psych.parse_stream(s).transform
+ assert_equal false, Psych.safe_load(s)
+ end
+
+ def test_inf
+ s = "-.inf"
+ assert_equal -Float::INFINITY, Psych.load(s)
+ assert_equal [-Float::INFINITY], Psych.load_stream(s)
+ assert_equal -Float::INFINITY, Psych.parse(s).transform
+ assert_equal [-Float::INFINITY], Psych.parse_stream(s).transform
+ assert_equal -Float::INFINITY, Psych.safe_load(s)
+ end
+
+ def test_NaN
+ s = ".NaN"
+ assert Float::NAN, Psych.load(s).nan?
+ assert [Float::NAN], Psych.load_stream(s).first.nan?
+ assert Psych.parse(s).transform.nan?
+ assert Psych.parse_stream(s).transform.first.nan?
+ assert Psych.safe_load(s).nan?
+ end
+
+ def test_0xC
+ s = "0xC"
+ assert_equal 12, Psych.load(s)
+ assert_equal [12], Psych.load_stream(s)
+ assert_equal 12, Psych.parse(s).transform
+ assert_equal [12], Psych.parse_stream(s).transform
+ assert_equal 12, Psych.safe_load(s)
+ end
+
+ def test_arrows
+ s = "<<"
+ assert_equal "<<", Psych.load(s)
+ assert_equal ["<<"], Psych.load_stream(s)
+ assert_equal "<<", Psych.parse(s).transform
+ assert_equal ["<<"], Psych.parse_stream(s).transform
+ assert_equal "<<", Psych.safe_load(s)
+ end
+
+ def test_arrows_hash
+ s = "<<: {}"
+ assert_equal({}, Psych.load(s))
+ assert_equal [{}], Psych.load_stream(s)
+ assert_equal({}, Psych.parse(s).transform)
+ assert_equal [{}], Psych.parse_stream(s).transform
+ assert_equal({}, Psych.safe_load(s))
+ end
+
+ def test_thousand
+ s = "- 1000\n- +1000\n- 1_000"
+ assert_equal [1000, 1000, 1000], Psych.load(s)
+ assert_equal [[1000, 1000, 1000]], Psych.load_stream(s)
+ assert_equal [1000, 1000, 1000], Psych.parse(s).transform
+ assert_equal [[1000, 1000, 1000]], Psych.parse_stream(s).transform
+ assert_equal [1000, 1000, 1000], Psych.safe_load(s)
+ end
+
+ def test_8
+ s = "[8, 08, 0o10, 010]"
+ assert_equal [8, "08", "0o10", 8], Psych.load(s)
+ assert_equal [[8, "08", "0o10", 8]], Psych.load_stream(s)
+ assert_equal [8, "08", "0o10", 8], Psych.parse(s).transform
+ assert_equal [[8, "08", "0o10", 8]], Psych.parse_stream(s).transform
+ assert_equal [8, "08", "0o10", 8], Psych.safe_load(s)
+ end
+
+ def test_null
+ s = "null"
+ assert_equal nil, Psych.load(s)
+ assert_equal [nil], Psych.load_stream(s)
+ assert_equal nil, Psych.parse(s).transform
+ assert_equal [nil], Psych.parse_stream(s).transform
+ assert_equal nil, Psych.safe_load(s)
+ end
+
+ private
+
+ def special_case_cycle(object)
+ %w[load load_stream parse parse_stream safe_load].map do |m|
+ Psych.public_send(m, object)
+ end
+ end
+ end
+end