Skip to content
This repository
Fetching contributors…

Cannot retrieve contributors at this time

file 580 lines (513 sloc) 12.41 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580
assert "42", "def foo; 42; end; p foo"
assert "42", "def foo(x); x + 41; end; p foo(1)"
assert "42", "def foo(x, y); x + y; end; p foo(40, 2)"
assert "42", "def foo(x, y, z); x + y + z; end; p foo(40, 1, 1)"

assert "42", "def foo(x=42); x; end; p foo"
assert "42", "def foo(x=0); x; end; p foo(42)"
assert "42", "def foo(x=30, y=10, z=2); x+y+z; end; p foo"
assert "42", "def foo(x=123, y=456, z=789); x+y+z; end; p foo(30, 10, 2)"
assert "42", "def foo(x, y=40); x+y; end; p foo(2)"
assert "42", "def foo(x, y=123); x+y; end; p foo(20, 22)"
assert "42", "def foo(x, y, z=2); x+y+z; end; p foo(20, 20)"
assert "42", "def foo(x, y, z=123); x+y+z; end; p foo(20, 20, 2)"
assert "42", "def foo(x, y=20, z=2); x+y+z; end; p foo(20)"
assert "42", "def foo(x, y=123, z=2); x+y+z; end; p foo(30, 10)"
assert "42", "def foo; :ko; end; begin; p self.foo; rescue NoMethodError; p 42 end"

assert "126", "def foo(a=b=c=42); a+b+c; end; p foo"
assert "[42, nil]", "def f(a=X::x=b=1) p [a, b] end; f(42)"

assert "42", "def foo; 1; end; i = 0; while i < 42; i += foo; end; p i"
assert "42", %q{
class X; def foo; 15; end; end
class Y; def foo; 01; end; end
class Z; def foo; 05; end; end
x = X.new
y = Y.new
z = Z.new
i = 0
i += x.foo; i += y.foo; i += z.foo
i += x.foo; i += y.foo; i += z.foo
p i
}

assert "42", %q{
class X; def foo; p 42; end; end
class Y < X; def foo; super; end; end
Y.new.foo
}

assert "42", %q{
class X; def foo(x); p x; end; end
class Y < X; def foo; super(42); end; end
Y.new.foo
}

assert "42", %q{
class X; def foo(x); p x; end; end
class Y < X; def foo(x); super; end; end
Y.new.foo(42)
}

assert "42", %q{
class X; def foo; 42; end; end
class Y < X; def foo; super; end; end
class Z < Y; def foo; super; end; end
p Z.new.foo
}

assert "42", %{
class X; def foo; yield; end; end
class Y < X; def foo; super; end; end
Y.new.foo { p 42 }
}

assert "true", %{
class X; def foo(x); x; end; end
module M1; def foo(x); super(true); end; end
module M2; def foo(x); return false if x; super; end; end
class Y < X; include M1; include M2; def foo; super(false); end; end
p Y.new.foo
}

assert "42", "def foo; 42; end; p send(:foo)"
assert "42", "def foo(x, y); x + y; end; p send(:foo, 40, 2)"

assert "42", %q{
def foo; :nok; end
def send(x); 42; end
p send(:foo)
}

assert "42", %q{
class Object
def send(x); 42; end
end
def foo; :nok; end
p send(:foo)
}

assert ":ok", %{
begin
send(:does_not_exist)
rescue NoMethodError
p :ok
end
}

assert ':ok', %{
class X
def method_missing(x, *args, &block)
p :ko
end
protected
def foo
p :ok if yield == 42
end
end
X.new.send(:foo) { 42 }
}

assert "42", "def foo; return 42; end; p foo"
assert "42", "def foo(x); if x; return 42; end; end; p foo(true)"
assert "42", "def foo(x); if x; x += 2; return x; end; end; p foo(40)"
assert "42", "def foo(x); if x; x += 2; return x; x += 666; end; end; p foo(40)"

assert "[1, 2, 3]", "def foo; return 1, 2, 3; end; p foo"

assert "42", "def foo=(x); @x = x + 1; end; self.foo=41; p @x"
assert "42", "def []=(x, y); @x = x + y; end; self[40]=2; p @x", :known_bug => true

assert "[]", "def foo; return *[]; end; p foo"

assert '42', "def foo; 1.times { return 42 }; p :nok; end; p foo"

assert "42", "def foo(x,y,z); x+y+z; end; a=[20,10,12]; p foo(*a)"
assert "42", "def foo(x,y,z); x+y+z; end; a=[20,10]; p foo(*a, 12)"
assert "42", "def foo(x,y,z); x+y+z; end; a=[20,10]; p foo(12, *a)"
assert "42", "def foo(x,y,z); x+y+z; end; a=[20]; p foo(10, 12, *a)"
assert "42", "def foo(x,y,z); x+y+z; end; a=[20]; p foo(10, *a, 12)"
assert "42", "def foo(x,y,z); x+y+z; end; a=[20]; p foo(*a, 10, 12)"
assert "42", "def foo(x,y,z); x+y+z; end; a=[20]; b=[10,12]; p foo(*a, *b)"
assert "42", "def foo(x,y,z); x+y+z; end; a=[20]; b=[10]; p foo(*a, 12, *b)"
assert "42", "def foo(x,y,z); x+y+z; end; a=[20]; b=[10]; p foo(12, *a, *b)"
assert "42", "def foo(x,y,z); x+y+z; end; a=[20]; b=[10]; p foo(*a, *b, 12)"
assert "42", "def foo(x,y,z); x+y+z; end; a=[20]; b=[10]; c=[12]; p foo(*a, *b, *c)"
assert "42", "def foo(x); x; end; a=42; p foo(*a)"

assert ":ok", "def foo(*args); :ok; end; p foo"
assert ":ok", "def foo(&block); :ok; end; p foo"
assert ":ok", "def foo(*args, &block); :ok; end; p foo"
assert ":ok", "def foo(x, *args, &block); x; end; p foo(:ok)"
assert ":ok", "def f(&proc) p :ok; end; f(&nil)"

assert ":ok", %{
def foo(&block) p(block ? :ko : :ok) end
def bar(&block) block.call() end
bar { foo }
}

assert "[1, nil, :c]", "def f(a, b = :b, c = :c) [a, b, c] end; p f(1, nil)"
assert "[1, :b, :c, 2]\n[1, 2, :c, 3]\n[1, 2, 3, 4]", %{
def f(a, b = :b, c = :c, d) [a, b, c, d] end
p f(1, 2)
p f(1, 2, 3)
p f(1, 2, 3, 4)
}
assert "[1, :b, :c, 2]\n[1, 2, :c, 3]\n[1, 2, 3, 4]", %{
def f(a, b = :b, c = :c, d) [a, b, c, d] end
p f(1, 2)
p f(1, 2, 3)
p f(1, 2, 3, 4)
}
assert "[1, :b, :c, [], 2, 3]\n[1, 2, :c, [], 3, 4]\n[1, 2, 3, [], 4, 5]\n[1, 2, 3, [4], 5, 6]\n[1, 2, 3, [4, 5], 6, 7]", %{
def f(a, b = :b, c = :c, *args, d, e) [a, b, c, args, d, e] end
p f(1, 2, 3)
p f(1, 2, 3, 4)
p f(1, 2, 3, 4, 5)
p f(1, 2, 3, 4, 5, 6)
p f(1, 2, 3, 4, 5, 6, 7)
}
assert "42", "def f((a, b)); a end; p f([42, 53])"
assert "42", "def f((a, b)); a end; p f([42, 53, 64])" # ignore additional elements in the array
assert "[42, nil]", "def f((a, b)); [a, b] end; p f([42])" # not used args are set to nil
assert "[1, 2, [], 3, nil]\n[1, 2, [], 3, 4]\n[1, 2, [3], 4, 5]", %{
def f((x, y, *a, b, c)); [x, y, a, b, c] end
p f([1, 2, 3])
p f([1, 2, 3, 4])
p f([1, 2, 3, 4, 5])
}
assert "true", %{
class A; def to_ary; [42]; end; end
def f((*a)); a; end;
p f(A.new) == [42]
} # to_ary (not to_a) is called on non-Array objects
assert "true", %{def f((*a)); a; end; o = Object.new; p f(o) == [o]} # objects without to_ary are just passed in a one element array

assert ":ok", "def f(x = 1) :ko; end; def f() end; begin p f(1); rescue ArgumentError; p :ok; end"
assert ":ok", "def f(x) :ko; end; def f() end; begin p f(1); rescue ArgumentError; p :ok; end"
assert ":ok", "def f() :ko; end; def f(x) end; begin p f(); rescue ArgumentError; p :ok; end"

assert ":ok", "def f(); end; begin f(1); rescue ArgumentError; p :ok; rescue; p :ko; end"
assert ":ok", "def f(a); end; begin f; rescue ArgumentError; p :ok; rescue; p :ko; end"
assert ":ok", "def f(a); end; begin f(1, 2); rescue ArgumentError; p :ok; rescue; p :ko; end"
assert ':ok', "def f(a, b); end; begin; f; rescue ArgumentError; p :ok; rescue; p :ko; end"
assert ':ok', "def f(a, b); end; begin; f(1, 2, 3); rescue ArgumentError; p :ok; rescue; p :ko; end"

assert ':ok', "def f(a, b); end; begin; a=[1]; f(*a); rescue ArgumentError; p :ok; rescue; p :ko; end"
assert ':ok', "def f(a, b); end; begin; a=[1,2,3]; f(*a); rescue ArgumentError; p :ok; rescue; p :ko; end"

assert ":ok", %{
def func()
1.times { |x| func() }
end
p :ok
}

assert "1\n2\n2", %q{
def func
p 1
def func
p 2
end
func
end
func
func
}

assert "[1, 2]", %{
def f
yield 1, 2
end
f {|*args| p args}
}

assert ':ok', %{
1.times do
def foo(&a)
a.call
end
foo { p :ok }
end
}

assert '42', %{
class Foo
def self.foo; 42; end
end
p Foo.foo
}
assert '42', %{
class Foo
class << self
def foo; 42; end
end
end
p Foo.foo
}
assert '42', %{
class Foo; end
def Foo.foo; 42; end
p Foo.foo
}
assert '42', %{
o = Object.new
def o.foo; 42; end
p o.foo
}
assert '42', %{
o = Object.new
class << o
def foo; 42; end
end
p o.foo
}

assert '42', %{
def foo; p 42; end
def bar(a = foo); end
bar
}

assert '42', %{
def foo() yield 1, 2 end
x = 1
w = 42
foo { |x, y = :y| p w }
}

# Tail-call elimination
assert '42', %{
def foo(n); return if n == 0; foo(n-1); end
foo(30000000)
p 42
}

assert 'true', 'p Object.new.methods.include?(:object_id)'
assert "false\ntrue", %{
obj = Object.new
meta = class << obj; self; end
p obj.respond_to?(:bar) # without this call to respond_to it works fine
meta.class_eval { def bar; end }
p obj.respond_to?(:bar)
}

assert '42', %{
module M; def foo; 42; end; end
module M2; include M; end
class X; include M2; end
p X.new.foo
}

assert '42', %{
module M; def foo; 42; end; end
module M2; def bar; foo; end; include M; end
class X; include M2; end
p X.new.bar
}

assert '42', %{
module M
def foo; 123; end
module_function :foo
end
class X; def foo; 42; end; end
include M
p X.new.foo
}

assert "42\n42\n42\n42", %{
module M
def foo(*args); 42; end
module_function :foo

module_function
def bar(*args); 42; end
end

p M.foo
p M.foo('blah')
p M.bar
p M.bar('blah')
}

assert '42', %{
def foo(x,y,z=42)
return z if z == 42
foo(1,2)
end
p foo(1,2,3)
}

assert ':ok', %{
class X
def method_missing(x, *args)
p :ok if x == :foo
end
end
X.new.foo
}

assert ':ok', %{
class X
def method_missing(x, *args)
p :ok if x == :foo and args == [42]
end
end
X.new.foo(42)
}

assert ':ok', %{
class X
def method_missing(x, *args, &block)
p :ok if x == :foo and block.call == 42
end
end
X.new.foo { 42 }
}

assert ':ok', %{
class X
def method_missing(x, *args, &block)
p :ok if x == :foo and block.call == 42
end
private
def foo; end
end
X.new.foo { 42 }
}

assert ':ok', %{
module M
def initialize(*args)
super
end
end
class X
include M
end
X.new
p :ok
}

assert '42', %{
class X
def foo(x); p x; end
end
class Y < X
def foo(*args); super; end
end
Y.new.foo(42)
}

assert %{"hello world"\n42}, %{
class X; def foo(x,y); p x,y; end; end
class Y < X; def foo(x,*args); x << ' world'; args[0] = 42; super; end; end
Y.new.foo('hello', 1)
}

assert %{"hello world"\n42}, %{
class X; def foo(x,y); p x,y; end; end
class Y < X; def foo(x,*args); x << ' world'; args = 42; super; end; end
Y.new.foo('hello', 1)
}

assert 'true', %{
class X; def foo; p block_given?; end; end
class Y < X; def foo(&b); super; end; end
Y.new.foo {}
}

assert "42", %{
class X; def foo(x); p x; end; end
class Y < X; def foo(x); 1.times { |; x| x = 1; super; } end; end
Y.new.foo(42)
}, :known_bug => true

assert "42", %{
module M
def foo; p 42; end
end
class Module
include M
end
class Range; foo; end
}

assert ':ok', %{
class B; end
class A < B
def initialize
super(nil)
end
end

begin
A.new
p :ok
rescue ArgumentError
p :ko
end
}

assert '42', %{
class C1
def foo(x); p x; end
end
class C2 < C1
def foo(x=42); super; end
end
C2.new.foo
}

assert ':ok', %{
class C1
def foo; p :ok; end
end
class C2 < C1
def foo; 1.times { return super }; end
end
C2.new.foo
}

assert ':ok', %{
begin
super
rescue NoMethodError
p :ok
end
}

assert ':ok', %{
begin
1.times { super }
rescue NoMethodError
p :ok
end
}

assert "true", %{
class Foo
def respond_to?(*x); super; end
end
p Foo.new.respond_to?(:object_id)
}

assert ":ok", %{
class C1
def foo(arg); end
end
class C2 < C1
def foo; super; end
end
begin
C2.new.foo
rescue ArgumentError
p :ok
end
}, :known_bug => true

# TODO: find a better place for this.
assert '', %{
$SAFE=4
s="omg"
s.freeze
}

assert ":X2_foo\n:X_foo\n:Y2_foo\n:Y_foo\n:Y2_foo\n:Y_foo\n:Y2_foo\n:Y_foo\n:X2_foo\n:X_foo\n:Y2_foo\n:Y_foo\n:Y2_foo\n:Y_foo\n:Y2_foo\n:Y_foo", %{
class X
def initialize
@children = []
3.times { @children << Y::Y2.new }
end
def foo
p :X_foo
@children.each { |o| o.foo }
end
class X2 < X
def foo
p :X2_foo
super
end
end
end
class Y
def foo
p :Y_foo
end
class Y2 < Y
def foo
p :Y2_foo
super
end
end
end
o = X::X2.new
o.foo
o.foo
}

assert '42', %{
class X
def initialize
yield
end
end
class Y<X
def initialize
super
end
end
Y.new { p 42 }
}

assert '42', %{
class X
def initialize
yield
end
end
class Y<X
def initialize
super()
end
end
Y.new { p 42 }
}
Something went wrong with that request. Please try again.