-
Notifications
You must be signed in to change notification settings - Fork 0
/
epoch_test.go
43 lines (39 loc) · 1014 Bytes
/
epoch_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
package epoch
import (
"encoding/json"
. "github.com/smartystreets/goconvey/convey"
"log"
"testing"
"time"
)
type Person struct {
Name string `json:"name"`
BirthTime Time `json:"birth_time"`
}
func TestTimeMarshal(t *testing.T) {
Convey("Given a struct", t, func() {
p := Person{
Name: "Kevin Chen",
BirthTime: Time(time.Date(1993, 04, 17, 23, 0, 0, 0, time.UTC)),
}
b, err := json.Marshal(p)
So(err, ShouldBeNil)
s := string(b)
log.Println(s)
So(s, ShouldEqual, `{"name":"Kevin Chen","birth_time":735087600}`)
})
}
func TestTimeUnmarshal(t *testing.T) {
Convey("Given a JSON string", t, func() {
p := Person{}
err := json.Unmarshal([]byte(`{"name":"Kevin Chen","birth_time":735087600}`), &p)
So(err, ShouldBeNil)
So(p.Name, ShouldEqual, "Kevin Chen")
So(p.BirthTime, ShouldNotBeNil)
t := time.Time(p.BirthTime)
So(t.Year(), ShouldEqual, 1993)
So(t.Month(), ShouldEqual, time.April)
So(t.Day(), ShouldEqual, 17)
So(t.Hour(), ShouldEqual, 23)
})
}