This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathyptr_test.go
145 lines (127 loc) · 3.13 KB
/
yptr_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
// Copyright 2020 VMware, Inc.
// SPDX-License-Identifier: BSD-2-Clause
package yptr_test
import (
"errors"
"fmt"
"testing"
yptr "github.com/vmware-labs/yaml-jsonpointer"
yaml "gopkg.in/yaml.v3"
)
func ExampleFind() {
src := `
a:
b:
c: 42
d:
- e
- f
`
var n yaml.Node
yaml.Unmarshal([]byte(src), &n)
r, _ := yptr.Find(&n, `/a/b/c`)
fmt.Printf("Scalar %q at %d:%d\n", r.Value, r.Line, r.Column)
r, _ = yptr.Find(&n, `/d/0`)
fmt.Printf("Scalar %q at %d:%d\n", r.Value, r.Line, r.Column)
// Output: Scalar "42" at 4:8
// Scalar "e" at 6:3
}
func ExampleFind_extension() {
src := `kind: Deployment
apiVersion: apps/v1
metadata:
name: foo
spec:
template:
spec:
replicas: 1
containers:
- name: app
image: nginx
- name: sidecar
image: mysidecar
`
var n yaml.Node
yaml.Unmarshal([]byte(src), &n)
r, _ := yptr.Find(&n, `/spec/template/spec/containers/1/image`)
fmt.Printf("Scalar %q at %d:%d\n", r.Value, r.Line, r.Column)
r, _ = yptr.Find(&n, `/spec/template/spec/containers/~{"name":"app"}/image`)
fmt.Printf("Scalar %q at %d:%d\n", r.Value, r.Line, r.Column)
// Output: Scalar "mysidecar" at 13:16
// Scalar "nginx" at 11:16
}
func ExampleFind_jsonPointerCompat() {
// the array item match syntax doesn't accidentally match a field that just happens
// to contain the same characters.
src := `a:
"{\"b\":\"c\"}": d
`
var n yaml.Node
yaml.Unmarshal([]byte(src), &n)
r, _ := yptr.Find(&n, `/a/{"b":"c"}`)
fmt.Printf("Scalar %q at %d:%d\n", r.Value, r.Line, r.Column)
// Output: Scalar "d" at 2:20
}
func TestParse(t *testing.T) {
src := `
spec:
template:
spec:
replicas: 1
containers:
- name: app
image: nginx
`
var root yaml.Node
if err := yaml.Unmarshal([]byte(src), &root); err != nil {
t.Fatal(err)
}
if _, err := yptr.Find(&root, "/bad/path"); !errors.Is(err, yptr.ErrNotFound) {
t.Fatalf("expecting not found error, got: %v", err)
}
testCases := []struct {
ptr string
value string
line int
column int
}{
{`/spec/template/spec/replicas`, "1", 5, 17},
{`/spec/template/spec/containers/0/image`, "nginx", 8, 16},
{`/spec/template/spec/containers/~{"name":"app"}/image`, "nginx", 8, 16},
}
for i, tc := range testCases {
t.Run(fmt.Sprint(i), func(t *testing.T) {
r, err := yptr.Find(&root, tc.ptr)
if err != nil {
t.Fatal(err)
}
if got, want := r.Value, tc.value; got != want {
t.Fatalf("got: %v, want: %v", got, want)
}
if got, want := r.Line, tc.line; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
if got, want := r.Column, tc.column; got != want {
t.Errorf("got: %v, want: %v", got, want)
}
})
}
errorCases := []struct {
ptr string
err error
}{
{"a", fmt.Errorf(`JSON pointer must be empty or start with a "/`)},
{"/a", yptr.ErrNotFound},
}
for i, tc := range errorCases {
t.Run(fmt.Sprint("error", i), func(t *testing.T) {
_, err := yptr.Find(&root, tc.ptr)
if err == nil {
t.Fatal("error expected")
}
if got, want := err, tc.err; got.Error() != want.Error() && !errors.Is(got, want) {
t.Errorf("got: %v, want: %v", got, want)
}
})
}
}