package xmlrpc import ( "sort" "strings" "testing" "time" ) var marshalTests = []struct { value interface{} xml string }{ {100, "100"}, {"Once upon a time", "Once upon a time"}, {"Mike & Mick ", "Mike & Mick <London, UK>"}, {Base64("T25jZSB1cG9uIGEgdGltZQ=="), "T25jZSB1cG9uIGEgdGltZQ=="}, {true, "1"}, {false, "0"}, {12.134, "12.134"}, {-12.134, "-12.134"}, {738777323.0, "738777323"}, {time.Unix(1386622812, 0).UTC(), "20131209T21:00:12"}, {[]interface{}{1, "one"}, "1one"}, {&struct { Title string Amount int }{"War and Piece", 20}, "TitleWar and PieceAmount20"}, {&struct { Value interface{} `xmlrpc:"value"` }{}, "value"}, { map[string]interface{}{"title": "War and Piece", "amount": 20}, "amount20titleWar and Piece", }, { map[string]interface{}{ "Name": "John Smith", "Age": 6, "Wight": []float32{66.67, 100.5}, "Dates": map[string]interface{}{"Birth": time.Date(1829, time.November, 10, 23, 0, 0, 0, time.UTC), "Death": time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC)}}, "Age6DatesBirth18291110T23:00:00Death20091110T23:00:00NameJohn SmithWight66.67100.5", }, } func Test_marshal(t *testing.T) { sortMapKeys = true for _, tt := range marshalTests { b, err := marshal(tt.value) if err != nil { t.Fatalf("unexpected marshal error: %v", err) } if string(b) != tt.xml { // Sometimes the XML will not be in the exact order it is in the reference // So try sorting both strings first, see if that helps. string_b := strings.Split(string(b), "") string_xml := strings.Split(tt.xml, "") sort.Strings(string_b) sort.Strings(string_xml) if strings.Join(string_b, "") != strings.Join(string_xml, "") { t.Fatalf("marshal error:\nexpected: %s\n got: %s", string_xml, string_b) } } } }