Sha256: 529b9b043dc7d68b79fa873cfed844dc677f282b570b6334af00f9d484396f54

Contents?: true

Size: 902 Bytes

Versions: 17

Compression:

Stored size: 902 Bytes

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_lines = expected.scan(/[^\n]+/)
    actual_lines = actual.scan(/[^\n]+/)
    differences = Diff::LCS::diff(expected_lines, 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

  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/string_matcher.rb
activefacts-1.5.3 spec/helpers/string_matcher.rb
activefacts-1.5.2 spec/helpers/string_matcher.rb
activefacts-1.5.1 spec/helpers/string_matcher.rb
activefacts-1.5.0 spec/helpers/string_matcher.rb
activefacts-1.3.0 spec/helpers/string_matcher.rb
activefacts-1.2.1 spec/helpers/string_matcher.rb
activefacts-1.2.0 spec/helpers/string_matcher.rb
activefacts-1.1.0 spec/helpers/string_matcher.rb
activefacts-1.0.2 spec/helpers/string_matcher.rb
activefacts-1.0.1 spec/helpers/string_matcher.rb
activefacts-1.0.0 spec/helpers/string_matcher.rb
activefacts-0.8.18 spec/helpers/string_matcher.rb
activefacts-0.8.16 spec/helpers/string_matcher.rb
activefacts-0.8.15 spec/helpers/string_matcher.rb
activefacts-0.8.13 spec/helpers/string_matcher.rb
activefacts-0.8.12 spec/helpers/string_matcher.rb