This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/xml" | |
"testing" | |
) | |
type ( | |
Int int | |
String string | |
Bool bool | |
IntStr struct { | |
int | |
} | |
StringStr struct { | |
string | |
} | |
BoolStr struct { | |
bool | |
} | |
) | |
func (i Int) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | |
v := int(i) | |
return e.EncodeElement(v, start) | |
} | |
func (i IntStr) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | |
v := int(i.int) | |
return e.EncodeElement(v, start) | |
} | |
func (s String) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | |
v := string(s) | |
return e.EncodeElement(v, start) | |
} | |
func (s StringStr) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | |
v := string(s.string) | |
return e.EncodeElement(v, start) | |
} | |
func (b Bool) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | |
var v int | |
if bool(b) { | |
v = 1 | |
} | |
return e.EncodeElement(v, start) | |
} | |
func (b BoolStr) MarshalXML(e *xml.Encoder, start xml.StartElement) error { | |
var v int | |
if bool(b.bool) { | |
v = 1 | |
} | |
return e.EncodeElement(v, start) | |
} | |
func TestInt(t *testing.T) { | |
type A struct { | |
XMLName xml.Name `xml:"a"` | |
Int Int `xml:"int,omitempty"` | |
IntPtr *Int `xml:"intPtr,omitempty"` | |
IntStr IntStr `xml:"intStr,omitempty"` | |
IntStrPtr *IntStr `xml:"intStrPtr,omitempty"` | |
} | |
type testCase struct { | |
Name string | |
A A | |
Expected string | |
} | |
intP := Int(1) | |
intStrP := IntStr{1} | |
intPZero := Int(0) | |
intStrPZero := IntStr{0} | |
testCases := []testCase{ | |
{ | |
"zero values", | |
A{}, | |
"<a><intStr>0</intStr></a>", | |
}, { | |
"not nil pointers", | |
A{ | |
Int: Int(1), | |
IntPtr: &intP, | |
IntStr: IntStr{1}, | |
IntStrPtr: &intStrP, | |
}, | |
"<a><int>1</int><intPtr>1</intPtr><intStr>1</intStr><intStrPtr>1</intStrPtr></a>", | |
}, { | |
"not nil pointers with same zero values", | |
A{ | |
Int: Int(0), | |
IntPtr: &intPZero, | |
IntStr: IntStr{0}, | |
IntStrPtr: &intStrPZero, | |
}, | |
"<a><intPtr>0</intPtr><intStr>0</intStr><intStrPtr>0</intStrPtr></a>", | |
}, | |
} | |
for _, tc := range testCases { | |
b, err := xml.Marshal(tc.A) | |
if err != nil { | |
t.Errorf("XML Marshal error on %s case", tc.Name, err) | |
} | |
obtained := string(b) | |
if obtained != tc.Expected { | |
t.Errorf("case %q:\n expected = %s\n obtained = %s", tc.Name, tc.Expected, obtained) | |
} | |
} | |
} | |
func TestString(t *testing.T) { | |
type A struct { | |
XMLName xml.Name `xml:"a"` | |
String String `xml:"string,omitempty"` | |
StringPtr *String `xml:"stringPtr,omitempty"` | |
StringStr StringStr `xml:"stringStr,omitempty"` | |
StringStrPtr *StringStr `xml:"stringStrPtr,omitempty"` | |
} | |
type testCase struct { | |
Name string | |
A A | |
Expected string | |
} | |
stringP := String("hello") | |
stringStrP := StringStr{"hello"} | |
stringPZero := String("") | |
stringStrPZero := StringStr{""} | |
testCases := []testCase{ | |
{ | |
"zero values", | |
A{}, | |
"<a><stringStr></stringStr></a>", | |
}, { | |
"not nil pointers", | |
A{ | |
String: String("hello"), | |
StringPtr: &stringP, | |
StringStr: StringStr{"hello"}, | |
StringStrPtr: &stringStrP, | |
}, | |
"<a><string>hello</string><stringPtr>hello</stringPtr><stringStr>hello</stringStr><stringStrPtr>hello</stringStrPtr></a>", | |
}, { | |
"not nil pointers with same zero values", | |
A{ | |
String: String(""), | |
StringPtr: &stringPZero, | |
StringStr: StringStr{""}, | |
StringStrPtr: &stringStrPZero, | |
}, | |
"<a><stringPtr></stringPtr><stringStr></stringStr><stringStrPtr></stringStrPtr></a>", | |
}, | |
} | |
for _, tc := range testCases { | |
b, err := xml.Marshal(tc.A) | |
if err != nil { | |
t.Errorf("XML Marshal error on %s case", tc.Name, err) | |
} | |
obtained := string(b) | |
if obtained != tc.Expected { | |
t.Errorf("case %q:\n expected = %s\n obtained = %s", tc.Name, tc.Expected, obtained) | |
} | |
} | |
} | |
func TestBool(t *testing.T) { | |
type A struct { | |
XMLName xml.Name `xml:"a"` | |
Bool Bool `xml:"bool,omitempty"` | |
BoolPtr *Bool `xml:"boolPtr,omitempty"` | |
BoolStr BoolStr `xml:"boolStr,omitempty"` | |
BoolStrPtr *BoolStr `xml:"boolStrPtr,omitempty"` | |
} | |
type testCase struct { | |
Name string | |
A A | |
Expected string | |
} | |
boolPZero := Bool(false) | |
boolStrPZero := BoolStr{false} | |
boolP := Bool(true) | |
boolStrP := BoolStr{true} | |
testCases := []testCase{ | |
{ | |
"zero values", | |
A{}, | |
"<a><boolStr>0</boolStr></a>", | |
}, { | |
"not nil pointers", | |
A{ | |
Bool: Bool(true), | |
BoolPtr: &boolP, | |
BoolStr: BoolStr{true}, | |
BoolStrPtr: &boolStrP, | |
}, | |
"<a><bool>1</bool><boolPtr>1</boolPtr><boolStr>1</boolStr><boolStrPtr>1</boolStrPtr></a>", | |
}, { | |
"not nil pointers with same zero values", | |
A{ | |
Bool: Bool(false), | |
BoolPtr: &boolPZero, | |
BoolStr: BoolStr{false}, | |
BoolStrPtr: &boolStrPZero, | |
}, | |
"<a><boolPtr>0</boolPtr><boolStr>0</boolStr><boolStrPtr>0</boolStrPtr></a>", | |
}, | |
} | |
for _, tc := range testCases { | |
b, err := xml.Marshal(tc.A) | |
if err != nil { | |
t.Errorf("XML Marshal error on %s case", tc.Name, err) | |
} | |
obtained := string(b) | |
if obtained != tc.Expected { | |
t.Errorf("case %q:\n expected = %s\n obtained = %s", tc.Name, tc.Expected, obtained) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment