source: EcnlProtoTool/trunk/mruby-2.1.1/mrbgems/mruby-proc-ext/test/proc.rb@ 439

Last change on this file since 439 was 439, checked in by coas-nagasima, 4 years ago

mrubyを2.1.1に更新

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-ruby;charset=UTF-8
File size: 3.3 KB
Line 
1##
2# Proc(Ext) Test
3
4def 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
11end
12
13assert('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
18end
19
20assert('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
30end
31
32assert('Proc#parameters') do
33 parameters = Proc.new{|x,y=42,*other|}.parameters
34 assert_equal [[:opt, :x], [:opt, :y], [:rest, :other]], parameters
35end
36
37assert('Proc#lambda?') do
38 assert_true lambda{}.lambda?
39 assert_true !Proc.new{}.lambda?
40end
41
42assert('Proc#===') do
43 proc = Proc.new {|a| a * 2}
44 assert_equal 20, (proc === 10)
45end
46
47assert('Proc#yield') do
48 proc = Proc.new {|a| a * 2}
49 assert_equal 20, proc.yield(10)
50end
51
52assert('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?)
68end
69
70assert('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)
81end
82
83assert('Proc#to_proc') do
84 proc = Proc.new {}
85 assert_equal proc, proc.to_proc
86end
87
88assert('Kernel#proc') do
89 assert_true !proc{|a|}.lambda?
90
91 assert_raise LocalJumpError do
92 proc{ break }.call
93 end
94end
95
96assert "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)
107end
108
109assert('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
117end
118
119assert('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)
130end
Note: See TracBrowser for help on using the repository browser.