forked from open-policy-agent/opa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmerge_test.go
59 lines (47 loc) · 1.33 KB
/
merge_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
// Copyright 2017 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 loader
import (
"reflect"
"testing"
"github.com/open-policy-agent/opa/util"
)
func TestMergeDocs(t *testing.T) {
tests := []struct {
a string
b string
c string
ok bool
}{
{`{"x": 1, "y": 2}`, `{"z": 3}`, `{"x": 1, "y": 2, "z": 3}`, true},
{`{"x": {"y": 2}}`, `{"z": 3, "x": {"q": 4}}`, `{"x": {"y": 2, "q": 4}, "z": 3}`, true},
{`{"x": 1}`, `{"x": 1}`, "", false},
{`{"x": {"y": [{"z": 2}]}}`, `{"x": {"y": [{"z": 3}]}}`, "", false},
}
for _, tc := range tests {
a := map[string]interface{}{}
if err := util.UnmarshalJSON([]byte(tc.a), &a); err != nil {
panic(err)
}
b := map[string]interface{}{}
if err := util.UnmarshalJSON([]byte(tc.b), &b); err != nil {
panic(err)
}
if len(tc.c) == 0 {
c, ok := mergeInterfaces(a, b)
if ok {
t.Errorf("Expected merge(%v,%v) == false but got: %v", a, b, c)
}
} else {
expected := map[string]interface{}{}
if err := util.UnmarshalJSON([]byte(tc.c), &expected); err != nil {
panic(err)
}
c, ok := mergeInterfaces(a, b)
if !ok || !reflect.DeepEqual(c, expected) {
t.Errorf("Expected merge(%v, %v) == %v but got: %v (ok: %v)", a, b, expected, c, ok)
}
}
}
}