forked from vmware-archive/yaml-patch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pathfinder_test.go
74 lines (66 loc) · 2.48 KB
/
pathfinder_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
package yamlpatch_test
import (
yamlpatch "github.com/EngineerBetter/yaml-patch"
yaml "gopkg.in/yaml.v2"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
)
var _ = Describe("Pathfinder", func() {
var pathfinder *yamlpatch.PathFinder
BeforeEach(func() {
var iface interface{}
bs := []byte(`
jobs:
- name: job1
plan:
- get: A
args:
- arg: arg1
- arg: arg2
bool: true
- get: B
- get: C/D
- name: job2
plan:
- aggregate:
- get: C
- get: A
`)
err := yaml.Unmarshal(bs, &iface)
Expect(err).NotTo(HaveOccurred())
container := yamlpatch.NewNode(&iface).Container()
pathfinder = yamlpatch.NewPathFinder(container)
})
Describe("Find", func() {
DescribeTable(
"should",
func(path string, expected []string) {
actual := pathfinder.Find(path)
Expect(actual).To(HaveLen(len(expected)))
for _, el := range expected {
Expect(actual).To(ContainElement(el))
}
},
Entry("return a route for the root object", "/", []string{"/"}),
Entry("return a route for an object under the root", "/jobs", []string{"/jobs"}),
Entry("return a route for an element within an object under the root", "/jobs/0", []string{"/jobs/0"}),
Entry("return a route for an object within an element within an object under the root", "/jobs/0/plan", []string{"/jobs/0/plan"}),
Entry("return a route for an object within an element within an object under the root", "/jobs/0/plan/1", []string{"/jobs/0/plan/1"}),
Entry("return routes for multiple matches", "/jobs/get=A", []string{"/jobs/0/plan/0", "/jobs/1/plan/0/aggregate/1"}),
Entry("return a route for a single submatch with help", "/jobs/get=A/args/arg=arg2", []string{"/jobs/0/plan/0/args/1"}),
Entry("return a route for a single submatch with no help", "/jobs/get=A/arg=arg2", []string{"/jobs/0/plan/0/args/1"}),
Entry("return a route for a single submatch with help using escape ordering", "/jobs/get=C~1D", []string{"/jobs/0/plan/2"}),
Entry("return a route when given a pointer with a leaf that does not exist", "/jobs/name=job1/nonexistent", []string{"/jobs/0/nonexistent"}),
Entry("return a route when given a pointer with an array thingy", "/jobs/name=job1/plan/-", []string{"/jobs/0/plan/-"}),
)
DescribeTable(
"should not",
func(path string) {
Expect(pathfinder.Find(path)).To(BeNil())
},
Entry("return any routes when given a bad index", "/jobs/2"),
Entry("return any routes when given a bad index", "/jobs/-1"),
)
})
})