Skip to content

Instantly share code, notes, and snippets.

@MetalBlueberry
Last active October 14, 2020 18:27
Show Gist options
  • Save MetalBlueberry/feea41d741a5017626dd4cbfc043ba6f to your computer and use it in GitHub Desktop.
Save MetalBlueberry/feea41d741a5017626dd4cbfc043ba6f to your computer and use it in GitHub Desktop.
Golang assert two XML are equal
package main_test
import (
"encoding/xml"
"io"
"testing"
"github.com/stretchr/testify/assert"
)
func assertXMLEqual(t *testing.T, expected io.Reader, obtained io.Reader) {
var expec node
var obt node
var err error
err = xml.NewDecoder(expected).Decode(&expec)
assert.Nil(t, err)
err = xml.NewDecoder(obtained).Decode(&obt)
assert.Nil(t, err)
assert.Equal(t, expec, obt)
}
type node struct {
XMLName xml.Name
Attrs []xml.Attr `xml:",any,attr"`
Content string `xml:",innerxml"`
Nodes []node `xml:",any"`
}
func (n *node) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
type inNode node
err := d.DecodeElement((*inNode)(n), &start)
if err != nil {
return err
}
//Discard content if there are child nodes
if len(n.Nodes) > 0 {
n.Content = ""
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment