forked from heetch/regula
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ruleset_test.go
99 lines (83 loc) · 3.02 KB
/
ruleset_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
package regula
import (
"encoding/json"
"testing"
"github.com/heetch/regula/rule"
"github.com/stretchr/testify/require"
)
func TestRulesetEval(t *testing.T) {
t.Run("Match string", func(t *testing.T) {
r, err := NewStringRuleset(
rule.New(rule.Eq(rule.StringValue("foo"), rule.StringValue("bar")), rule.StringValue("first")),
rule.New(rule.Eq(rule.StringValue("foo"), rule.StringValue("foo")), rule.StringValue("second")),
)
require.NoError(t, err)
res, err := r.Eval(nil)
require.NoError(t, err)
require.Equal(t, "second", res.Data)
})
t.Run("Match bool", func(t *testing.T) {
r, err := NewBoolRuleset(
rule.New(rule.Eq(rule.StringValue("foo"), rule.StringValue("bar")), rule.BoolValue(false)),
rule.New(rule.Eq(rule.StringValue("foo"), rule.StringValue("foo")), rule.BoolValue(true)),
)
require.NoError(t, err)
res, err := r.Eval(nil)
require.NoError(t, err)
require.Equal(t, "true", res.Data)
})
t.Run("Type mismatch", func(t *testing.T) {
_, err := NewStringRuleset(
rule.New(rule.Eq(rule.StringValue("foo"), rule.StringValue("bar")), rule.StringValue("first")),
rule.New(rule.Eq(rule.StringValue("foo"), rule.StringValue("foo")), rule.BoolValue(true)),
)
require.Equal(t, ErrRulesetIncoherentType, err)
})
t.Run("No match", func(t *testing.T) {
r, err := NewStringRuleset(
rule.New(rule.Eq(rule.StringValue("foo"), rule.StringValue("bar")), rule.StringValue("first")),
rule.New(rule.Eq(rule.StringValue("bar"), rule.StringValue("foo")), rule.StringValue("second")),
)
require.NoError(t, err)
_, err = r.Eval(nil)
require.Equal(t, rule.ErrNoMatch, err)
})
t.Run("Default", func(t *testing.T) {
r, err := NewStringRuleset(
rule.New(rule.Eq(rule.StringValue("foo"), rule.StringValue("bar")), rule.StringValue("first")),
rule.New(rule.Eq(rule.StringValue("bar"), rule.StringValue("foo")), rule.StringValue("second")),
rule.New(rule.True(), rule.StringValue("default")),
)
require.NoError(t, err)
res, err := r.Eval(nil)
require.NoError(t, err)
require.Equal(t, "default", res.Data)
})
}
func TestRulesetEncDec(t *testing.T) {
r1, err := NewStringRuleset(
rule.New(rule.Eq(rule.StringValue("foo"), rule.StringValue("bar")), rule.StringValue("first")),
rule.New(rule.Eq(rule.StringValue("bar"), rule.StringParam("foo")), rule.StringValue("second")),
rule.New(rule.True(), rule.StringValue("default")),
)
require.NoError(t, err)
raw, err := json.Marshal(r1)
require.NoError(t, err)
var r2 Ruleset
err = json.Unmarshal(raw, &r2)
require.NoError(t, err)
require.Equal(t, r1, &r2)
}
func TestRulesetParams(t *testing.T) {
r1, err := NewStringRuleset(
rule.New(rule.Eq(rule.StringParam("foo"), rule.Int64Param("bar")), rule.StringValue("first")),
rule.New(rule.Eq(rule.StringParam("foo"), rule.Float64Param("baz")), rule.StringValue("second")),
rule.New(rule.True(), rule.StringValue("default")),
)
require.NoError(t, err)
require.Equal(t, []rule.Param{
*rule.StringParam("foo"),
*rule.Int64Param("bar"),
*rule.Float64Param("baz"),
}, r1.Params())
}