forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap_test.go
129 lines (118 loc) · 3.13 KB
/
map_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
// 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 (
"reflect"
"sort"
"testing"
)
func TestValueMapOverwrite(t *testing.T) {
a := NewValueMap()
a.Put(String("x"), String("foo"))
a.Put(String("x"), String("bar"))
if a.Get(String("x")) != String("bar") {
t.Fatalf("Expected a['x'] = 'bar' but got: %v", a.Get(String("x")))
}
}
func TestValueMapIter(t *testing.T) {
a := NewValueMap()
a.Put(String("x"), String("foo"))
a.Put(String("y"), String("bar"))
a.Put(String("z"), String("baz"))
values := []string{}
a.Iter(func(k, v Value) bool {
values = append(values, string(v.(String)))
return false
})
sort.Strings(values)
expected := []string{"bar", "baz", "foo"}
if !reflect.DeepEqual(values, expected) {
t.Fatalf("Unexpected value from iteration: %v", values)
}
}
func TestValueMapCopy(t *testing.T) {
a := NewValueMap()
a.Put(String("x"), String("foo"))
a.Put(String("y"), String("bar"))
b := a.Copy()
b.Delete(String("y"))
if a.Get(String("y")) != String("bar") {
t.Fatalf("Unexpected a['y'] value: %v", a.Get(String("y")))
}
}
func TestValueMapEqual(t *testing.T) {
a := NewValueMap()
a.Put(String("x"), String("foo"))
a.Put(String("y"), String("bar"))
b := a.Copy()
if !a.Equal(b) {
t.Fatalf("Expected a == b but not for: %v / %v", a, b)
}
if a.Hash() != b.Hash() {
t.Fatalf("Expected a.Hash() == b.Hash() but not for: %v / %v", a, b)
}
a.Delete(String("x"))
if a.Equal(b) {
t.Fatalf("Expected a != b but not for: %v / %v", a, b)
}
}
func TestValueMapGetMissing(t *testing.T) {
a := NewValueMap()
a.Put(String("x"), String("foo"))
a.Put(String("y"), String("bar"))
if a.Get(String("z")) != nil {
t.Fatalf("Expected a['z'] = nil but got: %v", a.Get(String("z")))
}
}
func TestValueMapString(t *testing.T) {
a := NewValueMap()
a.Put(MustParseRef("a.b.c[x]"), String("foo"))
a.Put(Var("x"), Number("1"))
result := a.String()
o1 := `{a.b.c[x]: "foo", x: 1}`
o2 := `{x: 1, a.b.c[x]: "foo"}`
if result != o1 && result != o2 {
t.Fatalf("Expected string to equal either %v or %v but got: %v", o1, o2, result)
}
}
func TestValueMapNil(t *testing.T) {
var a *ValueMap
if a.Copy() != nil {
t.Fatalf("Expected nil map copy to be nil")
}
a.Delete(String("foo"))
var b *ValueMap
if !a.Equal(b) {
t.Fatalf("Expected nil maps to be equal")
}
b = NewValueMap()
if !a.Equal(b) {
t.Fatalf("Expected nil map to equal non-nil, empty map")
}
b.Put(String("foo"), String("bar"))
if a.Equal(b) {
t.Fatalf("Expected nil map to not equal non-empty map")
}
if b.Equal(a) {
t.Fatalf("Expected non-nil map to not equal nil map")
}
if a.Hash() != 0 {
t.Fatalf("Expected nil map to hash to zero")
}
if a.Iter(func(Value, Value) bool { return true }) {
t.Fatalf("Expected nil map iteration to return false")
}
if a.Len() != 0 {
t.Fatalf("Expected nil map length to be zero")
}
if a.String() != "{}" {
t.Fatalf("Expected nil map string to be {}")
}
defer func() {
if r := recover(); r == nil {
t.Fatalf("Expected put to panic")
}
}()
a.Put(String("foo"), String("bar"))
}