Omit empty <properties> tag when there are no properties.

Go doesn't omit empty parent tags for empty values[1], so we'll work
around this for now by creating a pointer to the property slice.

[1]: https://github.com/golang/go/issues/7233.
This commit is contained in:
Joël Stemmer
2022-03-11 23:36:03 +00:00
parent 43c784a63b
commit 458fe89a9a
3 changed files with 43 additions and 15 deletions

View File

@ -48,15 +48,21 @@ type Testsuite struct {
Time string `xml:"time,attr"` // duration in seconds
Timestamp string `xml:"timestamp,attr,omitempty"` // date and time in ISO8601
Properties []Property `xml:"properties>property,omitempty"`
Testcases []Testcase `xml:"testcase,omitempty"`
SystemOut *Output `xml:"system-out,omitempty"`
SystemErr *Output `xml:"system-err,omitempty"`
Properties *[]Property `xml:"properties>property,omitempty"`
Testcases []Testcase `xml:"testcase,omitempty"`
SystemOut *Output `xml:"system-out,omitempty"`
SystemErr *Output `xml:"system-err,omitempty"`
}
// AddProperty adds a property with the given name and value to this Testsuite.
func (t *Testsuite) AddProperty(name, value string) {
t.Properties = append(t.Properties, Property{Name: name, Value: value})
prop := Property{Name: name, Value: value}
if t.Properties == nil {
t.Properties = &[]Property{prop}
return
}
props := append(*t.Properties, prop)
t.Properties = &props
}
// AddTestcase adds Testcase tc to this Testsuite.

View File

@ -27,7 +27,7 @@ func TestMarshalUnmarshal(t *testing.T) {
Skipped: 1,
Time: "12.345",
Timestamp: "2012-03-09T14:38:06+01:00",
Properties: []Property{{"key", "value"}},
Properties: properties("key", "value"),
Testcases: []Testcase{
{
Name: "test1",
@ -62,3 +62,14 @@ func TestMarshalUnmarshal(t *testing.T) {
t.Errorf("Unmarshal result incorrect, diff (-want +got):\n%s\n", diff)
}
}
func properties(keyvals ...string) *[]Property {
if len(keyvals)%2 != 0 {
panic("invalid keyvals specified")
}
var props []Property
for i := 0; i < len(keyvals); i += 2 {
props = append(props, Property{keyvals[i], keyvals[i+1]})
}
return &props
}