forked from ardanlabs/gotraining
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_test.go
61 lines (50 loc) · 1.4 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
// 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 use the JSON encoder.
package main
import (
"bytes"
"encoding/json"
"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 JSON
// to the bytes buffer.
err := json.NewEncoder(&bb).Encode(&User{})
if err != nil {
t.Fatal(err)
}
// Validate we received the expected response.
got := strings.TrimSpace(bb.String())
want := `{"first_name":"","CreatedAt":"0001-01-01T00:00:00Z","Admin":false,"Bio":null}`
if got != want {
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 user value for Mary Jane.
u := User{
FirstName: "Mary",
LastName: "Jane",
}
// Encode the user and write the JSON to the bytes buffer.
err := json.NewEncoder(&bb).Encode(&u)
if err != nil {
t.Fatal(err)
}
// Validate we received the expected response.
got := strings.TrimSpace(bb.String())
want := `{"first_name":"Mary","LastName":"Jane","CreatedAt":"0001-01-01T00:00:00Z","Admin":false,"Bio":null}`
if got != want {
t.Log("Wanted:", want)
t.Log("Got :", got)
t.Fatal("Mismatch")
}
}