forked from ChannelMeter/iso8601duration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathduration_test.go
44 lines (37 loc) · 996 Bytes
/
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
package duration
import (
"log"
"testing"
"time"
)
func TestFromString(t *testing.T) {
// test with bad format
_, err := ParseString("asdf")
if err != ErrBadFormat {
t.Error("Should give exception on random string")
}
// test with month
_, err = ParseString("P1M")
if err != ErrNoMonth {
t.Error("Should give exception when there's month")
}
// test with good full string
dur, err := ParseString("P1Y2DT3H4M5S")
if err != nil {
t.Error("Full string should parse properly")
}
fullDuration := 24*365*time.Hour + 2*24*time.Hour + 3*time.Hour + 4*time.Minute + 5*time.Second
log.Println(dur)
if fullDuration.Nanoseconds() != dur.Nanoseconds() {
t.Error("Full string parsing is inaccurate")
}
// test with good week string
dur, err = ParseString("P1W")
if err != nil {
t.Error("Week string should parse properly")
}
smallDuration := 7 * 24 * time.Hour
if smallDuration.Nanoseconds() != dur.Nanoseconds() {
t.Error("Week string parsing is inaccurate")
}
}