Sha256: 6f93bed1eda3a26b8714fdb64ffa8b669022b192cdebc6f6a8844dcebec3ced2

Contents?: true

Size: 1.04 KB

Versions: 17

Compression:

Stored size: 1.04 KB

Contents

require 'diff/lcs'

RSpec::Matchers.define :have_different_contents do |x|
  match do |actual|
    perform_match(actual, expected)
  end

  def perform_match(actual, expected)
    expected = File.open(expected).read if expected.is_a?(Pathname)
    expected = expected.scan(/[^\n]+/) unless expected.is_a?(Array)

    actual = File.open(actual).read if actual.is_a?(Pathname)
    actual = actual.scan(/[^\n]+/) unless actual.is_a?(Array)

    differences = Diff::LCS::diff(expected, actual)
    @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

  failure_message_for_should do |actual|
    "expected a difference, but got none"
  end

  failure_message_for_should_not do |actual|
    "expected no difference, but got:\n#{@diff}"
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
activefacts-1.6.0 spec/helpers/file_matcher.rb
activefacts-1.5.3 spec/helpers/file_matcher.rb
activefacts-1.5.2 spec/helpers/file_matcher.rb
activefacts-1.5.1 spec/helpers/file_matcher.rb
activefacts-1.5.0 spec/helpers/file_matcher.rb
activefacts-1.3.0 spec/helpers/file_matcher.rb
activefacts-1.2.1 spec/helpers/file_matcher.rb
activefacts-1.2.0 spec/helpers/file_matcher.rb
activefacts-1.1.0 spec/helpers/file_matcher.rb
activefacts-1.0.2 spec/helpers/file_matcher.rb
activefacts-1.0.1 spec/helpers/file_matcher.rb
activefacts-1.0.0 spec/helpers/file_matcher.rb
activefacts-0.8.18 spec/helpers/file_matcher.rb
activefacts-0.8.16 spec/helpers/file_matcher.rb
activefacts-0.8.15 spec/helpers/file_matcher.rb
activefacts-0.8.13 spec/helpers/file_matcher.rb
activefacts-0.8.12 spec/helpers/file_matcher.rb