summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2018-05-21 15:01:46 -0700
committerGitHub <noreply@github.com>2018-05-21 15:01:46 -0700
commite01839af57df559b26f74e906062be6c692c89c8 (patch)
tree8496ecdcec183d1279cfbd205decf34021d5fada
parent1e050bff31872b2c4eaa81fa5feaf579d5dc2e3b (diff)
parent04ad1627b9f5eee57b64019cf2694c34c85edb10 (diff)
downloadpsych-e01839af57df559b26f74e906062be6c692c89c8.tar.gz
Merge pull request #346 from stomar/fix-fallback-arg
Fix fallback keyword argument of Psych.load
-rw-r--r--lib/psych.rb9
-rw-r--r--test/psych/test_psych.rb25
2 files changed, 30 insertions, 4 deletions
diff --git a/lib/psych.rb b/lib/psych.rb
index a728dd7..d314ebf 100644
--- a/lib/psych.rb
+++ b/lib/psych.rb
@@ -236,8 +236,9 @@ module Psych
###
# Load +yaml+ in to a Ruby data structure. If multiple documents are
# provided, the object contained in the first document will be returned.
- # +filename+ will be used in the exception message if any exception is raised
- # while parsing.
+ # +filename+ will be used in the exception message if any exception
+ # is raised while parsing. If +yaml+ is empty, it returns
+ # the specified +fallback+ return value, which defaults to +false+.
#
# Raises a Psych::SyntaxError when a YAML syntax error is detected.
#
@@ -260,7 +261,7 @@ module Psych
# Psych.load("---\n foo: bar", symbolize_names: true) # => {:foo=>"bar"}
#
def self.load yaml, filename = nil, fallback: false, symbolize_names: false
- result = parse(yaml, filename, fallback: fallback)
+ result = parse(yaml, filename, fallback: FALLBACK.new(fallback))
result = result.to_ruby if result
symbolize_names!(result) if symbolize_names
result
@@ -513,7 +514,7 @@ module Psych
# the specified +fallback+ return value, which defaults to +false+.
def self.load_file filename, fallback: false
File.open(filename, 'r:bom|utf-8') { |f|
- self.load f, filename, fallback: FALLBACK.new(fallback)
+ self.load f, filename, fallback: fallback
}
end
diff --git a/test/psych/test_psych.rb b/test/psych/test_psych.rb
index 8f9a100..a67342f 100644
--- a/test/psych/test_psych.rb
+++ b/test/psych/test_psych.rb
@@ -135,6 +135,31 @@ class TestPsych < Psych::TestCase
assert_equal({ 'hello' => 'world' }, got)
end
+ def test_load_default_return_value
+ assert_equal false, Psych.load("")
+ end
+
+ def test_load_with_fallback
+ assert_equal 42, Psych.load("", "file", fallback: 42)
+ end
+
+ def test_load_with_fallback_nil_or_false
+ assert_nil Psych.load("", "file", fallback: nil)
+ assert_equal false, Psych.load("", "file", fallback: false)
+ end
+
+ def test_load_with_fallback_hash
+ assert_equal Hash.new, Psych.load("", "file", fallback: Hash.new)
+ end
+
+ def test_load_with_fallback_for_nil
+ assert_nil Psych.load("--- null", "file", fallback: 42)
+ end
+
+ def test_load_with_fallback_for_false
+ assert_equal false, Psych.load("--- false", "file", fallback: 42)
+ end
+
def test_load_file
Tempfile.create(['yikes', 'yml']) {|t|
t.binmode