1 | ##
|
---|
2 | # Proc(Ext) Test
|
---|
3 |
|
---|
4 | assert('Proc#source_location') do
|
---|
5 | loc = Proc.new {}.source_location
|
---|
6 | next true if loc.nil?
|
---|
7 | assert_equal loc[0][-7, 7], 'proc.rb'
|
---|
8 | assert_equal loc[1], 5
|
---|
9 | end
|
---|
10 |
|
---|
11 | assert('Proc#inspect') do
|
---|
12 | ins = Proc.new{}.inspect
|
---|
13 | assert_kind_of String, ins
|
---|
14 | end
|
---|
15 |
|
---|
16 | assert('Proc#lambda?') do
|
---|
17 | assert_true lambda{}.lambda?
|
---|
18 | assert_true !Proc.new{}.lambda?
|
---|
19 | end
|
---|
20 |
|
---|
21 | assert('Proc#===') do
|
---|
22 | proc = Proc.new {|a| a * 2}
|
---|
23 | assert_equal 20, (proc === 10)
|
---|
24 | end
|
---|
25 |
|
---|
26 | assert('Proc#yield') do
|
---|
27 | proc = Proc.new {|a| a * 2}
|
---|
28 | assert_equal 20, proc.yield(10)
|
---|
29 | end
|
---|
30 |
|
---|
31 | assert('Proc#curry') do
|
---|
32 | b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
|
---|
33 | assert_equal 6, b.curry[1][2][3]
|
---|
34 | assert_equal 6, b.curry[1, 2][3, 4]
|
---|
35 | assert_equal 6, b.curry(5)[1][2][3][4][5]
|
---|
36 | assert_equal 6, b.curry(5)[1, 2][3, 4][5]
|
---|
37 | assert_equal 1, b.curry(1)[1]
|
---|
38 |
|
---|
39 | b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
|
---|
40 | assert_equal 6, b.curry[1][2][3]
|
---|
41 | assert_raise(ArgumentError) { b.curry[1, 2][3, 4] }
|
---|
42 | assert_raise(ArgumentError) { b.curry(5) }
|
---|
43 | assert_raise(ArgumentError) { b.curry(1) }
|
---|
44 |
|
---|
45 | assert_false(proc{}.curry.lambda?)
|
---|
46 | assert_true(lambda{}.curry.lambda?)
|
---|
47 | end
|
---|
48 |
|
---|
49 | assert('Proc#parameters') do
|
---|
50 | assert_equal([], Proc.new {}.parameters)
|
---|
51 | assert_equal([], Proc.new {||}.parameters)
|
---|
52 | assert_equal([[:opt, :a]], Proc.new {|a|}.parameters)
|
---|
53 | assert_equal([[:req, :a]], lambda {|a|}.parameters)
|
---|
54 | assert_equal([[:opt, :a]], lambda {|a=nil|}.parameters)
|
---|
55 | assert_equal([[:req, :a]], ->(a){}.parameters)
|
---|
56 | assert_equal([[:rest]], lambda { |*| }.parameters)
|
---|
57 | assert_equal([[:rest, :a]], Proc.new {|*a|}.parameters)
|
---|
58 | 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)
|
---|
59 | 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)
|
---|
60 | end
|
---|
61 |
|
---|
62 | assert('Proc#to_proc') do
|
---|
63 | proc = Proc.new {}
|
---|
64 | assert_equal proc, proc.to_proc
|
---|
65 | end
|
---|
66 |
|
---|
67 | assert('Kernel#proc') do
|
---|
68 | assert_true !proc{|a|}.lambda?
|
---|
69 | end
|
---|
70 |
|
---|
71 | assert('mrb_proc_new_cfunc_with_env') do
|
---|
72 | ProcExtTest.mrb_proc_new_cfunc_with_env(:test)
|
---|
73 | ProcExtTest.mrb_proc_new_cfunc_with_env(:mruby)
|
---|
74 |
|
---|
75 | t = ProcExtTest.new
|
---|
76 |
|
---|
77 | assert_equal :test, t.test
|
---|
78 | assert_equal :mruby, t.mruby
|
---|
79 | end
|
---|
80 |
|
---|
81 | assert('mrb_cfunc_env_get') do
|
---|
82 | ProcExtTest.mrb_cfunc_env_get :get_int, [0, 1, 2]
|
---|
83 |
|
---|
84 | t = ProcExtTest.new
|
---|
85 |
|
---|
86 | assert_raise(TypeError) { t.cfunc_without_env }
|
---|
87 |
|
---|
88 | assert_raise(IndexError) { t.get_int(-1) }
|
---|
89 | assert_raise(IndexError) { t.get_int(3) }
|
---|
90 |
|
---|
91 | assert_equal 1, t.get_int(1)
|
---|
92 | end
|
---|