Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package xml
import (
"testing"
)
var (
fooXML = `
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root xmlns="urn:ietf:params:xml:ns:root-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:root-1.0 root-1.0.xsd">
<foo:property xmlns:foo="urn:ietf:params:xml:ns:foo-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:foo-1.0 foo-1.0.xsd">
<foo:baz>
<foo:xpto>foo-string</foo:xpto>
</foo:baz>
</foo:property>
</root>`
barXML = `
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root xmlns="urn:ietf:params:xml:ns:root-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:root-1.0 root-1.0.xsd">
<bar:property xmlns:bar="urn:ietf:params:xml:ns:bar-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:bar-1.0 bar-1.0.xsd">
<bar:baz>
<bar:xpto>bar-string</bar:xpto>
</bar:baz>
</bar:property>
</root>`
)
type BarRoot struct {
XMLName Name `xml:"urn:ietf:params:xml:ns:root-1.0 root"`
Property PropertyBar `xml:"bar=urn:ietf:params:xml:ns:bar-1.0 property"`
}
type FooRoot struct {
XMLName Name `xml:"urn:ietf:params:xml:ns:root-1.0 root"`
Property PropertyFoo `xml:"foo=urn:ietf:params:xml:ns:foo-1.0 property"`
}
type PropertyBar struct {
Baz *Baz `xml:"baz"`
}
type PropertyFoo struct {
Baz *Baz `xml:"baz"`
}
type Baz struct {
XPTO string `xml:"xpto"`
}
func TestSharedStruct(t *testing.T) {
unmarshalDocuments(t, false)
}
func TestSharedStructWithCleanup(t *testing.T) {
unmarshalDocuments(t, true)
}
func unmarshalDocuments(t *testing.T, cleanup bool) {
var bar BarRoot
var foo FooRoot
Unmarshal([]byte(barXML), &bar)
if cleanup {
for k, _ := range tinfoMap {
delete(tinfoMap, k)
}
}
Unmarshal([]byte(fooXML), &foo)
t.Log("foo: ", foo.Property.Baz.XPTO)
t.Log("bar: ", bar.Property.Baz.XPTO)
if bar.Property.Baz.XPTO != "bar-string" ||
foo.Property.Baz.XPTO != "foo-string" {
t.Fatal()
}
}
@mendelgusmao

This comment has been minimized.

Copy link
Owner Author

mendelgusmao commented Nov 27, 2013

go test xml -v -run Shared

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.