diff options
Diffstat (limited to 'bootstraptest/test_method.rb')
| -rw-r--r-- | bootstraptest/test_method.rb | 454 |
1 files changed, 355 insertions, 99 deletions
diff --git a/bootstraptest/test_method.rb b/bootstraptest/test_method.rb index 2baf33539d..e894f6f601 100644 --- a/bootstraptest/test_method.rb +++ b/bootstraptest/test_method.rb @@ -3,7 +3,7 @@ assert_equal '1', 'def m() 1 end; m()' assert_equal '1', 'def m(a) a end; m(1)' assert_equal '[1, 2]', 'def m(a,b) [a, b] end; m(1,2)' assert_equal '[1, 2, 3]', 'def m(a,b,c) [a, b, c] end; m(1,2,3)' -assert_equal 'wrong number of arguments (1 for 0)', %q{ +assert_match /\Awrong number of arguments \(.*\b1\b.* 0\)\z/, %q{ def m; end begin m(1) @@ -12,7 +12,7 @@ assert_equal 'wrong number of arguments (1 for 0)', %q{ end } -assert_equal 'wrong number of arguments (0 for 1)', %q{ +assert_match /\Awrong number of arguments \(.*\b0\b.* 1\)\z/, %q{ def m a; end begin m @@ -22,7 +22,7 @@ assert_equal 'wrong number of arguments (0 for 1)', %q{ } # default argument -assert_equal '1', 'def m(x=1) x end; m()' +assert_equal '1', 'def m(x=1) x end; m();' assert_equal '1', 'def m(x=7) x end; m(1)' assert_equal '1', 'def m(a,x=1) x end; m(7)' assert_equal '1', 'def m(a,x=7) x end; m(7,1)' @@ -340,24 +340,6 @@ assert_equal '1', %q( class C; def m() 7 end; private :m end assert_equal '1', %q( class C; def m() 1 end; private :m end C.new.send(:m) ) -# with block -assert_equal '[[:ok1, :foo], [:ok2, :foo, :bar]]', -%q{ - class C - def [](a) - $ary << [yield, a] - end - def []=(a, b) - $ary << [yield, a, b] - end - end - - $ary = [] - C.new[:foo, &lambda{:ok1}] - C.new[:foo, &lambda{:ok2}] = :bar - $ary -} - # with assert_equal '[:ok1, [:ok2, 11]]', %q{ class C @@ -404,7 +386,6 @@ $result # aset and splat assert_equal '4', %q{class Foo;def []=(a,b,c,d);end;end;Foo.new[1,*a=[2,3]]=4} -assert_equal '4', %q{class Foo;def []=(a,b,c,d);end;end;def m(&blk)Foo.new[1,*a=[2,3],&blk]=4;end;m{}} # post test assert_equal %q{[1, 2, :o1, :o2, [], 3, 4, NilClass, nil, nil]}, %q{ @@ -886,50 +867,6 @@ class C0; def m *args; [:C0_m, args]; end; end class C1 < C0; def m a, o=:o; super; end; end ; C1.new.m 1, 2} -assert_equal %q{[:ok, :ok, :ok, :ok, :ok, :ok, :ng, :ng]}, %q{ - $ans = [] - class Foo - def m - end - end - - c1 = c2 = nil - - lambda{ - $SAFE = 4 - c1 = Class.new{ - def m - end - } - c2 = Class.new(Foo){ - alias mm m - } - }.call - - def test - begin - yield - rescue SecurityError - $ans << :ok - else - $ans << :ng - end - end - - o1 = c1.new - o2 = c2.new - - test{o1.m} - test{o2.mm} - test{o1.send :m} - test{o2.send :mm} - test{o1.public_send :m} - test{o2.public_send :mm} - test{o1.method(:m).call} - test{o2.method(:mm).call} - $ans -} - assert_equal 'ok', %q{ class C def x=(n) @@ -954,34 +891,6 @@ assert_equal 'ok', %q{ }, '[ruby-core:11998]' assert_equal 'ok', %q{ - proc{ - $SAFE = 2 - class C - def m - :ok - end - end - }.call - C.new.m -}, '[ruby-core:11998]' - -assert_equal 'ok', %q{ - proc{ - $SAFE = 3 - class C - def m - :ng - end - end - }.call - begin - C.new.m - rescue SecurityError - :ok - end -}, '[ruby-core:11998]' - -assert_equal 'ok', %q{ class B def m() :fail end end @@ -1001,8 +910,8 @@ assert_equal 'ok', %q{ assert_normal_exit %q{ begin - Process.setrlimit(Process::RLIMIT_STACK, 4_202_496) - # FreeBSD fails this less than 4M + 8K bytes. + Process.setrlimit(Process::RLIMIT_STACK, 4_206_592) + # FreeBSD SEGVs this less than 4M + 12K bytes. rescue Exception exit end @@ -1179,8 +1088,355 @@ assert_equal 'ok', %q{ 'ok' end } + +# should not cache when splat +assert_equal 'ok', %q{ + class C + attr_reader :a + def initialize + @a = 1 + end + end + + def m *args + C.new.a(*args) + end + + m() + begin + m(1) + rescue ArgumentError + 'ok' + end +} + +assert_equal 'DC', %q{ + $result = [] + + class C + def foo *args + $result << 'C' + end + end + class D + def foo *args + $result << 'D' + end + end + + o1 = $o1 = C.new + o2 = $o2 = D.new + + args = Object.new + def args.to_a + test1 $o2, nil + [] + end + def test1 o, args + o.foo(*args) + end + test1 o1, args + $result.join +} + +assert_equal 'DC', %q{ + $result = [] + + class C + def foo *args + $result << 'C' + end + end + class D + def foo *args + $result << 'D' + end + end + + o1 = $o1 = C.new + o2 = $o2 = D.new + + block = Object.new + def block.to_proc + test2 $o2, %w(a, b, c), nil + Proc.new{} + end + def test2 o, args, block + o.foo(*args, &block) + end + test2 o1, [], block + $result.join +} + +assert_equal 'ok', %q{ + def foo + binding + ["ok"].first + end + foo + foo +}, '[Bug #20178]' + +assert_equal 'ok', %q{ + def bar(x); x; end + def foo(...); bar(...); end + foo('ok') +} + +assert_equal 'ok', %q{ + def bar(x); x; end + def foo(z, ...); bar(...); end + foo(1, 'ok') +} + +assert_equal 'ok', %q{ + def bar(x, y); x; end + def foo(...); bar("ok", ...); end + foo(1) +} + +assert_equal 'ok', %q{ + def bar(x); x; end + def foo(...); 1.times { return bar(...) }; end + foo("ok") +} + +assert_equal 'ok', %q{ + def bar(x); x; end + def foo(...); x = nil; 1.times { x = bar(...) }; x; end + foo("ok") +} + +assert_equal 'ok', %q{ + def bar(x); yield; end + def foo(...); bar(...); end + foo(1) { "ok" } +} + +assert_equal 'ok', %q{ + def baz(x); x; end + def bar(...); baz(...); end + def foo(...); bar(...); end + foo("ok") +} + +assert_equal '[1, 2, 3, 4]', %q{ + def baz(a, b, c, d); [a, b, c, d]; end + def bar(...); baz(1, ...); end + def foo(...); bar(2, ...); end + foo(3, 4) +} + +assert_equal 'ok', %q{ + class Foo; def self.foo(x); x; end; end + class Bar < Foo; def self.foo(...); super; end; end + Bar.foo('ok') +} + +assert_equal 'ok', %q{ + class Foo; def self.foo(x); x; end; end + class Bar < Foo; def self.foo(...); super(...); end; end + Bar.foo('ok') +} + +assert_equal 'ok', %q{ + class Foo; def self.foo(x, y); x + y; end; end + class Bar < Foo; def self.foo(...); super("o", ...); end; end + Bar.foo('k') +} + +assert_equal 'ok', %q{ + def bar(a); a; end + def foo(...); lambda { bar(...) }; end + foo("ok").call +} + +assert_equal 'ok', %q{ + class Foo; def self.foo(x, y); x + y; end; end + class Bar < Foo; def self.y(&b); b; end; def self.foo(...); y { super("o", ...) }; end; end + Bar.foo('k').call +} + +assert_equal 'ok', %q{ + def baz(n); n; end + def foo(...); bar = baz(...); lambda { lambda { bar } }; end + foo("ok").call.call +} + +assert_equal 'ok', %q{ + class A; def self.foo(...); new(...); end; attr_reader :b; def initialize(a, b:"ng"); @a = a; @b = b; end end + A.foo(1).b + A.foo(1, b: "ok").b +} + +assert_equal 'ok', %q{ + class A; def initialize; @a = ["ok"]; end; def first(...); @a.first(...); end; end + def call x; x.first; end + def call1 x; x.first(1); end + call(A.new) + call1(A.new).first +} + +assert_equal 'ok', %q{ + class A; def foo; yield("o"); end; end + class B < A; def foo(...); super { |x| yield(x + "k") }; end; end + B.new.foo { |x| x } +} + +assert_equal "[1, 2, 3, 4]", %q{ + def foo(*b) = b + + def forward(...) + splat = [1,2,3] + foo(*splat, ...) + end + + forward(4) +} + +assert_equal "[1, 2, 3, 4]", %q{ +class A + def foo(*b) = b +end + +class B < A + def foo(...) + splat = [1,2,3] + super(*splat, ...) + end +end + +B.new.foo(4) +} + +assert_equal 'ok', %q{ + class A; attr_reader :iv; def initialize(...) = @iv = "ok"; end + A.new("foo", bar: []).iv +} + +assert_equal 'ok', %q{ + def foo(a, b) = a + b + def bar(...) = foo(...) + bar(1, 2) + bar(1, 2) + begin + bar(1, 2, 3) + "ng" + rescue ArgumentError + "ok" + end +} + +assert_equal 'ok', %q{ + class C + def foo(...) = :ok + def bar(...) = __send__(:foo, ...) + end + + C.new.bar +} + +assert_equal 'ok', %q{ + class C + def method_missing(...) = :ok + def foo(...) = xyzzy(...) + end + + C.new.foo +} + +assert_equal 'ok', %q{ + class C + def initialize(a) + end + end + + def foo(...) + C.new(...) + :ok + end + + foo(*["bar"]) + foo("baz") +} + +assert_equal 'ok', %q{ + class C + def foo(b:) + b + end + end + + def foo(...) + C.new.send(...) + end + + foo(:foo, b: :ok) + foo(*["foo"], b: :ok) +} + +assert_equal 'ok', %q{ + Thing = Struct.new(:value) + + Obj = Thing.new("ok") + + def delegate(...) + Obj.value(...) + end + + def no_args + delegate + end + + def splat_args(*args) + delegate(*args) + end + + no_args + splat_args +} + assert_equal 'ok', %q{ - [0][0, &proc{}] += 21 - 'ok' -}, '[ruby-core:30534]' + class A + private + def foo = "ng" + end + + class B + def initialize(o) + @o = o + end + + def foo(...) = @o.foo(...) + def internal_foo = foo + end + + b = B.new(A.new) + + begin + b.internal_foo + rescue NoMethodError + "ok" + end +} + +assert_equal 'ok', <<~RUBY + def test(*, kw: false) + "ok" + end + test +RUBY + +assert_equal '[1, 2, 3]', %q{ + def target(*args) = args + def x = [1] + def forwarder(...) = target(*x, 2, ...) + forwarder(3).inspect +}, '[Bug #21832] post-splat args before forwarding' + +assert_equal '[nil, nil]', %q{ + def self_reading(a = a, kw:) = a + def through_binding(a = binding.local_variable_get(:a), kw:) = a + [self_reading(kw: 1), through_binding(kw: 1)] +}, 'nil initialization of optional parameters' |
