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

Commit abc4124

Browse files
authored
Merge pull request #320 from pat/frozen-string-literals
Updates for frozen-string-literal compatibility.
2 parents b0b5795 + d628be5 commit abc4124

File tree

2 files changed

+29
-25
lines changed

2 files changed

+29
-25
lines changed

lib/rspec/support/differ.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,19 @@ def object_to_string(object)
181181
when Hash
182182
hash_to_string(object)
183183
when Array
184-
PP.pp(ObjectFormatter.prepare_for_inspection(object), "")
184+
PP.pp(ObjectFormatter.prepare_for_inspection(object), "".dup)
185185
when String
186186
object =~ /\n/ ? object : object.inspect
187187
else
188-
PP.pp(object, "")
188+
PP.pp(object, "".dup)
189189
end
190190
end
191191

192192
def hash_to_string(hash)
193193
formatted_hash = ObjectFormatter.prepare_for_inspection(hash)
194194
formatted_hash.keys.sort_by { |k| k.to_s }.map do |key|
195-
pp_key = PP.singleline_pp(key, "")
196-
pp_value = PP.singleline_pp(formatted_hash[key], "")
195+
pp_key = PP.singleline_pp(key, "".dup)
196+
pp_value = PP.singleline_pp(formatted_hash[key], "".dup)
197197

198198
"#{pp_key} => #{pp_value},"
199199
end.join("\n")

spec/rspec/support/encoded_string_spec.rb

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,15 @@ module RSpec::Support
1919
if String.method_defined?(:encoding)
2020
it "picks the default external encoding for incompatible encodings" do
2121

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")
2424
expect(Encoding.compatible?(str1, str2)).to be_nil
2525
expect(EncodedString.pick_encoding(str1, str2)).to eq(Encoding.default_external)
2626
end
2727

2828
# https://github.com/rubyspec/rubyspec/blob/91ce9f6549/core/encoding/compatible_spec.rb#L31
2929
it "picks a compatible encoding" do
30-
str1 = "abc".force_encoding Encoding::US_ASCII
30+
str1 = forced_encoding "abc", Encoding::US_ASCII
3131
str2 = "\u3042".encode("utf-8")
3232
expect(EncodedString.pick_encoding(str1, str2)).to eq(Encoding::UTF_8)
3333
end
@@ -54,7 +54,7 @@ module RSpec::Support
5454
# see https://github.com/jruby/jruby/blob/c1be61a501/test/mri/ruby/test_transcode.rb#L13
5555
let(:source_encoding) { Encoding.find('US-ASCII') }
5656
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) }
5858

5959
it 'normally raises an EncodedString::InvalidByteSequenceError' do
6060
expect {
@@ -66,7 +66,7 @@ module RSpec::Support
6666
it 'replaces invalid byte sequences with the REPLACE string', :pending => RSpec::Support::Ruby.jruby? && !RSpec::Support::Ruby.jruby_9000? do
6767
resulting_string = build_encoded_string(string, target_encoding).to_s
6868
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)
7070
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
7171
end
7272
end
@@ -75,7 +75,7 @@ module RSpec::Support
7575
# see https://github.com/rubyspec/rubyspec/blob/91ce9f6549/core/string/shared/encode.rb#L12
7676
let(:source_encoding) { Encoding.find('ASCII-8BIT') }
7777
let(:no_converter_encoding) { Encoding::Emacs_Mule }
78-
let(:string) { "\x80".force_encoding(source_encoding) }
78+
let(:string) { forced_encoding("\x80", source_encoding) }
7979

8080
it 'normally raises an Encoding::ConverterNotFoundError' do
8181
expect {
@@ -88,13 +88,13 @@ module RSpec::Support
8888
if RUBY_VERSION < '2.1'
8989
it 'does nothing' do
9090
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)
9292
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
9393
end
9494
else
9595
it 'forces the encoding and replaces invalid characters with the REPLACE string' do
9696
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)
9898
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
9999
end
100100

@@ -112,7 +112,7 @@ module RSpec::Support
112112
context 'when there is an undefined conversion to the target encoding' do
113113
let(:source_encoding) { Encoding.find('ISO-8859-1') }
114114
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) }
116116

117117
it 'normally raises an Encoding::UndefinedConversionError' do
118118
expect {
@@ -123,7 +123,7 @@ module RSpec::Support
123123
it 'replaces all undefines conversions with the REPLACE string' do
124124
resulting_string = build_encoded_string(string, incompatible_encoding).to_s
125125
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')
127127
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
128128
end
129129
end
@@ -134,20 +134,20 @@ module RSpec::Support
134134

135135
describe '#<<' do
136136
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') }
139139

140140
it 'encodes and appends the string' do
141141
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')
143143
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
144144
end
145145
end
146146

147147
context 'with a string that cannot be converted to the target encoding' do
148148
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') }
151151

152152
it "normally raises an Encoding::CompatibilityError" do
153153
expect {
@@ -165,11 +165,11 @@ module RSpec::Support
165165

166166
context 'with two ascii strings with a target encoding of UTF-8 ' do
167167
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")
170170

171171
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)
173173
expect(resulting_string).to be_identical_string(expected_string).with_same_encoding
174174
end
175175
end
@@ -187,7 +187,7 @@ module RSpec::Support
187187
end
188188

189189
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)
191191
resulting_string = build_encoded_string(wrapped_string, utf8_encoding).split(delimiter)
192192
exp1, exp2 = sprintf(wrapped_string_template, EncodedString::REPLACE).force_encoding(utf8_encoding).split(delimiter)
193193
expect(resulting_string).to match [
@@ -197,7 +197,7 @@ module RSpec::Support
197197
end
198198

199199
it 'handles invalidly encoded strings' do
200-
source_string = "an\xAE\nother".force_encoding('US-ASCII')
200+
source_string = forced_encoding("an\xAE\nother", 'US-ASCII')
201201
expect(
202202
build_encoded_string(source_string, utf8_encoding).split("\n")
203203
).to eq([
@@ -229,7 +229,7 @@ module RSpec::Support
229229
end
230230

231231
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) }
233233

234234
it 'normally raises an ArgumentError' do
235235
expect(message_with_invalid_byte_sequence).not_to be_valid_encoding
@@ -251,6 +251,10 @@ module RSpec::Support
251251
def build_encoded_string(string, target_encoding = string.encoding)
252252
EncodedString.new(string, target_encoding)
253253
end
254+
255+
def forced_encoding(string, encoding)
256+
string.dup.force_encoding(encoding)
257+
end
254258
else
255259

256260
describe '#source_encoding' do

0 commit comments

Comments
 (0)