-
-
Notifications
You must be signed in to change notification settings - Fork 40
/
types_test.go
102 lines (93 loc) · 2.54 KB
/
types_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
package gerrit_test
import (
"bytes"
"encoding/json"
"reflect"
"testing"
"time"
"github.com/andygrunwald/go-gerrit"
)
func TestTimestamp(t *testing.T) {
const jsonData = `{
"subject": "net/http: write status code in Redirect when Content-Type header set",
"created": "2018-05-04 17:24:39.000000000",
"updated": "0001-01-01 00:00:00.000000000",
"submitted": "2018-05-04 18:01:10.000000000",
"_number": 111517
}
`
type ChangeInfo struct {
Subject string `json:"subject"`
Created gerrit.Timestamp `json:"created"`
Updated gerrit.Timestamp `json:"updated"`
Submitted *gerrit.Timestamp `json:"submitted,omitempty"`
Omitted *gerrit.Timestamp `json:"omitted,omitempty"`
Number int `json:"_number"`
}
ci := ChangeInfo{
Subject: "net/http: write status code in Redirect when Content-Type header set",
Created: gerrit.Timestamp{Time: time.Date(2018, 5, 4, 17, 24, 39, 0, time.UTC)},
Updated: gerrit.Timestamp{},
Submitted: &gerrit.Timestamp{Time: time.Date(2018, 5, 4, 18, 1, 10, 0, time.UTC)},
Omitted: nil,
Number: 111517,
}
// Try decoding JSON data into a ChangeInfo struct.
var v ChangeInfo
err := json.Unmarshal([]byte(jsonData), &v)
if err != nil {
t.Fatal(err)
}
if got, want := v, ci; !reflect.DeepEqual(got, want) {
t.Errorf("decoding JSON data into a ChangeInfo struct:\ngot:\n%v\nwant:\n%v", got, want)
}
// Try encoding a ChangeInfo struct into JSON data.
var buf bytes.Buffer
e := json.NewEncoder(&buf)
e.SetIndent("", "\t")
err = e.Encode(ci)
if err != nil {
t.Fatal(err)
}
if got, want := buf.String(), jsonData; got != want {
t.Errorf("encoding a ChangeInfo struct into JSON data:\ngot:\n%v\nwant:\n%v", got, want)
}
}
func TestTypesNumber_String(t *testing.T) {
number := gerrit.Number("7")
if number.String() != "7" {
t.Fatalf("%s != 7", number.String())
}
}
func TestTypesNumber_Int(t *testing.T) {
number := gerrit.Number("7")
integer, err := number.Int()
if err != nil {
t.Fatal(err)
}
if integer != 7 {
t.Fatalf("%d != 7", integer)
}
}
func TestTypesNumber_UnmarshalJSON_String(t *testing.T) {
var number gerrit.Number
if err := json.Unmarshal([]byte(`"7"`), &number); err != nil {
t.Fatal(err)
}
if number.String() != "7" {
t.Fatalf("%s != 7", number.String())
}
}
func TestTypesNumber_UnmarshalJSON_Int(t *testing.T) {
var number gerrit.Number
if err := json.Unmarshal([]byte("7"), &number); err != nil {
t.Fatal(err)
}
integer, err := number.Int()
if err != nil {
t.Fatal(err)
}
if integer != 7 {
t.Fatalf("%d != 7", integer)
}
}