forked from torbiak/gopl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
json_test.go
127 lines (119 loc) · 2.76 KB
/
json_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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
package json
import (
"bytes"
"encoding/json"
"fmt"
"reflect"
"testing"
)
// Test verifies that encoding and decoding a complex data value
// produces an equal result.
//
// The test does not make direct assertions about the encoded output
// because the output depends on map iteration order, which is
// nondeterministic. The output of the t.Log statements can be
// inspected by running the test with the -v flag:
//
// $ go test -v gopl.io/ch12/sexpr
//
func Test(t *testing.T) {
type Movie struct {
Title, Subtitle string
Year int
Actor map[string]string
Oscars []string
Sequel *string
}
strangelove := Movie{
Title: "Dr. Strangelove",
Subtitle: "How I Learned to Stop Worrying and Love the Bomb",
Year: 1964,
Actor: map[string]string{
"Dr. Strangelove": "Peter Sellers",
"Grp. Capt. Lionel Mandrake": "Peter Sellers",
"Pres. Merkin Muffley": "Peter Sellers",
"Gen. Buck Turgidson": "George C. Scott",
"Brig. Gen. Jack D. Ripper": "Sterling Hayden",
`Maj. T.J. "King" Kong`: "Slim Pickens",
},
Oscars: []string{
"Best Actor (Nomin.)",
"Best Adapted Screenplay (Nomin.)",
"Best Director (Nomin.)",
"Best Picture (Nomin.)",
},
}
// Encode it
data, err := Marshal(strangelove)
if err != nil {
t.Fatalf("Marshal failed: %v", err)
}
t.Logf("Marshal() = %s\n", data)
fmt.Println(string(data))
// Decode it
var movie Movie
if err := json.NewDecoder(bytes.NewReader(data)).Decode(&movie); err != nil {
t.Fatalf("Unmarshal failed: %v", err)
}
t.Logf("Unmarshal() = %+v\n", movie)
// Check equality.
if !reflect.DeepEqual(movie, strangelove) {
t.Fatal("not equal")
}
}
func TestBool(t *testing.T) {
tests := []struct {
v bool
want string
}{
{true, "true"},
{false, "false"},
}
for _, test := range tests {
data, err := Marshal(test.v)
if err != nil {
t.Errorf("Marshal(%s): %s", err)
}
if string(data) != test.want {
t.Errorf("Marshal(%s) got %s, wanted %s", test.v, data, test.want)
}
}
}
func TestFloat32(t *testing.T) {
tests := []struct {
v float32
want string
}{
{3.2e9, "3.2e+09"},
{1.0, "1"},
{0, "0"},
}
for _, test := range tests {
data, err := Marshal(test.v)
if err != nil {
t.Errorf("Marshal(%s): %s", err)
}
if string(data) != test.want {
t.Errorf("Marshal(%s) got %s, wanted %s", test.v, data, test.want)
}
}
}
func TestFloat64(t *testing.T) {
tests := []struct {
v float64
want string
}{
{3.2e9, "3.2e+09"},
{1.0, "1"},
{0, "0"},
}
for _, test := range tests {
data, err := Marshal(test.v)
if err != nil {
t.Errorf("Marshal(%s): %s", err)
}
if string(data) != test.want {
t.Errorf("Marshal(%s) got %s, wanted %s", test.v, data, test.want)
}
}
}