Permalink
Browse files

Fix an issue with using ascii encoding internally

Also this describes some of the expected behaviour for differently
encoded strings.
  • Loading branch information...
JonRowe committed Mar 13, 2013
1 parent a68dfdc commit 474bea35978818444263100993d65afb7d83add4
Showing with 29 additions and 10 deletions.
  1. +13 −5 lib/rspec/expectations/differ.rb
  2. +16 −5 spec/rspec/expectations/differ_spec.rb
@@ -7,10 +7,10 @@ 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)
- data_old = data_old.split(/\n/).map! { |e| e.chomp }
- data_new = data_new.split(/\n/).map! { |e| e.chomp }
+ data_old = data_old.split(Regexp.new matching_encoding('\n',data_old)).map! { |e| e.chomp }
+ data_new = data_new.split(Regexp.new matching_encoding('\n',data_new)).map! { |e| e.chomp }
diffs = Diff::LCS.diff(data_old, data_new)
- output = ""
+ output = matching_encoding "", data_old
return output if diffs.empty?
oldhunk = hunk = nil
file_length_difference = 0
@@ -37,11 +37,11 @@ def diff_as_string(data_new, data_old)
end
ensure
oldhunk = hunk
- output << "\n"
+ output << matching_encoding("\n",oldhunk)
end
end
#Handle the last remaining hunk
- output << oldhunk.diff(format) << "\n"
+ output << oldhunk.diff(format) << matching_encoding("\n",oldhunk)
color_diff output
end
@@ -108,6 +108,14 @@ def object_to_string(object)
PP.pp(object,"")
end
end
+
+ def matching_encoding(string,source)
+ if source.respond_to? :encoding
+ string.encode(source.encoding)
+ else
+ string
+ end
+ end
end
end
@@ -13,10 +13,11 @@ module Expectations
# color disabled context
describe '#diff_as_string' do
+ subject { differ.diff_as_string(@expected, @actual) }
it "outputs unified diff of two strings" do
- expected="foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n"
- actual="foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n"
- expected_diff= <<'EOD'
+ @expected="foo\nbar\nzap\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nline\n"
+ @actual="foo\nzap\nbar\nthis\nis\nsoo\nvery\nvery\nequal\ninsert\na\nanother\nline\n"
+ expect(subject).to eql(<<-'EOD')
@@ -1,6 +1,6 @@
@@ -35,8 +36,18 @@ module Expectations
line
EOD
- diff = differ.diff_as_string(expected, actual)
- expect(diff).to eql(expected_diff)
+ end
+ if RUBY_VERSION.to_f > 1.9
+ it 'copes with encoded strings' do
+ @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("")
+ end
+ it 'copes with 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
+ end
end
end

0 comments on commit 474bea3

Please sign in to comment.