-
Notifications
You must be signed in to change notification settings - Fork 0
/
thennable_test.go
127 lines (110 loc) · 3.37 KB
/
thennable_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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package thennable_test
import (
"fmt"
"strconv"
"testing"
thennable "github.com/bastianrob/go-thennable"
)
func AddOne(val int) (int, error) {
return val + 1, nil
}
func Throwable(val interface{}) error {
return fmt.Errorf("Exception occurred with supplied value: %+v", val)
}
func LogError(err error) {
fmt.Printf("Log Error: %+v\n", err)
}
func Test_IThennable(t *testing.T) {
fmt.Println("\nStarting Test_IThennable.go")
addOne := AddOne
throw := Throwable
//Test 1
fmt.Println("Start Test 1: Expect result = 5 and err is nil")
result, err := thennable.
Start(1). //starts with 1
Then(addOne). //add 1 with 1 = 2
Then(addOne). //result = 3
Then(addOne). //result = 4
Then(addOne). //result = 5
End()
if result[0].(int) != 5 {
t.Errorf("Test 1 result should be = 5, actual value: %d", result[0])
}
if err != nil {
t.Errorf("Test 1 error should be = nil, actual value: %+v", err)
}
//Test 2
fmt.Println("\nStart Test 2: Expect result is nil and err is not nil")
result, err = thennable.
Start(1). //starts with 1
Then(addOne). //add 1 with 1 = 2
Then(addOne). //add 2 with 1 = 3
Then(throw). //err = exception
Handle(LogError).
Then(addOne). //skipped
Then(addOne). //skipped
End()
if len(result) != 0 {
t.Errorf("Test 2 result should be = empty, actual value: %d", len(result))
}
if err == nil {
t.Errorf("Test 2 error should not be = nil, actual value: %+v", err)
}
//Test 3
fmt.Println("\nStart Test 3: Expect result = 2 and err is nil")
result, err = thennable.
Start(1). //starts with 1
BreakOnError(false). //keep propagating the function chain
Then(addOne). //add 1 with 1 = 2
Then(addOne). //result = 3
Then(throw). //result = 0, err = exception ignored
Supply(0). //resupply with 0
Then(addOne). //add 0 with 1 = 1, err = nil
Then(addOne). //result = 2, err = nil
End()
if result[0].(int) != 2 {
t.Errorf("Test 3 result should be = 2, actual value: %d", result[0])
}
if err != nil {
t.Errorf("Test 3 error should be = nil, actual value: %+v", err)
}
//Test 4
fmt.Println("\nStart Test 4: Expect recover from error, result is 10 and err is nil")
result, err = thennable.
Start(1). //starts with 1
Then(addOne). //add 1 with 1 = 2
Then(throw). //result = nil, err = exception
Then(addOne). //skipped
Handle(LogError). //log the error
BreakOnError(false). //recover from error
Supply(8). //resuply the value with 8
Then(addOne). //result = 9
Then(addOne). //result = 10
Handle(LogError). //log error should be nil
End()
if result[0].(int) != 10 {
t.Errorf("Test 4 result should be = 10, actual value: %d", result[0])
}
if err != nil {
t.Errorf("Test 4 error should be nil, actual value: %+v", err)
}
//Test 5
fmt.Println("\nStart Test 5: Expect result is '6' and err is nil")
result, err = thennable.Start(1).
Then(func(one int) (int, int, error) {
return one, 2, nil
}).
Then(func(one, two int) (int, int, int, error) {
return one, two, 3, nil
}).
Then(func(one, two, three int) (string, error) {
return strconv.Itoa(one + two + three), nil
}).
End()
if result[0].(string) != "6" {
t.Errorf("Test 5 result should be = '6', actual value: %v\n", result)
}
if err != nil {
t.Errorf("Test 5 error should be nil, actual value: %+v\n", err)
}
}