-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrule_test.go
86 lines (62 loc) · 1.88 KB
/
rule_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
package golidate_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/studiolambda/golidate"
)
var (
FailRule = golidate.Rule(func(value any) golidate.Result {
return golidate.Fail(value, "default.fail")
})
PassRule = golidate.Rule(func(value any) golidate.Result {
return golidate.Pass(value, "default.pass")
})
)
func TestRuleWith(t *testing.T) {
ruleWith := PassRule.
With("key", "value").
With("key2", "value")
result := ruleWith(nil)
require.Equal(t, golidate.Metadata{"key": "value", "key2": "value"}, result.Metadata)
}
func TestRuleWithMetadata(t *testing.T) {
ruleWithMetadata := PassRule.WithMetadata(golidate.Metadata{"key": "value"})
result := ruleWithMetadata(nil)
require.Equal(t, golidate.Metadata{"key": "value"}, result.Metadata)
}
func TestRuleWithMetadataMerged(t *testing.T) {
ruleWithMetadata := PassRule.
WithMetadataMerged(golidate.Metadata{"key": "value"}).
WithMetadataMerged(golidate.Metadata{"key2": "value"})
result := ruleWithMetadata(nil)
require.Equal(t, golidate.Metadata{"key": "value", "key2": "value"}, result.Metadata)
}
func TestRuleWithCode(t *testing.T) {
t.Run("Pass", func(t *testing.T) {
result := PassRule.Code("custom")(nil)
require.True(t, result.Passes())
require.Equal(t, "custom", result.Code)
})
t.Run("Failure", func(t *testing.T) {
result := FailRule.Code("custom")(nil)
require.False(t, result.Passes())
require.Equal(t, "custom", result.Code)
})
}
func TestRuleCondition(t *testing.T) {
rule := FailRule.Conditions(func(value any) bool {
return value == "foo"
})
result := rule(nil)
require.True(t, result.Passes())
result = rule("foo")
require.False(t, result.Passes())
}
func TestRuleWhen(t *testing.T) {
rule := FailRule.When(false)
result := rule(nil)
require.True(t, result.Passes())
rule = FailRule.When(true)
result = rule(nil)
require.False(t, result.Passes())
}