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

Fix YAML::Schema::FailSafe.parse and parse_all and add specs #6790

Merged
merged 2 commits into from
Sep 25, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
71 changes: 71 additions & 0 deletions spec/std/yaml/schema/fail_safe_spec.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
require "spec"
require "yaml"

private def it_parses(string, expected, file = __FILE__, line = __LINE__)
it "parses #{string.inspect}", file, line do
YAML::Schema::FailSafe.parse(string).should eq(expected)
end
end

private def it_raises_on_parse(string, message, file = __FILE__, line = __LINE__)
it "raises on parse #{string.inspect}", file, line do
expect_raises(YAML::ParseException, message) do
YAML::Schema::FailSafe.parse(string)
end
end
end

private def it_parses_all(string, expected, file = __FILE__, line = __LINE__)
it "parses all #{string.inspect}", file, line do
YAML::Schema::FailSafe.parse_all(string).should eq(expected)
end
end

private def it_raises_on_parse_all(string, message, file = __FILE__, line = __LINE__)
it "raises on parse all #{string.inspect}", file, line do
expect_raises(YAML::ParseException, message) do
YAML::Schema::FailSafe.parse_all(string)
end
end
end

describe YAML::Schema::FailSafe do
# parse
it_parses "123", "123"
it_parses %(
context:
replace_me: "Yes please!"
), {"context" => {"replace_me" => "Yes please!"}}
it_parses %(
first:
document:

second:
document:
), {"first" => {"document" => ""}, "second" => {"document" => ""}}
it_raises_on_parse %(
this: "gives"
an: "error"
), "did not find expected key at line 3, column 7, while parsing a block mapping at line 2, column 5"
it_raises_on_parse ":", "did not find expected key at line 1, column 1, while parsing a block mapping at line 1, column 1"

# parse_all
it_parses "321", "321"
it_parses_all %(
context:
replace_me: "Yes please!"
), [{"context" => {"replace_me" => "Yes please!"}}]
it_parses_all %(
foo:
bar: 123

bar:
foo: 321
), [{"foo" => {"bar" => "123"}, "bar" => {"foo" => "321"}}]
it_raises_on_parse_all %(
this: "raises"
an: "yaml"
parse: "exception"
), "did not find expected key at line 3, column 7, while parsing a block mapping at line 2, column 5"
it_raises_on_parse_all ":", "did not find expected key at line 1, column 1, while parsing a block mapping at line 1, column 1"
end
4 changes: 2 additions & 2 deletions src/yaml/schema/fail_safe.cr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module YAML::Schema::FailSafe
end

# Deserializes multiple YAML documents.
def self.parse_all(data : String | IO) : Any
def self.parse_all(data : String | IO) : Array(Any)
Parser.new data, &.parse_all
end

Expand All @@ -35,7 +35,7 @@ module YAML::Schema::FailSafe
end

def cast_document(doc)
doc.first? || Any.new(nil)
doc[0]? || Any.new(nil)
end

def new_sequence
Expand Down