forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsets_test.go
75 lines (68 loc) · 1.7 KB
/
sets_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
// Copyright 2022 The OPA Authors. All rights reserved.
// Use of this source code is governed by an Apache2
// license that can be found in the LICENSE file.
package topdown
import (
"testing"
"github.com/open-policy-agent/opa/ast"
)
func TestSetUnionBuiltin(t *testing.T) {
tests := []struct {
note string
query string
input string
expected string
}{
// NOTE(philipc): These tests assume that erroneous types are
// checked elsewhere, and focus only on functional correctness.
{
note: "Empty",
input: `{set()}`,
expected: `set()`,
},
{
note: "Singletons",
input: `{{1}, {2}, {3}, {4}, {5}}`,
expected: `{1, 2, 3, 4, 5}`,
},
{
note: "One set",
input: `{{1, 2, 3, 4, 5}}`,
expected: `{1, 2, 3, 4, 5}`,
},
{
note: "One set + empty",
input: `{{1, 2, 3, 4, 5}, set()}`,
expected: `{1, 2, 3, 4, 5}`,
},
{
note: "Multiple sets, with duplicates",
input: `{{1, 2, 3}, {1, 2}, {3}, {4, 5}}`,
expected: `{1, 2, 3, 4, 5}`,
},
}
for _, tc := range tests {
inputs := ast.MustParseTerm(tc.input)
result, err := getResult(functionalWrapper1("union", builtinSetUnion), inputs)
if err != nil {
t.Fatal(err)
}
expected := ast.MustParseTerm(tc.expected)
if !result.Equal(expected) {
t.Fatalf("Expected %v but got %v", expected, result)
}
}
}
// Used to get older-style (ast.Term, error) tuples out of newer functions.
func getResult(fn BuiltinFunc, operands ...*ast.Term) (*ast.Term, error) {
var result *ast.Term
extractionFn := func(r *ast.Term) error {
result = r
return nil
}
err := fn(BuiltinContext{}, operands, extractionFn)
if err != nil {
return nil, err
}
return result, nil
}