@@ -19,15 +19,15 @@ module RSpec::Support
19
19
if String . method_defined? ( :encoding )
20
20
it "picks the default external encoding for incompatible encodings" do
21
21
22
- str1 = "\xa1 " . force_encoding ( "iso-8859-1" )
23
- str2 = "\xa1 \xa1 " . force_encoding ( "euc-jp" )
22
+ str1 = forced_encoding ( "\xa1 " , "iso-8859-1" )
23
+ str2 = forced_encoding ( "\xa1 \xa1 " , "euc-jp" )
24
24
expect ( Encoding . compatible? ( str1 , str2 ) ) . to be_nil
25
25
expect ( EncodedString . pick_encoding ( str1 , str2 ) ) . to eq ( Encoding . default_external )
26
26
end
27
27
28
28
# https://github.com/rubyspec/rubyspec/blob/91ce9f6549/core/encoding/compatible_spec.rb#L31
29
29
it "picks a compatible encoding" do
30
- str1 = "abc" . force_encoding Encoding ::US_ASCII
30
+ str1 = forced_encoding "abc" , Encoding ::US_ASCII
31
31
str2 = "\u3042 " . encode ( "utf-8" )
32
32
expect ( EncodedString . pick_encoding ( str1 , str2 ) ) . to eq ( Encoding ::UTF_8 )
33
33
end
@@ -54,7 +54,7 @@ module RSpec::Support
54
54
# see https://github.com/jruby/jruby/blob/c1be61a501/test/mri/ruby/test_transcode.rb#L13
55
55
let ( :source_encoding ) { Encoding . find ( 'US-ASCII' ) }
56
56
let ( :target_encoding ) { Encoding . find ( 'UTF-8' ) }
57
- let ( :string ) { "I have a bad byté\x80 " . force_encoding ( source_encoding ) }
57
+ let ( :string ) { forced_encoding ( "I have a bad byté\x80 " , source_encoding ) }
58
58
59
59
it 'normally raises an EncodedString::InvalidByteSequenceError' do
60
60
expect {
@@ -66,7 +66,7 @@ module RSpec::Support
66
66
it 'replaces invalid byte sequences with the REPLACE string' , :pending => RSpec ::Support ::Ruby . jruby? && !RSpec ::Support ::Ruby . jruby_9000? do
67
67
resulting_string = build_encoded_string ( string , target_encoding ) . to_s
68
68
replacement = EncodedString ::REPLACE * 3
69
- expected_string = "I have a bad byt#{ replacement } " . force_encoding ( target_encoding )
69
+ expected_string = forced_encoding ( "I have a bad byt#{ replacement } " , target_encoding )
70
70
expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
71
71
end
72
72
end
@@ -75,7 +75,7 @@ module RSpec::Support
75
75
# see https://github.com/rubyspec/rubyspec/blob/91ce9f6549/core/string/shared/encode.rb#L12
76
76
let ( :source_encoding ) { Encoding . find ( 'ASCII-8BIT' ) }
77
77
let ( :no_converter_encoding ) { Encoding ::Emacs_Mule }
78
- let ( :string ) { "\x80 " . force_encoding ( source_encoding ) }
78
+ let ( :string ) { forced_encoding ( "\x80 " , source_encoding ) }
79
79
80
80
it 'normally raises an Encoding::ConverterNotFoundError' do
81
81
expect {
@@ -88,13 +88,13 @@ module RSpec::Support
88
88
if RUBY_VERSION < '2.1'
89
89
it 'does nothing' do
90
90
resulting_string = build_encoded_string ( string , no_converter_encoding ) . to_s
91
- expected_string = "\x80 " . force_encoding ( no_converter_encoding )
91
+ expected_string = forced_encoding ( "\x80 " , no_converter_encoding )
92
92
expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
93
93
end
94
94
else
95
95
it 'forces the encoding and replaces invalid characters with the REPLACE string' do
96
96
resulting_string = build_encoded_string ( string , no_converter_encoding ) . to_s
97
- expected_string = EncodedString ::REPLACE . dup . force_encoding ( no_converter_encoding )
97
+ expected_string = forced_encoding ( EncodedString ::REPLACE , no_converter_encoding )
98
98
expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
99
99
end
100
100
@@ -112,7 +112,7 @@ module RSpec::Support
112
112
context 'when there is an undefined conversion to the target encoding' do
113
113
let ( :source_encoding ) { Encoding . find ( 'ISO-8859-1' ) }
114
114
let ( :incompatible_encoding ) { Encoding . find ( 'EUC-JP' ) }
115
- let ( :string ) { "\xa0 hi I am not going to work" . force_encoding ( source_encoding ) }
115
+ let ( :string ) { forced_encoding ( "\xa0 hi I am not going to work" , source_encoding ) }
116
116
117
117
it 'normally raises an Encoding::UndefinedConversionError' do
118
118
expect {
@@ -123,7 +123,7 @@ module RSpec::Support
123
123
it 'replaces all undefines conversions with the REPLACE string' do
124
124
resulting_string = build_encoded_string ( string , incompatible_encoding ) . to_s
125
125
replacement = EncodedString ::REPLACE
126
- expected_string = "#{ replacement } hi I am not going to work" . force_encoding ( 'EUC-JP' )
126
+ expected_string = forced_encoding ( "#{ replacement } hi I am not going to work" , 'EUC-JP' )
127
127
expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
128
128
end
129
129
end
@@ -134,20 +134,20 @@ module RSpec::Support
134
134
135
135
describe '#<<' do
136
136
context 'with strings that can be converted to the target encoding' do
137
- let ( :valid_ascii_string ) { "abcde" . force_encoding ( "ASCII-8BIT" ) }
138
- let ( :valid_unicode_string ) { utf_8_euro_symbol . force_encoding ( 'UTF-8' ) }
137
+ let ( :valid_ascii_string ) { forced_encoding ( "abcde" , "ASCII-8BIT" ) }
138
+ let ( :valid_unicode_string ) { forced_encoding ( utf_8_euro_symbol , 'UTF-8' ) }
139
139
140
140
it 'encodes and appends the string' do
141
141
resulting_string = build_encoded_string ( valid_unicode_string , utf8_encoding ) << valid_ascii_string
142
- expected_string = "#{ utf_8_euro_symbol } abcde" . force_encoding ( 'UTF-8' )
142
+ expected_string = forced_encoding ( "#{ utf_8_euro_symbol } abcde" , 'UTF-8' )
143
143
expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
144
144
end
145
145
end
146
146
147
147
context 'with a string that cannot be converted to the target encoding' do
148
148
context 'when appending a string with an incompatible character encoding' do
149
- let ( :ascii_string ) { ascii_arrow_symbol . force_encoding ( "ASCII-8BIT" ) }
150
- let ( :valid_unicode_string ) { utf_8_euro_symbol . force_encoding ( 'UTF-8' ) }
149
+ let ( :ascii_string ) { forced_encoding ( ascii_arrow_symbol , "ASCII-8BIT" ) }
150
+ let ( :valid_unicode_string ) { forced_encoding ( utf_8_euro_symbol , 'UTF-8' ) }
151
151
152
152
it "normally raises an Encoding::CompatibilityError" do
153
153
expect {
@@ -165,11 +165,11 @@ module RSpec::Support
165
165
166
166
context 'with two ascii strings with a target encoding of UTF-8 ' do
167
167
it 'has an encoding of UTF-8' do
168
- ascii_string = 'abc' . force_encoding ( "ASCII-8BIT" )
169
- other_ascii_string = '123' . force_encoding ( "ASCII-8BIT" )
168
+ ascii_string = forced_encoding ( 'abc' , "ASCII-8BIT" )
169
+ other_ascii_string = forced_encoding ( '123' , "ASCII-8BIT" )
170
170
171
171
resulting_string = build_encoded_string ( ascii_string , utf8_encoding ) << other_ascii_string
172
- expected_string = 'abc123' . force_encoding ( utf8_encoding )
172
+ expected_string = forced_encoding ( 'abc123' , utf8_encoding )
173
173
expect ( resulting_string ) . to be_identical_string ( expected_string ) . with_same_encoding
174
174
end
175
175
end
@@ -187,7 +187,7 @@ module RSpec::Support
187
187
end
188
188
189
189
it 'splits the string based on the delimiter accounting for encoding' do
190
- delimiter = "b" . force_encoding ( utf8_encoding )
190
+ delimiter = forced_encoding ( "b" , utf8_encoding )
191
191
resulting_string = build_encoded_string ( wrapped_string , utf8_encoding ) . split ( delimiter )
192
192
exp1 , exp2 = sprintf ( wrapped_string_template , EncodedString ::REPLACE ) . force_encoding ( utf8_encoding ) . split ( delimiter )
193
193
expect ( resulting_string ) . to match [
@@ -197,7 +197,7 @@ module RSpec::Support
197
197
end
198
198
199
199
it 'handles invalidly encoded strings' do
200
- source_string = "an\xAE \n other" . force_encoding ( 'US-ASCII' )
200
+ source_string = forced_encoding ( "an\xAE \n other" , 'US-ASCII' )
201
201
expect (
202
202
build_encoded_string ( source_string , utf8_encoding ) . split ( "\n " )
203
203
) . to eq ( [
@@ -229,7 +229,7 @@ module RSpec::Support
229
229
end
230
230
231
231
context 'when the string has an invalid byte sequence' do
232
- let ( :message_with_invalid_byte_sequence ) { "\xEF \255 \xAD I have bad bytes" . force_encoding ( utf8_encoding ) }
232
+ let ( :message_with_invalid_byte_sequence ) { forced_encoding ( "\xEF \255 \xAD I have bad bytes" , utf8_encoding ) }
233
233
234
234
it 'normally raises an ArgumentError' do
235
235
expect ( message_with_invalid_byte_sequence ) . not_to be_valid_encoding
@@ -251,6 +251,10 @@ module RSpec::Support
251
251
def build_encoded_string ( string , target_encoding = string . encoding )
252
252
EncodedString . new ( string , target_encoding )
253
253
end
254
+
255
+ def forced_encoding ( string , encoding )
256
+ string . dup . force_encoding ( encoding )
257
+ end
254
258
else
255
259
256
260
describe '#source_encoding' do
0 commit comments