Debian Codegraph @

field/path_test.go

raw
1 /*
2 Copyright 2015 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package field
18
19 import "testing"
20
21 func TestPath(t *testing.T) {
22 testCases := []struct {
23 op func(*Path) *Path
24 expected string
25 }{
26 {
27 func(p *Path) *Path { return p },
28 "root",
29 },
30 {
31 func(p *Path) *Path { return p.Child("first") },
32 "root.first",
33 },
34 {
35 func(p *Path) *Path { return p.Child("second") },
36 "root.first.second",
37 },
38 {
39 func(p *Path) *Path { return p.Index(0) },
40 "root.first.second[0]",
41 },
42 {
43 func(p *Path) *Path { return p.Child("third") },
44 "root.first.second[0].third",
45 },
46 {
47 func(p *Path) *Path { return p.Index(93) },
48 "root.first.second[0].third[93]",
49 },
50 {
51 func(p *Path) *Path { return p.parent },
52 "root.first.second[0].third",
53 },
54 {
55 func(p *Path) *Path { return p.parent },
56 "root.first.second[0]",
57 },
58 {
59 func(p *Path) *Path { return p.Key("key") },
60 "root.first.second[0][key]",
61 },
62 }
63
64 root := NewPath("root")
65 p := root
66 for i, tc := range testCases {
67 p = tc.op(p)
68 if p.String() != tc.expected {
69 t.Errorf("[%d] Expected %q, got %q", i, tc.expected, p.String())
70 }
71 if p.Root() != root {
72 t.Errorf("[%d] Wrong root: %#v", i, p.Root())
73 }
74 }
75 }
76
77 func TestPathMultiArg(t *testing.T) {
78 testCases := []struct {
79 op func(*Path) *Path
80 expected string
81 }{
82 {
83 func(p *Path) *Path { return p },
84 "root.first",
85 },
86 {
87 func(p *Path) *Path { return p.Child("second", "third") },
88 "root.first.second.third",
89 },
90 {
91 func(p *Path) *Path { return p.Index(0) },
92 "root.first.second.third[0]",
93 },
94 {
95 func(p *Path) *Path { return p.parent },
96 "root.first.second.third",
97 },
98 {
99 func(p *Path) *Path { return p.parent },
100 "root.first.second",
101 },
102 {
103 func(p *Path) *Path { return p.parent },
104 "root.first",
105 },
106 {
107 func(p *Path) *Path { return p.parent },
108 "root",
109 },
110 }
111
112 root := NewPath("root", "first")
113 p := root
114 for i, tc := range testCases {
115 p = tc.op(p)
116 if p.String() != tc.expected {
117 t.Errorf("[%d] Expected %q, got %q", i, tc.expected, p.String())
118 }
119 if p.Root() != root.Root() {
120 t.Errorf("[%d] Wrong root: %#v", i, p.Root())
121 }
122 }
123 }