| 1 |
## |
| 2 |
# Proc(Ext) Test |
| 3 |
|
| 4 |
def enable_debug_info? |
| 5 |
return @enable_debug_info unless @enable_debug_info == nil |
| 6 |
begin |
| 7 |
raise |
| 8 |
rescue => e |
| 9 |
@enable_debug_info = !e.backtrace.empty? |
| 10 |
if @enable_debug_info && e.backtrace[0].include?("(unknown)") |
| 11 |
@enable_debug_info = false |
| 12 |
end |
| 13 |
return @enable_debug_info |
| 14 |
end |
| 15 |
end |
| 16 |
|
| 17 |
assert('Proc#source_location') do |
| 18 |
skip unless enable_debug_info? |
| 19 |
file, line = Proc.new{}.source_location |
| 20 |
assert_equal __FILE__, file |
| 21 |
assert_equal __LINE__ - 2, line |
| 22 |
end |
| 23 |
|
| 24 |
assert('Proc#inspect') do |
| 25 |
ins = Proc.new{}.inspect |
| 26 |
if enable_debug_info? |
| 27 |
metas = %w(\\ * ? [ ] { }) |
| 28 |
file = __FILE__.split("").map{|c| metas.include?(c) ? "\\#{c}" : c}.join |
| 29 |
line = __LINE__ - 4 |
| 30 |
else |
| 31 |
file = line = "-" |
| 32 |
end |
| 33 |
assert_match "#<Proc:0x* #{file}:#{line}>", ins |
| 34 |
end |
| 35 |
|
| 36 |
assert('Proc#parameters') do |
| 37 |
parameters = Proc.new{|x,y=42,*other|}.parameters |
| 38 |
assert_equal [[:opt, :x], [:opt, :y], [:rest, :other]], parameters |
| 39 |
end |
| 40 |
|
| 41 |
assert('Proc#lambda?') do |
| 42 |
assert_true lambda{}.lambda? |
| 43 |
assert_true !Proc.new{}.lambda? |
| 44 |
end |
| 45 |
|
| 46 |
assert('Proc#===') do |
| 47 |
proc = Proc.new {|a| a * 2} |
| 48 |
assert_equal 20, (proc === 10) |
| 49 |
end |
| 50 |
|
| 51 |
assert('Proc#yield') do |
| 52 |
proc = Proc.new {|a| a * 2} |
| 53 |
assert_equal 20, proc.yield(10) |
| 54 |
end |
| 55 |
|
| 56 |
assert('Proc#curry') do |
| 57 |
b = proc {|x, y, z| (x||0) + (y||0) + (z||0) } |
| 58 |
assert_equal 6, b.curry[1][2][3] |
| 59 |
assert_equal 6, b.curry[1, 2][3, 4] |
| 60 |
assert_equal 6, b.curry(5)[1][2][3][4][5] |
| 61 |
assert_equal 6, b.curry(5)[1, 2][3, 4][5] |
| 62 |
assert_equal 1, b.curry(1)[1] |
| 63 |
|
| 64 |
b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) } |
| 65 |
assert_equal 6, b.curry[1][2][3] |
| 66 |
assert_raise(ArgumentError) { b.curry[1, 2][3, 4] } |
| 67 |
assert_raise(ArgumentError) { b.curry(5) } |
| 68 |
assert_raise(ArgumentError) { b.curry(1) } |
| 69 |
|
| 70 |
assert_false(proc{}.curry.lambda?) |
| 71 |
assert_true(lambda{}.curry.lambda?) |
| 72 |
end |
| 73 |
|
| 74 |
assert('Proc#parameters') do |
| 75 |
assert_equal([], Proc.new {}.parameters) |
| 76 |
assert_equal([], Proc.new {||}.parameters) |
| 77 |
assert_equal([[:opt, :a]], Proc.new {|a|}.parameters) |
| 78 |
assert_equal([[:req, :a]], lambda {|a|}.parameters) |
| 79 |
assert_equal([[:opt, :a]], lambda {|a=nil|}.parameters) |
| 80 |
assert_equal([[:req, :a]], ->(a){}.parameters) |
| 81 |
assert_equal([[:rest, :*]], lambda { |*| }.parameters) |
| 82 |
assert_equal([[:rest, :a]], Proc.new {|*a|}.parameters) |
| 83 |
assert_equal([[:opt, :a], [:opt, :b], [:opt, :c], [:opt, :d], [:rest, :e], [:opt, :f], [:opt, :g], [:block, :h]], Proc.new {|a,b,c=:c,d=:d,*e,f,g,&h|}.parameters) |
| 84 |
assert_equal([[:req, :a], [:req, :b], [:opt, :c], [:opt, :d], [:rest, :e], [:req, :f], [:req, :g], [:block, :h]], lambda {|a,b,c=:c,d=:d,*e,f,g,&h|}.parameters) |
| 85 |
end |
| 86 |
|
| 87 |
assert('Proc#to_proc') do |
| 88 |
proc = Proc.new {} |
| 89 |
assert_equal proc, proc.to_proc |
| 90 |
end |
| 91 |
|
| 92 |
assert('Kernel#proc') do |
| 93 |
assert_true !proc{|a|}.lambda? |
| 94 |
|
| 95 |
assert_raise LocalJumpError do |
| 96 |
proc{ break }.call |
| 97 |
end |
| 98 |
end |
| 99 |
|
| 100 |
assert "Proc#<< and Proc#>>" do |
| 101 |
add3 = ->(n) { n + 3 } |
| 102 |
mul2 = ->(n) { n * 2 } |
| 103 |
|
| 104 |
f1 = mul2 << add3 |
| 105 |
assert_kind_of Proc, f1 |
| 106 |
assert_equal 16, f1.call(5) |
| 107 |
|
| 108 |
f2 = mul2 >> add3 |
| 109 |
assert_kind_of Proc, f2 |
| 110 |
assert_equal 13, f2.call(5) |
| 111 |
end |
| 112 |
|
| 113 |
assert('mrb_proc_new_cfunc_with_env') do |
| 114 |
ProcExtTest.mrb_proc_new_cfunc_with_env(:test) |
| 115 |
ProcExtTest.mrb_proc_new_cfunc_with_env(:mruby) |
| 116 |
|
| 117 |
t = ProcExtTest.new |
| 118 |
|
| 119 |
assert_equal :test, t.test |
| 120 |
assert_equal :mruby, t.mruby |
| 121 |
end |
| 122 |
|
| 123 |
assert('mrb_cfunc_env_get') do |
| 124 |
ProcExtTest.mrb_cfunc_env_get :get_int, [0, 1, 2] |
| 125 |
|
| 126 |
t = ProcExtTest.new |
| 127 |
|
| 128 |
assert_raise(TypeError) { t.cfunc_without_env } |
| 129 |
|
| 130 |
assert_raise(IndexError) { t.get_int(-1) } |
| 131 |
assert_raise(IndexError) { t.get_int(3) } |
| 132 |
|
| 133 |
assert_equal 1, t.get_int(1) |
| 134 |
end |