forked from vmware-archive/yaml-jsonpointer
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsubset_test.go
61 lines (54 loc) · 1.53 KB
/
subset_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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
package yptr
import (
"fmt"
"testing"
"github.com/zillow/go-yaml/v3"
)
func TestIsTreeSubset(t *testing.T) {
testCases := []struct {
a string
b string
ok bool
}{
{`1`, `1`, true},
{`1`, `2`, false},
{`"a"`, `"a"`, true},
{`"a"`, `"b"`, false},
{`{"a":"b"}`, `{"a":"b","c":d"}`, true},
{`{"a":"b"}`, `{"c":d","a":"b"}`, true},
{`{"a":"x"}`, `{"a":"b","c":d"}`, false},
{`{"a":"b","c":d"}`, `{"a":"b"}`, false},
{`{"a":{"b": 1}}`, `{"a":{"b": 1}}`, true},
{`{"a":{"b": 1}}`, `{"a":{"b": 2}}`, false},
{`{"a":{"b": 1}}`, `{"a":{"b": 1, "c": 2}}`, true},
{`[0]`, `[0]`, true},
{`[0, 0]`, `[0, 0]`, true},
{`[0]`, `[0, 1]`, true},
{`[1]`, `[0, 1]`, true},
{`[0, 0]`, `[0, 1]`, true},
{`[1, 1]`, `[1, 0]`, true},
{`[1, 1]`, `[1, 1]`, true},
{`[1, 1]`, `[0, 1]`, true},
{`[0]`, `[1, 2]`, false},
{`{"a":{"b": [1]}}`, `{"a":{"b": [0,1,2], "c": 2}}`, true},
{`{"a":{"b": [1]}}`, `{"a":{"b": [0,2], "c": 2}}`, false},
{`[0,2]`, `[0,1,2]`, true},
{`{"a":{"b":1}}`, `{"x":2, "a":{"c":4, "b":1, "d":5}, "y":1}`, true}, // from isTreeSubset doc comment.
}
for i, tc := range testCases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
var a, b yaml.Node
if err := yaml.Unmarshal([]byte(tc.a), &a); err != nil {
t.Fatal(err)
}
if err := yaml.Unmarshal([]byte(tc.b), &b); err != nil {
t.Fatal(err)
}
if got, want := isTreeSubset(&a, &b), tc.ok; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
}
}