File: proc.rb

package info (click to toggle)
mruby 3.4.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,584 kB
  • sloc: ansic: 51,933; ruby: 29,510; yacc: 7,077; cpp: 517; makefile: 51; sh: 42
file content (130 lines) | stat: -rw-r--r-- 3,507 bytes parent folder | download
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
##
# Proc(Ext) Test

def enable_debug_info?
  return @enable_debug_info unless @enable_debug_info == nil
  begin
    raise
  rescue => e
    @enable_debug_info = !e.backtrace.empty?
    if @enable_debug_info && e.backtrace[0].include?("(unknown)")
      @enable_debug_info = false
    end
    return @enable_debug_info
  end
end

assert('Proc#source_location') do
  skip unless enable_debug_info?
  file, line = Proc.new{}.source_location
  assert_equal __FILE__, file
  assert_equal __LINE__ - 2, line
end

assert('Proc#inspect') do
  ins = Proc.new{}.inspect
  if enable_debug_info?
    metas = %w(\\ * ? [ ] { })
    file = __FILE__.split("").map{|c| metas.include?(c) ? "\\#{c}" : c}.join
    line = __LINE__ - 4
  else
    file = line = "-"
  end
  assert_match "#<Proc:0x* #{file}:#{line}>", ins
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) }

  assert_false(proc{}.curry.lambda?)
  assert_true(lambda{}.curry.lambda?)
end

assert('Proc#parameters') do
  assert_equal([], Proc.new {}.parameters)
  assert_equal([], Proc.new {||}.parameters)
  assert_equal([[:opt, :a]], Proc.new {|a|}.parameters)
  assert_equal([[:req, :a]], lambda {|a|}.parameters)
  assert_equal([[:opt, :a]], lambda {|a=nil|}.parameters)
  assert_equal([[:req, :a]], ->(a){}.parameters)
  assert_equal([[:rest, :*]], lambda { |*| }.parameters)
  assert_equal([[:rest, :a]], Proc.new {|*a|}.parameters)
  assert_equal([[:opt, :x], [:opt, :y], [:rest, :other]], Proc.new{|x,y=42,*other|}.parameters)
  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)
  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)
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?

  assert_raise LocalJumpError do
    proc{ break }.call
  end
end

assert "Proc#<< and Proc#>>" do
  add3 = ->(n) { n + 3 }
  mul2 = ->(n) { n * 2 }

  f1 = mul2 << add3
  assert_kind_of Proc, f1
  assert_equal 16, f1.call(5)

  f2 = mul2 >> add3
  assert_kind_of Proc, f2
  assert_equal 13, f2.call(5)
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