Skip to content

Commit

Permalink
Fix YAML::Schema::FailSafe.parse and parse_all and add specs (crystal…
Browse files Browse the repository at this point in the history
…-lang#6790)

* Fix cast_document

* Add specs and fix parse_all
  • Loading branch information
wooster0 authored and Ezra Stevens committed Oct 1, 2018
1 parent 8bff13f commit 8f9a1fe
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 2 deletions.
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

0 comments on commit 8f9a1fe

Please sign in to comment.