Permalink
Browse files

handle compatibility errors and increase test coverage

  • Loading branch information...
1 parent a9d825f commit bd9a7bf679a224ab62c76452dbf5970561e7c904 @JonRowe JonRowe committed Mar 14, 2013
Showing with 29 additions and 10 deletions.
  1. +14 −6 lib/rspec/expectations/differ.rb
  2. +15 −4 spec/rspec/expectations/differ_spec.rb
View
20 lib/rspec/expectations/differ.rb
@@ -6,10 +6,10 @@ module RSpec
module Expectations
class Differ
# This is snagged from diff/lcs/ldiff.rb (which is a commandline tool)
- def diff_as_string(data_new, data_old)
- output = matching_encoding("", data_old)
- data_old = data_old.split(matching_encoding("\n", data_old)).map! { |e| e.chomp }
- data_new = data_new.split(matching_encoding("\n", data_new)).map! { |e| e.chomp }
+ def diff_as_string(input_data_new, input_data_old)
+ output = matching_encoding("", input_data_old)
+ data_old = input_data_old.split(matching_encoding("\n", input_data_old)).map! { |e| e.chomp }
+ data_new = input_data_new.split(matching_encoding("\n", input_data_new)).map! { |e| e.chomp }
diffs = Diff::LCS.diff(data_old, data_new)
return output if diffs.empty?
oldhunk = hunk = nil
@@ -33,16 +33,24 @@ def diff_as_string(data_new, data_old)
hunk.unshift(oldhunk)
end
else
- output << oldhunk.diff(format)
+ output << matching_encoding(oldhunk.diff(format).to_s, output)
end
ensure
oldhunk = hunk
output << matching_encoding("\n", output)
end
end
#Handle the last remaining hunk
- output << oldhunk.diff(format) << matching_encoding("\n", output)
+ output << matching_encoding(oldhunk.diff(format).to_s,output)
+ output << matching_encoding("\n",output)
color_diff output
+ rescue Encoding::CompatibilityError
+ if input_data_new.encoding != input_data_old.encoding
+ "Could not produce a diff because the encoding of the actual string (#{input_data_old.encoding}) "+
+ "differs from the encoding of the expected string (#{input_data_new.encoding})"
+ else
+ "Could not produce a diff because of the encoding of the string (#{input_data_old.encoding})"
+ end
end
def diff_as_object(actual, expected)
View
19 spec/rspec/expectations/differ_spec.rb
@@ -40,14 +40,25 @@ module Expectations
end
if RUBY_VERSION.to_f > 1.9
it 'copes with encoded strings' do
+ pending "awaiting patch on diff-lcs"
@expected="Tu avec carté {count} itém has".encode('UTF-16LE')
- @actual="Tu avec carté {count} itém has".encode('UTF-16LE')
- expect(subject).to eql("")
+ @actual="Tu avec carte {count} item has".encode('UTF-16LE')
+ expect(subject).to eql(<<-EOD.encode('UTF-16LE'))
+
+@@ -1,2 +1,2 @@
+-Tu avec carté {count} itém has
++Tu avec carte {count} item has
+EOD
+ end
+ it 'copes with encoded strings' do
+ @expected="Tu avec carté {count} itém has".encode('UTF-16LE')
+ @actual="Tu avec carte {count} item has".encode('UTF-16LE')
+ expect(subject).to eql 'Could not produce a diff because of the encoding of the string (UTF-16LE)'
end
- it 'copes with differently encoded strings' do
+ it 'ouputs a message when encountering differently encoded strings' do
@expected="Tu avec carté {count} itém has".encode('UTF-16LE')
@actual="Tu avec carte {count} item has"
- expect { subject }.to raise_error Encoding::CompatibilityError
+ expect(subject).to eql 'Could not produce a diff because the encoding of the actual string (UTF-8) differs from the encoding of the expected string (UTF-16LE)'
end
end
end

0 comments on commit bd9a7bf

Please sign in to comment.