forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
94 lines (78 loc) · 2.66 KB
/
main_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// All material is licensed under the Apache License Version 2.0, January 2004
// http://www.apache.org/licenses/LICENSE-2.0
// Tests for the sample program to show how to implement the
// xml.Marshaler interface to dictate the marshaling.
package main
import (
"bytes"
"encoding/xml"
"strings"
"testing"
)
func TestEncodeZeroValueUser(t *testing.T) {
// Create a bytes buffer for our writer.
var bb bytes.Buffer
// Encode a zero value user and write the XML
// to the bytes buffer.
err := xml.NewEncoder(&bb).Encode(&User{})
if err != nil {
t.Fatal(err)
}
// Validate we received the expected response.
got := strings.TrimSpace(bb.String())
want := []string{
`<User><first_name></first_name><CreatedAt>0001-01-01T00:00:00Z</CreatedAt><Admin>false</Admin><Bio></Bio></User>`,
`<User><Bio></Bio><first_name></first_name><CreatedAt>0001-01-01T00:00:00Z</CreatedAt><Admin>false</Admin></User>`,
`<User><Admin>false</Admin><Bio></Bio><first_name></first_name><CreatedAt>0001-01-01T00:00:00Z</CreatedAt></User>`,
`<User><CreatedAt>0001-01-01T00:00:00Z</CreatedAt><Admin>false</Admin><Bio></Bio><first_name></first_name></User>`,
}
var found bool
for _, w := range want {
if got == w {
found = true
break
}
}
if !found {
t.Log("Wanted:", want)
t.Log("Got :", got)
t.Fatal("Mismatch")
}
}
func TestEncodeUser(t *testing.T) {
// Create a bytes buffer for our writer.
var bb bytes.Buffer
// Create a string variable so we can take its address.
bio := "An Awesome Coder!"
// Create a user value for Mary Jane.
u := User{
FirstName: "Mary",
LastName: "Jane",
Bio: &bio,
}
// Encode the user and write the XML to the bytes buffer.
err := xml.NewEncoder(&bb).Encode(&u)
if err != nil {
t.Fatal(err)
}
// Validate we received the expected response.
got := strings.TrimSpace(bb.String())
want := []string{
`<User><first_name>Mary</first_name><CreatedAt>0001-01-01T00:00:00Z</CreatedAt><Admin>false</Admin><Bio>An Awesome Coder!</Bio><LastName>Jane</LastName></User>`,
`<User><Admin>false</Admin><Bio>An Awesome Coder!</Bio><LastName>Jane</LastName><first_name>Mary</first_name><CreatedAt>0001-01-01T00:00:00Z</CreatedAt></User>`,
`<User><CreatedAt>0001-01-01T00:00:00Z</CreatedAt><Admin>false</Admin><Bio>An Awesome Coder!</Bio><LastName>Jane</LastName><first_name>Mary</first_name></User>`,
`<User><Bio>An Awesome Coder!</Bio><LastName>Jane</LastName><first_name>Mary</first_name><CreatedAt>0001-01-01T00:00:00Z</CreatedAt><Admin>false</Admin></User>`,
}
var found bool
for _, w := range want {
if got == w {
found = true
break
}
}
if !found {
t.Log("Wanted:", want)
t.Log("Got :", got)
t.Fatal("Mismatch")
}
}