-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff_value_test.go
More file actions
84 lines (69 loc) · 2.47 KB
/
Copy pathdiff_value_test.go
File metadata and controls
84 lines (69 loc) · 2.47 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
package chronos_test
import (
"testing"
"time"
"github.com/gomooth/chronos"
"github.com/stretchr/testify/assert"
)
func TestDiffValueConversions(t *testing.T) {
// 1 hour = 3600 seconds = 3600 * 1e9 nanoseconds
oneHour := chronos.DiffValue(3600 * 1e9)
t.Run("Nanoseconds", func(t *testing.T) {
assert.Equal(t, int64(3600*1e9), oneHour.Nanoseconds())
})
t.Run("Microseconds", func(t *testing.T) {
assert.Equal(t, int64(3600*1e6), oneHour.Microseconds())
})
t.Run("Milliseconds", func(t *testing.T) {
assert.Equal(t, int64(3600*1e3), oneHour.Milliseconds())
})
t.Run("Seconds", func(t *testing.T) {
assert.Equal(t, int64(3600), oneHour.Seconds())
})
t.Run("Minutes", func(t *testing.T) {
assert.Equal(t, int64(60), oneHour.Minutes())
})
t.Run("Hours", func(t *testing.T) {
assert.Equal(t, int64(1), oneHour.Hours())
})
}
func TestDiffValueString(t *testing.T) {
tests := []struct {
name string
value chronos.DiffValue
expected string
}{
{"zero", chronos.DiffValue(0), "0ns"},
{"1 nanosecond", chronos.DiffValue(1), "1ns"},
{"nanoseconds", chronos.DiffValue(500), "500ns"},
{"microseconds", chronos.DiffValue(5 * 1e3), "5μs"},
{"milliseconds", chronos.DiffValue(5 * 1e6), "5ms"},
{"seconds", chronos.DiffValue(30 * 1e9), "30s"},
{"59 seconds", chronos.DiffValue(59 * 1e9), "59s"},
{"60 seconds shows minutes", chronos.DiffValue(60 * 1e9), "1m"},
{"minutes + seconds", chronos.DiffValue(90 * 1e9), "1m30s"},
{"120 seconds omits zero seconds", chronos.DiffValue(120 * 1e9), "2m"},
{"3600 seconds shows hours", chronos.DiffValue(3600 * 1e9), "1h"},
{"hours + minutes + seconds", chronos.DiffValue(3661 * 1e9), "1h1m1s"},
{"hours + seconds no minutes", chronos.DiffValue(3659 * 1e9), "1h59s"},
{"negative seconds", chronos.DiffValue(-5 * 1e9), "-5s"},
{"negative minutes", chronos.DiffValue(-90 * 1e9), "-1m30s"},
{"negative 1 nanosecond", chronos.DiffValue(-1), "-1ns"},
{"zero", chronos.DiffValue(0), "0ns"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
assert.Equal(t, tt.expected, tt.value.String())
})
}
}
func TestDiffZeroValue(t *testing.T) {
t.Run("zero time returns zero diff", func(t *testing.T) {
result := chronos.Diff(time.Time{}, time.Date(2023, 5, 15, 0, 0, 0, 0, time.UTC))
assert.Equal(t, chronos.DiffValue(0), result)
})
t.Run("both zero returns zero diff", func(t *testing.T) {
result := chronos.Diff(time.Time{}, time.Time{})
assert.Equal(t, chronos.DiffValue(0), result)
})
}