OSDN Git Service

ec7f478bec3fdff5fbe049660402232dd3098748
[pf3gnuchains/gcc-fork.git] / libgo / go / encoding / xml / embed_test.go
1 // Copyright 2010 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 "testing"
8
9 type C struct {
10         Name string
11         Open bool
12 }
13
14 type A struct {
15         XMLName Name `xml:"http://domain a"`
16         C
17         B      B
18         FieldA string
19 }
20
21 type B struct {
22         XMLName Name `xml:"b"`
23         C
24         FieldB string
25 }
26
27 const _1a = `
28 <?xml version="1.0" encoding="UTF-8"?>
29 <a xmlns="http://domain">
30   <name>KmlFile</name>
31   <open>1</open>
32   <b>
33     <name>Absolute</name>
34     <open>0</open>
35     <fieldb>bar</fieldb>
36   </b>
37   <fielda>foo</fielda>
38 </a>
39 `
40
41 // Tests that embedded structs are marshalled.
42 func TestEmbedded1(t *testing.T) {
43         var a A
44         if e := Unmarshal(StringReader(_1a), &a); e != nil {
45                 t.Fatalf("Unmarshal: %s", e)
46         }
47         if a.FieldA != "foo" {
48                 t.Fatalf("Unmarshal: expected 'foo' but found '%s'", a.FieldA)
49         }
50         if a.Name != "KmlFile" {
51                 t.Fatalf("Unmarshal: expected 'KmlFile' but found '%s'", a.Name)
52         }
53         if !a.Open {
54                 t.Fatal("Unmarshal: expected 'true' but found otherwise")
55         }
56         if a.B.FieldB != "bar" {
57                 t.Fatalf("Unmarshal: expected 'bar' but found '%s'", a.B.FieldB)
58         }
59         if a.B.Name != "Absolute" {
60                 t.Fatalf("Unmarshal: expected 'Absolute' but found '%s'", a.B.Name)
61         }
62         if a.B.Open {
63                 t.Fatal("Unmarshal: expected 'false' but found otherwise")
64         }
65 }
66
67 type A2 struct {
68         XMLName Name `xml:"http://domain a"`
69         XY      string
70         Xy      string
71 }
72
73 const _2a = `
74 <?xml version="1.0" encoding="UTF-8"?>
75 <a xmlns="http://domain">
76   <xy>foo</xy>
77 </a>
78 `
79
80 // Tests that conflicting field names get excluded.
81 func TestEmbedded2(t *testing.T) {
82         var a A2
83         if e := Unmarshal(StringReader(_2a), &a); e != nil {
84                 t.Fatalf("Unmarshal: %s", e)
85         }
86         if a.XY != "" {
87                 t.Fatalf("Unmarshal: expected empty string but found '%s'", a.XY)
88         }
89         if a.Xy != "" {
90                 t.Fatalf("Unmarshal: expected empty string but found '%s'", a.Xy)
91         }
92 }
93
94 type A3 struct {
95         XMLName Name `xml:"http://domain a"`
96         xy      string
97 }
98
99 // Tests that private fields are not set.
100 func TestEmbedded3(t *testing.T) {
101         var a A3
102         if e := Unmarshal(StringReader(_2a), &a); e != nil {
103                 t.Fatalf("Unmarshal: %s", e)
104         }
105         if a.xy != "" {
106                 t.Fatalf("Unmarshal: expected empty string but found '%s'", a.xy)
107         }
108 }
109
110 type A4 struct {
111         XMLName Name `xml:"http://domain a"`
112         Any     string
113 }
114
115 // Tests that private fields are not set.
116 func TestEmbedded4(t *testing.T) {
117         var a A4
118         if e := Unmarshal(StringReader(_2a), &a); e != nil {
119                 t.Fatalf("Unmarshal: %s", e)
120         }
121         if a.Any != "foo" {
122                 t.Fatalf("Unmarshal: expected 'foo' but found '%s'", a.Any)
123         }
124 }