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.
  • Loading branch information
byroot committed Mar 13, 2024
1 parent f23d158 commit 9fc10ea
Show file tree
Hide file tree
Showing 280 changed files with 851 additions and 683 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 command_line/dash_v_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

describe "when used alone" do
it "prints version and ends" do
ruby_exe(nil, args: '-v').should include(RUBY_DESCRIPTION)
ruby_exe(nil, args: '-v').sub("+PRISM ", "").should include(RUBY_DESCRIPTION)
end unless (defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?) ||
(defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?) ||
(ENV['RUBY_MN_THREADS'] == '1')
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
14 changes: 7 additions & 7 deletions core/array/pack/buffer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
describe "Array#pack with :buffer option" do
it "returns specified buffer" do
n = [ 65, 66, 67 ]
buffer = " "*3
buffer = +" "*3
result = n.pack("ccc", buffer: buffer) #=> "ABC"
result.should equal(buffer)
end

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">.dup.encode!(Encoding::UTF_16BE))

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

it "calls #inspect on real and imaginary" do
real = NumericSpecs::Subclass.new
real.should_receive(:inspect).and_return("1")
real.should_receive(:inspect).and_return(+"1")
imaginary = NumericSpecs::Subclass.new
imaginary.should_receive(:inspect).and_return("2")
imaginary.should_receive(:inspect).and_return(+"2")
imaginary.should_receive(:<).any_number_of_times.and_return(false)
Complex(real, imaginary).inspect.should == "(1+2i)"
end

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)")
real.should_receive(:inspect).and_return(+"(1)")
imaginary = NumericSpecs::Subclass.new
imaginary.should_receive(:inspect).and_return("(2)")
imaginary.should_receive(:inspect).and_return(+"(2)")
imaginary.should_receive(:<).any_number_of_times.and_return(false)
Complex(real, imaginary).inspect.should == "((1)+(2)*i)"
end
Expand Down
12 changes: 10 additions & 2 deletions core/complex/to_r_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,16 @@
end

describe "when the imaginary part is Float 0.0" do
it "raises RangeError" do
-> { Complex(0, 0.0).to_r }.should raise_error(RangeError)
ruby_version_is ''...'3.4' do
it "raises RangeError" do
-> { Complex(0, 0.0).to_r }.should raise_error(RangeError)
end
end

ruby_version_is '3.4' do
it "returns a Rational" do
Complex(0, 0.0).to_r.should == 0r
end
end
end
end
4 changes: 2 additions & 2 deletions core/complex/to_s_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@

it "treats real and imaginary parts as strings" do
real = NumericSpecs::Subclass.new
real.should_receive(:to_s).and_return("1")
real.should_receive(:to_s).and_return(+"1")
imaginary = NumericSpecs::Subclass.new
imaginary.should_receive(:to_s).and_return("2")
imaginary.should_receive(:to_s).and_return(+"2")
imaginary.should_receive(:<).any_number_of_times.and_return(false)
Complex(real, imaginary).to_s.should == "1+2i"
end
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 9fc10ea

Please sign in to comment.