Skip to content

Commit

Permalink
Handle different VM encoding cases.
Browse files Browse the repository at this point in the history
Unconvertible characters are replaced with literal '?' characters.
Note that in unicode and other character encodings there are multiple
'?' characters, but the one I'm talking about is the one that you'd get
if you took ascii and transcoded it to whichever character encoding
you're working in.
  • Loading branch information
Sam Phippen committed Nov 12, 2013
1 parent 4cb1e3d commit 42346df
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
12 changes: 11 additions & 1 deletion lib/rspec/expectations/encoded_string.rb
Expand Up @@ -25,7 +25,17 @@ def split(regex_or_string)
def matching_encoding(string)
string.encode(encoding)
rescue Encoding::UndefinedConversionError
string.encode(encoding, :undef => :replace)
normalize_missing(string.encode(encoding, :invalid => :replace, :undef => :replace))
rescue Encoding::ConverterNotFoundError
normalize_missing(string.force_encoding(encoding).encode(:invalid => :replace))
end

def normalize_missing(string)
if encoding.to_s == "UTF-8"
string.gsub("\xEF\xBF\xBD".force_encoding(encoding), "?")
else
string
end
end
else
def matching_encoding(string)
Expand Down
8 changes: 5 additions & 3 deletions spec/rspec/expectations/encoded_string_spec.rb
Expand Up @@ -17,13 +17,15 @@ module RSpec::Expectations
end

context 'with a string that cannot be converted to the target encoding' do
it 'replaces undefined characters' do
it 'replaces undefined characters with either a ? or a unicode ?' do
ascii_string = "\xAE".force_encoding("ASCII-8BIT")
valid_unicode_string = "\xE2\x82\xAC".force_encoding('UTF-8')

resulting_string = build_encoded_string(valid_unicode_string, target_encoding) << ascii_string
expected_bytes = [226, 130, 172, 239, 191, 189]
expect(resulting_string.each_byte.to_a).to eq expected_bytes
expected_bytes = [226, 130, 172, "?".unpack("c").first]
actual_bytes = resulting_string.each_byte.to_a

expect(actual_bytes).to eq(expected_bytes)
end
end

Expand Down

0 comments on commit 42346df

Please sign in to comment.