1
16
17 package fieldpath
18
19 import "testing"
20
21 func TestPathElementRoundTrip(t *testing.T) {
22 tests := []string{
23 `i:0`,
24 `i:1234`,
25 `f:`,
26 `f:spec`,
27 `f:more-complicated-string`,
28 `k:{"name":"my-container"}`,
29 `k:{"port":"8080","protocol":"TCP"}`,
30 `k:{"optionalField":null}`,
31 `k:{"jsonField":{"A":1,"B":null,"C":"D","E":{"F":"G"}}}`,
32 `k:{"listField":["1","2","3"]}`,
33 `v:null`,
34 `v:"some-string"`,
35 `v:1234`,
36 `v:{"some":"json"}`,
37 }
38
39 for _, test := range tests {
40 t.Run(test, func(t *testing.T) {
41 pe, err := DeserializePathElement(test)
42 if err != nil {
43 t.Fatalf("Failed to create path element: %v", err)
44 }
45 output, err := SerializePathElement(pe)
46 if err != nil {
47 t.Fatalf("Failed to create string from path element (%#v): %v", pe, err)
48 }
49 if test != output {
50 t.Fatalf("Expected round-trip:\ninput: %v\noutput: %v", test, output)
51 }
52 })
53 }
54 }
55
56 func TestPathElementIgnoreUnknown(t *testing.T) {
57 _, err := DeserializePathElement("r:Hello")
58 if err != ErrUnknownPathElementType {
59 t.Fatalf("Unknown qualifiers must not return an invalid path element")
60 }
61 }
62
63 func TestDeserializePathElementError(t *testing.T) {
64 tests := []string{
65 ``,
66 `no-colon`,
67 `i:index is not a number`,
68 `i:1.23`,
69 `i:`,
70 `v:invalid json`,
71 `v:`,
72 `k:invalid json`,
73 `k:{"name":invalid}`,
74 }
75
76 for _, test := range tests {
77 t.Run(test, func(t *testing.T) {
78 pe, err := DeserializePathElement(test)
79 if err == nil {
80 t.Fatalf("Expected error, no error found. got: %#v, %s", pe, pe)
81 }
82 })
83 }
84 }
85
View as plain text