github.com/varialus/godfly@v0.0.0-20130904042352-1934f9f095ab/src/pkg/encoding/xml/marshal_test.go (about)

     1  // Copyright 2011 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package xml
     6  
     7  import (
     8  	"bytes"
     9  	"errors"
    10  	"fmt"
    11  	"io"
    12  	"reflect"
    13  	"strconv"
    14  	"strings"
    15  	"testing"
    16  	"time"
    17  )
    18  
    19  type DriveType int
    20  
    21  const (
    22  	HyperDrive DriveType = iota
    23  	ImprobabilityDrive
    24  )
    25  
    26  type Passenger struct {
    27  	Name   []string `xml:"name"`
    28  	Weight float32  `xml:"weight"`
    29  }
    30  
    31  type Ship struct {
    32  	XMLName struct{} `xml:"spaceship"`
    33  
    34  	Name      string       `xml:"name,attr"`
    35  	Pilot     string       `xml:"pilot,attr"`
    36  	Drive     DriveType    `xml:"drive"`
    37  	Age       uint         `xml:"age"`
    38  	Passenger []*Passenger `xml:"passenger"`
    39  	secret    string
    40  }
    41  
    42  type NamedType string
    43  
    44  type Port struct {
    45  	XMLName struct{} `xml:"port"`
    46  	Type    string   `xml:"type,attr,omitempty"`
    47  	Comment string   `xml:",comment"`
    48  	Number  string   `xml:",chardata"`
    49  }
    50  
    51  type Domain struct {
    52  	XMLName struct{} `xml:"domain"`
    53  	Country string   `xml:",attr,omitempty"`
    54  	Name    []byte   `xml:",chardata"`
    55  	Comment []byte   `xml:",comment"`
    56  }
    57  
    58  type Book struct {
    59  	XMLName struct{} `xml:"book"`
    60  	Title   string   `xml:",chardata"`
    61  }
    62  
    63  type Event struct {
    64  	XMLName struct{} `xml:"event"`
    65  	Year    int      `xml:",chardata"`
    66  }
    67  
    68  type Movie struct {
    69  	XMLName struct{} `xml:"movie"`
    70  	Length  uint     `xml:",chardata"`
    71  }
    72  
    73  type Pi struct {
    74  	XMLName       struct{} `xml:"pi"`
    75  	Approximation float32  `xml:",chardata"`
    76  }
    77  
    78  type Universe struct {
    79  	XMLName struct{} `xml:"universe"`
    80  	Visible float64  `xml:",chardata"`
    81  }
    82  
    83  type Particle struct {
    84  	XMLName struct{} `xml:"particle"`
    85  	HasMass bool     `xml:",chardata"`
    86  }
    87  
    88  type Departure struct {
    89  	XMLName struct{}  `xml:"departure"`
    90  	When    time.Time `xml:",chardata"`
    91  }
    92  
    93  type SecretAgent struct {
    94  	XMLName   struct{} `xml:"agent"`
    95  	Handle    string   `xml:"handle,attr"`
    96  	Identity  string
    97  	Obfuscate string `xml:",innerxml"`
    98  }
    99  
   100  type NestedItems struct {
   101  	XMLName struct{} `xml:"result"`
   102  	Items   []string `xml:">item"`
   103  	Item1   []string `xml:"Items>item1"`
   104  }
   105  
   106  type NestedOrder struct {
   107  	XMLName struct{} `xml:"result"`
   108  	Field1  string   `xml:"parent>c"`
   109  	Field2  string   `xml:"parent>b"`
   110  	Field3  string   `xml:"parent>a"`
   111  }
   112  
   113  type MixedNested struct {
   114  	XMLName struct{} `xml:"result"`
   115  	A       string   `xml:"parent1>a"`
   116  	B       string   `xml:"b"`
   117  	C       string   `xml:"parent1>parent2>c"`
   118  	D       string   `xml:"parent1>d"`
   119  }
   120  
   121  type NilTest struct {
   122  	A interface{} `xml:"parent1>parent2>a"`
   123  	B interface{} `xml:"parent1>b"`
   124  	C interface{} `xml:"parent1>parent2>c"`
   125  }
   126  
   127  type Service struct {
   128  	XMLName struct{} `xml:"service"`
   129  	Domain  *Domain  `xml:"host>domain"`
   130  	Port    *Port    `xml:"host>port"`
   131  	Extra1  interface{}
   132  	Extra2  interface{} `xml:"host>extra2"`
   133  }
   134  
   135  var nilStruct *Ship
   136  
   137  type EmbedA struct {
   138  	EmbedC
   139  	EmbedB EmbedB
   140  	FieldA string
   141  }
   142  
   143  type EmbedB struct {
   144  	FieldB string
   145  	*EmbedC
   146  }
   147  
   148  type EmbedC struct {
   149  	FieldA1 string `xml:"FieldA>A1"`
   150  	FieldA2 string `xml:"FieldA>A2"`
   151  	FieldB  string
   152  	FieldC  string
   153  }
   154  
   155  type NameCasing struct {
   156  	XMLName struct{} `xml:"casing"`
   157  	Xy      string
   158  	XY      string
   159  	XyA     string `xml:"Xy,attr"`
   160  	XYA     string `xml:"XY,attr"`
   161  }
   162  
   163  type NamePrecedence struct {
   164  	XMLName     Name              `xml:"Parent"`
   165  	FromTag     XMLNameWithoutTag `xml:"InTag"`
   166  	FromNameVal XMLNameWithoutTag
   167  	FromNameTag XMLNameWithTag
   168  	InFieldName string
   169  }
   170  
   171  type XMLNameWithTag struct {
   172  	XMLName Name   `xml:"InXMLNameTag"`
   173  	Value   string `xml:",chardata"`
   174  }
   175  
   176  type XMLNameWithoutTag struct {
   177  	XMLName Name
   178  	Value   string `xml:",chardata"`
   179  }
   180  
   181  type NameInField struct {
   182  	Foo Name `xml:"ns foo"`
   183  }
   184  
   185  type AttrTest struct {
   186  	Int   int     `xml:",attr"`
   187  	Named int     `xml:"int,attr"`
   188  	Float float64 `xml:",attr"`
   189  	Uint8 uint8   `xml:",attr"`
   190  	Bool  bool    `xml:",attr"`
   191  	Str   string  `xml:",attr"`
   192  	Bytes []byte  `xml:",attr"`
   193  }
   194  
   195  type OmitAttrTest struct {
   196  	Int   int     `xml:",attr,omitempty"`
   197  	Named int     `xml:"int,attr,omitempty"`
   198  	Float float64 `xml:",attr,omitempty"`
   199  	Uint8 uint8   `xml:",attr,omitempty"`
   200  	Bool  bool    `xml:",attr,omitempty"`
   201  	Str   string  `xml:",attr,omitempty"`
   202  	Bytes []byte  `xml:",attr,omitempty"`
   203  }
   204  
   205  type OmitFieldTest struct {
   206  	Int   int           `xml:",omitempty"`
   207  	Named int           `xml:"int,omitempty"`
   208  	Float float64       `xml:",omitempty"`
   209  	Uint8 uint8         `xml:",omitempty"`
   210  	Bool  bool          `xml:",omitempty"`
   211  	Str   string        `xml:",omitempty"`
   212  	Bytes []byte        `xml:",omitempty"`
   213  	Ptr   *PresenceTest `xml:",omitempty"`
   214  }
   215  
   216  type AnyTest struct {
   217  	XMLName  struct{}  `xml:"a"`
   218  	Nested   string    `xml:"nested>value"`
   219  	AnyField AnyHolder `xml:",any"`
   220  }
   221  
   222  type AnyOmitTest struct {
   223  	XMLName  struct{}   `xml:"a"`
   224  	Nested   string     `xml:"nested>value"`
   225  	AnyField *AnyHolder `xml:",any,omitempty"`
   226  }
   227  
   228  type AnySliceTest struct {
   229  	XMLName  struct{}    `xml:"a"`
   230  	Nested   string      `xml:"nested>value"`
   231  	AnyField []AnyHolder `xml:",any"`
   232  }
   233  
   234  type AnyHolder struct {
   235  	XMLName Name
   236  	XML     string `xml:",innerxml"`
   237  }
   238  
   239  type RecurseA struct {
   240  	A string
   241  	B *RecurseB
   242  }
   243  
   244  type RecurseB struct {
   245  	A *RecurseA
   246  	B string
   247  }
   248  
   249  type PresenceTest struct {
   250  	Exists *struct{}
   251  }
   252  
   253  type IgnoreTest struct {
   254  	PublicSecret string `xml:"-"`
   255  }
   256  
   257  type MyBytes []byte
   258  
   259  type Data struct {
   260  	Bytes  []byte
   261  	Attr   []byte `xml:",attr"`
   262  	Custom MyBytes
   263  }
   264  
   265  type Plain struct {
   266  	V interface{}
   267  }
   268  
   269  type MyInt int
   270  
   271  type EmbedInt struct {
   272  	MyInt
   273  }
   274  
   275  type Strings struct {
   276  	X []string `xml:"A>B,omitempty"`
   277  }
   278  
   279  type PointerFieldsTest struct {
   280  	XMLName  Name    `xml:"dummy"`
   281  	Name     *string `xml:"name,attr"`
   282  	Age      *uint   `xml:"age,attr"`
   283  	Empty    *string `xml:"empty,attr"`
   284  	Contents *string `xml:",chardata"`
   285  }
   286  
   287  type ChardataEmptyTest struct {
   288  	XMLName  Name    `xml:"test"`
   289  	Contents *string `xml:",chardata"`
   290  }
   291  
   292  type MyMarshalerTest struct {
   293  }
   294  
   295  var _ Marshaler = (*MyMarshalerTest)(nil)
   296  
   297  func (m *MyMarshalerTest) MarshalXML(e *Encoder, start StartElement) error {
   298  	e.EncodeToken(start)
   299  	e.EncodeToken(CharData([]byte("hello world")))
   300  	e.EncodeToken(EndElement{start.Name})
   301  	return nil
   302  }
   303  
   304  type MyMarshalerAttrTest struct {
   305  }
   306  
   307  var _ MarshalerAttr = (*MyMarshalerAttrTest)(nil)
   308  
   309  func (m *MyMarshalerAttrTest) MarshalXMLAttr(name Name) (Attr, error) {
   310  	return Attr{name, "hello world"}, nil
   311  }
   312  
   313  type MarshalerStruct struct {
   314  	Foo MyMarshalerAttrTest `xml:",attr"`
   315  }
   316  
   317  var (
   318  	nameAttr     = "Sarah"
   319  	ageAttr      = uint(12)
   320  	contentsAttr = "lorem ipsum"
   321  )
   322  
   323  // Unless explicitly stated as such (or *Plain), all of the
   324  // tests below are two-way tests. When introducing new tests,
   325  // please try to make them two-way as well to ensure that
   326  // marshalling and unmarshalling are as symmetrical as feasible.
   327  var marshalTests = []struct {
   328  	Value         interface{}
   329  	ExpectXML     string
   330  	MarshalOnly   bool
   331  	UnmarshalOnly bool
   332  }{
   333  	// Test nil marshals to nothing
   334  	{Value: nil, ExpectXML: ``, MarshalOnly: true},
   335  	{Value: nilStruct, ExpectXML: ``, MarshalOnly: true},
   336  
   337  	// Test value types
   338  	{Value: &Plain{true}, ExpectXML: `<Plain><V>true</V></Plain>`},
   339  	{Value: &Plain{false}, ExpectXML: `<Plain><V>false</V></Plain>`},
   340  	{Value: &Plain{int(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
   341  	{Value: &Plain{int8(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
   342  	{Value: &Plain{int16(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
   343  	{Value: &Plain{int32(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
   344  	{Value: &Plain{uint(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
   345  	{Value: &Plain{uint8(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
   346  	{Value: &Plain{uint16(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
   347  	{Value: &Plain{uint32(42)}, ExpectXML: `<Plain><V>42</V></Plain>`},
   348  	{Value: &Plain{float32(1.25)}, ExpectXML: `<Plain><V>1.25</V></Plain>`},
   349  	{Value: &Plain{float64(1.25)}, ExpectXML: `<Plain><V>1.25</V></Plain>`},
   350  	{Value: &Plain{uintptr(0xFFDD)}, ExpectXML: `<Plain><V>65501</V></Plain>`},
   351  	{Value: &Plain{"gopher"}, ExpectXML: `<Plain><V>gopher</V></Plain>`},
   352  	{Value: &Plain{[]byte("gopher")}, ExpectXML: `<Plain><V>gopher</V></Plain>`},
   353  	{Value: &Plain{"</>"}, ExpectXML: `<Plain><V>&lt;/&gt;</V></Plain>`},
   354  	{Value: &Plain{[]byte("</>")}, ExpectXML: `<Plain><V>&lt;/&gt;</V></Plain>`},
   355  	{Value: &Plain{[3]byte{'<', '/', '>'}}, ExpectXML: `<Plain><V>&lt;/&gt;</V></Plain>`},
   356  	{Value: &Plain{NamedType("potato")}, ExpectXML: `<Plain><V>potato</V></Plain>`},
   357  	{Value: &Plain{[]int{1, 2, 3}}, ExpectXML: `<Plain><V>1</V><V>2</V><V>3</V></Plain>`},
   358  	{Value: &Plain{[3]int{1, 2, 3}}, ExpectXML: `<Plain><V>1</V><V>2</V><V>3</V></Plain>`},
   359  
   360  	// Test time.
   361  	{
   362  		Value:     &Plain{time.Unix(1e9, 123456789).UTC()},
   363  		ExpectXML: `<Plain><V>2001-09-09T01:46:40.123456789Z</V></Plain>`,
   364  	},
   365  
   366  	// A pointer to struct{} may be used to test for an element's presence.
   367  	{
   368  		Value:     &PresenceTest{new(struct{})},
   369  		ExpectXML: `<PresenceTest><Exists></Exists></PresenceTest>`,
   370  	},
   371  	{
   372  		Value:     &PresenceTest{},
   373  		ExpectXML: `<PresenceTest></PresenceTest>`,
   374  	},
   375  
   376  	// A pointer to struct{} may be used to test for an element's presence.
   377  	{
   378  		Value:     &PresenceTest{new(struct{})},
   379  		ExpectXML: `<PresenceTest><Exists></Exists></PresenceTest>`,
   380  	},
   381  	{
   382  		Value:     &PresenceTest{},
   383  		ExpectXML: `<PresenceTest></PresenceTest>`,
   384  	},
   385  
   386  	// A []byte field is only nil if the element was not found.
   387  	{
   388  		Value:         &Data{},
   389  		ExpectXML:     `<Data></Data>`,
   390  		UnmarshalOnly: true,
   391  	},
   392  	{
   393  		Value:         &Data{Bytes: []byte{}, Custom: MyBytes{}, Attr: []byte{}},
   394  		ExpectXML:     `<Data Attr=""><Bytes></Bytes><Custom></Custom></Data>`,
   395  		UnmarshalOnly: true,
   396  	},
   397  
   398  	// Check that []byte works, including named []byte types.
   399  	{
   400  		Value:     &Data{Bytes: []byte("ab"), Custom: MyBytes("cd"), Attr: []byte{'v'}},
   401  		ExpectXML: `<Data Attr="v"><Bytes>ab</Bytes><Custom>cd</Custom></Data>`,
   402  	},
   403  
   404  	// Test innerxml
   405  	{
   406  		Value: &SecretAgent{
   407  			Handle:    "007",
   408  			Identity:  "James Bond",
   409  			Obfuscate: "<redacted/>",
   410  		},
   411  		ExpectXML:   `<agent handle="007"><Identity>James Bond</Identity><redacted/></agent>`,
   412  		MarshalOnly: true,
   413  	},
   414  	{
   415  		Value: &SecretAgent{
   416  			Handle:    "007",
   417  			Identity:  "James Bond",
   418  			Obfuscate: "<Identity>James Bond</Identity><redacted/>",
   419  		},
   420  		ExpectXML:     `<agent handle="007"><Identity>James Bond</Identity><redacted/></agent>`,
   421  		UnmarshalOnly: true,
   422  	},
   423  
   424  	// Test structs
   425  	{Value: &Port{Type: "ssl", Number: "443"}, ExpectXML: `<port type="ssl">443</port>`},
   426  	{Value: &Port{Number: "443"}, ExpectXML: `<port>443</port>`},
   427  	{Value: &Port{Type: "<unix>"}, ExpectXML: `<port type="&lt;unix&gt;"></port>`},
   428  	{Value: &Port{Number: "443", Comment: "https"}, ExpectXML: `<port><!--https-->443</port>`},
   429  	{Value: &Port{Number: "443", Comment: "add space-"}, ExpectXML: `<port><!--add space- -->443</port>`, MarshalOnly: true},
   430  	{Value: &Domain{Name: []byte("google.com&friends")}, ExpectXML: `<domain>google.com&amp;friends</domain>`},
   431  	{Value: &Domain{Name: []byte("google.com"), Comment: []byte(" &friends ")}, ExpectXML: `<domain>google.com<!-- &friends --></domain>`},
   432  	{Value: &Book{Title: "Pride & Prejudice"}, ExpectXML: `<book>Pride &amp; Prejudice</book>`},
   433  	{Value: &Event{Year: -3114}, ExpectXML: `<event>-3114</event>`},
   434  	{Value: &Movie{Length: 13440}, ExpectXML: `<movie>13440</movie>`},
   435  	{Value: &Pi{Approximation: 3.14159265}, ExpectXML: `<pi>3.1415927</pi>`},
   436  	{Value: &Universe{Visible: 9.3e13}, ExpectXML: `<universe>9.3e+13</universe>`},
   437  	{Value: &Particle{HasMass: true}, ExpectXML: `<particle>true</particle>`},
   438  	{Value: &Departure{When: ParseTime("2013-01-09T00:15:00-09:00")}, ExpectXML: `<departure>2013-01-09T00:15:00-09:00</departure>`},
   439  	{Value: atomValue, ExpectXML: atomXml},
   440  	{
   441  		Value: &Ship{
   442  			Name:  "Heart of Gold",
   443  			Pilot: "Computer",
   444  			Age:   1,
   445  			Drive: ImprobabilityDrive,
   446  			Passenger: []*Passenger{
   447  				{
   448  					Name:   []string{"Zaphod", "Beeblebrox"},
   449  					Weight: 7.25,
   450  				},
   451  				{
   452  					Name:   []string{"Trisha", "McMillen"},
   453  					Weight: 5.5,
   454  				},
   455  				{
   456  					Name:   []string{"Ford", "Prefect"},
   457  					Weight: 7,
   458  				},
   459  				{
   460  					Name:   []string{"Arthur", "Dent"},
   461  					Weight: 6.75,
   462  				},
   463  			},
   464  		},
   465  		ExpectXML: `<spaceship name="Heart of Gold" pilot="Computer">` +
   466  			`<drive>` + strconv.Itoa(int(ImprobabilityDrive)) + `</drive>` +
   467  			`<age>1</age>` +
   468  			`<passenger>` +
   469  			`<name>Zaphod</name>` +
   470  			`<name>Beeblebrox</name>` +
   471  			`<weight>7.25</weight>` +
   472  			`</passenger>` +
   473  			`<passenger>` +
   474  			`<name>Trisha</name>` +
   475  			`<name>McMillen</name>` +
   476  			`<weight>5.5</weight>` +
   477  			`</passenger>` +
   478  			`<passenger>` +
   479  			`<name>Ford</name>` +
   480  			`<name>Prefect</name>` +
   481  			`<weight>7</weight>` +
   482  			`</passenger>` +
   483  			`<passenger>` +
   484  			`<name>Arthur</name>` +
   485  			`<name>Dent</name>` +
   486  			`<weight>6.75</weight>` +
   487  			`</passenger>` +
   488  			`</spaceship>`,
   489  	},
   490  
   491  	// Test a>b
   492  	{
   493  		Value: &NestedItems{Items: nil, Item1: nil},
   494  		ExpectXML: `<result>` +
   495  			`<Items>` +
   496  			`</Items>` +
   497  			`</result>`,
   498  	},
   499  	{
   500  		Value: &NestedItems{Items: []string{}, Item1: []string{}},
   501  		ExpectXML: `<result>` +
   502  			`<Items>` +
   503  			`</Items>` +
   504  			`</result>`,
   505  		MarshalOnly: true,
   506  	},
   507  	{
   508  		Value: &NestedItems{Items: nil, Item1: []string{"A"}},
   509  		ExpectXML: `<result>` +
   510  			`<Items>` +
   511  			`<item1>A</item1>` +
   512  			`</Items>` +
   513  			`</result>`,
   514  	},
   515  	{
   516  		Value: &NestedItems{Items: []string{"A", "B"}, Item1: nil},
   517  		ExpectXML: `<result>` +
   518  			`<Items>` +
   519  			`<item>A</item>` +
   520  			`<item>B</item>` +
   521  			`</Items>` +
   522  			`</result>`,
   523  	},
   524  	{
   525  		Value: &NestedItems{Items: []string{"A", "B"}, Item1: []string{"C"}},
   526  		ExpectXML: `<result>` +
   527  			`<Items>` +
   528  			`<item>A</item>` +
   529  			`<item>B</item>` +
   530  			`<item1>C</item1>` +
   531  			`</Items>` +
   532  			`</result>`,
   533  	},
   534  	{
   535  		Value: &NestedOrder{Field1: "C", Field2: "B", Field3: "A"},
   536  		ExpectXML: `<result>` +
   537  			`<parent>` +
   538  			`<c>C</c>` +
   539  			`<b>B</b>` +
   540  			`<a>A</a>` +
   541  			`</parent>` +
   542  			`</result>`,
   543  	},
   544  	{
   545  		Value: &NilTest{A: "A", B: nil, C: "C"},
   546  		ExpectXML: `<NilTest>` +
   547  			`<parent1>` +
   548  			`<parent2><a>A</a></parent2>` +
   549  			`<parent2><c>C</c></parent2>` +
   550  			`</parent1>` +
   551  			`</NilTest>`,
   552  		MarshalOnly: true, // Uses interface{}
   553  	},
   554  	{
   555  		Value: &MixedNested{A: "A", B: "B", C: "C", D: "D"},
   556  		ExpectXML: `<result>` +
   557  			`<parent1><a>A</a></parent1>` +
   558  			`<b>B</b>` +
   559  			`<parent1>` +
   560  			`<parent2><c>C</c></parent2>` +
   561  			`<d>D</d>` +
   562  			`</parent1>` +
   563  			`</result>`,
   564  	},
   565  	{
   566  		Value:     &Service{Port: &Port{Number: "80"}},
   567  		ExpectXML: `<service><host><port>80</port></host></service>`,
   568  	},
   569  	{
   570  		Value:     &Service{},
   571  		ExpectXML: `<service></service>`,
   572  	},
   573  	{
   574  		Value: &Service{Port: &Port{Number: "80"}, Extra1: "A", Extra2: "B"},
   575  		ExpectXML: `<service>` +
   576  			`<host><port>80</port></host>` +
   577  			`<Extra1>A</Extra1>` +
   578  			`<host><extra2>B</extra2></host>` +
   579  			`</service>`,
   580  		MarshalOnly: true,
   581  	},
   582  	{
   583  		Value: &Service{Port: &Port{Number: "80"}, Extra2: "example"},
   584  		ExpectXML: `<service>` +
   585  			`<host><port>80</port></host>` +
   586  			`<host><extra2>example</extra2></host>` +
   587  			`</service>`,
   588  		MarshalOnly: true,
   589  	},
   590  
   591  	// Test struct embedding
   592  	{
   593  		Value: &EmbedA{
   594  			EmbedC: EmbedC{
   595  				FieldA1: "", // Shadowed by A.A
   596  				FieldA2: "", // Shadowed by A.A
   597  				FieldB:  "A.C.B",
   598  				FieldC:  "A.C.C",
   599  			},
   600  			EmbedB: EmbedB{
   601  				FieldB: "A.B.B",
   602  				EmbedC: &EmbedC{
   603  					FieldA1: "A.B.C.A1",
   604  					FieldA2: "A.B.C.A2",
   605  					FieldB:  "", // Shadowed by A.B.B
   606  					FieldC:  "A.B.C.C",
   607  				},
   608  			},
   609  			FieldA: "A.A",
   610  		},
   611  		ExpectXML: `<EmbedA>` +
   612  			`<FieldB>A.C.B</FieldB>` +
   613  			`<FieldC>A.C.C</FieldC>` +
   614  			`<EmbedB>` +
   615  			`<FieldB>A.B.B</FieldB>` +
   616  			`<FieldA>` +
   617  			`<A1>A.B.C.A1</A1>` +
   618  			`<A2>A.B.C.A2</A2>` +
   619  			`</FieldA>` +
   620  			`<FieldC>A.B.C.C</FieldC>` +
   621  			`</EmbedB>` +
   622  			`<FieldA>A.A</FieldA>` +
   623  			`</EmbedA>`,
   624  	},
   625  
   626  	// Test that name casing matters
   627  	{
   628  		Value:     &NameCasing{Xy: "mixed", XY: "upper", XyA: "mixedA", XYA: "upperA"},
   629  		ExpectXML: `<casing Xy="mixedA" XY="upperA"><Xy>mixed</Xy><XY>upper</XY></casing>`,
   630  	},
   631  
   632  	// Test the order in which the XML element name is chosen
   633  	{
   634  		Value: &NamePrecedence{
   635  			FromTag:     XMLNameWithoutTag{Value: "A"},
   636  			FromNameVal: XMLNameWithoutTag{XMLName: Name{Local: "InXMLName"}, Value: "B"},
   637  			FromNameTag: XMLNameWithTag{Value: "C"},
   638  			InFieldName: "D",
   639  		},
   640  		ExpectXML: `<Parent>` +
   641  			`<InTag>A</InTag>` +
   642  			`<InXMLName>B</InXMLName>` +
   643  			`<InXMLNameTag>C</InXMLNameTag>` +
   644  			`<InFieldName>D</InFieldName>` +
   645  			`</Parent>`,
   646  		MarshalOnly: true,
   647  	},
   648  	{
   649  		Value: &NamePrecedence{
   650  			XMLName:     Name{Local: "Parent"},
   651  			FromTag:     XMLNameWithoutTag{XMLName: Name{Local: "InTag"}, Value: "A"},
   652  			FromNameVal: XMLNameWithoutTag{XMLName: Name{Local: "FromNameVal"}, Value: "B"},
   653  			FromNameTag: XMLNameWithTag{XMLName: Name{Local: "InXMLNameTag"}, Value: "C"},
   654  			InFieldName: "D",
   655  		},
   656  		ExpectXML: `<Parent>` +
   657  			`<InTag>A</InTag>` +
   658  			`<FromNameVal>B</FromNameVal>` +
   659  			`<InXMLNameTag>C</InXMLNameTag>` +
   660  			`<InFieldName>D</InFieldName>` +
   661  			`</Parent>`,
   662  		UnmarshalOnly: true,
   663  	},
   664  
   665  	// xml.Name works in a plain field as well.
   666  	{
   667  		Value:     &NameInField{Name{Space: "ns", Local: "foo"}},
   668  		ExpectXML: `<NameInField><foo xmlns="ns"></foo></NameInField>`,
   669  	},
   670  	{
   671  		Value:         &NameInField{Name{Space: "ns", Local: "foo"}},
   672  		ExpectXML:     `<NameInField><foo xmlns="ns"><ignore></ignore></foo></NameInField>`,
   673  		UnmarshalOnly: true,
   674  	},
   675  
   676  	// Marshaling zero xml.Name uses the tag or field name.
   677  	{
   678  		Value:       &NameInField{},
   679  		ExpectXML:   `<NameInField><foo xmlns="ns"></foo></NameInField>`,
   680  		MarshalOnly: true,
   681  	},
   682  
   683  	// Test attributes
   684  	{
   685  		Value: &AttrTest{
   686  			Int:   8,
   687  			Named: 9,
   688  			Float: 23.5,
   689  			Uint8: 255,
   690  			Bool:  true,
   691  			Str:   "str",
   692  			Bytes: []byte("byt"),
   693  		},
   694  		ExpectXML: `<AttrTest Int="8" int="9" Float="23.5" Uint8="255"` +
   695  			` Bool="true" Str="str" Bytes="byt"></AttrTest>`,
   696  	},
   697  	{
   698  		Value: &AttrTest{Bytes: []byte{}},
   699  		ExpectXML: `<AttrTest Int="0" int="0" Float="0" Uint8="0"` +
   700  			` Bool="false" Str="" Bytes=""></AttrTest>`,
   701  	},
   702  	{
   703  		Value: &OmitAttrTest{
   704  			Int:   8,
   705  			Named: 9,
   706  			Float: 23.5,
   707  			Uint8: 255,
   708  			Bool:  true,
   709  			Str:   "str",
   710  			Bytes: []byte("byt"),
   711  		},
   712  		ExpectXML: `<OmitAttrTest Int="8" int="9" Float="23.5" Uint8="255"` +
   713  			` Bool="true" Str="str" Bytes="byt"></OmitAttrTest>`,
   714  	},
   715  	{
   716  		Value:     &OmitAttrTest{},
   717  		ExpectXML: `<OmitAttrTest></OmitAttrTest>`,
   718  	},
   719  
   720  	// pointer fields
   721  	{
   722  		Value:       &PointerFieldsTest{Name: &nameAttr, Age: &ageAttr, Contents: &contentsAttr},
   723  		ExpectXML:   `<dummy name="Sarah" age="12">lorem ipsum</dummy>`,
   724  		MarshalOnly: true,
   725  	},
   726  
   727  	// empty chardata pointer field
   728  	{
   729  		Value:       &ChardataEmptyTest{},
   730  		ExpectXML:   `<test></test>`,
   731  		MarshalOnly: true,
   732  	},
   733  
   734  	// omitempty on fields
   735  	{
   736  		Value: &OmitFieldTest{
   737  			Int:   8,
   738  			Named: 9,
   739  			Float: 23.5,
   740  			Uint8: 255,
   741  			Bool:  true,
   742  			Str:   "str",
   743  			Bytes: []byte("byt"),
   744  			Ptr:   &PresenceTest{},
   745  		},
   746  		ExpectXML: `<OmitFieldTest>` +
   747  			`<Int>8</Int>` +
   748  			`<int>9</int>` +
   749  			`<Float>23.5</Float>` +
   750  			`<Uint8>255</Uint8>` +
   751  			`<Bool>true</Bool>` +
   752  			`<Str>str</Str>` +
   753  			`<Bytes>byt</Bytes>` +
   754  			`<Ptr></Ptr>` +
   755  			`</OmitFieldTest>`,
   756  	},
   757  	{
   758  		Value:     &OmitFieldTest{},
   759  		ExpectXML: `<OmitFieldTest></OmitFieldTest>`,
   760  	},
   761  
   762  	// Test ",any"
   763  	{
   764  		ExpectXML: `<a><nested><value>known</value></nested><other><sub>unknown</sub></other></a>`,
   765  		Value: &AnyTest{
   766  			Nested: "known",
   767  			AnyField: AnyHolder{
   768  				XMLName: Name{Local: "other"},
   769  				XML:     "<sub>unknown</sub>",
   770  			},
   771  		},
   772  	},
   773  	{
   774  		Value: &AnyTest{Nested: "known",
   775  			AnyField: AnyHolder{
   776  				XML:     "<unknown/>",
   777  				XMLName: Name{Local: "AnyField"},
   778  			},
   779  		},
   780  		ExpectXML: `<a><nested><value>known</value></nested><AnyField><unknown/></AnyField></a>`,
   781  	},
   782  	{
   783  		ExpectXML: `<a><nested><value>b</value></nested></a>`,
   784  		Value: &AnyOmitTest{
   785  			Nested: "b",
   786  		},
   787  	},
   788  	{
   789  		ExpectXML: `<a><nested><value>b</value></nested><c><d>e</d></c><g xmlns="f"><h>i</h></g></a>`,
   790  		Value: &AnySliceTest{
   791  			Nested: "b",
   792  			AnyField: []AnyHolder{
   793  				{
   794  					XMLName: Name{Local: "c"},
   795  					XML:     "<d>e</d>",
   796  				},
   797  				{
   798  					XMLName: Name{Space: "f", Local: "g"},
   799  					XML:     "<h>i</h>",
   800  				},
   801  			},
   802  		},
   803  	},
   804  	{
   805  		ExpectXML: `<a><nested><value>b</value></nested></a>`,
   806  		Value: &AnySliceTest{
   807  			Nested: "b",
   808  		},
   809  	},
   810  
   811  	// Test recursive types.
   812  	{
   813  		Value: &RecurseA{
   814  			A: "a1",
   815  			B: &RecurseB{
   816  				A: &RecurseA{"a2", nil},
   817  				B: "b1",
   818  			},
   819  		},
   820  		ExpectXML: `<RecurseA><A>a1</A><B><A><A>a2</A></A><B>b1</B></B></RecurseA>`,
   821  	},
   822  
   823  	// Test ignoring fields via "-" tag
   824  	{
   825  		ExpectXML: `<IgnoreTest></IgnoreTest>`,
   826  		Value:     &IgnoreTest{},
   827  	},
   828  	{
   829  		ExpectXML:   `<IgnoreTest></IgnoreTest>`,
   830  		Value:       &IgnoreTest{PublicSecret: "can't tell"},
   831  		MarshalOnly: true,
   832  	},
   833  	{
   834  		ExpectXML:     `<IgnoreTest><PublicSecret>ignore me</PublicSecret></IgnoreTest>`,
   835  		Value:         &IgnoreTest{},
   836  		UnmarshalOnly: true,
   837  	},
   838  
   839  	// Test escaping.
   840  	{
   841  		ExpectXML: `<a><nested><value>dquote: &#34;; squote: &#39;; ampersand: &amp;; less: &lt;; greater: &gt;;</value></nested><empty></empty></a>`,
   842  		Value: &AnyTest{
   843  			Nested:   `dquote: "; squote: '; ampersand: &; less: <; greater: >;`,
   844  			AnyField: AnyHolder{XMLName: Name{Local: "empty"}},
   845  		},
   846  	},
   847  	{
   848  		ExpectXML: `<a><nested><value>newline: &#xA;; cr: &#xD;; tab: &#x9;;</value></nested><AnyField></AnyField></a>`,
   849  		Value: &AnyTest{
   850  			Nested:   "newline: \n; cr: \r; tab: \t;",
   851  			AnyField: AnyHolder{XMLName: Name{Local: "AnyField"}},
   852  		},
   853  	},
   854  	{
   855  		ExpectXML: "<a><nested><value>1\r2\r\n3\n\r4\n5</value></nested></a>",
   856  		Value: &AnyTest{
   857  			Nested: "1\n2\n3\n\n4\n5",
   858  		},
   859  		UnmarshalOnly: true,
   860  	},
   861  	{
   862  		ExpectXML: `<EmbedInt><MyInt>42</MyInt></EmbedInt>`,
   863  		Value: &EmbedInt{
   864  			MyInt: 42,
   865  		},
   866  	},
   867  	// Test omitempty with parent chain; see golang.org/issue/4168.
   868  	{
   869  		ExpectXML: `<Strings><A></A></Strings>`,
   870  		Value:     &Strings{},
   871  	},
   872  	// Custom marshalers.
   873  	{
   874  		ExpectXML: `<MyMarshalerTest>hello world</MyMarshalerTest>`,
   875  		Value:     &MyMarshalerTest{},
   876  	},
   877  	{
   878  		ExpectXML: `<MarshalerStruct Foo="hello world"></MarshalerStruct>`,
   879  		Value:     &MarshalerStruct{},
   880  	},
   881  }
   882  
   883  func TestMarshal(t *testing.T) {
   884  	for idx, test := range marshalTests {
   885  		if test.UnmarshalOnly {
   886  			continue
   887  		}
   888  		data, err := Marshal(test.Value)
   889  		if err != nil {
   890  			t.Errorf("#%d: Error: %s", idx, err)
   891  			continue
   892  		}
   893  		if got, want := string(data), test.ExpectXML; got != want {
   894  			if strings.Contains(want, "\n") {
   895  				t.Errorf("#%d: marshal(%#v):\nHAVE:\n%s\nWANT:\n%s", idx, test.Value, got, want)
   896  			} else {
   897  				t.Errorf("#%d: marshal(%#v):\nhave %#q\nwant %#q", idx, test.Value, got, want)
   898  			}
   899  		}
   900  	}
   901  }
   902  
   903  type AttrParent struct {
   904  	X string `xml:"X>Y,attr"`
   905  }
   906  
   907  var marshalErrorTests = []struct {
   908  	Value interface{}
   909  	Err   string
   910  	Kind  reflect.Kind
   911  }{
   912  	{
   913  		Value: make(chan bool),
   914  		Err:   "xml: unsupported type: chan bool",
   915  		Kind:  reflect.Chan,
   916  	},
   917  	{
   918  		Value: map[string]string{
   919  			"question": "What do you get when you multiply six by nine?",
   920  			"answer":   "42",
   921  		},
   922  		Err:  "xml: unsupported type: map[string]string",
   923  		Kind: reflect.Map,
   924  	},
   925  	{
   926  		Value: map[*Ship]bool{nil: false},
   927  		Err:   "xml: unsupported type: map[*xml.Ship]bool",
   928  		Kind:  reflect.Map,
   929  	},
   930  	{
   931  		Value: &Domain{Comment: []byte("f--bar")},
   932  		Err:   `xml: comments must not contain "--"`,
   933  	},
   934  	// Reject parent chain with attr, never worked; see golang.org/issue/5033.
   935  	{
   936  		Value: &AttrParent{},
   937  		Err:   `xml: X>Y chain not valid with attr flag`,
   938  	},
   939  }
   940  
   941  var marshalIndentTests = []struct {
   942  	Value     interface{}
   943  	Prefix    string
   944  	Indent    string
   945  	ExpectXML string
   946  }{
   947  	{
   948  		Value: &SecretAgent{
   949  			Handle:    "007",
   950  			Identity:  "James Bond",
   951  			Obfuscate: "<redacted/>",
   952  		},
   953  		Prefix:    "",
   954  		Indent:    "\t",
   955  		ExpectXML: fmt.Sprintf("<agent handle=\"007\">\n\t<Identity>James Bond</Identity><redacted/>\n</agent>"),
   956  	},
   957  }
   958  
   959  func TestMarshalErrors(t *testing.T) {
   960  	for idx, test := range marshalErrorTests {
   961  		data, err := Marshal(test.Value)
   962  		if err == nil {
   963  			t.Errorf("#%d: marshal(%#v) = [success] %q, want error %v", idx, test.Value, data, test.Err)
   964  			continue
   965  		}
   966  		if err.Error() != test.Err {
   967  			t.Errorf("#%d: marshal(%#v) = [error] %v, want %v", idx, test.Value, err, test.Err)
   968  		}
   969  		if test.Kind != reflect.Invalid {
   970  			if kind := err.(*UnsupportedTypeError).Type.Kind(); kind != test.Kind {
   971  				t.Errorf("#%d: marshal(%#v) = [error kind] %s, want %s", idx, test.Value, kind, test.Kind)
   972  			}
   973  		}
   974  	}
   975  }
   976  
   977  // Do invertibility testing on the various structures that we test
   978  func TestUnmarshal(t *testing.T) {
   979  	for i, test := range marshalTests {
   980  		if test.MarshalOnly {
   981  			continue
   982  		}
   983  		if _, ok := test.Value.(*Plain); ok {
   984  			continue
   985  		}
   986  
   987  		vt := reflect.TypeOf(test.Value)
   988  		dest := reflect.New(vt.Elem()).Interface()
   989  		err := Unmarshal([]byte(test.ExpectXML), dest)
   990  
   991  		switch fix := dest.(type) {
   992  		case *Feed:
   993  			fix.Author.InnerXML = ""
   994  			for i := range fix.Entry {
   995  				fix.Entry[i].Author.InnerXML = ""
   996  			}
   997  		}
   998  
   999  		if err != nil {
  1000  			t.Errorf("#%d: unexpected error: %#v", i, err)
  1001  		} else if got, want := dest, test.Value; !reflect.DeepEqual(got, want) {
  1002  			t.Errorf("#%d: unmarshal(%q):\nhave %#v\nwant %#v", i, test.ExpectXML, got, want)
  1003  		}
  1004  	}
  1005  }
  1006  
  1007  func TestMarshalIndent(t *testing.T) {
  1008  	for i, test := range marshalIndentTests {
  1009  		data, err := MarshalIndent(test.Value, test.Prefix, test.Indent)
  1010  		if err != nil {
  1011  			t.Errorf("#%d: Error: %s", i, err)
  1012  			continue
  1013  		}
  1014  		if got, want := string(data), test.ExpectXML; got != want {
  1015  			t.Errorf("#%d: MarshalIndent:\nGot:%s\nWant:\n%s", i, got, want)
  1016  		}
  1017  	}
  1018  }
  1019  
  1020  type limitedBytesWriter struct {
  1021  	w      io.Writer
  1022  	remain int // until writes fail
  1023  }
  1024  
  1025  func (lw *limitedBytesWriter) Write(p []byte) (n int, err error) {
  1026  	if lw.remain <= 0 {
  1027  		println("error")
  1028  		return 0, errors.New("write limit hit")
  1029  	}
  1030  	if len(p) > lw.remain {
  1031  		p = p[:lw.remain]
  1032  		n, _ = lw.w.Write(p)
  1033  		lw.remain = 0
  1034  		return n, errors.New("write limit hit")
  1035  	}
  1036  	n, err = lw.w.Write(p)
  1037  	lw.remain -= n
  1038  	return n, err
  1039  }
  1040  
  1041  func TestMarshalWriteErrors(t *testing.T) {
  1042  	var buf bytes.Buffer
  1043  	const writeCap = 1024
  1044  	w := &limitedBytesWriter{&buf, writeCap}
  1045  	enc := NewEncoder(w)
  1046  	var err error
  1047  	var i int
  1048  	const n = 4000
  1049  	for i = 1; i <= n; i++ {
  1050  		err = enc.Encode(&Passenger{
  1051  			Name:   []string{"Alice", "Bob"},
  1052  			Weight: 5,
  1053  		})
  1054  		if err != nil {
  1055  			break
  1056  		}
  1057  	}
  1058  	if err == nil {
  1059  		t.Error("expected an error")
  1060  	}
  1061  	if i == n {
  1062  		t.Errorf("expected to fail before the end")
  1063  	}
  1064  	if buf.Len() != writeCap {
  1065  		t.Errorf("buf.Len() = %d; want %d", buf.Len(), writeCap)
  1066  	}
  1067  }
  1068  
  1069  func TestMarshalWriteIOErrors(t *testing.T) {
  1070  	enc := NewEncoder(errWriter{})
  1071  
  1072  	expectErr := "unwritable"
  1073  	err := enc.Encode(&Passenger{})
  1074  	if err == nil || err.Error() != expectErr {
  1075  		t.Errorf("EscapeTest = [error] %v, want %v", err, expectErr)
  1076  	}
  1077  }
  1078  
  1079  func TestEncodeTokenFlush(t *testing.T) {
  1080  	var buf bytes.Buffer
  1081  	enc := NewEncoder(&buf)
  1082  	enc.EncodeToken(StartElement{Name: Name{Local: "some-tag"}})
  1083  	if g, w := buf.String(), "<some-tag>"; g != w {
  1084  		t.Errorf("Encoder wrote %q, want %q", g, w)
  1085  	}
  1086  }
  1087  
  1088  func BenchmarkMarshal(b *testing.B) {
  1089  	for i := 0; i < b.N; i++ {
  1090  		Marshal(atomValue)
  1091  	}
  1092  }
  1093  
  1094  func BenchmarkUnmarshal(b *testing.B) {
  1095  	xml := []byte(atomXml)
  1096  	for i := 0; i < b.N; i++ {
  1097  		Unmarshal(xml, &Feed{})
  1098  	}
  1099  }