File: testing.go

package info (click to toggle)
golang-github-apparentlymart-go-rundeck-api 0.0.1%2Bgit20170705.2c962ac-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 120 kB
  • sloc: makefile: 2
file content (46 lines) | stat: -rw-r--r-- 971 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package rundeck

import (
	"encoding/xml"
	"testing"
)

type marshalTest struct {
	Name        string
	Input       interface{}
	ExpectedXML string
}

type unmarshalTestFunc func (result interface {}) error

type unmarshalTest struct {
	Name string
	Input string
	Output interface {}
	TestFunc unmarshalTestFunc
}

func testMarshalXML(t *testing.T, tests []marshalTest) {
	for _, test := range tests {
		xmlBytes, err := xml.Marshal(test.Input)
		if err != nil {
			t.Errorf("Error in Marshall for test %s: %s", test.Name, err.Error())
			continue
		}
		xmlStr := string(xmlBytes)
		if xmlStr != test.ExpectedXML {
			t.Errorf("Test %s got %s, but wanted %s", test.Name, xmlStr, test.ExpectedXML)
			continue
		}
	}
}

func testUnmarshalXML(t *testing.T, tests []unmarshalTest) {
	for _, test := range tests {
		xml.Unmarshal([]byte(test.Input), test.Output)
		err := test.TestFunc(test.Output)
		if err != nil {
			t.Errorf("Test %s %s", test.Name, err.Error())
		}
	}
}