-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtime_test.go
More file actions
124 lines (114 loc) · 2.29 KB
/
time_test.go
File metadata and controls
124 lines (114 loc) · 2.29 KB
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
package channels
import (
"encoding/json"
"fmt"
"testing"
)
func Test_Period(t *testing.T) {
tests := []struct {
s string
v Period
}{
{
s: "",
v: Period{
Count: 0,
Type: PeriodTypeUnspecified,
},
},
{
s: "5 days",
v: Period{
Count: 5,
Type: PeriodTypeDay,
},
},
{
s: "week",
v: Period{
Count: 1,
Type: PeriodTypeWeek,
},
},
{
s: "month",
v: Period{
Count: 1,
Type: PeriodTypeMonth,
},
},
{
s: "2 months",
v: Period{
Count: 2,
Type: PeriodTypeMonth,
},
},
{
s: "2 years",
v: Period{
Count: 2,
Type: PeriodTypeYear,
},
},
{
s: "100 seconds",
v: Period{
Count: 100,
Type: PeriodTypeSecond,
},
},
{
s: "1000 minutes",
v: Period{
Count: 1000,
Type: PeriodTypeMinute,
},
},
}
for _, tt := range tests {
t.Run(tt.s, func(t *testing.T) {
t.Logf("String : %s", tt.v.String())
if tt.v.String() != tt.s {
t.Errorf("Wrong string value : got %s, want %s", tt.v.String(), tt.s)
}
js, err := json.Marshal(tt.v)
if err != nil {
t.Fatalf("Failed to marshal json : %s", err)
}
t.Logf("JSON : %s", js)
if tt.v.Type == PeriodTypeUnspecified {
if string(js) != "null" {
t.Errorf("Wrong JSON : got %s, want %s", js, "null")
}
} else if string(js) != fmt.Sprintf("\"%s\"", tt.s) {
t.Errorf("Wrong JSON : got %s, want %s", js, fmt.Sprintf("\"%s\"", tt.s))
}
v := &Period{}
if err := json.Unmarshal(js, v); err != nil {
t.Fatalf("Failed to unmarshal json : %s", err)
}
if v.Count != tt.v.Count {
t.Errorf("Wrong json unmarshal count : got %d, want %d", v.Count, tt.v.Count)
}
if v.Type != tt.v.Type {
t.Errorf("Wrong json unmarshal type : got %d, want %d", v.Type, tt.v.Type)
}
b, err := tt.v.MarshalBinary()
if err != nil {
t.Fatalf("Failed to marshal binary : %s", err)
}
t.Logf("Binary : 0x%x", b)
v = &Period{}
if err := v.UnmarshalBinary(b); err != nil {
t.Fatalf("Failed to unmarshal binary : %s", err)
}
if v.Count != tt.v.Count {
t.Errorf("Wrong binary unmarshal count : got %d, want %d", v.Count, tt.v.Count)
}
if v.Type != tt.v.Type {
t.Errorf("Wrong binary unmarshal type : got %d, want %d", v.Type, tt.v.Type)
}
})
}
}