-
Notifications
You must be signed in to change notification settings - Fork 1
/
time_test.go
80 lines (70 loc) · 1.77 KB
/
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
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
package spectest_test
import (
"fmt"
"testing"
"time"
"github.com/nao1215/spectest"
"github.com/tenntenn/testtime"
)
func TestIntervalDuration(t *testing.T) {
type fields struct {
start time.Time
end time.Time
}
tests := []struct {
name string
fields fields
want time.Duration
}{
{
name: "get 1[s] duration",
fields: fields{
start: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2023, 1, 1, 0, 0, 1, 0, time.UTC),
},
want: time.Second,
},
{
name: "get 1[ns] duration",
fields: fields{
start: time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC),
end: time.Date(2023, 1, 1, 0, 0, 0, 1, time.UTC),
},
want: time.Nanosecond,
},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
interval := spectest.NewInterval()
if !testtime.SetTime(t, tt.fields.start) {
t.Fatal("failed to set start time")
}
interval.Start()
if !testtime.SetTime(t, tt.fields.end) {
t.Fatal("failed to set end time")
}
interval.End()
if interval.Duration() != tt.want {
t.Errorf("duration should be 1 second, but %s", interval.Duration())
}
})
}
}
// nolint
func ExampleInterval_Duration() {
t := &testing.T{}
interval := spectest.NewInterval()
// Set started time. Usually, you don't need to set the time. You only call Start() method.
if !testtime.SetTime(t, time.Date(2023, 1, 1, 0, 0, 0, 0, time.UTC)) {
t.Fatal("failed to set start time")
}
interval.Start()
// Set finished time. Usually, you don't need to set the time. You only call End() method.
if !testtime.SetTime(t, time.Date(2023, 1, 1, 0, 0, 1, 0, time.UTC)) {
t.Fatal("failed to set end time")
}
interval.End()
fmt.Printf("duration=%f[s]", interval.Duration().Seconds())
// Output: duration=1.000000[s]
}