forked from educlos/testrail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timespan_test.go
92 lines (80 loc) · 1.85 KB
/
timespan_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
81
82
83
84
85
86
87
88
89
90
91
92
package resttail
import (
"encoding/json"
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestTimespanUnmarshal(t *testing.T) {
var testData = []struct{ json, stringDuration string }{
{`null`, "0s"},
{`"15s"`, "15s"},
{`"12m"`, "12m"},
{`"11h"`, "11h"},
{`"4h 5m 6s"`, "4h5m6s"},
{`"1d"`, "8h"},
{`"1w"`, "40h"},
{`"1d 2h"`, "8h2h"},
{`"1w 2d 3h"`, "40h16h3h"},
}
for _, data := range testData {
var results []Result
js := []byte(fmt.Sprintf(`[{"elapsed":%v}]`, data.json))
if err := json.Unmarshal(js, &results); err != nil {
t.Fatal(err)
}
r := results[0]
expected, err := time.ParseDuration(data.stringDuration)
if err != nil {
t.Fatal(err)
}
if r.Elapsed.Duration != expected {
t.Fatalf("Wrong duration: %v", r.Elapsed.Duration)
}
}
}
func TestTimespanMarshal(t *testing.T) {
var testData = []struct{ json, stringDuration string }{
{`null`, "0s"},
{`"0h 0m 15s"`, "15s"},
{`"0h 12m 0s"`, "12m"},
{`"11h 0m 0s"`, "11h"},
{`"4h 5m 6s"`, "4h5m6s"},
{`"8h 0m 0s"`, "8h"},
{`"40h 0m 0s"`, "40h"},
{`"10h 0m 0s"`, "8h2h"},
{`"59h 0m 0s"`, "40h16h3h"},
}
for _, td := range testData {
duration, err := time.ParseDuration(td.stringDuration)
if err != nil {
t.Fatal(err)
}
result := Result{
Elapsed: timespan{
Duration: duration,
},
}
data, err := json.Marshal(result.Elapsed)
if err != nil {
t.Fatal(err)
}
if string(data) != td.json {
t.Fatalf("Wrong data: %v", string(data))
}
}
}
func TestTimespanFromDurationValidDuration(t *testing.T) {
start := time.Now()
time.Sleep(5 * time.Millisecond)
d := time.Since(start)
ts := TimespanFromDuration(d)
assert.NotNil(t, ts)
assert.Equal(t, d, ts.Duration)
}
func TestTimespanFromDurationInvalidDuration(t *testing.T) {
d, _ := time.ParseDuration("0s")
ts := TimespanFromDuration(d)
assert.Nil(t, ts)
}