forked from hrfee/mediabrowser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtime_test.go
50 lines (45 loc) · 1.33 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
package mediabrowser
import (
"testing"
"time"
)
func TestUnmarshalJSON(t *testing.T) {
parseFmt := "2006-01-02T15:04:05"
tests := map[string]time.Time{}
tests["2021-01-27T03:16:36.28538ZZ"], _ = time.Parse(parseFmt, "2021-01-27T03:16:36")
tests["\"2021-01-27T03:16:36.28538ZZ\""], _ = time.Parse(parseFmt, "2021-01-27T03:16:36")
tests["\"2021-01-27T03:16:36.28538Z\""], _ = time.Parse(parseFmt, "2021-01-27T03:16:36")
tests["\"2021-01-09T20:58:41.5907920+00:00\""], _ = time.Parse(parseFmt, "2021-01-09T20:58:41")
for in, expected := range tests {
parsed := Time{}
err := parsed.UnmarshalJSON([]byte(in))
if err != nil {
t.Errorf("%s failed to parse with error: %v", in, err)
continue
}
if parsed.Time != expected {
t.Errorf("%s parsed incorrectly to %v, should have been %v", in, parsed.Time, expected)
}
}
}
func benchUnmarshalJSON(tests []string, b *testing.B) {
t := Time{}
for n := 0; n < b.N; n++ {
for _, s := range tests {
t.UnmarshalJSON([]byte(s))
}
}
}
func BenchmarkUnmarshalJSON1(b *testing.B) {
benchUnmarshalJSON([]string{
"\"2021-01-27T03:16:36.28538Z\"",
}, b)
}
func BenchmarkUnmarshalJSON4(b *testing.B) {
benchUnmarshalJSON([]string{
"2021-01-27T03:16:36.28538ZZ",
"\"2021-01-27T03:16:36.28538ZZ\"",
"\"2021-01-27T03:16:36.28538Z\"",
"\"2021-01-09T20:58:41.5907920+00:00\"",
}, b)
}