@@ -17,50 +17,61 @@ limitations under the License.
1717package fieldpath
1818
1919import (
20+ "reflect"
2021 "testing"
22+
23+ "sigs.k8s.io/structured-merge-diff/v6/value"
2124)
2225
2326func TestPathElementRoundTrip (t * testing.T ) {
24- tests := []string {
25- `i:0` ,
26- `i:1234` ,
27- `f:` ,
28- `f:spec` ,
29- `f:more-complicated-string` ,
30- `f: string-with-spaces ` ,
31- `f:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789` ,
32- `k:{"name":"my-container"}` ,
33- `k:{"name":" name with spaces "}` ,
34- `k:{"port":"8080","protocol":"TCP"}` ,
35- `k:{"optionalField":null}` ,
36- `k:{"jsonField":{"A":1,"B":null,"C":"D","E":{"F":"G"}}}` ,
37- `k:{"listField":["1","2","3"]}` ,
38- `v:null` ,
39- `v:"some-string"` ,
40- `v:1234` ,
41- `v:{"some":"json"}` ,
42- `v:{"some":" some with spaces "}` ,
43- `k:{"name":"app-🚀"}` ,
44- `k:{"name":"app-💻"}` ,
45- `k:{"name":"app with-unicøde"}` ,
46- `k:{"name":"你好世界"}` ,
47- `k:{"name":"Привет, мир"}` ,
48- `k:{"name":"नमस्ते दुनिया"}` ,
49- `k:{"name":"👋"}` ,
27+ type testCase struct {
28+ stringValue string
29+ pathElement PathElement
30+ }
31+
32+ tests := []testCase {
33+ {`i:0` , IndexElement (0 )},
34+ {`i:1234` , IndexElement (1234 )},
35+ {`f:` , FieldNameElement ("" )},
36+ {`f:spec` , FieldNameElement ("spec" )},
37+ {`f:more-complicated-string` , FieldNameElement ("more-complicated-string" )},
38+ {`f: string-with-spaces ` , FieldNameElement (" string-with-spaces " )},
39+ {`f:abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789` , FieldNameElement ("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" )},
40+ {`k:{"name":"my-container"}` , KeyElement (value.Field {Name : "name" , Value : value .NewValueInterface ("my-container" )})},
41+ {`k:{"name":" name with spaces "}` , KeyElement (value.Field {Name : "name" , Value : value .NewValueInterface (" name with spaces " )})},
42+ {`k:{"port":"8080","protocol":"TCP"}` , KeyElement (value.Field {Name : "port" , Value : value .NewValueInterface ("8080" )}, value.Field {Name : "protocol" , Value : value .NewValueInterface ("TCP" )})},
43+ {`k:{"optionalField":null}` , KeyElement (value.Field {Name : "optionalField" , Value : value .NewValueInterface (nil )})},
44+ {`k:{"jsonField":{"A":1,"B":null,"C":"D","E":{"F":"G"}}}` , KeyElement (value.Field {Name : "jsonField" , Value : value .NewValueInterface (map [string ]interface {}{"A" : float64 (1 ), "B" : nil , "C" : "D" , "E" : map [string ]interface {}{"F" : "G" }})})},
45+ {`k:{"listField":["1","2","3"]}` , KeyElement (value.Field {Name : "listField" , Value : value .NewValueInterface ([]interface {}{"1" , "2" , "3" })})},
46+ {`v:null` , ValueElement (value .NewValueInterface (nil ))},
47+ {`v:"some-string"` , ValueElement (value .NewValueInterface ("some-string" ))},
48+ {`v:1234` , ValueElement (value .NewValueInterface (float64 (1234 )))},
49+ {`v:{"some":"json"}` , ValueElement (value .NewValueInterface (map [string ]interface {}{"some" : "json" }))},
50+ {`v:{"some":" some with spaces "}` , ValueElement (value .NewValueInterface (map [string ]interface {}{"some" : " some with spaces " }))},
51+ {`k:{"name":"app-🚀"}` , KeyElement (value.Field {Name : "name" , Value : value .NewValueInterface ("app-🚀" )})},
52+ {`k:{"name":"app-💻"}` , KeyElement (value.Field {Name : "name" , Value : value .NewValueInterface ("app-💻" )})},
53+ {`k:{"name":"app with-unicøde"}` , KeyElement (value.Field {Name : "name" , Value : value .NewValueInterface ("app with-unicøde" )})},
54+ {`k:{"name":"你好世界"}` , KeyElement (value.Field {Name : "name" , Value : value .NewValueInterface ("你好世界" )})},
55+ {`k:{"name":"Привет, мир"}` , KeyElement (value.Field {Name : "name" , Value : value .NewValueInterface ("Привет, мир" )})},
56+ {`k:{"name":"नमस्ते दुनिया"}` , KeyElement (value.Field {Name : "name" , Value : value .NewValueInterface ("नमस्ते दुनिया" )})},
57+ {`k:{"name":"👋"}` , KeyElement (value.Field {Name : "name" , Value : value .NewValueInterface ("👋" )})},
5058 }
5159
5260 for _ , test := range tests {
53- t .Run (test , func (t * testing.T ) {
54- pe , err := DeserializePathElement (test )
61+ t .Run (test . stringValue , func (t * testing.T ) {
62+ pe , err := DeserializePathElement (test . stringValue )
5563 if err != nil {
5664 t .Fatalf ("Failed to create path element: %v" , err )
5765 }
66+ if ! reflect .DeepEqual (pe , test .pathElement ) {
67+ t .Fatalf ("Expected round-trip:\n input: %#v\n output: %#v" , test .pathElement , pe )
68+ }
5869 output , err := SerializePathElement (pe )
5970 if err != nil {
6071 t .Fatalf ("Failed to create string from path element (%#v): %v" , pe , err )
6172 }
62- if test != output {
63- t .Fatalf ("Expected round-trip:\n input: %v\n output: %v" , test , output )
73+ if test . stringValue != output {
74+ t .Fatalf ("Expected round-trip:\n input: %v\n output: %v" , test . stringValue , output )
6475 }
6576 })
6677 }
0 commit comments