Skip to content

Commit 959bab4

Browse files
KrisKennawayDDanderseknert
authored andcommitted
Add a test case for unmarshalling IsSetStmt, thanks to @johanfylling
Signed-off-by: Kris Kennaway <[email protected]>
1 parent 3356122 commit 959bab4

File tree

1 file changed

+69
-46
lines changed

1 file changed

+69
-46
lines changed

v1/ir/encoding/encoding_test.go

+69-46
Original file line numberDiff line numberDiff line change
@@ -11,62 +11,85 @@ import (
1111
)
1212

1313
func TestRoundTrip(t *testing.T) {
14+
tests := []struct {
15+
note string
16+
modules map[string]string
17+
}{
18+
{
19+
note: "simple",
20+
modules: map[string]string{
21+
"test.rego": `
22+
package test
23+
p if {
24+
input.foo == 7
25+
}
26+
`,
27+
},
28+
},
29+
{
30+
note: "every",
31+
modules: map[string]string{
32+
"test.rego": `
33+
package test
34+
p if {
35+
every i in input.foo { i > 0 }
36+
}
37+
`,
38+
},
39+
},
40+
}
1441

15-
// Note: v1 module
16-
c, err := ast.CompileModules(map[string]string{
17-
"test.rego": `
18-
package test
42+
for _, tc := range tests {
43+
t.Run(tc.note, func(t *testing.T) {
44+
// Note: v1 module
45+
c, err := ast.CompileModules(tc.modules)
1946

20-
p if {
21-
input.foo == 7
47+
if err != nil {
48+
t.Fatal(err)
2249
}
23-
`,
24-
})
25-
26-
if err != nil {
27-
t.Fatal(err)
28-
}
2950

30-
modules := []*ast.Module{}
51+
modules := []*ast.Module{}
3152

32-
for _, m := range c.Modules {
33-
modules = append(modules, m)
34-
}
53+
for _, m := range c.Modules {
54+
modules = append(modules, m)
55+
}
3556

36-
planner := planner.New().
37-
WithQueries([]planner.QuerySet{
38-
{
39-
Name: "main",
40-
Queries: []ast.Body{
41-
ast.MustParseBody("data.test.p = true"),
42-
},
43-
},
44-
}).
45-
WithModules(modules).
46-
WithBuiltinDecls(ast.BuiltinMap)
57+
planner := planner.New().
58+
WithQueries([]planner.QuerySet{
59+
{
60+
Name: "main",
61+
Queries: []ast.Body{
62+
ast.MustParseBody("data.test.p = true"),
63+
},
64+
},
65+
}).
66+
WithModules(modules).
67+
WithBuiltinDecls(ast.BuiltinMap)
4768

48-
plan, err := planner.Plan()
49-
if err != nil {
50-
t.Fatal(err)
51-
}
69+
plan, err := planner.Plan()
70+
if err != nil {
71+
t.Fatal(err)
72+
}
5273

53-
bs, err := json.MarshalIndent(plan, "", " ")
54-
if err != nil {
55-
t.Fatal(err)
56-
}
74+
bs, err := json.MarshalIndent(plan, "", " ")
75+
if err != nil {
76+
t.Fatal(err)
77+
}
5778

58-
var cpy ir.Policy
59-
err = json.Unmarshal(bs, &cpy)
60-
if err != nil {
61-
t.Fatal(err)
62-
}
79+
var cpy ir.Policy
80+
err = json.Unmarshal(bs, &cpy)
81+
if err != nil {
82+
t.Fatal(err)
83+
}
6384

64-
bs2, err := json.MarshalIndent(plan, "", " ")
65-
if err != nil {
66-
t.Fatal(err)
67-
}
85+
bs2, err := json.MarshalIndent(plan, "", " ")
86+
if err != nil {
87+
t.Fatal(err)
88+
}
6889

69-
if !bytes.Equal(bs, bs2) {
70-
t.Fatal("expected bytes to be equal")
90+
if !bytes.Equal(bs, bs2) {
91+
t.Fatal("expected bytes to be equal")
92+
}
93+
})
7194
}
7295
}

0 commit comments

Comments
 (0)