-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherrs_test.go
90 lines (81 loc) · 1.49 KB
/
errs_test.go
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package errs
import (
"errors"
"fmt"
"reflect"
"testing"
)
var errFunc = func() error { return errors.New("error") }
var okFunc = func() error { return nil }
func TestNonNil(t *testing.T) {
var e Group
e.Add(errFunc)
e.Add(okFunc)
e.Add(okFunc)
if e.Exec() == nil {
t.Error("Expected error, found nil")
}
e = Group{}
e.Add(okFunc)
e.Add(errFunc)
e.Add(okFunc)
if e.Exec() == nil {
t.Error("Expected error, found nil")
}
}
func TestNil(t *testing.T) {
var e Group
e.Add(okFunc)
e.Add(okFunc)
e.Add(okFunc)
if e.Exec() != nil {
t.Error("Expected nil, found error")
}
e = Group{}
if e.Exec() != nil {
t.Error("Expected nil, found error")
}
}
func TestDefer(t *testing.T) {
var e Group
var l []int
e.Defer(func() {
l = append(l, 3)
})
e.Add(func() error {
l = append(l, 1)
return nil
})
e.Defer(func() {
l = append(l, 2)
})
if err := e.Exec(); err != nil {
t.Errorf("Expected nil, found error: %v", err)
}
expected := []int{1, 2, 3}
if fmt.Sprint(l) != fmt.Sprint(expected) {
t.Errorf("Expected %v, found %v", expected, l)
}
}
func TestFinal(t *testing.T) {
var e Group
e.Add(errFunc)
var a, b int
e.Final(func() { a = 100 })
e.Final(func() { b = 101 })
if e.Exec() == nil {
t.Error("expected error, found nil")
}
if a != 100 {
t.Errorf("Expected 100, found %v", a)
}
if b != 101 {
t.Errorf("Expected 101, found %v", b)
}
}
func assert(t *testing.T, a, b interface{}) {
if !reflect.DeepEqual(a, b) {
t.Errorf("%v != %v", a, b)
t.Fail()
}
}