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
|
require 'test/minirunit'
test_check "Test Methods"
def testMethod
$toto = true
"some output"
end
test_ok("some output" == testMethod)
test_ok($toto)
def testMethodWithArgs(a,b)
end
begin
testMethodWithArgs()
rescue ArgumentError => boom
test_ok(true)
end
begin
testMethodWithArgs(1)
rescue ArgumentError => boom
test_ok(true)
end
test_exception(ArgumentError) { testMethod(1, 2, 3) }
class MethodTestingOne
def method_missing(name, *args)
[name, args]
end
end
mt1 = MethodTestingOne.new
test_equal([:hello, [1, 2]], mt1.hello(1, 2))
def f(x, &proc)
proc.call()
end
y = "hello"
f(10) {
test_equal("hello", y)
}
class TestMethods_X
def hello
"yeah"
end
end
x = TestMethods_X.new
test_equal("TestMethods_X", "#{x.class}")
test_equal("yeah", "#{x.hello}")
p = Proc.new {|z| "#{z.size}"}
test_equal("hello".size.to_s, p.call("hello"))
class TM1
define_method("foo") {
$foo = true
}
end
$foo = false
TM1.new.foo
test_ok($foo)
o = Object.new
def o.foo(x)
"hello" + x
end
foo_method = o.method(:foo)
test_equal(1, foo_method.arity)
foo_proc = foo_method.to_proc
test_equal(Proc, foo_proc.class)
test_equal("helloworld", foo_proc.call("world"))
class TM_A
def foo
"x"
end
def bar
"y"
end
end
class TM_B < TM_A
def foo
super
end
def bar
super()
end
end
test_equal("x", TM_B.new.foo) # Tests ZSuperNode
test_equal("y", TM_B.new.bar) # Tests SuperNode
def no_arg_opt(*); end
test_no_exception { no_arg_opt }
class BadInspect
def inspect; nil; end
end
test_exception(NoMethodError) { BadInspect.new.hop }
# method_missing tests
class Foo
def method_missing(sym)
end
end
# method_missing 1. Redefine to one which throws no Error
test_no_exception { Foo.new.frogger }
class Foo
undef method_missing
end
# method_missing 2. Undef user-defined for normal behavior
test_exception(NoMethodError) { Foo.new.frogger }
class Object
undef method_missing
end
# method_missing 3. Undef Object to make sure we handle that
test_exception(NoMethodError) { Foo.new.frogger }
module Kernel
undef method_missing
end
# method_missing 4. Undef Kernel to make sure we handle that
test_exception(NoMethodError) { Foo.new.frogger }
# test that methods added to Object are not made private by default (old code in evaluator did this, but was removed)
class Object
def newmethod
end
end
test_no_exception {"".newmethod}
#####
class PrivMethod
attr_accessor :a
def bar
self.foo = 'good'
end
private
def foo=(a)
@a = a
end
end
f = PrivMethod.new
f.bar
test_equal('good', f.a)
# Method to_s and inspect tests
test_equal('#<Method: String#to_s>', "string".method(:to_s).to_s)
test_equal('#<Method: String#to_s>', "string".method(:to_s).inspect)
test_equal('#<UnboundMethod: String#to_s>', "string".method(:to_s).unbind.to_s)
test_equal('#<UnboundMethod: String#to_s>', "string".method(:to_s).unbind.inspect)
class C
def var=(val)
test_equal(nil, val)
end
end
C.new.var =()
|