forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransform_test.go
115 lines (92 loc) · 2.57 KB
/
transform_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
// Copyright 2016 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 ast
import (
"testing"
)
func TestTransform(t *testing.T) {
module := MustParseModule(`package ex.this
import input.foo
import data.bar.this as qux
import future.keywords.every
p = true { "this" = "that" }
p = "this" { false }
p["this"] { false }
p[y] = {"this": ["this"]} { false }
p = true { ["this" | "this"] }
p = n { count({"this", "that"}, n) with input.foo.this as {"this": true} }
p { false } else = "this" { "this" } else = ["this"] { true }
foo(x) = y { split(x, "this", y) }
p { every x in ["this"] { x == "this" } }
`)
result, err := Transform(&GenericTransformer{
func(x interface{}) (interface{}, error) {
if s, ok := x.(String); ok && s == String("this") {
return String("that"), nil
}
return x, nil
},
}, module)
if err != nil {
t.Fatalf("Unexpected error during transform: %v", err)
}
resultMod, ok := result.(*Module)
if !ok {
t.Fatalf("Expected module from transform but got: %v", result)
}
expected := MustParseModule(`package ex.that
import input.foo
import data.bar.that as qux
import future.keywords.every
p = true { "that" = "that" }
p = "that" { false }
p["that"] { false }
p[y] = {"that": ["that"]} { false }
p = true { ["that" | "that"] }
p = n { count({"that"}, n) with input.foo.that as {"that": true} }
p { false } else = "that" { "that" } else = ["that"] { true }
foo(x) = y { split(x, "that", y) }
p { every x in ["that"] { x == "that" } }
`)
if !expected.Equal(resultMod) {
t.Fatalf("Expected module:\n%v\n\nGot:\n%v\n", expected, resultMod)
}
}
func TestTransformAnnotations(t *testing.T) {
module, err := ParseModuleWithOpts("test.rego", `package test
# METADATA
# scope: rule
p := 7`, ParserOptions{ProcessAnnotation: true})
if err != nil {
t.Fatal(err)
}
result, err := Transform(&GenericTransformer{
func(x interface{}) (interface{}, error) {
if s, ok := x.(*Annotations); ok {
cpy := *s
cpy.Scope = "deadbeef"
return &cpy, nil
}
return x, nil
},
}, module)
if err != nil {
t.Fatalf("Unexpected error: %s", err)
}
resultMod, ok := result.(*Module)
if !ok {
t.Fatalf("Expected module from transform but got: %v", result)
}
exp, err := ParseModuleWithOpts("test.rego", `package test
# METADATA
# scope: rule
p := 7`, ParserOptions{ProcessAnnotation: true})
if err != nil {
t.Fatal(err)
}
exp.Annotations[0].Scope = "deadbeef"
if resultMod.Compare(exp) != 0 {
t.Fatalf("expected:\n\n%v\n\ngot:\n\n%v", exp, resultMod)
}
}