Sha256: 8956a7d105f6b5291772526f916009904661376325ec570516728c57acf98703

Contents?: true

Size: 1.99 KB

Versions: 618

Compression:

Stored size: 1.99 KB

Contents

# Matches tokens produced by lexer
# The given exepected is one or more entries where an entry is one of
# - a token symbol
# - an Array with a token symbol and the text value
# - an Array with a token symbol and a Hash specifying all attributes of the token
# - nil (ignore)
#
RSpec::Matchers.define :match_tokens2 do | *expected |
  match do | actual |
    expected.zip(actual).all? do |e, a|
      compare(e, a)
    end
  end

  def failure_message
    msg = ["Expected (#{expected.size}):"]
    expected.each {|e| msg << e.to_s }

    zipped = expected.zip(actual)
    msg << "\nGot (#{actual.size}):"
    actual.each_with_index do |e, idx|
      if zipped[idx]
        zipped_expected = zipped[idx][0]
        zipped_actual = zipped[idx][1]

        prefix = compare(zipped_expected, zipped_actual) ? ' ' : '*'
        msg2 = ["#{prefix}[:"]
        msg2 << e[0].to_s
        msg2 << ', '
        if e[1] == false
          msg2 << 'false'
        else
          msg2 << e[1][:value].to_s.dump
        end
        # If expectation has options, output them
        if zipped_expected.is_a?(Array) && zipped_expected[2] && zipped_expected[2].is_a?(Hash)
          msg2 << ", {"
          msg3 = []
          zipped_expected[2].each do |k,v|
            prefix = e[1][k] != v ? "*" : ''
            msg3 << "#{prefix}:#{k}=>#{e[1][k]}"
          end
          msg2 << msg3.join(", ")
          msg2 << "}"
        end
        msg2 << ']'
        msg << msg2.join('')
      end
    end
    msg.join("\n")
  end

  def compare(e, a)
    # if expected ends before actual
    return true if !e

    # If actual ends before expected
    return false if !a

    # Simple - only expect token to match
    return true if a[0] == e

    # Expect value and optional attributes to match
    if e.is_a? Array
      # tokens must match
      return false unless a[0] == e[0]
      if e[2].is_a?(Hash)
        e[2].each {|k,v| return false unless a[1][k] == v }
      end
      return (a[1] == e[1] || (a[1][:value] == e[1]))
    end
    false
  end
end

Version data entries

618 entries across 618 versions & 1 rubygems

Version Path
puppet-8.3.0 spec/lib/matchers/match_tokens2.rb
puppet-8.3.0-x86-mingw32 spec/lib/matchers/match_tokens2.rb
puppet-8.3.0-x64-mingw32 spec/lib/matchers/match_tokens2.rb
puppet-8.3.0-universal-darwin spec/lib/matchers/match_tokens2.rb
puppet-8.4.0 spec/lib/matchers/match_tokens2.rb
puppet-8.4.0-x86-mingw32 spec/lib/matchers/match_tokens2.rb
puppet-8.4.0-x64-mingw32 spec/lib/matchers/match_tokens2.rb
puppet-8.4.0-universal-darwin spec/lib/matchers/match_tokens2.rb
puppet-7.28.0 spec/lib/matchers/match_tokens2.rb
puppet-7.28.0-x86-mingw32 spec/lib/matchers/match_tokens2.rb
puppet-7.28.0-x64-mingw32 spec/lib/matchers/match_tokens2.rb
puppet-7.28.0-universal-darwin spec/lib/matchers/match_tokens2.rb
puppet-8.3.1 spec/lib/matchers/match_tokens2.rb
puppet-8.3.1-x86-mingw32 spec/lib/matchers/match_tokens2.rb
puppet-8.3.1-x64-mingw32 spec/lib/matchers/match_tokens2.rb
puppet-8.3.1-universal-darwin spec/lib/matchers/match_tokens2.rb
puppet-7.27.0 spec/lib/matchers/match_tokens2.rb
puppet-7.27.0-x86-mingw32 spec/lib/matchers/match_tokens2.rb
puppet-7.27.0-x64-mingw32 spec/lib/matchers/match_tokens2.rb
puppet-7.27.0-universal-darwin spec/lib/matchers/match_tokens2.rb