-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp6y_test.go
126 lines (121 loc) · 5.1 KB
/
p6y_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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package p6y
import (
"testing"
)
type testCase struct {
title string
s string
err bool
years int
months int
days int
weeks int
hours int
minutes int
seconds int
}
func TestNewDuration(t *testing.T) {
testCases := []testCase{
{"ERR no P", "1Y", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR only P", "P", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR only T", "T", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR only PT", "PT", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR trash before", "AP6Y1M2DT15H4M5S", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR trash after", "P6Y1M2DT15H4M5SA", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR trash before W", "0P1W", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR trash after W", "P1W0", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR non int Y", "P.Y", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR non int M", "P M", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR non int D", "PaD", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR non int W", "PZW", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR non int TH", "PT-H", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR non int TM", "PT'M", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR non int TS", "PT\"S", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR negative Y", "P-6Y", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR negative M", "P-001M", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR negative D", "P-2D", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR negative W", "P-8W", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR negative TH", "PT-15H", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR negative TM", "PT-04M", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR negative TS", "PT-5S", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR combine W with Y", "P1Y1W", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR combine W with M", "P1M1W", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR combine W with D", "P1W1D", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR combine W with TH", "P1WT1H", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR combine W with TM", "P1WT1M", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR combine W with TS", "P1WT1S", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR double Y", "P2Y1Y", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR double M", "P1M2M", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR double D", "P0D2D", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR double W", "P2W0W", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR double TH", "PT1H0H", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR double TM", "PT0M2M", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR double TS", "PT60S2S", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR invalid token order", "P1M1Y", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR invalid token order", "P1D1Y", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR invalid token order", "PT1S2M", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR invalid token order", "P1D1Y2M1H", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR invalid token order", "P1D1Y2H1M", true, 0, 0, 0, 0, 0, 0, 0},
{"ERR invalid token order", "P1Y1M2M1H", true, 0, 0, 0, 0, 0, 0, 0},
{"OK 0Y", "P0Y", false, 0, 0, 0, 0, 0, 0, 0},
{"OK 0M", "P0M", false, 0, 0, 0, 0, 0, 0, 0},
{"OK 0D", "P0D", false, 0, 0, 0, 0, 0, 0, 0},
{"OK 0W", "P0W", false, 0, 0, 0, 0, 0, 0, 0},
{"OK T0H", "PT0H", false, 0, 0, 0, 0, 0, 0, 0},
{"OK T0M", "PT0M", false, 0, 0, 0, 0, 0, 0, 0},
{"OK T0S", "PT0S", false, 0, 0, 0, 0, 0, 0, 0},
{"OK only 0", "P0Y0M0DT0H0M0S", false, 0, 0, 0, 0, 0, 0, 0},
{"OK 1Y", "P1Y", false, 1, 0, 0, 0, 0, 0, 0},
{"OK 1M", "P1M", false, 0, 1, 0, 0, 0, 0, 0},
{"OK 1D", "P1D", false, 0, 0, 1, 0, 0, 0, 0},
{"OK 1W", "P1W", false, 0, 0, 0, 1, 0, 0, 0},
{"OK T1H", "PT1H", false, 0, 0, 0, 0, 1, 0, 0},
{"OK T1M", "PT1M", false, 0, 0, 0, 0, 0, 1, 0},
{"OK T1S", "PT1S", false, 0, 0, 0, 0, 0, 0, 1},
{"OK 1Y", "P999Y", false, 999, 0, 0, 0, 0, 0, 0},
{"OK 1M", "P999M", false, 0, 999, 0, 0, 0, 0, 0},
{"OK 1D", "P999D", false, 0, 0, 999, 0, 0, 0, 0},
{"OK 1W", "P999W", false, 0, 0, 0, 999, 0, 0, 0},
{"OK T1H", "PT999H", false, 0, 0, 0, 0, 999, 0, 0},
{"OK T1M", "PT999M", false, 0, 0, 0, 0, 0, 999, 0},
{"OK T1S", "PT999S", false, 0, 0, 0, 0, 0, 0, 999},
{"OK date only", "P6Y1M2D", false, 6, 1, 2, 0, 0, 0, 0},
{"OK time only", "PT15H4M5S", false, 0, 0, 0, 0, 15, 4, 5},
{"OK full", "P6Y1M2DT15H4M5S", false, 6, 1, 2, 0, 15, 4, 5},
{"OK partial", "P6YT15H", false, 6, 0, 0, 0, 15, 0, 0},
{"OK partial", "P2M1DT4M5S", false, 0, 2, 1, 0, 0, 4, 5},
}
for _, tc := range testCases {
d, e := NewDuration(tc.s)
if tc.err {
if e == nil {
t.Errorf("[%s] error should not be nil", tc.title)
}
} else {
if e != nil {
t.Errorf("[%s] error should be nil, got '%s'", tc.title, e.Error())
}
}
if d.Years() != tc.years {
t.Errorf("[%s] expected years: %d, got: %d", tc.title, tc.years, d.Years())
}
if d.Months() != tc.months {
t.Errorf("[%s] expected months: %d, got: %d", tc.title, tc.months, d.Months())
}
if d.Days() != tc.days {
t.Errorf("[%s] expected days: %d, got: %d", tc.title, tc.days, d.Days())
}
if d.Weeks() != tc.weeks {
t.Errorf("[%s] expected weeks: %d, got: %d", tc.title, tc.weeks, d.Weeks())
}
if d.Hours() != tc.hours {
t.Errorf("[%s] expected hours: %d, got: %d", tc.title, tc.hours, d.Hours())
}
if d.Minutes() != tc.minutes {
t.Errorf("[%s] expected minutes: %d, got: %d", tc.title, tc.minutes, d.Minutes())
}
if d.Seconds() != tc.seconds {
t.Errorf("[%s] expected seconds: %d, got: %d", tc.title, tc.seconds, d.Seconds())
}
}
}