Sha256: 7409d5004dfd6090d4abc5de530f63d98f037be4ff02745505d055f7a635eaeb

Contents?: true

Size: 984 Bytes

Versions: 2

Compression:

Stored size: 984 Bytes

Contents

require 'diff/lcs'

module FileMatcher
  class BeDifferent
    def initialize(expected)
      @expected = expected.scan(/[^\n]+/)
    end

    def matches?(actual)
      actual_lines = actual.scan(/[^\n]+/)
      differences = Diff::LCS::diff(@expected, actual_lines)
      @diff = differences.map do |chunk|
          added_at = (add = chunk.detect{|d| d.action == '+'}) && add.position+1
          removed_at = (remove = chunk.detect{|d| d.action == '-'}) && remove.position+1
          "Line #{added_at}/#{removed_at}:\n"+
          chunk.map do |change|
            "#{change.action} #{change.element}"
          end*"\n"
        end*"\n"
      @diff != ''
    end

    def failure_message
      "expected a difference, but got none"
    end

    def negative_failure_message
      "expected no difference, but got:\n#{@diff}"
    end
  end

  def differ_from(expected)
    BeDifferent.new(expected)
  end
end

Spec::Runner.configure do |config|
  config.include(FileMatcher)
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activefacts-0.8.6 spec/helpers/file_matcher.rb
activefacts-0.8.5 spec/helpers/file_matcher.rb