| 1 |
assert "return throw value" do |
| 2 |
val = ["val"] |
| 3 |
result = catch :foo do |
| 4 |
loop do |
| 5 |
loop do |
| 6 |
begin |
| 7 |
throw :foo, val |
| 8 |
rescue Exception |
| 9 |
flunk("should not reach here 1") |
| 10 |
end |
| 11 |
break |
| 12 |
end |
| 13 |
flunk("should not reach here 2") |
| 14 |
end |
| 15 |
false |
| 16 |
end |
| 17 |
assert_same(val, result) |
| 18 |
end |
| 19 |
|
| 20 |
assert "no throw" do |
| 21 |
assert_equal(:foo, catch(:bar){:foo}) |
| 22 |
end |
| 23 |
|
| 24 |
assert "no throw value" do |
| 25 |
result = catch :foo do |
| 26 |
throw :foo |
| 27 |
1 |
| 28 |
end |
| 29 |
assert_equal(nil, result) |
| 30 |
end |
| 31 |
|
| 32 |
assert "pass the given tag to block" do |
| 33 |
tag = [:foo] |
| 34 |
catch(tag){|t| assert_same(tag, t)} |
| 35 |
end |
| 36 |
|
| 37 |
assert "tag identity, uncaught throw" do |
| 38 |
tag, val = [:tag], [:val] |
| 39 |
catch [:tag] do |
| 40 |
throw tag, val |
| 41 |
end |
| 42 |
flunk("should not reach here") |
| 43 |
rescue Exception => e |
| 44 |
assert_match("uncaught throw *", e.message) |
| 45 |
assert_same(tag, e.tag) |
| 46 |
assert_same(val, e.value) |
| 47 |
end |
| 48 |
|
| 49 |
assert "without catch arguments" do |
| 50 |
result = catch do |tag1| |
| 51 |
catch do |tag2| |
| 52 |
throw tag1, 1 |
| 53 |
flunk("should not reach here 1") |
| 54 |
end |
| 55 |
flunk("should not reach here 2") |
| 56 |
end |
| 57 |
assert_equal(1, result) |
| 58 |
end |
| 59 |
|
| 60 |
assert "catches across invocation boundaries" do |
| 61 |
v = [] |
| 62 |
catch :one do |
| 63 |
v << 1 |
| 64 |
catch :two do |
| 65 |
v << 2 |
| 66 |
throw :one |
| 67 |
v << 3 |
| 68 |
end |
| 69 |
v << 4 |
| 70 |
end |
| 71 |
assert_equal([1,2], v) |
| 72 |
end |
| 73 |
|
| 74 |
assert "catches in the nested invocation with the same key" do |
| 75 |
v = [] |
| 76 |
catch :tag do |
| 77 |
v << 1 |
| 78 |
catch :tag do |
| 79 |
v << 2 |
| 80 |
throw :tag |
| 81 |
v << 3 |
| 82 |
end |
| 83 |
v << 4 |
| 84 |
end |
| 85 |
assert_equal([1,2,4], v) |
| 86 |
end |