forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpath_test.go
160 lines (146 loc) · 4.29 KB
/
path_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
// 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 storage
import (
"reflect"
"testing"
"fmt"
"github.com/open-policy-agent/opa/ast"
)
func TestNewPathForString(t *testing.T) {
tests := []struct {
input string
result Path
ok bool
}{
{"", nil, false},
{"foo", nil, false},
{"/", Path{}, true},
{"/", nil, true},
{"/foo", Path{"foo"}, true},
{"/foo/bar", Path{"foo", "bar"}, true},
}
for _, tc := range tests {
result, ok := ParsePath(tc.input)
if (tc.ok != ok) || !tc.result.Equal(result) {
t.Errorf("For %v wanted (%v, %v) but got (%v, %v)", tc.input, tc.result, tc.ok, result, ok)
}
}
}
func TestNewPathForRef(t *testing.T) {
tests := []struct {
input ast.Ref
result Path
err error
}{
{ast.Ref{}, nil, fmt.Errorf("empty reference (indicates error in caller)")},
{ast.MustParseRef("data.foo[x]"), nil, fmt.Errorf("unresolved reference (indicates error in caller): data.foo[x]")},
{ast.MustParseRef("data.foo[true]"), nil, &Error{
Code: NotFoundErr,
Message: fmt.Sprintf("%v: does not exist", ast.MustParseRef("data.foo[true]")),
}},
{ast.MustParseRef("data.foo[[1, 2]]"), nil, fmt.Errorf("composites cannot be base document keys: %v", ast.MustParseRef("data.foo[[1, 2]]"))},
{ast.MustParseRef("data.foo[{1, 2}]"), nil, fmt.Errorf("composites cannot be base document keys: %v", ast.MustParseRef("data.foo[{1, 2}]"))},
{ast.MustParseRef(`data.foo[{"foo": 2}]`), nil, fmt.Errorf("composites cannot be base document keys: %v", ast.MustParseRef(`data.foo[{"foo": 2}]`))},
{ast.MustParseRef("data"), Path{}, nil},
{ast.MustParseRef("data.foo"), Path{"foo"}, nil},
{ast.MustParseRef("data.foo[1]"), Path{"foo", "1"}, nil},
{ast.MustParseRef("data.foo.bar"), Path{"foo", "bar"}, nil},
}
for _, tc := range tests {
result, err := NewPathForRef(tc.input)
if tc.err != nil && !reflect.DeepEqual(tc.err, err) {
t.Errorf("For %v expected %v but got %v", tc.input, tc.err, err)
} else if !result.Equal(tc.result) {
t.Errorf("For %v expected %v but got %v", tc.input, tc.result, result)
}
}
}
func TestPathCompare(t *testing.T) {
tests := []struct {
a Path
b Path
result int
}{
{Path{}, Path{}, 0},
{Path{}, Path{"x"}, -1},
{Path{"x"}, Path{}, 1},
{Path{"x"}, Path{"x"}, 0},
{Path{"x"}, Path{"y"}, -1},
{Path{"x"}, Path{"w"}, 1},
{Path{"x"}, Path{"wz"}, 1},
{Path{"x"}, Path{"xx"}, -1},
{Path{"xx"}, Path{"x"}, 1},
{Path{"xx"}, Path{"xx"}, 0},
{Path{"xy"}, Path{"xx"}, 1},
}
for _, tc := range tests {
result := tc.a.Compare(tc.b)
if result != tc.result {
t.Errorf("For %v.Compare(%v) expected %v but got %v", tc.a, tc.b, tc.result, result)
}
}
}
func TestPathEqual(t *testing.T) {
tests := []struct {
a Path
b Path
result bool
}{
{Path{}, Path{}, true},
{Path{}, Path{"foo"}, false},
{Path{"foo"}, Path{}, false},
{Path{"foo", "bar"}, Path{"foo"}, false},
{Path{"foo", "bar"}, Path{"foo", "bar"}, true},
}
for _, tc := range tests {
result := tc.a.Equal(tc.b)
if result != tc.result {
t.Errorf("For %v.HasPrefix(%v) expected %v but got %v", tc.a, tc.b, tc.result, result)
}
}
}
func TestPathHasPrefix(t *testing.T) {
tests := []struct {
a Path
b Path
result bool
}{
{Path{}, Path{}, true},
{Path{}, Path{"foo"}, false},
{Path{"foo"}, Path{}, true},
{Path{"foo"}, Path{"bar"}, false},
{Path{"bar"}, Path{"foo"}, false},
{Path{"foo", "bar"}, Path{"foo"}, true},
{Path{"foo", "bar"}, Path{"foo", "bar"}, true},
{Path{"foo", "bar"}, Path{"foo", "bar", "baz"}, false},
{Path{"foo", "bar", "baz"}, Path{}, true},
}
for _, tc := range tests {
result := tc.a.HasPrefix(tc.b)
if result != tc.result {
t.Errorf("For %v.HasPrefix(%v) expected %v but got %v", tc.a, tc.b, tc.result, result)
}
}
}
func TestPathRef(t *testing.T) {
tests := []struct {
path string
head string
ref string
}{
{"/", "data", "data"},
{"/foo/bar", "data", "data.foo.bar"},
{"/foo/bar/3", "data", "data.foo.bar[3]"},
}
for _, tc := range tests {
path := MustParsePath(tc.path)
head := ast.VarTerm(tc.head)
ref := ast.MustParseRef(tc.ref)
result := path.Ref(head)
if !result.Equal(ref) {
t.Errorf("Expected %v but got %v", ref, result)
}
}
}