| 1 | package rundeck |
| 2 | |
| 3 | import ( |
| 4 | "encoding/xml" |
| 5 | "testing" |
| 6 | ) |
| 7 | |
| 8 | type marshalTest struct { |
| 9 | Name string |
| 10 | Input interface{} |
| 11 | ExpectedXML string |
| 12 | } |
| 13 | |
| 14 | type unmarshalTestFunc func (result interface {}) error |
| 15 | |
| 16 | type unmarshalTest struct { |
| 17 | Name string |
| 18 | Input string |
| 19 | Output interface {} |
| 20 | TestFunc unmarshalTestFunc |
| 21 | } |
| 22 | |
| 23 | func testMarshalXML(t *testing.T, tests []marshalTest) { |
| 24 | for _, test := range tests { |
| 25 | xmlBytes, err := xml.Marshal(test.Input) |
| 26 | if err != nil { |
| 27 | t.Errorf("Error in Marshall for test %s: %s", test.Name, err.Error()) |
| 28 | continue |
| 29 | } |
| 30 | xmlStr := string(xmlBytes) |
| 31 | if xmlStr != test.ExpectedXML { |
| 32 | t.Errorf("Test %s got %s, but wanted %s", test.Name, xmlStr, test.ExpectedXML) |
| 33 | continue |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | func testUnmarshalXML(t *testing.T, tests []unmarshalTest) { |
| 39 | for _, test := range tests { |
| 40 | xml.Unmarshal([]byte(test.Input), test.Output) |
| 41 | err := test.TestFunc(test.Output) |
| 42 | if err != nil { |
| 43 | t.Errorf("Test %s %s", test.Name, err.Error()) |
| 44 | } |
| 45 | } |
| 46 | } |