-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_try_catch.dry
74 lines (61 loc) · 2.05 KB
/
test_try_catch.dry
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
class TestTryCatch {
def test_single_catch() {
let x = 10;
try {
19 / 0;
x = 20; // shouldn't be executed
} catch (: DivisionByZeroError) {}
assert_equals("Handle an exception with a single empty catch-block", 10, x);
try {
19 / 0;
x = 20;
} catch (: DivisionByZeroError) {
x = 30;
}
assert_equals("Handle an exception with a single non-empty catch-block", 30, x);
}
def test_multiple_catches() {
let x = 10;
let y = 10;
let xs = [1, 2, 3];
try {
xs[3];
} catch (: DivisionByZeroError) {
y = 20; // skipped
} catch (: IndexOutOfBoundsError) {
x = 50;
}
assert_equals("Skip catch-blocks that don't capture the exception", (50, 10), (x, y));
try {
raise(UndefinedVariableError("x"));
} catch (: UndefinedVariableError) {
x = 20;
} catch (: IncorrectArityError) {
x = 70; // ignored
}
assert_equals("Stop at the first catch-block that captures the exception", 20, x);
}
def test_no_match() {
assert_error_type("Throw the error if no catch-blocks capture it", UndefinedVariableError,
lambda() {
try {
raise(UndefinedVariableError("y"));
} catch (: IncorrectArityError) {} catch (: DivisionByZeroError) {}
});
}
def test_captured_error_scope() {
try {
10 / 0;
} catch (division_by_zero_error: DivisionByZeroError) {}
assert_error("Captured errors should not be visible outside of the catch block",
UndefinedVariableError("Undefined variable: division_by_zero_error"),
lambda() {
println(division_by_zero_error);
});
}
}
let try_catch = TestTryCatch();
try_catch.test_single_catch();
try_catch.test_multiple_catches();
try_catch.test_no_match();
try_catch.test_captured_error_scope();