lib/parslet/rig/rspec.rb in parslet-1.1.1 vs lib/parslet/rig/rspec.rb in parslet-1.2.0
- old
+ new
@@ -1,24 +1,50 @@
-RSpec::Matchers.define(:parse) do |input|
- chain(:as) { |as| @as = as }
-
+RSpec::Matchers.define(:parse) do |input, opts|
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 " << (@result ?
- "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}")
+ 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
+
+ # NOTE: This has a nodoc tag since the rdoc parser puts this into
+ # Object, a thing I would never allow.
+ def as(expected_output = nil, &block) # :nodoc:
+ @as = expected_output
+ @block = block
+ self
end
end