summaryrefslogtreecommitdiffhomepage
path: root/mrbgems/mruby-proc-ext/test/proc.rb
blob: f5aacd490deccb0e983f7de034bca8d02b273cbc (plain)
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
##
# Proc(Ext) Test

assert('Proc#source_location') do
  loc = Proc.new {}.source_location
  next true if loc.nil?
  assert_equal loc[0][-7, 7], 'proc.rb'
  assert_equal loc[1], 5
end

assert('Proc#lambda?') do
  assert_true lambda{}.lambda?
  assert_true !Proc.new{}.lambda?
end

assert('Proc#===') do
  proc = Proc.new {|a| a * 2}
  assert_equal 20, (proc === 10)
end

assert('Proc#yield') do
  proc = Proc.new {|a| a * 2}
  assert_equal 20, proc.yield(10)
end

assert('Proc#curry') do
  b = proc {|x, y, z| (x||0) + (y||0) + (z||0) }
  assert_equal 6, b.curry[1][2][3]
  assert_equal 6, b.curry[1, 2][3, 4]
  assert_equal 6, b.curry(5)[1][2][3][4][5]
  assert_equal 6, b.curry(5)[1, 2][3, 4][5]
  assert_equal 1, b.curry(1)[1]

  b = lambda {|x, y, z| (x||0) + (y||0) + (z||0) }
  assert_equal 6, b.curry[1][2][3]
  assert_raise(ArgumentError) { b.curry[1, 2][3, 4] }
  assert_raise(ArgumentError) { b.curry(5) }
  assert_raise(ArgumentError) { b.curry(1) }
end

assert('Proc#to_proc') do
  proc = Proc.new {}
  assert_equal proc, proc.to_proc
end

assert('Kernel#proc') do
  assert_true !proc{|a|}.lambda?
end

assert('mrb_proc_new_cfunc_with_env') do
  ProcExtTest.mrb_proc_new_cfunc_with_env(:test)
  ProcExtTest.mrb_proc_new_cfunc_with_env(:mruby)

  t = ProcExtTest.new

  assert_equal :test, t.test
  assert_equal :mruby, t.mruby
end

assert('mrb_cfunc_env_get') do
  ProcExtTest.mrb_cfunc_env_get :get_int, [0, 1, 2]

  t = ProcExtTest.new

  assert_raise(TypeError) { t.cfunc_without_env }

  assert_raise(IndexError) { t.get_int(-1) }
  assert_raise(IndexError) { t.get_int(3) }

  assert_equal 1, t.get_int(1)
end