I'm testing XML marshaling with testify and using strings.Contains
to check if lines I expect to be included in the XML are in fact there.
However, I want to diff the actual vs. desired xml.
Currently, my code looks something like:
func (suite *BookSuite) TestXMLMarshal() {
priceXML, priceErr := xml.Marshal(PriceType{Price: 10, Type: "IND"})
suite.Nil(priceErr)
linePresent := strings.Contains(string(priceXML), `<PriceType Price="10" Type="IND"></PriceType>`)
if true != linePresent {
err := errors.New("Expected: \n" + `<PriceType Price="10" Type="IND"></PriceType>` + "\nGot: \n" + bookString)
suite.Error(err, err.Error())
fmt.Println(err)
}
}
There are more lines in the xml file than the single one in the test, so as you can imagine that if statement is going to be gross. Any ideas on cleaning this up that's more scalable?
Unless the formatting matters a whole bunch, a quick thorough way to test something like xml.Marshal is to marshal to and from and compare the objects
func (suite *BookSuite) TestXMLMarshal() {
priceXML, priceErr := xml.Marshal(PriceType{Price: 10, Type: "IND"})
suite.Nil(priceErr)
var secondPrice PriceType
unerr := xml.Unmarshal(priceXML, &secondPrice)
suite.Nil(unerr)
if !reflect.DeepEqual(&priceXML,&secondPrice){
err := fmt.Errorf("Expected: '%+v'\nGot: %+v\n",priceXML,secondPrice)
suite.Error(err, err.Error())
fmt.Println(err)
}
}
not tested but should be something like that.