~ubuntu-branches/ubuntu/vivid/golang/vivid

« back to all changes in this revision

Viewing changes to src/pkg/xml/marshal_test.go

  • Committer: Bazaar Package Importer
  • Author(s): Ondřej Surý
  • Date: 2011-08-03 17:04:59 UTC
  • mfrom: (14.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20110803170459-wzd99m3567y80ila
Tags: 1:59-1
* Imported Upstream version 59
* Refresh patches to a new release
* Fix FTBFS on ARM (Closes: #634270)
* Update version.bash to work with Debian packaging and not hg
  repository

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
        "reflect"
 
9
        "testing"
 
10
 
 
11
        "os"
 
12
        "bytes"
 
13
        "strings"
 
14
        "strconv"
 
15
)
 
16
 
 
17
type DriveType int
 
18
 
 
19
const (
 
20
        HyperDrive DriveType = iota
 
21
        ImprobabilityDrive
 
22
)
 
23
 
 
24
type Passenger struct {
 
25
        Name   []string `xml:"name"`
 
26
        Weight float32  `xml:"weight"`
 
27
}
 
28
 
 
29
type Ship struct {
 
30
        XMLName Name `xml:"spaceship"`
 
31
 
 
32
        Name      string       `xml:"attr"`
 
33
        Pilot     string       `xml:"attr"`
 
34
        Drive     DriveType    `xml:"drive"`
 
35
        Age       uint         `xml:"age"`
 
36
        Passenger []*Passenger `xml:"passenger"`
 
37
        secret    string
 
38
}
 
39
 
 
40
type RawXML string
 
41
 
 
42
func (rx RawXML) MarshalXML() ([]byte, os.Error) {
 
43
        return []byte(rx), nil
 
44
}
 
45
 
 
46
type NamedType string
 
47
 
 
48
type Port struct {
 
49
        XMLName Name   `xml:"port"`
 
50
        Type    string `xml:"attr"`
 
51
        Number  string `xml:"chardata"`
 
52
}
 
53
 
 
54
type Domain struct {
 
55
        XMLName Name   `xml:"domain"`
 
56
        Country string `xml:"attr"`
 
57
        Name    []byte `xml:"chardata"`
 
58
}
 
59
 
 
60
type SecretAgent struct {
 
61
        XMLName   Name   `xml:"agent"`
 
62
        Handle    string `xml:"attr"`
 
63
        Identity  string
 
64
        Obfuscate string `xml:"innerxml"`
 
65
}
 
66
 
 
67
var nilStruct *Ship
 
68
 
 
69
var marshalTests = []struct {
 
70
        Value     interface{}
 
71
        ExpectXML string
 
72
}{
 
73
        // Test nil marshals to nothing
 
74
        {Value: nil, ExpectXML: ``},
 
75
        {Value: nilStruct, ExpectXML: ``},
 
76
 
 
77
        // Test value types (no tag name, so ???)
 
78
        {Value: true, ExpectXML: `<???>true</???>`},
 
79
        {Value: int(42), ExpectXML: `<???>42</???>`},
 
80
        {Value: int8(42), ExpectXML: `<???>42</???>`},
 
81
        {Value: int16(42), ExpectXML: `<???>42</???>`},
 
82
        {Value: int32(42), ExpectXML: `<???>42</???>`},
 
83
        {Value: uint(42), ExpectXML: `<???>42</???>`},
 
84
        {Value: uint8(42), ExpectXML: `<???>42</???>`},
 
85
        {Value: uint16(42), ExpectXML: `<???>42</???>`},
 
86
        {Value: uint32(42), ExpectXML: `<???>42</???>`},
 
87
        {Value: float32(1.25), ExpectXML: `<???>1.25</???>`},
 
88
        {Value: float64(1.25), ExpectXML: `<???>1.25</???>`},
 
89
        {Value: uintptr(0xFFDD), ExpectXML: `<???>65501</???>`},
 
90
        {Value: "gopher", ExpectXML: `<???>gopher</???>`},
 
91
        {Value: []byte("gopher"), ExpectXML: `<???>gopher</???>`},
 
92
        {Value: "</>", ExpectXML: `<???>&lt;/&gt;</???>`},
 
93
        {Value: []byte("</>"), ExpectXML: `<???>&lt;/&gt;</???>`},
 
94
        {Value: [3]byte{'<', '/', '>'}, ExpectXML: `<???>&lt;/&gt;</???>`},
 
95
        {Value: NamedType("potato"), ExpectXML: `<???>potato</???>`},
 
96
        {Value: []int{1, 2, 3}, ExpectXML: `<???>1</???><???>2</???><???>3</???>`},
 
97
        {Value: [3]int{1, 2, 3}, ExpectXML: `<???>1</???><???>2</???><???>3</???>`},
 
98
 
 
99
        // Test innerxml
 
100
        {Value: RawXML("</>"), ExpectXML: `</>`},
 
101
        {
 
102
                Value: &SecretAgent{
 
103
                        Handle:    "007",
 
104
                        Identity:  "James Bond",
 
105
                        Obfuscate: "<redacted/>",
 
106
                },
 
107
                //ExpectXML: `<agent handle="007"><redacted/></agent>`,
 
108
                ExpectXML: `<agent handle="007"><Identity>James Bond</Identity><redacted/></agent>`,
 
109
        },
 
110
 
 
111
        // Test structs
 
112
        {Value: &Port{Type: "ssl", Number: "443"}, ExpectXML: `<port type="ssl">443</port>`},
 
113
        {Value: &Port{Number: "443"}, ExpectXML: `<port>443</port>`},
 
114
        {Value: &Port{Type: "<unix>"}, ExpectXML: `<port type="&lt;unix&gt;"></port>`},
 
115
        {Value: &Domain{Name: []byte("google.com&friends")}, ExpectXML: `<domain>google.com&amp;friends</domain>`},
 
116
        {Value: atomValue, ExpectXML: atomXml},
 
117
        {
 
118
                Value: &Ship{
 
119
                        Name:  "Heart of Gold",
 
120
                        Pilot: "Computer",
 
121
                        Age:   1,
 
122
                        Drive: ImprobabilityDrive,
 
123
                        Passenger: []*Passenger{
 
124
                                &Passenger{
 
125
                                        Name:   []string{"Zaphod", "Beeblebrox"},
 
126
                                        Weight: 7.25,
 
127
                                },
 
128
                                &Passenger{
 
129
                                        Name:   []string{"Trisha", "McMillen"},
 
130
                                        Weight: 5.5,
 
131
                                },
 
132
                                &Passenger{
 
133
                                        Name:   []string{"Ford", "Prefect"},
 
134
                                        Weight: 7,
 
135
                                },
 
136
                                &Passenger{
 
137
                                        Name:   []string{"Arthur", "Dent"},
 
138
                                        Weight: 6.75,
 
139
                                },
 
140
                        },
 
141
                },
 
142
                ExpectXML: `<spaceship name="Heart of Gold" pilot="Computer">` +
 
143
                        `<drive>` + strconv.Itoa(int(ImprobabilityDrive)) + `</drive>` +
 
144
                        `<age>1</age>` +
 
145
                        `<passenger>` +
 
146
                        `<name>Zaphod</name>` +
 
147
                        `<name>Beeblebrox</name>` +
 
148
                        `<weight>7.25</weight>` +
 
149
                        `</passenger>` +
 
150
                        `<passenger>` +
 
151
                        `<name>Trisha</name>` +
 
152
                        `<name>McMillen</name>` +
 
153
                        `<weight>5.5</weight>` +
 
154
                        `</passenger>` +
 
155
                        `<passenger>` +
 
156
                        `<name>Ford</name>` +
 
157
                        `<name>Prefect</name>` +
 
158
                        `<weight>7</weight>` +
 
159
                        `</passenger>` +
 
160
                        `<passenger>` +
 
161
                        `<name>Arthur</name>` +
 
162
                        `<name>Dent</name>` +
 
163
                        `<weight>6.75</weight>` +
 
164
                        `</passenger>` +
 
165
                        `</spaceship>`,
 
166
        },
 
167
}
 
168
 
 
169
func TestMarshal(t *testing.T) {
 
170
        for idx, test := range marshalTests {
 
171
                buf := bytes.NewBuffer(nil)
 
172
                err := Marshal(buf, test.Value)
 
173
                if err != nil {
 
174
                        t.Errorf("#%d: Error: %s", idx, err)
 
175
                        continue
 
176
                }
 
177
                if got, want := buf.String(), test.ExpectXML; got != want {
 
178
                        if strings.Contains(want, "\n") {
 
179
                                t.Errorf("#%d: marshal(%#v) - GOT:\n%s\nWANT:\n%s", idx, test.Value, got, want)
 
180
                        } else {
 
181
                                t.Errorf("#%d: marshal(%#v) = %#q want %#q", idx, test.Value, got, want)
 
182
                        }
 
183
                }
 
184
        }
 
185
}
 
186
 
 
187
var marshalErrorTests = []struct {
 
188
        Value      interface{}
 
189
        ExpectErr  string
 
190
        ExpectKind reflect.Kind
 
191
}{
 
192
        {
 
193
                Value:      make(chan bool),
 
194
                ExpectErr:  "xml: unsupported type: chan bool",
 
195
                ExpectKind: reflect.Chan,
 
196
        },
 
197
        {
 
198
                Value: map[string]string{
 
199
                        "question": "What do you get when you multiply six by nine?",
 
200
                        "answer":   "42",
 
201
                },
 
202
                ExpectErr:  "xml: unsupported type: map[string] string",
 
203
                ExpectKind: reflect.Map,
 
204
        },
 
205
        {
 
206
                Value:      map[*Ship]bool{nil: false},
 
207
                ExpectErr:  "xml: unsupported type: map[*xml.Ship] bool",
 
208
                ExpectKind: reflect.Map,
 
209
        },
 
210
}
 
211
 
 
212
func TestMarshalErrors(t *testing.T) {
 
213
        for idx, test := range marshalErrorTests {
 
214
                buf := bytes.NewBuffer(nil)
 
215
                err := Marshal(buf, test.Value)
 
216
                if got, want := err, test.ExpectErr; got == nil {
 
217
                        t.Errorf("#%d: want error %s", idx, want)
 
218
                        continue
 
219
                } else if got.String() != want {
 
220
                        t.Errorf("#%d: marshal(%#v) = [error] %q, want %q", idx, test.Value, got, want)
 
221
                }
 
222
                if got, want := err.(*UnsupportedTypeError).Type.Kind(), test.ExpectKind; got != want {
 
223
                        t.Errorf("#%d: marshal(%#v) = [error kind] %s, want %s", idx, test.Value, got, want)
 
224
                }
 
225
        }
 
226
}
 
227
 
 
228
// Do invertibility testing on the various structures that we test
 
229
func TestUnmarshal(t *testing.T) {
 
230
        for i, test := range marshalTests {
 
231
                // Skip the nil pointers
 
232
                if i <= 1 {
 
233
                        continue
 
234
                }
 
235
 
 
236
                var dest interface{}
 
237
 
 
238
                switch test.Value.(type) {
 
239
                case *Ship, Ship:
 
240
                        dest = &Ship{}
 
241
                case *Port, Port:
 
242
                        dest = &Port{}
 
243
                case *Domain, Domain:
 
244
                        dest = &Domain{}
 
245
                case *Feed, Feed:
 
246
                        dest = &Feed{}
 
247
                default:
 
248
                        continue
 
249
                }
 
250
 
 
251
                buffer := bytes.NewBufferString(test.ExpectXML)
 
252
                err := Unmarshal(buffer, dest)
 
253
 
 
254
                // Don't compare XMLNames
 
255
                switch fix := dest.(type) {
 
256
                case *Ship:
 
257
                        fix.XMLName = Name{}
 
258
                case *Port:
 
259
                        fix.XMLName = Name{}
 
260
                case *Domain:
 
261
                        fix.XMLName = Name{}
 
262
                case *Feed:
 
263
                        fix.XMLName = Name{}
 
264
                        fix.Author.InnerXML = ""
 
265
                        for i := range fix.Entry {
 
266
                                fix.Entry[i].Author.InnerXML = ""
 
267
                        }
 
268
                }
 
269
 
 
270
                if err != nil {
 
271
                        t.Errorf("#%d: unexpected error: %#v", i, err)
 
272
                } else if got, want := dest, test.Value; !reflect.DeepEqual(got, want) {
 
273
                        t.Errorf("#%d: unmarshal(%#s) = %#v, want %#v", i, test.ExpectXML, got, want)
 
274
                }
 
275
        }
 
276
}
 
277
 
 
278
func BenchmarkMarshal(b *testing.B) {
 
279
        idx := len(marshalTests) - 1
 
280
        test := marshalTests[idx]
 
281
 
 
282
        buf := bytes.NewBuffer(nil)
 
283
        for i := 0; i < b.N; i++ {
 
284
                Marshal(buf, test.Value)
 
285
                buf.Truncate(0)
 
286
        }
 
287
}
 
288
 
 
289
func BenchmarkUnmarshal(b *testing.B) {
 
290
        idx := len(marshalTests) - 1
 
291
        test := marshalTests[idx]
 
292
        sm := &Ship{}
 
293
        xml := []byte(test.ExpectXML)
 
294
 
 
295
        for i := 0; i < b.N; i++ {
 
296
                buffer := bytes.NewBuffer(xml)
 
297
                Unmarshal(buffer, sm)
 
298
        }
 
299
}