/
test_restarts.rb
71 lines (57 loc) · 1.35 KB
/
test_restarts.rb
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
# -*- ruby -*-
require 'restarts'
class RestartsTest < Test::Unit::TestCase
class ExampleError < RuntimeError
def initialize(a_message)
@message = a_message
end
end
class Example
attr_reader :restart1_used, :restart2_used
def initialize()
@restart1_used = false
@restart2_used = false
end
def i_will_raise_a_condition
raise_with_restarts(ExampleError.new("I'm really just a condition")) do |restart_at|
case restart_at
when :restart1 then @restart1_used = true
when :restart2 then @restart2_used = true
end
end
end
end
def setup
@ex = Example.new
end
def test_calls_raise
assert_raises(ExampleError) { @ex.i_will_raise_a_condition }
end
def test_restart1
begin
@ex.i_will_raise_a_condition
rescue ExampleError
$!.restart.call(:restart1)
end
assert @ex.restart1_used
assert !@ex.restart2_used
end
def test_restart2
begin
@ex.i_will_raise_a_condition
rescue ExampleError
$!.restart.call(:restart2)
end
assert !@ex.restart1_used
assert @ex.restart2_used
end
def test_alternative_syntax
begin
@ex.i_will_raise_a_condition
rescue ExampleError
$!.restart(:restart1)
end
assert @ex.restart1_used
end
end
# vim: syntax=Ruby