-
Notifications
You must be signed in to change notification settings - Fork 473
/
gomega_test.go
164 lines (154 loc) · 4.15 KB
/
gomega_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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package resource
import (
"encoding/json"
"testing"
"github.com/goss-org/goss/matchers"
"github.com/stretchr/testify/assert"
)
var gomegaTests = []struct {
in string
want any
useNegateTester bool
}{
// Default for simple types
{
in: `"foo"`,
want: matchers.WithSafeTransform(matchers.ToString{}, matchers.Equal("foo")),
},
{
in: `1`,
want: matchers.WithSafeTransform(matchers.ToNumeric{}, matchers.BeNumerically("eq", float64(1))),
},
{
in: `true`,
want: matchers.Equal(true),
},
// Default for Array
{
in: `["foo", "bar"]`,
want: matchers.ContainElements(
matchers.WithSafeTransform(matchers.ToString{}, matchers.Equal("foo")),
matchers.WithSafeTransform(matchers.ToString{}, matchers.Equal("bar"))),
useNegateTester: true,
},
// Numeric
// Golang json escapes '>', '<' symbols, so we use 'gt', 'le' instead
{
in: `{"gt": 1}`,
want: matchers.WithSafeTransform(matchers.ToNumeric{}, matchers.BeNumerically("gt", float64(1))),
},
{
in: `{"ge": 1}`,
want: matchers.WithSafeTransform(matchers.ToNumeric{}, matchers.BeNumerically("ge", float64(1))),
},
{
in: `{"lt": 1}`,
want: matchers.WithSafeTransform(matchers.ToNumeric{}, matchers.BeNumerically("lt", float64(1))),
},
{
in: `{"le": 1}`,
want: matchers.WithSafeTransform(matchers.ToNumeric{}, matchers.BeNumerically("le", float64(1))),
},
// String
{
in: `{"have-prefix": "foo"}`,
want: matchers.WithSafeTransform(matchers.ToString{}, matchers.HavePrefix("foo")),
},
{
in: `{"have-suffix": "foo"}`,
want: matchers.WithSafeTransform(matchers.ToString{}, matchers.HaveSuffix("foo")),
},
// Regex support is based on golangs regex engine https://golang.org/pkg/regexp/syntax/
{
in: `{"match-regexp": "foo"}`,
want: matchers.WithSafeTransform(matchers.ToString{}, matchers.MatchRegexp("foo")),
},
// Collection
{
in: `{"consist-of": ["foo"]}`,
want: matchers.ConsistOf(matchers.WithSafeTransform(matchers.ToString{}, matchers.Equal("foo"))),
},
{
in: `{"contain-element": "foo"}`,
want: matchers.WithSafeTransform(matchers.ToArray{},
matchers.ContainElement(
matchers.WithSafeTransform(matchers.ToString{},
matchers.Equal("foo")))),
},
{
in: `{"have-len": 3}`,
want: matchers.HaveLen(3),
},
{
in: `{"have-key": "foo"}`,
want: matchers.HaveKey(matchers.WithSafeTransform(matchers.ToString{}, matchers.Equal("foo"))),
},
// Negation
{
in: `{"not": "foo"}`,
want: matchers.Not(matchers.WithSafeTransform(matchers.ToString{}, matchers.Equal("foo"))),
},
// Complex logic
{
in: `{"and": ["foo", "foo"]}`,
want: matchers.And(
matchers.WithSafeTransform(matchers.ToString{},
matchers.Equal("foo")),
matchers.WithSafeTransform(matchers.ToString{},
matchers.Equal("foo")),
),
useNegateTester: true,
},
{
in: `{"and": [{"have-prefix": "foo"}, "foo"]}`,
want: matchers.And(
matchers.WithSafeTransform(matchers.ToString{},
matchers.HavePrefix("foo")),
matchers.WithSafeTransform(matchers.ToString{},
matchers.Equal("foo")),
),
useNegateTester: true,
},
{
in: `{"not": {"have-prefix": "foo"}}`,
want: matchers.Not(
matchers.WithSafeTransform(matchers.ToString{},
matchers.HavePrefix("foo"))),
},
{
in: `{"or": ["foo", "foo"]}`,
want: matchers.Or(
matchers.WithSafeTransform(matchers.ToString{},
matchers.Equal("foo")),
matchers.WithSafeTransform(matchers.ToString{},
matchers.Equal("foo"))),
},
{
in: `{"not": {"and": [{"have-prefix": "foo"}]}}`,
want: matchers.Not(
matchers.And(
matchers.WithSafeTransform(matchers.ToString{},
matchers.HavePrefix("foo")))),
},
// Semver Constraint
{
in: `{"semver-constraint": "> 1.0.0"}`,
want: matchers.BeSemverConstraint("> 1.0.0"),
},
}
func TestMatcherToGomegaMatcher(t *testing.T) {
for _, c := range gomegaTests {
var dat any
if err := json.Unmarshal([]byte(c.in), &dat); err != nil {
t.Fatal(err)
}
got, err := matcherToGomegaMatcher(dat)
if err != nil {
t.Fatal(err)
}
gomegaTestEqual(t, got, c.want, c.useNegateTester, c.in)
}
}
func gomegaTestEqual(t *testing.T, got, want any, useNegateTester bool, in string) {
assert.Equal(t, got, want)
}