-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RSpec matcher :as chain now accepts a block. This allows for partial assertions on the output, rather than just equality. Also: try :trace => true in parse for error traces output.
- Loading branch information
Showing
2 changed files
with
40 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,43 @@ | ||
RSpec::Matchers.define(:parse) do |input, opts| | ||
chain(:as) { |as| @as = as } | ||
|
||
match do |parser| | ||
begin | ||
@result = parser.parse(input) | ||
@as == @result or @as.nil? | ||
@block ? @block.call(@result) | ||
: (@as == @result || @as.nil?) | ||
rescue Parslet::ParseFailed | ||
@trace = parser.error_tree.ascii_tree if opts && opts[:trace] | ||
false | ||
end | ||
end | ||
|
||
failure_message_for_should do |is| | ||
"expected " << | ||
(@as ? | ||
"output of parsing #{input.inspect} with #{is.inspect} to equal #{@as.inspect}, but was #{@result.inspect}" : | ||
"#{is.inspect} to be able to parse #{input.inspect}") << | ||
(@trace ? | ||
"\n"+@trace : '') | ||
if @block | ||
"expected output of parsing #{input.inspect}" << | ||
" with #{is.inspect} to meet block conditions, but it didn't" | ||
else | ||
"expected " << | ||
(@as ? "output of parsing #{input.inspect}"<< | ||
" with #{is.inspect} to equal #{@as.inspect}, but was #{@result.inspect}" | ||
: "#{is.inspect} to be able to parse #{input.inspect}") << | ||
(@trace ? "\n"+@trace | ||
: '') | ||
end | ||
end | ||
|
||
failure_message_for_should_not do |is| | ||
"expected " << | ||
(@as ? | ||
"output of parsing #{input.inspect} with #{is.inspect} not to equal #{@as.inspect}" : | ||
"#{is.inspect} to not parse #{input.inspect}, but it did") | ||
if @block | ||
"expected output of parsing #{input.inspect} with #{is.inspect} not to meet block conditions, but it did" | ||
else | ||
"expected " << | ||
(@as ? "output of parsing #{input.inspect}"<< | ||
" with #{is.inspect} not to equal #{@as.inspect}" | ||
: "#{is.inspect} to not parse #{input.inspect}, but it did") | ||
end | ||
end | ||
|
||
def as(expected_output = nil, &block) | ||
@as = expected_output | ||
@block = block | ||
self | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters