File: catch.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 (86 lines) | stat: -rw-r--r-- 1,526 bytes parent folder | download | duplicates (5)
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
assert "return throw value" do
  val = ["val"]
  result = catch :foo do
    loop do
      loop do
        begin
          throw :foo, val
        rescue Exception
          flunk("should not reach here 1")
        end
        break
      end
      flunk("should not reach here 2")
    end
    false
  end
  assert_same(val, result)
end

assert "no throw" do
  assert_equal(:foo, catch(:bar){:foo})
end

assert "no throw value" do
  result = catch :foo do
    throw :foo
    1
  end
  assert_equal(nil, result)
end

assert "pass the given tag to block" do
  tag = [:foo]
  catch(tag){|t| assert_same(tag, t)}
end

assert "tag identity, uncaught throw" do
  tag, val = [:tag], [:val]
  catch [:tag] do
    throw tag, val
  end
  flunk("should not reach here")
rescue Exception => e
  assert_match("uncaught throw *", e.message)
  assert_same(tag, e.tag)
  assert_same(val, e.value)
end

assert "without catch arguments" do
  result = catch do |tag1|
    catch do |tag2|
      throw tag1, 1
      flunk("should not reach here 1")
    end
    flunk("should not reach here 2")
  end
  assert_equal(1, result)
end

assert "catches across invocation boundaries" do
  v = []
  catch :one do
    v << 1
    catch :two do
      v << 2
      throw :one
      v << 3
    end
    v << 4
  end
  assert_equal([1,2], v)
end

assert "catches in the nested invocation with the same key" do
  v = []
  catch :tag do
    v << 1
    catch :tag do
      v << 2
      throw :tag
      v << 3
    end
    v << 4
  end
  assert_equal([1,2,4], v)
end