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