Skip to content

Commit

Permalink
Ensure test suite is compatible with --frozen-string-literal
Browse files Browse the repository at this point in the history
As preparation for https://bugs.ruby-lang.org/issues/20205
making sure the test suite is compatible with frozen string
literals is making things easier.
  • Loading branch information
byroot committed Mar 14, 2024
1 parent 4e03d56 commit 09d8c99
Show file tree
Hide file tree
Showing 30 changed files with 159 additions and 147 deletions.
18 changes: 9 additions & 9 deletions basictest/test.rb
Expand Up @@ -879,7 +879,7 @@ def r(val); a,b,*c = *yield(); test_ok([a,b,c] == val, 2); end
test_ok($x == [7,5,3,2,1])

# split test
$x = "The Book of Mormon"
$x = +"The Book of Mormon"
test_ok($x.split(//).reverse!.join == $x.reverse)
test_ok($x.reverse == $x.reverse!)
test_ok("1 byte string".split(//).reverse.join(":") == "g:n:i:r:t:s: :e:t:y:b: :1")
Expand Down Expand Up @@ -1643,7 +1643,7 @@ def shift_test(a)
test_ok(/(\s+\d+){2}/ =~ " 1 2" && $& == " 1 2")
test_ok(/(?:\s+\d+){2}/ =~ " 1 2" && $& == " 1 2")

$x = <<END;
$x = +<<END;
ABCD
ABCD
END
Expand Down Expand Up @@ -1682,12 +1682,12 @@ def shift_test(a)
test_ok(?\C-a == "\1")
test_ok(?\M-a == "\341")
test_ok(?\M-\C-a == "\201")
test_ok("a".upcase![0] == ?A)
test_ok("A".downcase![0] == ?a)
test_ok("abc".tr!("a-z", "A-Z") == "ABC")
test_ok("aabbcccc".tr_s!("a-z", "A-Z") == "ABC")
test_ok("abcc".squeeze!("a-z") == "abc")
test_ok("abcd".delete!("bc") == "ad")
test_ok("a".dup.upcase![0] == ?A)
test_ok("A".dup.downcase![0] == ?a)
test_ok("abc".dup.tr!("a-z", "A-Z") == "ABC")
test_ok("aabbcccc".dup.tr_s!("a-z", "A-Z") == "ABC")
test_ok("abcc".dup.squeeze!("a-z") == "abc")
test_ok("abcd".dup.delete!("bc") == "ad")

$x = "abcdef"
$y = [ ?a, ?b, ?c, ?d, ?e, ?f ]
Expand All @@ -1700,7 +1700,7 @@ def shift_test(a)
}
test_ok(!$bad)

s = "a string"
s = +"a string"
s[0..s.size]="another string"
test_ok(s == "another string")

Expand Down
12 changes: 6 additions & 6 deletions bootstraptest/runner.rb
Expand Up @@ -548,10 +548,10 @@ def get_result_string(opt = '', **argh)
end
end

def make_srcfile(frozen_string_literal: nil)
def make_srcfile(frozen_string_literal: true)
filename = "bootstraptest.#{self.path}_#{self.lineno}_#{self.id}.rb"
File.open(filename, 'w') {|f|
f.puts "#frozen_string_literal:true" if frozen_string_literal
f.puts "#frozen_string_literal:#{frozen_string_literal}" unless frozen_string_literal.nil?
if $stress
f.puts "GC.stress = true" if $stress
else
Expand All @@ -572,9 +572,9 @@ def add_assertion src, pr
Assertion.new(src, path, lineno, pr)
end

def assert_equal(expected, testsrc, message = '', opt = '', **argh)
def assert_equal(expected, testsrc, message = '', opt = '', **kwargs)
add_assertion testsrc, -> as do
as.assert_check(message, opt, **argh) {|result|
as.assert_check(message, opt, **kwargs) {|result|
if expected == result
nil
else
Expand All @@ -585,9 +585,9 @@ def assert_equal(expected, testsrc, message = '', opt = '', **argh)
end
end

def assert_match(expected_pattern, testsrc, message = '')
def assert_match(expected_pattern, testsrc, message = '', **argh)
add_assertion testsrc, -> as do
as.assert_check(message) {|result|
as.assert_check(message, **argh) {|result|
if expected_pattern =~ result
nil
else
Expand Down
2 changes: 1 addition & 1 deletion bootstraptest/test_flow.rb
Expand Up @@ -363,7 +363,7 @@ class C
; $a << 8
; rescue Exception; $a << 99; end; $a}
assert_equal %q{[1, 2, 6, 3, 5, 7, 8]}, %q{$a = []; begin; ; $a << 1
o = "test"; $a << 2
o = "test".dup; $a << 2
def o.test(a); $a << 3
return a; $a << 4
ensure; $a << 5
Expand Down
4 changes: 2 additions & 2 deletions bootstraptest/test_insns.rb
Expand Up @@ -354,7 +354,7 @@ class X; def != other; true; end; end
[ 'opt_ge', %q{ +0.0.next_float >= 0.0 }, ],
[ 'opt_ge', %q{ ?z >= ?a }, ],

[ 'opt_ltlt', %q{ '' << 'true' }, ],
[ 'opt_ltlt', %q{ +'' << 'true' }, ],
[ 'opt_ltlt', %q{ ([] << 'true').join }, ],
[ 'opt_ltlt', %q{ (1 << 31) == 2147483648 }, ],

Expand All @@ -363,7 +363,7 @@ class X; def != other; true; end; end
[ 'opt_aref', %q{ 'true'[0] == ?t }, ],
[ 'opt_aset', %q{ [][0] = true }, ],
[ 'opt_aset', %q{ {}[0] = true }, ],
[ 'opt_aset', %q{ x = 'frue'; x[0] = 't'; x }, ],
[ 'opt_aset', %q{ x = +'frue'; x[0] = 't'; x }, ],
[ 'opt_aset', <<-'},', ], # {
# opt_aref / opt_aset mixup situation
class X; def x; {}; end; end
Expand Down
2 changes: 1 addition & 1 deletion bootstraptest/test_jump.rb
Expand Up @@ -292,7 +292,7 @@ class << self
end
end
end
s = "foo"
s = +"foo"
s.return_eigenclass == class << s; self; end
}, '[ruby-core:21379]'

Expand Down
47 changes: 23 additions & 24 deletions bootstraptest/test_ractor.rb
Expand Up @@ -628,7 +628,7 @@ def test n
}

# send shareable and unshareable objects
assert_equal "ok", %q{
assert_equal "ok", <<~'RUBY', frozen_string_literal: false
echo_ractor = Ractor.new do
loop do
v = Ractor.receive
Expand Down Expand Up @@ -695,10 +695,10 @@ module M; end
else
results.inspect
end
}
RUBY

# frozen Objects are shareable
assert_equal [false, true, false].inspect, %q{
assert_equal [false, true, false].inspect, <<~'RUBY', frozen_string_literal: false
class C
def initialize freeze
@a = 1
Expand All @@ -721,11 +721,11 @@ def check obj1
results << check(C.new(true)) # false
results << check(C.new(true).freeze) # true
results << check(C.new(false).freeze) # false
}
RUBY

# move example2: String
# touching moved object causes an error
assert_equal 'hello world', %q{
assert_equal 'hello world', <<~'RUBY', frozen_string_literal: false
# move
r = Ractor.new do
obj = Ractor.receive
Expand All @@ -743,7 +743,7 @@ def check obj1
else
raise 'unreachable'
end
}
RUBY

# move example2: Array
assert_equal '[0, 1]', %q{
Expand Down Expand Up @@ -946,7 +946,7 @@ def ractor_local_globals
}

# ivar in shareable-objects are not allowed to access from non-main Ractor
assert_equal "can not get unshareable values from instance variables of classes/modules from non-main Ractors", %q{
assert_equal "can not get unshareable values from instance variables of classes/modules from non-main Ractors", <<~'RUBY', frozen_string_literal: false
class C
@iv = 'str'
end
Expand All @@ -957,13 +957,12 @@ class C
end
end
begin
r.take
rescue Ractor::RemoteError => e
e.cause.message
end
}
RUBY

# ivar in shareable-objects are not allowed to access from non-main Ractor
assert_equal 'can not access instance variables of shareable objects from non-main Ractors', %q{
Expand Down Expand Up @@ -1150,7 +1149,7 @@ def self.cv
}

# Getting non-shareable objects via constants by other Ractors is not allowed
assert_equal 'can not access non-shareable objects in constant C::CONST by non-main Ractor.', %q{
assert_equal 'can not access non-shareable objects in constant C::CONST by non-main Ractor.', <<~'RUBY', frozen_string_literal: false
class C
CONST = 'str'
end
Expand All @@ -1162,10 +1161,10 @@ class C
rescue Ractor::RemoteError => e
e.cause.message
end
}
RUBY

# Constant cache should care about non-sharable constants
assert_equal "can not access non-shareable objects in constant Object::STR by non-main Ractor.", %q{
assert_equal "can not access non-shareable objects in constant Object::STR by non-main Ractor.", <<~'RUBY', frozen_string_literal: false
STR = "hello"
def str; STR; end
s = str() # fill const cache
Expand All @@ -1174,10 +1173,10 @@ def str; STR; end
rescue Ractor::RemoteError => e
e.cause.message
end
}
RUBY

# Setting non-shareable objects into constants by other Ractors is not allowed
assert_equal 'can not set constants with non-shareable objects by non-main Ractors', %q{
assert_equal 'can not set constants with non-shareable objects by non-main Ractors', <<~'RUBY', frozen_string_literal: false
class C
end
r = Ractor.new do
Expand All @@ -1188,7 +1187,7 @@ class C
rescue Ractor::RemoteError => e
e.cause.message
end
}
RUBY

# define_method is not allowed
assert_equal "defined with an un-shareable Proc in a different Ractor", %q{
Expand Down Expand Up @@ -1241,7 +1240,7 @@ class C
}

# ObjectSpace._id2ref can not handle unshareable objects with Ractors
assert_equal 'ok', %q{
assert_equal 'ok', <<~'RUBY', frozen_string_literal: false
s = 'hello'
Ractor.new s.object_id do |id ;s|
Expand All @@ -1251,10 +1250,10 @@ class C
:ok
end
end.take
}
RUBY

# Ractor.make_shareable(obj)
assert_equal 'true', %q{
assert_equal 'true', <<~'RUBY', frozen_string_literal: false
class C
def initialize
@a = 'foo'
Expand Down Expand Up @@ -1325,7 +1324,7 @@ def /(other)
}
Ractor.shareable?(a)
}
RUBY

# Ractor.make_shareable(obj) doesn't freeze shareable objects
assert_equal 'true', %q{
Expand Down Expand Up @@ -1422,14 +1421,14 @@ class C
}

# TracePoint with normal Proc should be Ractor local
assert_equal '[6, 10]', %q{
assert_equal '[7, 11]', %q{
rs = []
TracePoint.new(:line){|tp| rs << tp.lineno if tp.path == __FILE__}.enable do
Ractor.new{ # line 4
Ractor.new{ # line 5
a = 1
b = 2
}.take
c = 3 # line 8
c = 3 # line 9
end
rs
}
Expand Down Expand Up @@ -1503,7 +1502,7 @@ class C
2.times.map{
Ractor.new do
#{n}.times do
obj = ''
obj = +''
obj.instance_variable_set("@a", 1)
obj.instance_variable_set("@b", 1)
obj.instance_variable_set("@c", 1)
Expand Down Expand Up @@ -1662,7 +1661,7 @@ class C8; def self.foo = 17; end
Warning[:experimental] = $VERBOSE = true
STDERR.reopen(STDOUT)
eval("Ractor.new{}.take", nil, "test_ractor.rb", 1)
}
}, frozen_string_literal: false

# check moved object
assert_equal 'ok', %q{
Expand Down
2 changes: 1 addition & 1 deletion bootstraptest/test_syntax.rb
Expand Up @@ -629,7 +629,7 @@ class << (ary=[]); def []; 0; end; def []=(x); super(0,x);end;end; ary[]+=1

assert_match /invalid multibyte char/, %q{
$stderr = STDOUT
eval("\"\xf0".force_encoding("utf-8"))
eval("\"\xf0".dup.force_encoding("utf-8"))
}, '[ruby-dev:32429]'

# method ! and !=
Expand Down
18 changes: 9 additions & 9 deletions bootstraptest/test_yjit.rb
Expand Up @@ -185,7 +185,7 @@ def carray(iter)
end
def cstring(iter)
string = ""
string = "".dup
string.sum(iter.times { def string.sum(_) = :sum })
end
Expand Down Expand Up @@ -1250,7 +1250,7 @@ def foo
# Test polymorphic getinstancevariable. T_OBJECT -> T_STRING
assert_equal 'ok', %q{
@hello = @h1 = @h2 = @h3 = @h4 = 'ok'
str = ""
str = +""
str.instance_variable_set(:@hello, 'ok')
public def get
Expand Down Expand Up @@ -1395,7 +1395,7 @@ def index(obj, idx)
}

# Test default value block for Hash with opt_aref_with
assert_equal "false", %q{
assert_equal "false", <<~RUBY, frozen_string_literal: false
def index_with_string(h)
h["foo"]
end
Expand All @@ -1404,7 +1404,7 @@ def index_with_string(h)
index_with_string(h)
index_with_string(h)
}
RUBY

# A regression test for making sure cfp->sp is proper when
# hitting stubs. See :stub-sp-flush:
Expand Down Expand Up @@ -1904,7 +1904,7 @@ def make_str(foo)
}

# Test that String unary plus returns the same object ID for an unfrozen string.
assert_equal 'true', %q{
assert_equal 'true', <<~RUBY, frozen_string_literal: false
def jittable_method
str = "bar"
Expand All @@ -1914,7 +1914,7 @@ def jittable_method
uplus_str.object_id == old_obj_id
end
jittable_method
}
RUBY

# Test that String unary plus returns a different unfrozen string when given a frozen string
assert_equal 'false', %q{
Expand Down Expand Up @@ -2048,7 +2048,7 @@ def getbyte(s, i)

# Basic test for String#setbyte
assert_equal 'AoZ', %q{
s = "foo"
s = +"foo"
s.setbyte(0, 65)
s.setbyte(-1, 90)
s
Expand Down Expand Up @@ -2091,7 +2091,7 @@ def to_int
0
end
def ccall = "a".setbyte(self, 98)
def ccall = "a".dup.setbyte(self, 98)
ccall
@caller.first.split("'").last
Expand Down Expand Up @@ -4167,7 +4167,7 @@ def foo
assert_equal 'Hello World', %q{
def bar
args = ["Hello "]
greeting = "World"
greeting = +"World"
greeting.insert(0, *args)
greeting
end
Expand Down

0 comments on commit 09d8c99

Please sign in to comment.