Skip to content

Commit 10fd5a6

Browse files
tekknolagitenderlove
authored andcommitted
Add tests
1 parent b42c839 commit 10fd5a6

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

test/ruby/test_optimization.rb

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,4 +1215,50 @@ def == x
12151215
end
12161216
RUBY
12171217
end
1218+
1219+
def test_opt_new
1220+
pos_initialize = "
1221+
def initialize a, b
1222+
@a = a
1223+
@b = b
1224+
end
1225+
"
1226+
kw_initialize = "
1227+
def initialize a:, b:
1228+
@a = a
1229+
@b = b
1230+
end
1231+
"
1232+
kw_hash_initialize = "
1233+
def initialize a, **kw
1234+
@a = a
1235+
@b = kw[:b]
1236+
end
1237+
"
1238+
pos_prelude = "class OptNewFoo; #{pos_initialize}; end;"
1239+
kw_prelude = "class OptNewFoo; #{kw_initialize}; end;"
1240+
kw_hash_prelude = "class OptNewFoo; #{kw_hash_initialize}; end;"
1241+
[
1242+
"#{pos_prelude} OptNewFoo.new 1, 2",
1243+
"#{pos_prelude} a = 1; b = 2; OptNewFoo.new a, b",
1244+
"#{pos_prelude} def optnew_foo(a, b) = OptNewFoo.new(a, b); optnew_foo 1, 2",
1245+
"#{pos_prelude} def optnew_foo(*a) = OptNewFoo.new(*a); optnew_foo 1, 2",
1246+
"#{pos_prelude} def optnew_foo(...) = OptNewFoo.new(...); optnew_foo 1, 2",
1247+
"#{kw_prelude} def optnew_foo(**a) = OptNewFoo.new(**a); optnew_foo a: 1, b: 2",
1248+
"#{kw_hash_prelude} def optnew_foo(*a, **b) = OptNewFoo.new(*a, **b); optnew_foo 1, b: 2",
1249+
].each do |code|
1250+
iseq = RubyVM::InstructionSequence.compile(code)
1251+
insn = iseq.disasm
1252+
assert_match(/opt_new/, insn)
1253+
assert_match(/OptNewFoo:.+@a=1, @b=2/, iseq.eval.inspect)
1254+
end
1255+
[
1256+
'def optnew_foo(&) = OptNewFoo.new(&)',
1257+
'def optnew_foo(a, ...) = OptNewFoo.new(a, ...)',
1258+
].each do |code|
1259+
iseq = RubyVM::InstructionSequence.compile(code)
1260+
insn = iseq.disasm
1261+
assert_no_match(/opt_new/, insn)
1262+
end
1263+
end
12181264
end

0 commit comments

Comments
 (0)