-
Notifications
You must be signed in to change notification settings - Fork 0
/
duration_test.go
71 lines (67 loc) · 2.1 KB
/
duration_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
package vast
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestDurationMarshaler(t *testing.T) {
b, err := Duration(0).MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "00:00:00", string(b))
}
b, err = Duration(2 * time.Millisecond).MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "00:00:00.002", string(b))
}
b, err = Duration(2 * time.Second).MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "00:00:02", string(b))
}
b, err = Duration(2 * time.Minute).MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "00:02:00", string(b))
}
b, err = Duration(2 * time.Hour).MarshalText()
if assert.NoError(t, err) {
assert.Equal(t, "02:00:00", string(b))
}
}
func TestDurationUnmarshal(t *testing.T) {
var d Duration
if assert.NoError(t, d.UnmarshalText([]byte("00:00:00"))) {
assert.Equal(t, Duration(0), d)
}
d = 0
if assert.NoError(t, d.UnmarshalText([]byte("00:00:02"))) {
assert.Equal(t, Duration(2*time.Second), d)
}
d = 0
if assert.NoError(t, d.UnmarshalText([]byte(" 00:00:02 "))) {
assert.Equal(t, Duration(2*time.Second), d)
}
d = 0
if assert.NoError(t, d.UnmarshalText([]byte("00:02:00"))) {
assert.Equal(t, Duration(2*time.Minute), d)
}
d = 0
if assert.NoError(t, d.UnmarshalText([]byte("02:00:00"))) {
assert.Equal(t, Duration(2*time.Hour), d)
}
d = 0
if assert.NoError(t, d.UnmarshalText([]byte("00:00:00.123"))) {
assert.Equal(t, Duration(123*time.Millisecond), d)
}
d = 0
if assert.NoError(t, d.UnmarshalText([]byte("undefined"))) {
assert.Equal(t, Duration(0), d)
}
d = 0
if assert.NoError(t, d.UnmarshalText([]byte(""))) {
assert.Equal(t, Duration(0), d)
}
assert.EqualError(t, d.UnmarshalText([]byte("00:00:60")), "invalid duration: 00:00:60")
assert.EqualError(t, d.UnmarshalText([]byte("00:60:00")), "invalid duration: 00:60:00")
assert.EqualError(t, d.UnmarshalText([]byte("00:00:00.-1")), "invalid duration: 00:00:00.-1")
assert.EqualError(t, d.UnmarshalText([]byte("00:00:00.1000")), "invalid duration: 00:00:00.1000")
assert.EqualError(t, d.UnmarshalText([]byte("00h01m")), "invalid duration: 00h01m")
}