forked from lilith44/easy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_test.go
37 lines (30 loc) · 819 Bytes
/
time_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
package easy
import (
"encoding/json"
"testing"
"time"
)
func TestTime_MarshalJSON(t *testing.T) {
format := "2023-09-01 00:00:00"
want := `"2023-09-01 00:00:00"`
t1, _ := time.ParseInLocation(time.DateTime, format, time.Local)
t2 := Time(t1)
got, err := json.Marshal(t2)
if err != nil {
t.Fatalf("error occurs while json.Marshal: %s", err)
}
if string(got) != want {
t.Errorf("(%v).MarshalJSON() = %v, want %v", t2, string(got), want)
}
}
func TestTime_UnmarshalJSON(t *testing.T) {
data := `"2023-09-01 00:00:00"`
want := "2023-09-01 00:00:00"
var tm Time
if err := json.Unmarshal([]byte(data), &tm); err != nil {
t.Fatalf("error occurs while json.Unmarshal: %s", err)
}
if tm.Time().Format(time.DateTime) != want {
t.Errorf("(%v).UnmarshalJSON() = %v, want %v", data, tm, want)
}
}