Skip to content
This repository was archived by the owner on Nov 30, 2024. It is now read-only.

Commit ff988ae

Browse files
committed
Test EncodedSring#to_s for undefined conversion / invalid byte sequence
1 parent ee80462 commit ff988ae

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

spec/rspec/support/encoded_string_spec.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,47 @@ module RSpec::Support
2121
end
2222
end
2323

24+
describe '#to_s' do
25+
if RUBY_VERSION == '1.9.2' && RSpec::Support::Ruby.mri?
26+
it 'does nothing to an invalid byte sequence' do
27+
source_encoding = Encoding.find('UTF8-MAC')
28+
incompatible_encoding = Encoding.find('IBM737')
29+
string = "\xEF hi I am not going to work".force_encoding(source_encoding)
30+
resulting_string = build_encoded_string(string, incompatible_encoding)
31+
32+
expect(resulting_string.to_s).to eq("\xEF hi I am not going to work")
33+
end
34+
35+
it 'does nothing to unconvertable characters' do
36+
source_encoding = Encoding.find('UTF-16LE')
37+
incompatible_encoding = Encoding.find('IBM737')
38+
string = "\xEF hi I am not going to work".force_encoding(source_encoding)
39+
resulting_string = build_encoded_string(string, incompatible_encoding)
40+
41+
expect(resulting_string.to_s).to eq("\xEF hi I am not going to work")
42+
end
43+
else
44+
45+
it 'replaces invalid byte sequences with either a ? or a unicode ?' do
46+
source_encoding = Encoding.find('UTF8-MAC')
47+
incompatible_encoding = Encoding.find('IBM737')
48+
string = "\xEF hi I am not going to work".force_encoding(source_encoding)
49+
resulting_string = build_encoded_string(string, incompatible_encoding)
50+
51+
expect(resulting_string.to_s).to eq("? hi I am not going to work")
52+
end
53+
54+
it 'replaces all characters with either a ? or a unicode ?' do
55+
source_encoding = Encoding.find('UTF-16LE')
56+
incompatible_encoding = Encoding.find('IBM737')
57+
string = "\xEF hi I am not going to work".force_encoding(source_encoding)
58+
resulting_string = build_encoded_string(string, incompatible_encoding)
59+
60+
expect(resulting_string.to_s).to eq("??????????????")
61+
end
62+
end
63+
end
64+
2465
let(:ascii_arrow_symbol) { "\xAE" }
2566

2667
let(:utf_8_euro_symbol) { "\xE2\x82\xAC" }

0 commit comments

Comments
 (0)