github.com/cobalt77/jfrog-client-go@v0.14.5/artifactory/services/utils/properties_test.go (about) 1 package utils 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestToEncodedString(t *testing.T) { 9 tests := []struct { 10 props Properties 11 expected string 12 }{ 13 {Properties{[]Property{{Key: "a", Value: "b"}}}, "a=b"}, 14 {Properties{[]Property{{Key: "a;a", Value: "b;a"}}}, "a%3Ba=b%3Ba"}, 15 {Properties{[]Property{{Key: "a", Value: "b"}}}, "a=b"}, 16 {Properties{[]Property{{Key: ";a", Value: ";b"}}}, "%3Ba=%3Bb"}, 17 {Properties{[]Property{{Key: ";a", Value: ";b"}, {Key: ";a", Value: ";b"}, {Key: "aaa", Value: "bbb"}}}, "%3Ba=%3Bb;%3Ba=%3Bb;aaa=bbb"}, 18 {Properties{[]Property{{Key: "a;", Value: "b;"}}}, "a%3B=b%3B"}, 19 } 20 for _, test := range tests { 21 t.Run(test.expected, func(t *testing.T) { 22 test.props.ToEncodedString() 23 if test.expected != test.props.ToEncodedString() { 24 t.Error("Failed to encode properties. The propertyes", test.props.ToEncodedString(), "expected to be encoded to", test.expected) 25 } 26 }) 27 } 28 } 29 30 func TestParseProperties(t *testing.T) { 31 tests := []struct { 32 propsString string 33 option PropertyParseOptions 34 expected Properties 35 }{ 36 {"y=a,b", SplitCommas, Properties{[]Property{{Key: "y", Value: "a"}, {Key: "y", Value: "b"}}}}, 37 {"y=a\\,b", SplitCommas, Properties{[]Property{{Key: "y", Value: "a,b"}}}}, 38 {"y=a,b\\", SplitCommas, Properties{[]Property{{Key: "y", Value: "a"}, {Key: "y", Value: "b\\"}}}}, 39 {"y=a,b\\,", SplitCommas, Properties{[]Property{{Key: "y", Value: "a"}, {Key: "y", Value: "b,"}}}}, 40 {"y=a,b\\,c,d", SplitCommas, Properties{[]Property{{Key: "y", Value: "a"}, {Key: "y", Value: "b,c"}, {Key: "y", Value: "d"}}}}, 41 {"y=a,b\\,c\\,d", SplitCommas, Properties{[]Property{{Key: "y", Value: "a"}, {Key: "y", Value: "b,c,d"}}}}, 42 {"y=a,b\\,c\\,d\\,e", SplitCommas, Properties{[]Property{{Key: "y", Value: "a"}, {Key: "y", Value: "b,c,d,e"}}}}, 43 {"y=\\,a b", SplitCommas, Properties{[]Property{{Key: "y", Value: ",a b"}}}}, 44 } 45 for _, test := range tests { 46 t.Run(test.propsString, func(t *testing.T) { 47 props, err := ParseProperties(test.propsString, test.option) 48 if err != nil { 49 t.Error("Failed to parse property string.", err) 50 } 51 if !reflect.DeepEqual(test.expected.Properties, props.Properties) { 52 t.Error("Failed to parse property string.", props, "expected to be parsed to", test.expected) 53 } 54 }) 55 } 56 }