Debian Codegraph @

common/types/duration_test.go

raw
1 // Copyright 2018 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package types
16
17 import (
18 "math"
19 "reflect"
20 "testing"
21 "time"
22
23 "google.golang.org/protobuf/proto"
24
25 "github.com/google/cel-go/common/overloads"
26 "github.com/google/cel-go/common/types/ref"
27
28 anypb "google.golang.org/protobuf/types/known/anypb"
29 dpb "google.golang.org/protobuf/types/known/durationpb"
30 structpb "google.golang.org/protobuf/types/known/structpb"
31 )
32
33 func TestDurationOperators(t *testing.T) {
34 d := duration(7506, 567)
35 dSecond := duration(1, 0)
36 dNano := duration(0, 1)
37 dMax := duration(0, math.MaxInt64)
38 dMin := duration(0, math.MinInt64)
39
40 tests := []struct {
41 name string
42 op func() ref.Val
43 out any
44 }{
45 // Addition tests.
46 {
47 name: "DurationAddSelf",
48 op: func() ref.Val {
49 return durationOf(d).Add(durationOf(d))
50 },
51 out: d + d,
52 },
53 {
54 name: "DurationMaxAddOneNanoOverflow",
55 op: func() ref.Val {
56 return durationOf(dMax).Add(durationOf(dNano))
57 },
58 out: errIntOverflow,
59 },
60 {
61 name: "DurationMaxAddOneSecondOverflow",
62 op: func() ref.Val {
63 return durationOf(dMax).Add(durationOf(dSecond))
64 },
65 out: errIntOverflow,
66 },
67 {
68 name: "DurationMinAddMinusOneOverflow",
69 op: func() ref.Val {
70 return durationOf(dMin).Add(durationOf(-dSecond))
71 },
72 out: errIntOverflow,
73 },
74
75 // Subtraction tests.
76 {
77 name: "DurationSubSelf",
78 op: func() ref.Val {
79 return durationOf(d).Subtract(durationOf(d))
80 },
81 out: duration(0, 0),
82 },
83 {
84 name: "DurationMaxSubMinusOneOverflow",
85 op: func() ref.Val {
86 return durationOf(dMax).Subtract(durationOf(-dNano))
87 },
88 out: errIntOverflow,
89 },
90 {
91 name: "DurationMinSubOneOverflow",
92 op: func() ref.Val {
93 return durationOf(dMin).Subtract(durationOf(dNano))
94 },
95 out: errIntOverflow,
96 },
97 }
98 for _, tst := range tests {
99 got := tst.op()
100 switch v := got.Value().(type) {
101 case error:
102 if want, ok := tst.out.(error); !ok || v.Error() != want.Error() {
103 t.Errorf("%s: got %v, wanted %v", tst.name, v, tst.out)
104 }
105 default:
106 if !reflect.DeepEqual(v, tst.out) {
107 t.Errorf("%s: got %v, wanted %v", tst.name, v, tst.out)
108 }
109 }
110 }
111 }
112
113 func TestDurationCompare(t *testing.T) {
114 d := Duration{(&dpb.Duration{Seconds: 7506}).AsDuration()}
115 lt := Duration{(&dpb.Duration{Seconds: -10}).AsDuration()}
116 if d.Compare(lt).(Int) != IntOne {
117 t.Error("Larger duration was not considered greater than smaller one.")
118 }
119 if lt.Compare(d).(Int) != IntNegOne {
120 t.Error("Smaller duration was not less than larger one.")
121 }
122 if d.Compare(d).(Int) != IntZero {
123 t.Error("Durations were not considered equal.")
124 }
125 if !IsError(d.Compare(False)) {
126 t.Error("Got comparison result, expected error.")
127 }
128 }
129
130 func TestDurationConvertToNative(t *testing.T) {
131 dur := Duration{Duration: duration(7506, 1000)}
132 val, err := dur.ConvertToNative(reflect.TypeOf(&dpb.Duration{}))
133 if err != nil ||
134 !proto.Equal(val.(proto.Message), &dpb.Duration{Seconds: 7506, Nanos: 1000}) {
135 t.Errorf("Got '%v', expected backing proto message value", err)
136 }
137 val, err = dur.ConvertToNative(reflect.TypeOf(Duration{}))
138 if err != nil {
139 t.Fatalf("ConvertToNative() failed: %v", err)
140 }
141 if !reflect.DeepEqual(val, dur) {
142 t.Errorf("got value %v, wanted %v", val, dur)
143 }
144 val, err = dur.ConvertToNative(reflect.TypeOf(time.Duration(0)))
145 if err != nil {
146 t.Fatalf("ConvertToNative() failed: %v", err)
147 }
148 if !reflect.DeepEqual(val, dur.Duration) {
149 t.Errorf("got value %v, wanted %v", val, dur.Duration)
150 }
151 }
152
153 func TestDurationConvertToNative_Any(t *testing.T) {
154 d := Duration{Duration: duration(7506, 1000)}
155 val, err := d.ConvertToNative(anyValueType)
156 if err != nil {
157 t.Error(err)
158 }
159 want, err := anypb.New(dpb.New(d.Duration))
160 if err != nil {
161 t.Error(err)
162 }
163 if !proto.Equal(val.(proto.Message), want) {
164 t.Errorf("Got '%v', wanted %v", val, want)
165 }
166 }
167
168 func TestDurationConvertToNative_Error(t *testing.T) {
169 val, err := Duration{Duration: duration(7506, 1000)}.ConvertToNative(JSONValueType)
170 if err != nil {
171 t.Errorf("Got error: '%v', expected value", err)
172 }
173 json := val.(*structpb.Value)
174 want := structpb.NewStringValue("7506.000001s")
175 if !proto.Equal(json, want) {
176 t.Errorf("Got %v, wanted %v", json, want)
177 }
178 }
179
180 func TestDurationConvertToNative_Json(t *testing.T) {
181 val, err := Duration{Duration: duration(7506, 1000)}.ConvertToNative(JSONValueType)
182 if err != nil {
183 t.Error(err)
184 }
185 want := structpb.NewStringValue("7506.000001s")
186 if !proto.Equal(val.(proto.Message), want) {
187 t.Errorf("Got '%v', wanted %v", val, want)
188 }
189 }
190
191 func TestDurationConvertToType_Identity(t *testing.T) {
192 d := Duration{Duration: duration(7506, 1000)}
193 str := d.ConvertToType(StringType).(String)
194 if str != "7506.000001s" {
195 t.Errorf("Got '%v', wanted 7506.000001s", str)
196 }
197 i := d.ConvertToType(IntType).(Int)
198 if i != Int(7506000001000) {
199 t.Errorf("Got '%v', wanted 7506000001000", i)
200 }
201 if !d.ConvertToType(DurationType).Equal(d).(Bool) {
202 t.Errorf("Got '%v', wanted identity", d.ConvertToType(DurationType))
203 }
204 if d.ConvertToType(TypeType) != DurationType {
205 t.Errorf("Got '%v', expected duration type", d.ConvertToType(TypeType))
206 }
207 if !IsError(d.ConvertToType(UintType)) {
208 t.Errorf("Got value, expected error.")
209 }
210 }
211
212 func TestDurationNegate(t *testing.T) {
213 neg := Duration{Duration: duration(1234, 1)}.Negate()
214 want := duration(-1234, -1)
215 if neg.Value().(time.Duration) != want {
216 t.Errorf("Got %v, expected %v", neg, want)
217 }
218 if v := time.Duration(math.MinInt64); !IsError(durationOf(v).Negate()) {
219 t.Errorf("Expected negating %d to result in overflow.", v)
220 }
221 if v := time.Duration(math.MaxInt64); !durationOf(v).Negate().Equal(durationOf(time.Duration(math.MinInt64 + 1))).(Bool) {
222 t.Errorf("Expected negating %d to yield %d", v, time.Duration(math.MinInt64+1))
223 }
224 }
225
226 func TestDurationGetHours(t *testing.T) {
227 d := Duration{Duration: duration(7506, 0)}
228 hr := d.Receive(overloads.TimeGetHours, overloads.DurationToHours, []ref.Val{})
229 if !hr.Equal(Int(2)).(Bool) {
230 t.Error("Expected 2 hours, got", hr)
231 }
232 }
233
234 func TestDurationGetMinutes(t *testing.T) {
235 d := Duration{Duration: duration(7506, 0)}
236 min := d.Receive(overloads.TimeGetMinutes, overloads.DurationToMinutes, []ref.Val{})
237 if !min.Equal(Int(125)).(Bool) {
238 t.Error("Expected 5 minutes, got", min)
239 }
240 }
241
242 func TestDurationGetSeconds(t *testing.T) {
243 d := Duration{Duration: duration(7506, 0)}
244 sec := d.Receive(overloads.TimeGetSeconds, overloads.DurationToSeconds, []ref.Val{})
245 if !sec.Equal(Int(7506)).(Bool) {
246 t.Error("Expected 6 seconds, got", sec)
247 }
248 }
249
250 func TestDurationGetMilliseconds(t *testing.T) {
251 d := Duration{Duration: duration(7506, 0)}
252 sec := d.Receive(overloads.TimeGetMilliseconds, overloads.DurationToMilliseconds, []ref.Val{})
253 if !sec.Equal(Int(7506000)).(Bool) {
254 t.Error("Expected 6 seconds, got", sec)
255 }
256 }
257
258 func TestDurationIsZeroValue(t *testing.T) {
259 if (&Duration{Duration: time.Duration(1)}).IsZeroValue() {
260 t.Error("Duration(1).IsZeroValue() returned true, wanted false.")
261 }
262 if !(&Duration{Duration: time.Duration(0)}).IsZeroValue() {
263 t.Error("Duration(0).IsZeroValue() returned false, wanted true")
264 }
265 }
266
267 func duration(seconds, nanos int64) time.Duration {
268 return time.Duration(seconds)*time.Second + time.Duration(nanos)
269 }