-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatement_test.go
74 lines (56 loc) · 1.96 KB
/
statement_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
package pipeline_test
import (
"context"
"fmt"
"testing"
"github.com/stretchr/testify/assert"
"github.com/saantiaguilera/go-pipeline"
)
// This examples shows a simple statement that lets
// us evaluate it with a given input to yield
// a boolean result
//
// This example uses dummy data to showcase as simple as possible this scenario.
func ExampleStatement() {
stmt := pipeline.NewStatement(
"is_number_odd",
func(ctx context.Context, i int) bool {
return i%2 != 0
},
)
out := stmt.Evaluate(context.Background(), 25)
fmt.Println(out)
// output: true
}
func TestStatement_GivenAnAnonymousStatement_WhenNamed_ThenReturnsEmpty(t *testing.T) {
statement := pipeline.NewAnonymousStatement(func(ctx context.Context, in int) bool {
return true
})
assert.Empty(t, statement.Name())
}
func TestStatement_GivenAnAnonymousStatement_WhenEvaluated_ThenEvaluatesPassed(t *testing.T) {
statement := pipeline.NewAnonymousStatement(func(ctx context.Context, in int) bool {
return true
})
assert.True(t, statement.Evaluate(context.Background(), 1))
}
func TestStatement_GivenAStatement_WhenNamed_ThenReturnsName(t *testing.T) {
statement := pipeline.NewStatement("some name", func(ctx context.Context, in int) bool {
return true
})
assert.Equal(t, "some name", statement.Name())
}
func TestStatement_GivenAStatement_WhenEvaluated_ThenEvaluatesPassed(t *testing.T) {
statement := pipeline.NewStatement("some name", func(ctx context.Context, in int) bool {
return true
})
assert.True(t, statement.Evaluate(context.Background(), 1))
}
func TestStatement_GivenAnAnonymousStatementWithNoFunc_WhenEvaluated_ThenReturnsFalse(t *testing.T) {
statement := pipeline.NewAnonymousStatement[int](nil)
assert.False(t, statement.Evaluate(context.Background(), 1))
}
func TestStatement_GivenAStatementWithNoFunc_WhenEvaluated_ThenReturnsFalse(t *testing.T) {
statement := pipeline.NewStatement[int]("some name", nil)
assert.False(t, statement.Evaluate(context.Background(), 1))
}