-
Notifications
You must be signed in to change notification settings - Fork 10
/
duration_test.go
225 lines (205 loc) · 6.19 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package types_test
import (
"encoding/json"
"fmt"
"math"
"testing"
"time"
"github.com/cedar-policy/cedar-go/internal"
"github.com/cedar-policy/cedar-go/internal/testutil"
"github.com/cedar-policy/cedar-go/types"
)
func TestDuration(t *testing.T) {
t.Parallel()
{
tests := []struct{ in, out string }{
{"1h", "1h"},
{"60m", "1h"},
{"3600s", "1h"},
{"3600000ms", "1h"},
{"24h", "1d"},
{"36h", "1d12h"},
{"1d12h", "1d12h"},
{"1d11h60m", "1d12h"},
{"1d11h59m60s", "1d12h"},
{"1d11h59m59s1000ms", "1d12h"},
{"60s60000ms", "2m"},
{"62m", "1h2m"},
{"2m3600s", "1h2m"},
{"-1h", "-1h"},
{"-60m", "-1h"},
{"-3600s", "-1h"},
{"-3600000ms", "-1h"},
{"-24h", "-1d"},
{"-36h", "-1d12h"},
{"-1d12h", "-1d12h"},
{"-1d11h60m", "-1d12h"},
{"-1d11h59m60s", "-1d12h"},
{"-1d11h59m59s1000ms", "-1d12h"},
{"-60s60000ms", "-2m"},
{"-62m", "-1h2m"},
{"-2m3600s", "-1h2m"},
}
for ti, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("%d_%s->%s", ti, tt.in, tt.out), func(t *testing.T) {
t.Parallel()
d, err := types.ParseDuration(tt.in)
testutil.OK(t, err)
testutil.Equals(t, d.String(), tt.out)
})
}
}
{
tests := []struct{ in, errStr string }{
{"", "error parsing duration value: string too short"},
{"-", "error parsing duration value: string too short"},
{"h", "error parsing duration value: string too short"},
{"3", "error parsing duration value: string too short"},
{"-m", "error parsing duration value: unit found without quantity"},
{"-1t", "error parsing duration value: unexpected character 't'"},
{"-1h1h", "error parsing duration value: unexpected unit 'h'"},
{"-3h3", "error parsing duration value: expected unit"},
{"3h-1m", "error parsing duration value: unexpected character '-'"},
{"3h1m ", "error parsing duration value: unexpected character ' '"},
{"3600ms30ms", "error parsing duration value: invalid duration"},
{"36ms30h", "error parsing duration value: invalid duration"},
{"999999999999999999999ms", "error parsing duration value: overflow"},
}
for ti, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("%d_%s->%s", ti, tt.in, tt.errStr), func(t *testing.T) {
t.Parallel()
_, err := types.ParseDuration(tt.in)
testutil.ErrorIs(t, err, internal.ErrDuration)
testutil.Equals(t, err.Error(), tt.errStr)
})
}
}
t.Run("Construct", func(t *testing.T) {
t.Parallel()
one := types.NewDurationFromMillis(1)
two := types.NewDuration(1 * time.Millisecond)
testutil.Equals(t, one.ToMilliseconds(), two.ToMilliseconds())
})
t.Run("Duration", func(t *testing.T) {
t.Parallel()
tests := []struct {
name string
in types.Duration
out time.Duration
err func(testutil.TB, error)
}{
{"ok", types.NewDuration(time.Millisecond * 42), time.Millisecond * 42, testutil.OK},
{"maxPlusOne", types.NewDurationFromMillis(math.MaxInt64/1000 + 1), 0, testutil.Error},
{"minMinusOne", types.NewDurationFromMillis(math.MinInt64/1000 - 1), 0, testutil.Error},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
out, err := tt.in.Duration()
testutil.Equals(t, out, tt.out)
tt.err(t, err)
})
}
})
t.Run("Equal", func(t *testing.T) {
t.Parallel()
one := types.NewDurationFromMillis(1)
one2 := types.NewDuration(1 * time.Millisecond)
zero := types.NewDuration(time.Duration(0))
f := types.Boolean(false)
testutil.FatalIf(t, !one.Equal(one), "%v not Equal to %v", one, one)
testutil.FatalIf(t, !one.Equal(one2), "%v not Equal to %v", one, one2)
testutil.FatalIf(t, one.Equal(zero), "%v Equal to %v", one, zero)
testutil.FatalIf(t, zero.Equal(one), "%v Equal to %v", zero, one)
testutil.FatalIf(t, zero.Equal(f), "%v Equal to %v", zero, f)
})
t.Run("LessThan", func(t *testing.T) {
t.Parallel()
one := types.NewDuration(1 * time.Millisecond)
zero := types.NewDuration(time.Duration(0))
f := types.Boolean(false)
tests := []struct {
l types.Duration
r types.Value
want bool
wantErr error
}{
{one, zero, false, nil},
{zero, one, true, nil},
{zero, zero, false, nil},
{zero, f, false, internal.ErrNotComparable},
}
for ti, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("LessThan_%d_%v<%v", ti, tt.l, tt.r), func(t *testing.T) {
t.Parallel()
got, gotErr := tt.l.LessThan(tt.r)
testutil.Equals(t, got, tt.want)
testutil.ErrorIs(t, gotErr, tt.wantErr)
})
}
})
t.Run("LessThanOrEqual", func(t *testing.T) {
t.Parallel()
one := types.NewDuration(1 * time.Millisecond)
zero := types.NewDuration(time.Duration(0))
f := types.Boolean(false)
tests := []struct {
l types.Duration
r types.Value
want bool
wantErr error
}{
{one, zero, false, nil},
{zero, one, true, nil},
{zero, zero, true, nil},
{zero, f, false, internal.ErrNotComparable},
}
for ti, tt := range tests {
tt := tt
t.Run(fmt.Sprintf("LessThanOrEqual_%d_%v<%v", ti, tt.l, tt.r), func(t *testing.T) {
t.Parallel()
got, gotErr := tt.l.LessThanOrEqual(tt.r)
testutil.Equals(t, got, tt.want)
testutil.ErrorIs(t, gotErr, tt.wantErr)
})
}
})
t.Run("ToUnit", func(t *testing.T) {
t.Parallel()
dur := types.NewDuration((26 * time.Hour) + (31 * time.Minute) + (43 * time.Second) + (17 * time.Millisecond))
testutil.Equals(t, dur.ToDays(), 1)
testutil.Equals(t, dur.ToHours(), 26)
testutil.Equals(t, dur.ToMinutes(), 1591)
testutil.Equals(t, dur.ToSeconds(), 95503)
testutil.Equals(t, dur.ToMilliseconds(), 95503017)
})
t.Run("MarshalCedar", func(t *testing.T) {
t.Parallel()
testutil.Equals(t, string(types.NewDuration(42*time.Millisecond).MarshalCedar()), `duration("42ms")`)
})
t.Run("MarshalJSON", func(t *testing.T) {
t.Parallel()
expected := `{
"__extn": {
"fn": "duration",
"arg": "42ms"
}
}`
d1 := types.NewDuration(42 * time.Millisecond)
testutil.JSONMarshalsTo(t, d1, expected)
var d2 types.Duration
err := json.Unmarshal([]byte(expected), &d2)
testutil.OK(t, err)
testutil.Equals(t, d1, d2)
})
t.Run("UnmarshalJSON/error", func(t *testing.T) {
t.Parallel()
var dt2 types.Duration
err := json.Unmarshal([]byte("{}"), &dt2)
testutil.Error(t, err)
})
}