Skip to content

Commit

Permalink
Make the spec suite compatible with --enable-frozen-string-literal
Browse files Browse the repository at this point in the history
Extracted from: ruby/ruby#10235

Ref: https://bugs.ruby-lang.org/issues/20205

Ruby will gradually move towards enabling frozen string literals
by default. Making the ruby spec suite compatible is a good first
step.

Co-authored-by: Benoit Daloze <eregontp@gmail.com>
  • Loading branch information
byroot and eregon committed Mar 14, 2024
1 parent f23d158 commit 0be2f8b
Show file tree
Hide file tree
Showing 210 changed files with 753 additions and 689 deletions.
7 changes: 7 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ jobs:
matrix:
os: [ ubuntu, macos, windows ]
ruby: [ 3.0.6, 3.1.4, 3.2.2, 3.3.0 ]
rubyopt: [""]
include:
- os: ubuntu
ruby: 3.3.0
rubyopt: "--enable-frozen-string-literal"

runs-on: ${{ matrix.os }}-latest
steps:
- name: git config autocrlf
Expand All @@ -28,6 +34,7 @@ jobs:
if: matrix.os == 'ubuntu'
env:
CHECK_LEAKS: true
RUBYOPT: "${{ matrix.rubyopt }}"
run: ../mspec/bin/mspec -j --timeout 30

- name: Run specs (macOS)
Expand Down
2 changes: 1 addition & 1 deletion core/argf/readpartial_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

it "clears output buffer even if EOFError is raised because @argf is at end" do
begin
output = "to be cleared"
output = +"to be cleared"

argf [@file1_name] do
@argf.read
Expand Down
2 changes: 1 addition & 1 deletion core/argf/shared/getc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

it "reads each char of files" do
argf [@file1, @file2] do
chars = ""
chars = +""
@chars.size.times { chars << @argf.send(@method) }
chars.should == @chars
end
Expand Down
4 changes: 2 additions & 2 deletions core/argf/shared/read.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@

it "treats second argument as an output buffer" do
argf [@file1_name] do
buffer = ""
buffer = +""
@argf.send(@method, @file1.size, buffer)
buffer.should == @file1
end
end

it "clears output buffer before appending to it" do
argf [@file1_name] do
buffer = "to be cleared"
buffer = +"to be cleared"
@argf.send(@method, @file1.size, buffer)
buffer.should == @file1
end
Expand Down
2 changes: 1 addition & 1 deletion core/array/fill_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

it "does not replicate the filler" do
ary = [1, 2, 3, 4]
str = "x"
str = +"x"
ary.fill(str).should == [str, str, str, str]
str << "y"
ary.should == [str, str, str, str]
Expand Down
18 changes: 9 additions & 9 deletions core/array/fixtures/encoded_strings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,36 @@
module ArraySpecs
def self.array_with_usascii_and_7bit_utf8_strings
[
'foo'.force_encoding('US-ASCII'),
'foo'.dup.force_encoding('US-ASCII'),
'bar'
]
end

def self.array_with_usascii_and_utf8_strings
[
'foo'.force_encoding('US-ASCII'),
'foo'.dup.force_encoding('US-ASCII'),
'báz'
]
end

def self.array_with_7bit_utf8_and_usascii_strings
[
'bar',
'foo'.force_encoding('US-ASCII')
'foo'.dup.force_encoding('US-ASCII')
]
end

def self.array_with_utf8_and_usascii_strings
[
'báz',
'bar',
'foo'.force_encoding('US-ASCII')
'foo'.dup.force_encoding('US-ASCII')
]
end

def self.array_with_usascii_and_utf8_strings
[
'foo'.force_encoding('US-ASCII'),
'foo'.dup.force_encoding('US-ASCII'),
'bar',
'báz'
]
Expand All @@ -41,7 +41,7 @@ def self.array_with_utf8_and_7bit_binary_strings
[
'bar',
'báz',
'foo'.force_encoding('BINARY')
'foo'.dup.force_encoding('BINARY')
]
end

Expand All @@ -55,14 +55,14 @@ def self.array_with_utf8_and_binary_strings

def self.array_with_usascii_and_7bit_binary_strings
[
'bar'.force_encoding('US-ASCII'),
'foo'.force_encoding('BINARY')
'bar'.dup.force_encoding('US-ASCII'),
'foo'.dup.force_encoding('BINARY')
]
end

def self.array_with_usascii_and_binary_strings
[
'bar'.force_encoding('US-ASCII'),
'bar'.dup.force_encoding('US-ASCII'),
[255].pack('C').force_encoding('BINARY')
]
end
Expand Down
12 changes: 6 additions & 6 deletions core/array/pack/buffer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@
it "adds result at the end of buffer content" do
n = [ 65, 66, 67 ] # result without buffer is "ABC"

buffer = ""
buffer = +""
n.pack("ccc", buffer: buffer).should == "ABC"

buffer = "123"
buffer = +"123"
n.pack("ccc", buffer: buffer).should == "123ABC"

buffer = "12345"
buffer = +"12345"
n.pack("ccc", buffer: buffer).should == "12345ABC"
end

Expand All @@ -31,19 +31,19 @@
context "offset (@) is specified" do
it 'keeps buffer content if it is longer than offset' do
n = [ 65, 66, 67 ]
buffer = "123456"
buffer = +"123456"
n.pack("@3ccc", buffer: buffer).should == "123ABC"
end

it "fills the gap with \\0 if buffer content is shorter than offset" do
n = [ 65, 66, 67 ]
buffer = "123"
buffer = +"123"
n.pack("@6ccc", buffer: buffer).should == "123\0\0\0ABC"
end

it 'does not keep buffer content if it is longer than offset + result' do
n = [ 65, 66, 67 ]
buffer = "1234567890"
buffer = +"1234567890"
n.pack("@3ccc", buffer: buffer).should == "123ABC"
end
end
Expand Down
2 changes: 1 addition & 1 deletion core/array/pack/shared/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
f = pack_format("*")
[ [["\u{3042 3044 3046 3048}", 0x2000B].pack(f+"U"), Encoding::BINARY],
[["abcde\xd1", "\xFF\xFe\x81\x82"].pack(f+"u"), Encoding::BINARY],
[["a".force_encoding("ascii"), "\xFF\xFe\x81\x82"].pack(f+"u"), Encoding::BINARY],
[["a".dup.force_encoding("ascii"), "\xFF\xFe\x81\x82"].pack(f+"u"), Encoding::BINARY],
# under discussion [ruby-dev:37294]
[["\u{3042 3044 3046 3048}", 1].pack(f+"N"), Encoding::BINARY]
].should be_computed_by(:encoding)
Expand Down
6 changes: 3 additions & 3 deletions core/array/shared/inspect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
end

it "does not call #to_s on a String returned from #inspect" do
str = "abc"
str = +"abc"
str.should_not_receive(:to_s)

[str].send(@method).should == '["abc"]'
Expand Down Expand Up @@ -98,8 +98,8 @@
end

it "does not raise if inspected result is not default external encoding" do
utf_16be = mock("utf_16be")
utf_16be.should_receive(:inspect).and_return(%<"utf_16be \u3042">.encode!(Encoding::UTF_16BE))
utf_16be = mock(+"utf_16be")
utf_16be.should_receive(:inspect).and_return(%<"utf_16be \u3042">.encode(Encoding::UTF_16BE))

[utf_16be].send(@method).should == '["utf_16be \u3042"]'
end
Expand Down
6 changes: 4 additions & 2 deletions core/complex/inspect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@

it "calls #inspect on real and imaginary" do
real = NumericSpecs::Subclass.new
real.should_receive(:inspect).and_return("1")
# + because of https://bugs.ruby-lang.org/issues/20337
real.should_receive(:inspect).and_return(+"1")
imaginary = NumericSpecs::Subclass.new
imaginary.should_receive(:inspect).and_return("2")
imaginary.should_receive(:<).any_number_of_times.and_return(false)
Expand All @@ -26,7 +27,8 @@

it "adds an `*' before the `i' if the last character of the imaginary part is not numeric" do
real = NumericSpecs::Subclass.new
real.should_receive(:inspect).and_return("(1)")
# + because of https://bugs.ruby-lang.org/issues/20337
real.should_receive(:inspect).and_return(+"(1)")
imaginary = NumericSpecs::Subclass.new
imaginary.should_receive(:inspect).and_return("(2)")
imaginary.should_receive(:<).any_number_of_times.and_return(false)
Expand Down
3 changes: 2 additions & 1 deletion core/complex/to_s_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@

it "treats real and imaginary parts as strings" do
real = NumericSpecs::Subclass.new
real.should_receive(:to_s).and_return("1")
# + because of https://bugs.ruby-lang.org/issues/20337
real.should_receive(:to_s).and_return(+"1")
imaginary = NumericSpecs::Subclass.new
imaginary.should_receive(:to_s).and_return("2")
imaginary.should_receive(:<).any_number_of_times.and_return(false)
Expand Down
4 changes: 2 additions & 2 deletions core/dir/children_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
encoding = Encoding.find("filesystem")
encoding = Encoding::BINARY if encoding == Encoding::US_ASCII
platform_is_not :windows do
children.should include("こんにちは.txt".force_encoding(encoding))
children.should include("こんにちは.txt".dup.force_encoding(encoding))
end
children.first.encoding.should equal(Encoding.find("filesystem"))
end
Expand Down Expand Up @@ -113,7 +113,7 @@
encoding = Encoding.find("filesystem")
encoding = Encoding::BINARY if encoding == Encoding::US_ASCII
platform_is_not :windows do
children.should include("こんにちは.txt".force_encoding(encoding))
children.should include("こんにちは.txt".dup.force_encoding(encoding))
end
children.first.encoding.should equal(Encoding.find("filesystem"))
end
Expand Down
2 changes: 1 addition & 1 deletion core/dir/entries_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
encoding = Encoding.find("filesystem")
encoding = Encoding::BINARY if encoding == Encoding::US_ASCII
platform_is_not :windows do
entries.should include("こんにちは.txt".force_encoding(encoding))
entries.should include("こんにちは.txt".dup.force_encoding(encoding))
end
entries.first.encoding.should equal(Encoding.find("filesystem"))
end
Expand Down
2 changes: 1 addition & 1 deletion core/dir/shared/glob.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
end

it "raises an Encoding::CompatibilityError if the argument encoding is not compatible with US-ASCII" do
pattern = "file*".force_encoding Encoding::UTF_16BE
pattern = "file*".dup.force_encoding Encoding::UTF_16BE
-> { Dir.send(@method, pattern) }.should raise_error(Encoding::CompatibilityError)
end

Expand Down
Loading

0 comments on commit 0be2f8b

Please sign in to comment.