-
Notifications
You must be signed in to change notification settings - Fork 0
/
expr_test.go
112 lines (102 loc) · 2.26 KB
/
expr_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
package clade_test
import (
"testing"
"github.com/lesomnus/boolal"
"github.com/lesomnus/clade"
"github.com/lesomnus/pl"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestPipelineUnmarshalYAML(t *testing.T) {
tcs := []struct {
desc string
data string
expected *pl.Fn
}{
{
desc: "given string is passed if it is not in parentheses",
data: `foo "bar"`,
expected: must(pl.NewFn("pass", `foo "bar"`)),
},
{
desc: "pipeline expression",
data: `(foo "bar")`,
expected: must(pl.NewFn("foo", "bar")),
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
require := require.New(t)
var actual clade.Pipeline
err := yaml.Unmarshal([]byte(tc.data), &actual)
require.NoError(err)
require.Len(actual.Funcs, 1)
require.Equal(tc.expected, actual.Funcs[0])
})
}
t.Run("fails if", func(t *testing.T) {
tcs := []struct {
desc string
data string
msgs []string
}{
{
desc: "not a string",
data: "{}",
msgs: []string{"map into string"},
},
{
desc: "invalid pipeline expression",
data: "(foo bar)",
msgs: []string{"bar"},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
require := require.New(t)
var actual clade.Pipeline
err := yaml.Unmarshal([]byte(tc.data), &actual)
for _, msg := range tc.msgs {
require.ErrorContains(err, msg)
}
})
}
})
}
func TestBoolAlgebraUnmarshalYAML(t *testing.T) {
t.Run("string is parsed", func(t *testing.T) {
require := require.New(t)
var actual clade.BoolAlgebra
err := yaml.Unmarshal([]byte("x & y"), &actual)
require.NoError(err)
require.Equal(boolal.And("x", "y"), actual.Expr())
})
t.Run("fails if", func(t *testing.T) {
tcs := []struct {
desc string
data string
msgs []string
}{
{
desc: "not a string",
data: "{}",
msgs: []string{"map into string"},
},
{
desc: "invalid expression",
data: "x % y",
msgs: []string{"%"},
},
}
for _, tc := range tcs {
t.Run(tc.desc, func(t *testing.T) {
require := require.New(t)
var actual clade.BoolAlgebra
err := yaml.Unmarshal([]byte(tc.data), &actual)
for _, msg := range tc.msgs {
require.ErrorContains(err, msg)
}
})
}
})
}