Sha256: 295e868ae7510435d9156b73ae2c4df515d109342bcc390ee36f0a4dd639e670

Contents?: true

Size: 1.66 KB

Versions: 5

Compression:

Stored size: 1.66 KB

Contents

RSpec::Matchers.define(:parse) do |input, opts|
  as = block = nil
  result = trace = nil

  unless self.respond_to? :failure_message # if RSpec 2.x
    class << self
      alias_method :failure_message, :failure_message_for_should
      alias_method :failure_message_when_negated, :failure_message_for_should_not
    end
  end

  match do |parser|
    begin
      result = parser.parse(input)
      block ? 
        block.call(result) : 
        (as == result || as.nil?)
    rescue Parslet::ParseFailed => ex
      trace = ex.cause.ascii_tree if opts && opts[:trace]
      false
    end
  end

  failure_message do |is|
    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_when_negated do |is|
    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. 
  chain :as do |expected_output=nil, &block|
    as = expected_output
    block = block
  end
end

Version data entries

5 entries across 5 versions & 2 rubygems

Version Path
swift-pyrite-0.1.1 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/lib/parslet/rig/rspec.rb
swift-pyrite-0.1.0 vendor/bundle/ruby/2.0.0/gems/parslet-1.7.1/lib/parslet/rig/rspec.rb
parslet-1.7.1 lib/parslet/rig/rspec.rb
parslet-1.7.0 lib/parslet/rig/rspec.rb
parslet-1.6.2 lib/parslet/rig/rspec.rb