-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredicate_test.go
106 lines (100 loc) · 2.97 KB
/
predicate_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
package pipeline
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
)
func Test_Predicates(t *testing.T) {
counter := 0
tests := map[string]struct {
givenPredicate Predicate[context.Context]
expectedCounts int
expectedResult bool
}{
"GivenBoolPredicate_WhenRunning_ThenExpectTrue": {
givenPredicate: Bool[context.Context](true),
expectedCounts: 0,
expectedResult: true,
},
"GivenAnd_WhenBothFalse_ThenExpectFalseAndIgnoreSecondPredicate": {
givenPredicate: And[context.Context](falsePredicate(&counter), falsePredicate(&counter)),
expectedCounts: -1,
expectedResult: false,
},
"GivenAndPredicate_WhenSecondFalse_ThenExpectFalse": {
givenPredicate: And[context.Context](truePredicate(&counter), falsePredicate(&counter)),
expectedCounts: 0,
expectedResult: false,
},
"GivenAndPredicate_WhenBothTrue_ThenExpectTrue": {
givenPredicate: And[context.Context](truePredicate(&counter), truePredicate(&counter)),
expectedCounts: 2,
expectedResult: true,
},
"GivenNotPredicate_WhenFalse_ThenExpectTrue": {
givenPredicate: Not[context.Context](falsePredicate(&counter)),
expectedCounts: -1,
expectedResult: true,
},
"GivenNotPredicate_WhenTrue_ThenExpectFalse": {
givenPredicate: Not[context.Context](truePredicate(&counter)),
expectedCounts: 1,
expectedResult: false,
},
"GivenOrPredicate_WhenBothFalse_ThenExpectFalse": {
givenPredicate: Or[context.Context](falsePredicate(&counter), falsePredicate(&counter)),
expectedCounts: -2,
expectedResult: false,
},
"GivenOrPredicate_WhenFirstTrue_ThenExpectTrue": {
givenPredicate: Or[context.Context](truePredicate(&counter), falsePredicate(&counter)),
expectedCounts: 1,
expectedResult: true,
},
"GivenOrPredicate_WhenSecondTrue_ThenExpectTrue": {
givenPredicate: Or[context.Context](falsePredicate(&counter), truePredicate(&counter)),
expectedCounts: 0,
expectedResult: true,
},
"GivenOrPredicate_WhenBothTrue_ThenExpectTrueAfterFirstPredicate": {
givenPredicate: Or[context.Context](truePredicate(&counter), truePredicate(&counter)),
expectedCounts: 1,
expectedResult: true,
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
counter = 0
step := Step[context.Context]{Condition: tt.givenPredicate}
result := step.Condition(nil)
assert.Equal(t, tt.expectedCounts, counter)
assert.Equal(t, tt.expectedResult, result)
})
}
}
func TestBoolPtr(t *testing.T) {
called := false
b := false
p := NewPipeline[context.Context]()
p.WithSteps(
p.When(BoolPtr[context.Context](&b), "boolptr", func(_ context.Context) error {
called = true
return nil
}),
)
b = true
_ = p.RunWithContext(context.Background())
assert.True(t, called)
}
func truePredicate(counter *int) Predicate[context.Context] {
return func(_ context.Context) bool {
*counter++
return true
}
}
func falsePredicate(counter *int) Predicate[context.Context] {
return func(_ context.Context) bool {
*counter--
return false
}
}