-
Notifications
You must be signed in to change notification settings - Fork 0
/
gowith_test.go
89 lines (71 loc) · 1.78 KB
/
gowith_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
package gowith
import (
"errors"
"testing"
)
// Struct for full working test.
type Db struct{}
// Implement EnterExiter's Enter method.
func (db Db) Enter() (bool, error) {
return true, nil
}
// Implement EnterExiter's Exit method.
func (db Db) Exit(ent bool, err error) error {
return err
}
// Struct for error on enter.
type DbErrOnEnt struct{}
// Implement EnterExiter's Enter method.
func (db DbErrOnEnt) Enter() (*string, error) {
return nil, errors.New("error")
}
// Implement EnterExiter's Exit method.
func (db DbErrOnEnt) Exit(ent *string, err error) error {
return err
}
// Test full implementation
func TestFull(t *testing.T) {
var ran bool
var erv bool
err := New[bool](Db{}, func(ent bool) error {
ran = true
erv = ent
return nil
})
if err != nil {
t.Errorf("error happened during enter, action, or exit: %v", err)
}
if !ran {
t.Errorf("expected action to run but it did not")
}
if erv != true {
t.Errorf("expected enter value to be %d, but got %v", 3, erv)
}
}
// Test when an error happens on enter, which means action should not run.
// And Exit should recieve the error.
func TestErrorOnEnter(t *testing.T) {
var ran bool
var erv *string
err := New[*string](DbErrOnEnt{}, func(ent *string) error {
ran = true
erv = ent
return nil
})
if err.Error() != "error" {
t.Errorf("expected error to match but did not, got \"%v\" expected \"error\"", err)
}
if ran || erv != nil {
t.Errorf("action should not have ran, but it did")
}
}
// Test when an error happens on action.
// And Exit should recieve the error.
func TestErrorOnAction(t *testing.T) {
err := New[bool](Db{}, func(er bool) error {
return errors.New("error")
})
if err.Error() != "error" {
t.Errorf("expected error to match but did not, got \"%v\" expected \"error\"", err)
}
}