summaryrefslogtreecommitdiff
path: root/src/pkg/exp/exception/exception_test.go
blob: 91f742ea03b9b1d26e5efc00c15d566d8b5eb1c4 (plain)
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
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package exception

import "testing"

func TestNoException(t *testing.T) {
	e := Try(func(throw Handler) {});
	if e != nil {
		t.Fatalf("no exception expected, found: %v", e)
	}
}


func TestNilException(t *testing.T) {
	e := Try(func(throw Handler) { throw(nil) });
	if e == nil {
		t.Fatalf("exception expected", e)
	}
	if e.Value != nil {
		t.Fatalf("nil exception expected, found: %v", e)
	}
}


func TestTry(t *testing.T) {
	s := 0;
	for i := 1; i <= 10; i++ {
		e := Try(func(throw Handler) {
			if i%3 == 0 {
				throw(i);
				panic("throw returned");
			}
		});
		if e != nil {
			s += e.Value.(int)
		}
	}
	result := 3 + 6 + 9;
	if s != result {
		t.Fatalf("expected: %d, found: %d", result, s)
	}
}


func TestCatch(t *testing.T) {
	s := 0;
	for i := 1; i <= 10; i++ {
		Try(func(throw Handler) {
			if i%3 == 0 {
				throw(i)
			}
		}).Catch(func(x interface{}) { s += x.(int) })
	}
	result := 3 + 6 + 9;
	if s != result {
		t.Fatalf("expected: %d, found: %d", result, s)
	}
}