LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 package xml | 5 package xml |
6 | 6 |
7 import ( | 7 import ( |
8 "bytes" | 8 "bytes" |
9 "errors" | 9 "errors" |
10 "fmt" | 10 "fmt" |
(...skipping 258 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
269 type MyInt int | 269 type MyInt int |
270 | 270 |
271 type EmbedInt struct { | 271 type EmbedInt struct { |
272 MyInt | 272 MyInt |
273 } | 273 } |
274 | 274 |
275 type Strings struct { | 275 type Strings struct { |
276 X []string `xml:"A>B,omitempty"` | 276 X []string `xml:"A>B,omitempty"` |
277 } | 277 } |
278 | 278 |
279 type DummyNode struct { | 279 type PointerFieldsTest struct { |
280 » XMLName Name `xml:"dummy"` | 280 » XMLName Name `xml:"dummy"` |
281 » Name *string `xml:"name,attr"` | 281 » Name *string `xml:"name,attr"` |
282 » Age *uint `xml:"age,attr"` | 282 » Age *uint `xml:"age,attr"` |
283 » Empty *string `xml:"empty,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"` |
284 } | 290 } |
285 | 291 |
286 var ( | 292 var ( |
287 » nameAttr = "Sarah" | 293 » nameAttr = "Sarah" |
288 » ageAttr = uint(12) | 294 » ageAttr = uint(12) |
| 295 » contentsAttr = "lorem ipsum" |
289 ) | 296 ) |
290 | 297 |
291 // Unless explicitly stated as such (or *Plain), all of the | 298 // Unless explicitly stated as such (or *Plain), all of the |
292 // tests below are two-way tests. When introducing new tests, | 299 // tests below are two-way tests. When introducing new tests, |
293 // please try to make them two-way as well to ensure that | 300 // please try to make them two-way as well to ensure that |
294 // marshalling and unmarshalling are as symmetrical as feasible. | 301 // marshalling and unmarshalling are as symmetrical as feasible. |
295 var marshalTests = []struct { | 302 var marshalTests = []struct { |
296 Value interface{} | 303 Value interface{} |
297 ExpectXML string | 304 ExpectXML string |
298 MarshalOnly bool | 305 MarshalOnly bool |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
678 Bytes: []byte("byt"), | 685 Bytes: []byte("byt"), |
679 }, | 686 }, |
680 ExpectXML: `<OmitAttrTest Int="8" int="9" Float="23.5" Uint8="25
5"` + | 687 ExpectXML: `<OmitAttrTest Int="8" int="9" Float="23.5" Uint8="25
5"` + |
681 ` Bool="true" Str="str" Bytes="byt"></OmitAttrTest>`, | 688 ` Bool="true" Str="str" Bytes="byt"></OmitAttrTest>`, |
682 }, | 689 }, |
683 { | 690 { |
684 Value: &OmitAttrTest{}, | 691 Value: &OmitAttrTest{}, |
685 ExpectXML: `<OmitAttrTest></OmitAttrTest>`, | 692 ExpectXML: `<OmitAttrTest></OmitAttrTest>`, |
686 }, | 693 }, |
687 | 694 |
688 » // Test if attributes stored in pointers are correctly marshaled | 695 » // pointer fields |
689 » { | 696 » { |
690 » » Value: &DummyNode{Name: &nameAttr, Age: &ageAttr}, | 697 » » Value: &PointerFieldsTest{Name: &nameAttr, Age: &ageAttr,
Contents: &contentsAttr}, |
691 » » ExpectXML: `<dummy name="Sarah" age="12"></dummy>`, | 698 » » ExpectXML: `<dummy name="Sarah" age="12">lorem ipsum</dummy>`, |
| 699 » » MarshalOnly: true, |
| 700 » }, |
| 701 |
| 702 » // empty chardata pointer field |
| 703 » { |
| 704 » » Value: &ChardataEmptyTest{}, |
| 705 » » ExpectXML: `<test></test>`, |
692 MarshalOnly: true, | 706 MarshalOnly: true, |
693 }, | 707 }, |
694 | 708 |
695 // omitempty on fields | 709 // omitempty on fields |
696 { | 710 { |
697 Value: &OmitFieldTest{ | 711 Value: &OmitFieldTest{ |
698 Int: 8, | 712 Int: 8, |
699 Named: 9, | 713 Named: 9, |
700 Float: 23.5, | 714 Float: 23.5, |
701 Uint8: 255, | 715 Uint8: 255, |
(...skipping 331 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1033 Marshal(atomValue) | 1047 Marshal(atomValue) |
1034 } | 1048 } |
1035 } | 1049 } |
1036 | 1050 |
1037 func BenchmarkUnmarshal(b *testing.B) { | 1051 func BenchmarkUnmarshal(b *testing.B) { |
1038 xml := []byte(atomXml) | 1052 xml := []byte(atomXml) |
1039 for i := 0; i < b.N; i++ { | 1053 for i := 0; i < b.N; i++ { |
1040 Unmarshal(xml, &Feed{}) | 1054 Unmarshal(xml, &Feed{}) |
1041 } | 1055 } |
1042 } | 1056 } |
LEFT | RIGHT |