OLD | NEW |
| (Empty) |
1 // Copyright 2009 The Go Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style | |
3 // license that can be found in the LICENSE file. | |
4 | |
5 package exception | |
6 | |
7 import "testing" | |
8 | |
9 func TestNoException(t *testing.T) { | |
10 e := Try(func(throw Handler) {}) | |
11 if e != nil { | |
12 t.Fatalf("no exception expected, found: %v", e) | |
13 } | |
14 } | |
15 | |
16 | |
17 func TestNilException(t *testing.T) { | |
18 e := Try(func(throw Handler) { throw(nil) }) | |
19 if e == nil { | |
20 t.Fatalf("exception expected", e) | |
21 } | |
22 if e.Value != nil { | |
23 t.Fatalf("nil exception expected, found: %v", e) | |
24 } | |
25 } | |
26 | |
27 | |
28 func TestTry(t *testing.T) { | |
29 s := 0 | |
30 for i := 1; i <= 10; i++ { | |
31 e := Try(func(throw Handler) { | |
32 if i%3 == 0 { | |
33 throw(i) | |
34 panic("throw returned") | |
35 } | |
36 }) | |
37 if e != nil { | |
38 s += e.Value.(int) | |
39 } | |
40 } | |
41 result := 3 + 6 + 9 | |
42 if s != result { | |
43 t.Fatalf("expected: %d, found: %d", result, s) | |
44 } | |
45 } | |
46 | |
47 | |
48 func TestCatch(t *testing.T) { | |
49 s := 0 | |
50 for i := 1; i <= 10; i++ { | |
51 Try(func(throw Handler) { | |
52 if i%3 == 0 { | |
53 throw(i) | |
54 } | |
55 }).Catch(func(x interface{}) { s += x.(int) }) | |
56 } | |
57 result := 3 + 6 + 9 | |
58 if s != result { | |
59 t.Fatalf("expected: %d, found: %d", result, s) | |
60 } | |
61 } | |
OLD | NEW |