github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/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 // Unless explicitly stated as such (or *Plain), all of the 280 // tests below are two-way tests. When introducing new tests, 281 // please try to make them two-way as well to ensure that 282 // marshalling and unmarshalling are as symmetrical as feasible. 283 var marshalTests = []struct { 284 Value interface{} 285 ExpectXML string 286 MarshalOnly bool 287 UnmarshalOnly bool 288 }{ 289 // Test nil marshals to nothing 290 {Value: nil, ExpectXML: ``, MarshalOnly: true}, 291 {Value: nilStruct, ExpectXML: ``, MarshalOnly: true}, 292 293 // Test value types 294 {Value: &Plain{true}, ExpectXML: `<Plain><V>true</V></Plain>`}, 295 {Value: &Plain{false}, ExpectXML: `<Plain><V>false</V></Plain>`}, 296 {Value: &Plain{int(42)}, ExpectXML: `<Plain><V>42</V></Plain>`}, 297 {Value: &Plain{int8(42)}, ExpectXML: `<Plain><V>42</V></Plain>`}, 298 {Value: &Plain{int16(42)}, ExpectXML: `<Plain><V>42</V></Plain>`}, 299 {Value: &Plain{int32(42)}, ExpectXML: `<Plain><V>42</V></Plain>`}, 300 {Value: &Plain{uint(42)}, ExpectXML: `<Plain><V>42</V></Plain>`}, 301 {Value: &Plain{uint8(42)}, ExpectXML: `<Plain><V>42</V></Plain>`}, 302 {Value: &Plain{uint16(42)}, ExpectXML: `<Plain><V>42</V></Plain>`}, 303 {Value: &Plain{uint32(42)}, ExpectXML: `<Plain><V>42</V></Plain>`}, 304 {Value: &Plain{float32(1.25)}, ExpectXML: `<Plain><V>1.25</V></Plain>`}, 305 {Value: &Plain{float64(1.25)}, ExpectXML: `<Plain><V>1.25</V></Plain>`}, 306 {Value: &Plain{uintptr(0xFFDD)}, ExpectXML: `<Plain><V>65501</V></Plain>`}, 307 {Value: &Plain{"gopher"}, ExpectXML: `<Plain><V>gopher</V></Plain>`}, 308 {Value: &Plain{[]byte("gopher")}, ExpectXML: `<Plain><V>gopher</V></Plain>`}, 309 {Value: &Plain{"</>"}, ExpectXML: `<Plain><V></></V></Plain>`}, 310 {Value: &Plain{[]byte("</>")}, ExpectXML: `<Plain><V></></V></Plain>`}, 311 {Value: &Plain{[3]byte{'<', '/', '>'}}, ExpectXML: `<Plain><V></></V></Plain>`}, 312 {Value: &Plain{NamedType("potato")}, ExpectXML: `<Plain><V>potato</V></Plain>`}, 313 {Value: &Plain{[]int{1, 2, 3}}, ExpectXML: `<Plain><V>1</V><V>2</V><V>3</V></Plain>`}, 314 {Value: &Plain{[3]int{1, 2, 3}}, ExpectXML: `<Plain><V>1</V><V>2</V><V>3</V></Plain>`}, 315 316 // Test time. 317 { 318 Value: &Plain{time.Unix(1e9, 123456789).UTC()}, 319 ExpectXML: `<Plain><V>2001-09-09T01:46:40.123456789Z</V></Plain>`, 320 }, 321 322 // A pointer to struct{} may be used to test for an element's presence. 323 { 324 Value: &PresenceTest{new(struct{})}, 325 ExpectXML: `<PresenceTest><Exists></Exists></PresenceTest>`, 326 }, 327 { 328 Value: &PresenceTest{}, 329 ExpectXML: `<PresenceTest></PresenceTest>`, 330 }, 331 332 // A pointer to struct{} may be used to test for an element's presence. 333 { 334 Value: &PresenceTest{new(struct{})}, 335 ExpectXML: `<PresenceTest><Exists></Exists></PresenceTest>`, 336 }, 337 { 338 Value: &PresenceTest{}, 339 ExpectXML: `<PresenceTest></PresenceTest>`, 340 }, 341 342 // A []byte field is only nil if the element was not found. 343 { 344 Value: &Data{}, 345 ExpectXML: `<Data></Data>`, 346 UnmarshalOnly: true, 347 }, 348 { 349 Value: &Data{Bytes: []byte{}, Custom: MyBytes{}, Attr: []byte{}}, 350 ExpectXML: `<Data Attr=""><Bytes></Bytes><Custom></Custom></Data>`, 351 UnmarshalOnly: true, 352 }, 353 354 // Check that []byte works, including named []byte types. 355 { 356 Value: &Data{Bytes: []byte("ab"), Custom: MyBytes("cd"), Attr: []byte{'v'}}, 357 ExpectXML: `<Data Attr="v"><Bytes>ab</Bytes><Custom>cd</Custom></Data>`, 358 }, 359 360 // Test innerxml 361 { 362 Value: &SecretAgent{ 363 Handle: "007", 364 Identity: "James Bond", 365 Obfuscate: "<redacted/>", 366 }, 367 ExpectXML: `<agent handle="007"><Identity>James Bond</Identity><redacted/></agent>`, 368 MarshalOnly: true, 369 }, 370 { 371 Value: &SecretAgent{ 372 Handle: "007", 373 Identity: "James Bond", 374 Obfuscate: "<Identity>James Bond</Identity><redacted/>", 375 }, 376 ExpectXML: `<agent handle="007"><Identity>James Bond</Identity><redacted/></agent>`, 377 UnmarshalOnly: true, 378 }, 379 380 // Test structs 381 {Value: &Port{Type: "ssl", Number: "443"}, ExpectXML: `<port type="ssl">443</port>`}, 382 {Value: &Port{Number: "443"}, ExpectXML: `<port>443</port>`}, 383 {Value: &Port{Type: "<unix>"}, ExpectXML: `<port type="<unix>"></port>`}, 384 {Value: &Port{Number: "443", Comment: "https"}, ExpectXML: `<port><!--https-->443</port>`}, 385 {Value: &Port{Number: "443", Comment: "add space-"}, ExpectXML: `<port><!--add space- -->443</port>`, MarshalOnly: true}, 386 {Value: &Domain{Name: []byte("google.com&friends")}, ExpectXML: `<domain>google.com&friends</domain>`}, 387 {Value: &Domain{Name: []byte("google.com"), Comment: []byte(" &friends ")}, ExpectXML: `<domain>google.com<!-- &friends --></domain>`}, 388 {Value: &Book{Title: "Pride & Prejudice"}, ExpectXML: `<book>Pride & Prejudice</book>`}, 389 {Value: &Event{Year: -3114}, ExpectXML: `<event>-3114</event>`}, 390 {Value: &Movie{Length: 13440}, ExpectXML: `<movie>13440</movie>`}, 391 {Value: &Pi{Approximation: 3.14159265}, ExpectXML: `<pi>3.1415927</pi>`}, 392 {Value: &Universe{Visible: 9.3e13}, ExpectXML: `<universe>9.3e+13</universe>`}, 393 {Value: &Particle{HasMass: true}, ExpectXML: `<particle>true</particle>`}, 394 {Value: &Departure{When: ParseTime("2013-01-09T00:15:00-09:00")}, ExpectXML: `<departure>2013-01-09T00:15:00-09:00</departure>`}, 395 {Value: atomValue, ExpectXML: atomXml}, 396 { 397 Value: &Ship{ 398 Name: "Heart of Gold", 399 Pilot: "Computer", 400 Age: 1, 401 Drive: ImprobabilityDrive, 402 Passenger: []*Passenger{ 403 { 404 Name: []string{"Zaphod", "Beeblebrox"}, 405 Weight: 7.25, 406 }, 407 { 408 Name: []string{"Trisha", "McMillen"}, 409 Weight: 5.5, 410 }, 411 { 412 Name: []string{"Ford", "Prefect"}, 413 Weight: 7, 414 }, 415 { 416 Name: []string{"Arthur", "Dent"}, 417 Weight: 6.75, 418 }, 419 }, 420 }, 421 ExpectXML: `<spaceship name="Heart of Gold" pilot="Computer">` + 422 `<drive>` + strconv.Itoa(int(ImprobabilityDrive)) + `</drive>` + 423 `<age>1</age>` + 424 `<passenger>` + 425 `<name>Zaphod</name>` + 426 `<name>Beeblebrox</name>` + 427 `<weight>7.25</weight>` + 428 `</passenger>` + 429 `<passenger>` + 430 `<name>Trisha</name>` + 431 `<name>McMillen</name>` + 432 `<weight>5.5</weight>` + 433 `</passenger>` + 434 `<passenger>` + 435 `<name>Ford</name>` + 436 `<name>Prefect</name>` + 437 `<weight>7</weight>` + 438 `</passenger>` + 439 `<passenger>` + 440 `<name>Arthur</name>` + 441 `<name>Dent</name>` + 442 `<weight>6.75</weight>` + 443 `</passenger>` + 444 `</spaceship>`, 445 }, 446 447 // Test a>b 448 { 449 Value: &NestedItems{Items: nil, Item1: nil}, 450 ExpectXML: `<result>` + 451 `<Items>` + 452 `</Items>` + 453 `</result>`, 454 }, 455 { 456 Value: &NestedItems{Items: []string{}, Item1: []string{}}, 457 ExpectXML: `<result>` + 458 `<Items>` + 459 `</Items>` + 460 `</result>`, 461 MarshalOnly: true, 462 }, 463 { 464 Value: &NestedItems{Items: nil, Item1: []string{"A"}}, 465 ExpectXML: `<result>` + 466 `<Items>` + 467 `<item1>A</item1>` + 468 `</Items>` + 469 `</result>`, 470 }, 471 { 472 Value: &NestedItems{Items: []string{"A", "B"}, Item1: nil}, 473 ExpectXML: `<result>` + 474 `<Items>` + 475 `<item>A</item>` + 476 `<item>B</item>` + 477 `</Items>` + 478 `</result>`, 479 }, 480 { 481 Value: &NestedItems{Items: []string{"A", "B"}, Item1: []string{"C"}}, 482 ExpectXML: `<result>` + 483 `<Items>` + 484 `<item>A</item>` + 485 `<item>B</item>` + 486 `<item1>C</item1>` + 487 `</Items>` + 488 `</result>`, 489 }, 490 { 491 Value: &NestedOrder{Field1: "C", Field2: "B", Field3: "A"}, 492 ExpectXML: `<result>` + 493 `<parent>` + 494 `<c>C</c>` + 495 `<b>B</b>` + 496 `<a>A</a>` + 497 `</parent>` + 498 `</result>`, 499 }, 500 { 501 Value: &NilTest{A: "A", B: nil, C: "C"}, 502 ExpectXML: `<NilTest>` + 503 `<parent1>` + 504 `<parent2><a>A</a></parent2>` + 505 `<parent2><c>C</c></parent2>` + 506 `</parent1>` + 507 `</NilTest>`, 508 MarshalOnly: true, // Uses interface{} 509 }, 510 { 511 Value: &MixedNested{A: "A", B: "B", C: "C", D: "D"}, 512 ExpectXML: `<result>` + 513 `<parent1><a>A</a></parent1>` + 514 `<b>B</b>` + 515 `<parent1>` + 516 `<parent2><c>C</c></parent2>` + 517 `<d>D</d>` + 518 `</parent1>` + 519 `</result>`, 520 }, 521 { 522 Value: &Service{Port: &Port{Number: "80"}}, 523 ExpectXML: `<service><host><port>80</port></host></service>`, 524 }, 525 { 526 Value: &Service{}, 527 ExpectXML: `<service></service>`, 528 }, 529 { 530 Value: &Service{Port: &Port{Number: "80"}, Extra1: "A", Extra2: "B"}, 531 ExpectXML: `<service>` + 532 `<host><port>80</port></host>` + 533 `<Extra1>A</Extra1>` + 534 `<host><extra2>B</extra2></host>` + 535 `</service>`, 536 MarshalOnly: true, 537 }, 538 { 539 Value: &Service{Port: &Port{Number: "80"}, Extra2: "example"}, 540 ExpectXML: `<service>` + 541 `<host><port>80</port></host>` + 542 `<host><extra2>example</extra2></host>` + 543 `</service>`, 544 MarshalOnly: true, 545 }, 546 547 // Test struct embedding 548 { 549 Value: &EmbedA{ 550 EmbedC: EmbedC{ 551 FieldA1: "", // Shadowed by A.A 552 FieldA2: "", // Shadowed by A.A 553 FieldB: "A.C.B", 554 FieldC: "A.C.C", 555 }, 556 EmbedB: EmbedB{ 557 FieldB: "A.B.B", 558 EmbedC: &EmbedC{ 559 FieldA1: "A.B.C.A1", 560 FieldA2: "A.B.C.A2", 561 FieldB: "", // Shadowed by A.B.B 562 FieldC: "A.B.C.C", 563 }, 564 }, 565 FieldA: "A.A", 566 }, 567 ExpectXML: `<EmbedA>` + 568 `<FieldB>A.C.B</FieldB>` + 569 `<FieldC>A.C.C</FieldC>` + 570 `<EmbedB>` + 571 `<FieldB>A.B.B</FieldB>` + 572 `<FieldA>` + 573 `<A1>A.B.C.A1</A1>` + 574 `<A2>A.B.C.A2</A2>` + 575 `</FieldA>` + 576 `<FieldC>A.B.C.C</FieldC>` + 577 `</EmbedB>` + 578 `<FieldA>A.A</FieldA>` + 579 `</EmbedA>`, 580 }, 581 582 // Test that name casing matters 583 { 584 Value: &NameCasing{Xy: "mixed", XY: "upper", XyA: "mixedA", XYA: "upperA"}, 585 ExpectXML: `<casing Xy="mixedA" XY="upperA"><Xy>mixed</Xy><XY>upper</XY></casing>`, 586 }, 587 588 // Test the order in which the XML element name is chosen 589 { 590 Value: &NamePrecedence{ 591 FromTag: XMLNameWithoutTag{Value: "A"}, 592 FromNameVal: XMLNameWithoutTag{XMLName: Name{Local: "InXMLName"}, Value: "B"}, 593 FromNameTag: XMLNameWithTag{Value: "C"}, 594 InFieldName: "D", 595 }, 596 ExpectXML: `<Parent>` + 597 `<InTag>A</InTag>` + 598 `<InXMLName>B</InXMLName>` + 599 `<InXMLNameTag>C</InXMLNameTag>` + 600 `<InFieldName>D</InFieldName>` + 601 `</Parent>`, 602 MarshalOnly: true, 603 }, 604 { 605 Value: &NamePrecedence{ 606 XMLName: Name{Local: "Parent"}, 607 FromTag: XMLNameWithoutTag{XMLName: Name{Local: "InTag"}, Value: "A"}, 608 FromNameVal: XMLNameWithoutTag{XMLName: Name{Local: "FromNameVal"}, Value: "B"}, 609 FromNameTag: XMLNameWithTag{XMLName: Name{Local: "InXMLNameTag"}, Value: "C"}, 610 InFieldName: "D", 611 }, 612 ExpectXML: `<Parent>` + 613 `<InTag>A</InTag>` + 614 `<FromNameVal>B</FromNameVal>` + 615 `<InXMLNameTag>C</InXMLNameTag>` + 616 `<InFieldName>D</InFieldName>` + 617 `</Parent>`, 618 UnmarshalOnly: true, 619 }, 620 621 // xml.Name works in a plain field as well. 622 { 623 Value: &NameInField{Name{Space: "ns", Local: "foo"}}, 624 ExpectXML: `<NameInField><foo xmlns="ns"></foo></NameInField>`, 625 }, 626 { 627 Value: &NameInField{Name{Space: "ns", Local: "foo"}}, 628 ExpectXML: `<NameInField><foo xmlns="ns"><ignore></ignore></foo></NameInField>`, 629 UnmarshalOnly: true, 630 }, 631 632 // Marshaling zero xml.Name uses the tag or field name. 633 { 634 Value: &NameInField{}, 635 ExpectXML: `<NameInField><foo xmlns="ns"></foo></NameInField>`, 636 MarshalOnly: true, 637 }, 638 639 // Test attributes 640 { 641 Value: &AttrTest{ 642 Int: 8, 643 Named: 9, 644 Float: 23.5, 645 Uint8: 255, 646 Bool: true, 647 Str: "str", 648 Bytes: []byte("byt"), 649 }, 650 ExpectXML: `<AttrTest Int="8" int="9" Float="23.5" Uint8="255"` + 651 ` Bool="true" Str="str" Bytes="byt"></AttrTest>`, 652 }, 653 { 654 Value: &AttrTest{Bytes: []byte{}}, 655 ExpectXML: `<AttrTest Int="0" int="0" Float="0" Uint8="0"` + 656 ` Bool="false" Str="" Bytes=""></AttrTest>`, 657 }, 658 { 659 Value: &OmitAttrTest{ 660 Int: 8, 661 Named: 9, 662 Float: 23.5, 663 Uint8: 255, 664 Bool: true, 665 Str: "str", 666 Bytes: []byte("byt"), 667 }, 668 ExpectXML: `<OmitAttrTest Int="8" int="9" Float="23.5" Uint8="255"` + 669 ` Bool="true" Str="str" Bytes="byt"></OmitAttrTest>`, 670 }, 671 { 672 Value: &OmitAttrTest{}, 673 ExpectXML: `<OmitAttrTest></OmitAttrTest>`, 674 }, 675 676 // omitempty on fields 677 { 678 Value: &OmitFieldTest{ 679 Int: 8, 680 Named: 9, 681 Float: 23.5, 682 Uint8: 255, 683 Bool: true, 684 Str: "str", 685 Bytes: []byte("byt"), 686 Ptr: &PresenceTest{}, 687 }, 688 ExpectXML: `<OmitFieldTest>` + 689 `<Int>8</Int>` + 690 `<int>9</int>` + 691 `<Float>23.5</Float>` + 692 `<Uint8>255</Uint8>` + 693 `<Bool>true</Bool>` + 694 `<Str>str</Str>` + 695 `<Bytes>byt</Bytes>` + 696 `<Ptr></Ptr>` + 697 `</OmitFieldTest>`, 698 }, 699 { 700 Value: &OmitFieldTest{}, 701 ExpectXML: `<OmitFieldTest></OmitFieldTest>`, 702 }, 703 704 // Test ",any" 705 { 706 ExpectXML: `<a><nested><value>known</value></nested><other><sub>unknown</sub></other></a>`, 707 Value: &AnyTest{ 708 Nested: "known", 709 AnyField: AnyHolder{ 710 XMLName: Name{Local: "other"}, 711 XML: "<sub>unknown</sub>", 712 }, 713 }, 714 }, 715 { 716 Value: &AnyTest{Nested: "known", 717 AnyField: AnyHolder{ 718 XML: "<unknown/>", 719 XMLName: Name{Local: "AnyField"}, 720 }, 721 }, 722 ExpectXML: `<a><nested><value>known</value></nested><AnyField><unknown/></AnyField></a>`, 723 }, 724 { 725 ExpectXML: `<a><nested><value>b</value></nested></a>`, 726 Value: &AnyOmitTest{ 727 Nested: "b", 728 }, 729 }, 730 { 731 ExpectXML: `<a><nested><value>b</value></nested><c><d>e</d></c><g xmlns="f"><h>i</h></g></a>`, 732 Value: &AnySliceTest{ 733 Nested: "b", 734 AnyField: []AnyHolder{ 735 { 736 XMLName: Name{Local: "c"}, 737 XML: "<d>e</d>", 738 }, 739 { 740 XMLName: Name{Space: "f", Local: "g"}, 741 XML: "<h>i</h>", 742 }, 743 }, 744 }, 745 }, 746 { 747 ExpectXML: `<a><nested><value>b</value></nested></a>`, 748 Value: &AnySliceTest{ 749 Nested: "b", 750 }, 751 }, 752 753 // Test recursive types. 754 { 755 Value: &RecurseA{ 756 A: "a1", 757 B: &RecurseB{ 758 A: &RecurseA{"a2", nil}, 759 B: "b1", 760 }, 761 }, 762 ExpectXML: `<RecurseA><A>a1</A><B><A><A>a2</A></A><B>b1</B></B></RecurseA>`, 763 }, 764 765 // Test ignoring fields via "-" tag 766 { 767 ExpectXML: `<IgnoreTest></IgnoreTest>`, 768 Value: &IgnoreTest{}, 769 }, 770 { 771 ExpectXML: `<IgnoreTest></IgnoreTest>`, 772 Value: &IgnoreTest{PublicSecret: "can't tell"}, 773 MarshalOnly: true, 774 }, 775 { 776 ExpectXML: `<IgnoreTest><PublicSecret>ignore me</PublicSecret></IgnoreTest>`, 777 Value: &IgnoreTest{}, 778 UnmarshalOnly: true, 779 }, 780 781 // Test escaping. 782 { 783 ExpectXML: `<a><nested><value>dquote: "; squote: '; ampersand: &; less: <; greater: >;</value></nested><empty></empty></a>`, 784 Value: &AnyTest{ 785 Nested: `dquote: "; squote: '; ampersand: &; less: <; greater: >;`, 786 AnyField: AnyHolder{XMLName: Name{Local: "empty"}}, 787 }, 788 }, 789 { 790 ExpectXML: `<a><nested><value>newline: 
; cr: 
; tab: 	;</value></nested><AnyField></AnyField></a>`, 791 Value: &AnyTest{ 792 Nested: "newline: \n; cr: \r; tab: \t;", 793 AnyField: AnyHolder{XMLName: Name{Local: "AnyField"}}, 794 }, 795 }, 796 { 797 ExpectXML: "<a><nested><value>1\r2\r\n3\n\r4\n5</value></nested></a>", 798 Value: &AnyTest{ 799 Nested: "1\n2\n3\n\n4\n5", 800 }, 801 UnmarshalOnly: true, 802 }, 803 { 804 ExpectXML: `<EmbedInt><MyInt>42</MyInt></EmbedInt>`, 805 Value: &EmbedInt{ 806 MyInt: 42, 807 }, 808 }, 809 // Test omitempty with parent chain; see golang.org/issue/4168. 810 { 811 ExpectXML: `<Strings><A></A></Strings>`, 812 Value: &Strings{}, 813 }, 814 } 815 816 func TestMarshal(t *testing.T) { 817 for idx, test := range marshalTests { 818 if test.UnmarshalOnly { 819 continue 820 } 821 data, err := Marshal(test.Value) 822 if err != nil { 823 t.Errorf("#%d: Error: %s", idx, err) 824 continue 825 } 826 if got, want := string(data), test.ExpectXML; got != want { 827 if strings.Contains(want, "\n") { 828 t.Errorf("#%d: marshal(%#v):\nHAVE:\n%s\nWANT:\n%s", idx, test.Value, got, want) 829 } else { 830 t.Errorf("#%d: marshal(%#v):\nhave %#q\nwant %#q", idx, test.Value, got, want) 831 } 832 } 833 } 834 } 835 836 type AttrParent struct { 837 X string `xml:"X>Y,attr"` 838 } 839 840 var marshalErrorTests = []struct { 841 Value interface{} 842 Err string 843 Kind reflect.Kind 844 }{ 845 { 846 Value: make(chan bool), 847 Err: "xml: unsupported type: chan bool", 848 Kind: reflect.Chan, 849 }, 850 { 851 Value: map[string]string{ 852 "question": "What do you get when you multiply six by nine?", 853 "answer": "42", 854 }, 855 Err: "xml: unsupported type: map[string]string", 856 Kind: reflect.Map, 857 }, 858 { 859 Value: map[*Ship]bool{nil: false}, 860 Err: "xml: unsupported type: map[*xml.Ship]bool", 861 Kind: reflect.Map, 862 }, 863 { 864 Value: &Domain{Comment: []byte("f--bar")}, 865 Err: `xml: comments must not contain "--"`, 866 }, 867 // Reject parent chain with attr, never worked; see golang.org/issue/5033. 868 { 869 Value: &AttrParent{}, 870 Err: `xml: X>Y chain not valid with attr flag`, 871 }, 872 } 873 874 var marshalIndentTests = []struct { 875 Value interface{} 876 Prefix string 877 Indent string 878 ExpectXML string 879 }{ 880 { 881 Value: &SecretAgent{ 882 Handle: "007", 883 Identity: "James Bond", 884 Obfuscate: "<redacted/>", 885 }, 886 Prefix: "", 887 Indent: "\t", 888 ExpectXML: fmt.Sprintf("<agent handle=\"007\">\n\t<Identity>James Bond</Identity><redacted/>\n</agent>"), 889 }, 890 } 891 892 func TestMarshalErrors(t *testing.T) { 893 for idx, test := range marshalErrorTests { 894 data, err := Marshal(test.Value) 895 if err == nil { 896 t.Errorf("#%d: marshal(%#v) = [success] %q, want error %v", idx, test.Value, data, test.Err) 897 continue 898 } 899 if err.Error() != test.Err { 900 t.Errorf("#%d: marshal(%#v) = [error] %v, want %v", idx, test.Value, err, test.Err) 901 } 902 if test.Kind != reflect.Invalid { 903 if kind := err.(*UnsupportedTypeError).Type.Kind(); kind != test.Kind { 904 t.Errorf("#%d: marshal(%#v) = [error kind] %s, want %s", idx, test.Value, kind, test.Kind) 905 } 906 } 907 } 908 } 909 910 // Do invertibility testing on the various structures that we test 911 func TestUnmarshal(t *testing.T) { 912 for i, test := range marshalTests { 913 if test.MarshalOnly { 914 continue 915 } 916 if _, ok := test.Value.(*Plain); ok { 917 continue 918 } 919 920 vt := reflect.TypeOf(test.Value) 921 dest := reflect.New(vt.Elem()).Interface() 922 err := Unmarshal([]byte(test.ExpectXML), dest) 923 924 switch fix := dest.(type) { 925 case *Feed: 926 fix.Author.InnerXML = "" 927 for i := range fix.Entry { 928 fix.Entry[i].Author.InnerXML = "" 929 } 930 } 931 932 if err != nil { 933 t.Errorf("#%d: unexpected error: %#v", i, err) 934 } else if got, want := dest, test.Value; !reflect.DeepEqual(got, want) { 935 t.Errorf("#%d: unmarshal(%q):\nhave %#v\nwant %#v", i, test.ExpectXML, got, want) 936 } 937 } 938 } 939 940 func TestMarshalIndent(t *testing.T) { 941 for i, test := range marshalIndentTests { 942 data, err := MarshalIndent(test.Value, test.Prefix, test.Indent) 943 if err != nil { 944 t.Errorf("#%d: Error: %s", i, err) 945 continue 946 } 947 if got, want := string(data), test.ExpectXML; got != want { 948 t.Errorf("#%d: MarshalIndent:\nGot:%s\nWant:\n%s", i, got, want) 949 } 950 } 951 } 952 953 type limitedBytesWriter struct { 954 w io.Writer 955 remain int // until writes fail 956 } 957 958 func (lw *limitedBytesWriter) Write(p []byte) (n int, err error) { 959 if lw.remain <= 0 { 960 println("error") 961 return 0, errors.New("write limit hit") 962 } 963 if len(p) > lw.remain { 964 p = p[:lw.remain] 965 n, _ = lw.w.Write(p) 966 lw.remain = 0 967 return n, errors.New("write limit hit") 968 } 969 n, err = lw.w.Write(p) 970 lw.remain -= n 971 return n, err 972 } 973 974 func TestMarshalWriteErrors(t *testing.T) { 975 var buf bytes.Buffer 976 const writeCap = 1024 977 w := &limitedBytesWriter{&buf, writeCap} 978 enc := NewEncoder(w) 979 var err error 980 var i int 981 const n = 4000 982 for i = 1; i <= n; i++ { 983 err = enc.Encode(&Passenger{ 984 Name: []string{"Alice", "Bob"}, 985 Weight: 5, 986 }) 987 if err != nil { 988 break 989 } 990 } 991 if err == nil { 992 t.Error("expected an error") 993 } 994 if i == n { 995 t.Errorf("expected to fail before the end") 996 } 997 if buf.Len() != writeCap { 998 t.Errorf("buf.Len() = %d; want %d", buf.Len(), writeCap) 999 } 1000 } 1001 1002 func TestMarshalWriteIOErrors(t *testing.T) { 1003 enc := NewEncoder(errWriter{}) 1004 1005 expectErr := "unwritable" 1006 err := enc.Encode(&Passenger{}) 1007 if err == nil || err.Error() != expectErr { 1008 t.Errorf("EscapeTest = [error] %v, want %v", err, expectErr) 1009 } 1010 } 1011 1012 func BenchmarkMarshal(b *testing.B) { 1013 for i := 0; i < b.N; i++ { 1014 Marshal(atomValue) 1015 } 1016 } 1017 1018 func BenchmarkUnmarshal(b *testing.B) { 1019 xml := []byte(atomXml) 1020 for i := 0; i < b.N; i++ { 1021 Unmarshal(xml, &Feed{}) 1022 } 1023 }