Debian Codegraph @

tpl/compare/compare_test.go

raw
1 // Copyright 2017 The Hugo Authors. All rights reserved.
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 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package compare
15
16 import (
17 "math"
18 "path"
19 "reflect"
20 "runtime"
21 "testing"
22 "time"
23
24 qt "github.com/frankban/quicktest"
25 "github.com/gohugoio/hugo/common/version"
26 "github.com/gohugoio/hugo/htesting/hqt"
27 "github.com/spf13/cast"
28 )
29
30 type T struct {
31 NonEmptyInterfaceNil I
32 NonEmptyInterfaceTypedNil I
33 }
34
35 type I interface {
36 Foo() string
37 }
38
39 func (t *T) Foo() string {
40 return "foo"
41 }
42
43 var testT = &T{
44 NonEmptyInterfaceTypedNil: (*T)(nil),
45 }
46
47 type (
48 tstEqerType1 string
49 tstEqerType2 string
50 )
51
52 func (t tstEqerType2) Eq(other any) bool {
53 return cast.ToString(t) == cast.ToString(other)
54 }
55
56 func (t tstEqerType2) String() string {
57 return string(t)
58 }
59
60 func (t tstEqerType1) Eq(other any) bool {
61 return cast.ToString(t) == cast.ToString(other)
62 }
63
64 func (t tstEqerType1) String() string {
65 return string(t)
66 }
67
68 type stringType string
69
70 type tstCompareType int
71
72 const (
73 tstEq tstCompareType = iota
74 tstNe
75 tstGt
76 tstGe
77 tstLt
78 tstLe
79 )
80
81 func tstIsEq(tp tstCompareType) bool { return tp == tstEq || tp == tstGe || tp == tstLe }
82 func tstIsGt(tp tstCompareType) bool { return tp == tstGt || tp == tstGe }
83 func tstIsLt(tp tstCompareType) bool { return tp == tstLt || tp == tstLe }
84
85 func TestDefaultFunc(t *testing.T) {
86 t.Parallel()
87 c := qt.New(t)
88
89 then := time.Now()
90 now := time.Now()
91 ns := New(time.UTC, false)
92
93 for i, test := range []struct {
94 dflt any
95 given any
96 expect any
97 }{
98 {true, false, false},
99 {"5", 0, "5"},
100
101 {"test1", "set", "set"},
102 {"test2", "", "test2"},
103 {"test3", nil, "test3"},
104
105 {[2]int{10, 20}, [2]int{1, 2}, [2]int{1, 2}},
106 {[2]int{10, 20}, [0]int{}, [2]int{10, 20}},
107 {[2]int{100, 200}, nil, [2]int{100, 200}},
108
109 {[]string{"one"}, []string{"uno"}, []string{"uno"}},
110 {[]string{"two"}, []string{}, []string{"two"}},
111 {[]string{"three"}, nil, []string{"three"}},
112
113 {map[string]int{"one": 1}, map[string]int{"uno": 1}, map[string]int{"uno": 1}},
114 {map[string]int{"one": 1}, map[string]int{}, map[string]int{"one": 1}},
115 {map[string]int{"two": 2}, nil, map[string]int{"two": 2}},
116
117 {10, 1, 1},
118 {10, 0, 10},
119 {20, nil, 20},
120
121 {float32(10), float32(1), float32(1)},
122 {float32(10), 0, float32(10)},
123 {float32(20), nil, float32(20)},
124
125 {complex(2, -2), complex(1, -1), complex(1, -1)},
126 {complex(2, -2), complex(0, 0), complex(2, -2)},
127 {complex(3, -3), nil, complex(3, -3)},
128
129 {struct{ f string }{f: "one"}, struct{}{}, struct{}{}},
130 {struct{ f string }{f: "two"}, nil, struct{ f string }{f: "two"}},
131
132 {then, now, now},
133 {then, time.Time{}, then},
134 } {
135
136 eq := qt.CmpEquals(hqt.DeepAllowUnexported(test.dflt))
137
138 errMsg := qt.Commentf("[%d] %v", i, test)
139
140 result, err := ns.Default(test.dflt, test.given)
141
142 c.Assert(err, qt.IsNil, errMsg)
143 c.Assert(result, eq, test.expect, errMsg)
144 }
145 }
146
147 func TestCompare(t *testing.T) {
148 t.Parallel()
149
150 n := New(time.UTC, false)
151
152 twoEq := func(a, b any) bool {
153 return n.Eq(a, b)
154 }
155
156 twoGt := func(a, b any) bool {
157 return n.Gt(a, b)
158 }
159
160 twoLt := func(a, b any) bool {
161 return n.Lt(a, b)
162 }
163
164 twoGe := func(a, b any) bool {
165 return n.Ge(a, b)
166 }
167
168 twoLe := func(a, b any) bool {
169 return n.Le(a, b)
170 }
171
172 twoNe := func(a, b any) bool {
173 return n.Ne(a, b)
174 }
175
176 for _, test := range []struct {
177 tstCompareType
178 funcUnderTest func(a, b any) bool
179 }{
180 {tstGt, twoGt},
181 {tstLt, twoLt},
182 {tstGe, twoGe},
183 {tstLe, twoLe},
184 {tstEq, twoEq},
185 {tstNe, twoNe},
186 } {
187 doTestCompare(t, test.tstCompareType, test.funcUnderTest)
188 }
189 }
190
191 func doTestCompare(t *testing.T, tp tstCompareType, funcUnderTest func(a, b any) bool) {
192 for i, test := range []struct {
193 left any
194 right any
195 expectIndicator int
196 }{
197 {5, 8, -1},
198 {8, 5, 1},
199 {5, 5, 0},
200 {int(5), int64(5), 0},
201 {int32(5), int(5), 0},
202 {int16(4), 4, 0},
203 {uint8(4), 4, 0},
204 {uint16(4), 4, 0},
205 {uint16(4), 4, 0},
206 {uint32(4), uint16(4), 0},
207 {uint32(4), uint16(3), 1},
208 {uint64(4), 4, 0},
209 {4, uint64(4), 0},
210 {uint64(4), 5, -1},
211 {5, uint64(4), 1},
212 {uint64(5), uint64(4), 1},
213 {uint64(4), uint64(5), -1},
214 {uint64(5), uint64(5), 0},
215 {uint64(math.MaxUint32), uint32(math.MaxUint32), 0},
216 {uint64(math.MaxUint16), int(math.MaxUint16), 0},
217 {int16(4), int(5), -1},
218 {uint(15), uint64(15), 0},
219 {-2, 1, -1},
220 {2, -5, 1},
221 {0.0, 1.23, -1},
222 {1.1, 1.1, 0},
223 {float32(1.0), float64(1.0), 0},
224 {1, 1.0, 0},
225 {1.0, 1, 0},
226 {0, 0.0, 0},
227 {-1, -1.0, 0},
228 {int64(3), float64(3), 0},
229 {uint(4), 4.0, 0},
230 {1, float32(1.0), 0},
231 {float32(1.0), 1, 0},
232 {1, 1.5, -1},
233 {1.5, 1, 1},
234 {2, 1.5, 1},
235 {int64(1<<53 + 1), float64(1 << 53), 1},
236 {float64(1 << 53), int64(1<<53 + 1), -1},
237 {int64(1<<53 + 1), uint64(1<<53 + 1), 0},
238 {-1, uint64(math.MaxUint64), -1},
239 {uint64(math.MaxUint64), -1, 1},
240 {uint64(math.MaxUint64), uint8(1), 1},
241 {1.23, 0.0, 1},
242 {"5", "5", 0},
243 {"8", "5", 1},
244 {"5", "0001", 1},
245 {[]int{100, 99}, []int{1, 2, 3, 4}, -1},
246 {cast.ToTime("2015-11-20"), cast.ToTime("2015-11-20"), 0},
247 {cast.ToTime("2015-11-19"), cast.ToTime("2015-11-20"), -1},
248 {cast.ToTime("2015-11-20"), cast.ToTime("2015-11-19"), 1},
249 {"a", "a", 0},
250 {"a", "b", -1},
251 {"b", "a", 1},
252 {"infinity", "infinity", 0},
253 {"nan", "nan", 0},
254 {tstEqerType1("a"), tstEqerType1("a"), 0},
255 {tstEqerType1("a"), tstEqerType2("a"), 0},
256 {tstEqerType2("a"), tstEqerType1("a"), 0},
257 {tstEqerType2("a"), tstEqerType1("b"), -1},
258 {version.MustParseVersion("0.32.1").Version(), version.MustParseVersion("0.32").Version(), 1},
259 {version.MustParseVersion("0.35").Version(), version.MustParseVersion("0.32").Version(), 1},
260 {version.MustParseVersion("0.36").Version(), version.MustParseVersion("0.36").Version(), 0},
261 {version.MustParseVersion("0.32").Version(), version.MustParseVersion("0.36").Version(), -1},
262 {version.MustParseVersion("0.32").Version(), "0.36", -1},
263 {"0.36", version.MustParseVersion("0.32").Version(), 1},
264 {"0.36", version.MustParseVersion("0.36").Version(), 0},
265 {"0.37", version.MustParseVersion("0.37-DEV").Version(), 1},
266 {"0.37-DEV", version.MustParseVersion("0.37").Version(), -1},
267 {"0.36", version.MustParseVersion("0.37-DEV").Version(), -1},
268 {"0.37-DEV", version.MustParseVersion("0.37-DEV").Version(), 0},
269 // https://github.com/gohugoio/hugo/issues/5905
270 {nil, nil, 0},
271 {testT.NonEmptyInterfaceNil, nil, 0},
272 {testT.NonEmptyInterfaceTypedNil, nil, 0},
273 } {
274
275 result := funcUnderTest(test.left, test.right)
276 success := false
277
278 if test.expectIndicator == 0 {
279 if tstIsEq(tp) {
280 success = result
281 } else {
282 success = !result
283 }
284 }
285
286 if test.expectIndicator < 0 {
287 success = result && (tstIsLt(tp) || tp == tstNe)
288 success = success || (!result && !tstIsLt(tp))
289 }
290
291 if test.expectIndicator > 0 {
292 success = result && (tstIsGt(tp) || tp == tstNe)
293 success = success || (!result && (!tstIsGt(tp) || tp != tstNe))
294 }
295
296 if !success {
297 t.Fatalf("[%d][%s] %v compared to %v: %t", i, path.Base(runtime.FuncForPC(reflect.ValueOf(funcUnderTest).Pointer()).Name()), test.left, test.right, result)
298 }
299 }
300 }
301
302 func TestEqualExtend(t *testing.T) {
303 t.Parallel()
304 c := qt.New(t)
305
306 ns := New(time.UTC, false)
307 one := 1
308
309 for _, test := range []struct {
310 first any
311 others []any
312 expect bool
313 }{
314 {1, []any{1, 2}, true},
315 {1, []any{2, 1}, true},
316 {1, []any{2, 3}, false},
317 {1, []any{2, 1.0}, true},
318 {1.0, []any{2, 1}, true},
319 {1, []any{1.5}, false},
320 {uint64(math.MaxUint64), []any{-1}, false},
321 {-1, []any{uint64(math.MaxUint64)}, false},
322 {float64(9007199254740992), []any{int64(9007199254740993)}, false},
323 {int64(9007199254740993), []any{float64(9007199254740992)}, false},
324 {float64(9007199254740992), []any{int64(9007199254740992)}, true},
325 {int64(9007199254740992), []any{float64(9007199254740992)}, true},
326 {float32(16777216), []any{int64(16777217)}, false},
327 {int64(16777217), []any{float32(16777216)}, false},
328 {float64(3.14), []any{float32(3.14)}, false},
329 {float32(3.14), []any{float64(3.14)}, false},
330 {float64(0.5), []any{float32(0.5)}, true},
331 {float32(0.5), []any{float64(0.5)}, true},
332 {stringType("a"), []any{"b", "a"}, true},
333 {[]int{1, 2}, []any{[]int{1, 2}}, true},
334 {[]int{1, 2}, []any{[]int{2, 1}}, false},
335 {map[string]any{"a": []int{1}}, []any{map[string]any{"a": []int{1}}}, true},
336 {struct{ V any }{[]int{1}}, []any{struct{ V any }{[]int{1}}}, true},
337 {&T{}, []any{&T{}}, true},
338 {T{}, []any{&T{}}, false},
339 {&T{}, []any{T{}}, false},
340 {1, []any{&one}, false},
341 {&one, []any{1}, false},
342 {tstEqerType1("a"), []any{tstEqerType1("a"), tstEqerType1("b")}, true},
343 {tstEqerType1("a"), []any{tstEqerType1("b"), tstEqerType1("a")}, true},
344 {tstEqerType1("a"), []any{tstEqerType1("b"), tstEqerType1("c")}, false},
345 } {
346
347 result := ns.Eq(test.first, test.others...)
348
349 c.Assert(result, qt.Equals, test.expect)
350 }
351 }
352
353 func TestNotEqualExtend(t *testing.T) {
354 t.Parallel()
355 c := qt.New(t)
356
357 ns := New(time.UTC, false)
358
359 for _, test := range []struct {
360 first any
361 others []any
362 expect bool
363 }{
364 {1, []any{2, 3}, true},
365 {1, []any{2, 1}, false},
366 {1, []any{1, 2}, false},
367 } {
368 result := ns.Ne(test.first, test.others...)
369 c.Assert(result, qt.Equals, test.expect)
370 }
371 }
372
373 func TestGreaterEqualExtend(t *testing.T) {
374 t.Parallel()
375 c := qt.New(t)
376
377 ns := New(time.UTC, false)
378
379 for _, test := range []struct {
380 first any
381 others []any
382 expect bool
383 }{
384 {5, []any{2, 3}, true},
385 {5, []any{5, 5}, true},
386 {3, []any{4, 2}, false},
387 {3, []any{2, 4}, false},
388 } {
389 result := ns.Ge(test.first, test.others...)
390 c.Assert(result, qt.Equals, test.expect)
391 }
392 }
393
394 func TestGreaterThanExtend(t *testing.T) {
395 t.Parallel()
396 c := qt.New(t)
397
398 ns := New(time.UTC, false)
399
400 for _, test := range []struct {
401 first any
402 others []any
403 expect bool
404 }{
405 {5, []any{2, 3}, true},
406 {5, []any{5, 4}, false},
407 {3, []any{4, 2}, false},
408 } {
409 result := ns.Gt(test.first, test.others...)
410 c.Assert(result, qt.Equals, test.expect)
411 }
412 }
413
414 func TestLessEqualExtend(t *testing.T) {
415 t.Parallel()
416 c := qt.New(t)
417
418 ns := New(time.UTC, false)
419
420 for _, test := range []struct {
421 first any
422 others []any
423 expect bool
424 }{
425 {1, []any{2, 3}, true},
426 {1, []any{1, 2}, true},
427 {2, []any{1, 2}, false},
428 {3, []any{2, 4}, false},
429 } {
430 result := ns.Le(test.first, test.others...)
431 c.Assert(result, qt.Equals, test.expect)
432 }
433 }
434
435 func TestLessThanExtend(t *testing.T) {
436 t.Parallel()
437 c := qt.New(t)
438
439 ns := New(time.UTC, false)
440
441 for _, test := range []struct {
442 first any
443 others []any
444 expect bool
445 }{
446 {1, []any{2, 3}, true},
447 {1, []any{1, 2}, false},
448 {2, []any{1, 2}, false},
449 {3, []any{2, 4}, false},
450 } {
451 result := ns.Lt(test.first, test.others...)
452 c.Assert(result, qt.Equals, test.expect)
453 }
454 }
455
456 func TestCase(t *testing.T) {
457 c := qt.New(t)
458 n := New(time.UTC, false)
459
460 c.Assert(n.Eq("az", "az"), qt.Equals, true)
461 c.Assert(n.Eq("az", stringType("az")), qt.Equals, true)
462 }
463
464 func TestStringType(t *testing.T) {
465 c := qt.New(t)
466 n := New(time.UTC, true)
467
468 c.Assert(n.Lt("az", "Za"), qt.Equals, true)
469 c.Assert(n.Gt("ab", "Ab"), qt.Equals, true)
470 }
471
472 func TestTimeUnix(t *testing.T) {
473 t.Parallel()
474 n := New(time.UTC, false)
475 var sec int64 = 1234567890
476 tv := reflect.ValueOf(time.Unix(sec, 0))
477 i := 1
478
479 res := n.toTimeUnix(tv)
480 if sec != res {
481 t.Errorf("[%d] timeUnix got %v but expected %v", i, res, sec)
482 }
483
484 i++
485 func(t *testing.T) {
486 defer func() {
487 if err := recover(); err == nil {
488 t.Errorf("[%d] timeUnix didn't return an expected error", i)
489 }
490 }()
491 iv := reflect.ValueOf(sec)
492 n.toTimeUnix(iv)
493 }(t)
494 }
495
496 func TestConditional(t *testing.T) {
497 t.Parallel()
498 c := qt.New(t)
499 ns := New(time.UTC, false)
500
501 type args struct {
502 cond any
503 v1 any
504 v2 any
505 }
506 tests := []struct {
507 name string
508 args args
509 want any
510 }{
511 {"a", args{cond: true, v1: "true", v2: "false"}, "true"},
512 {"b", args{cond: false, v1: "true", v2: "false"}, "false"},
513 {"c", args{cond: 1, v1: "true", v2: "false"}, "true"},
514 {"d", args{cond: 0, v1: "true", v2: "false"}, "false"},
515 {"e", args{cond: "foo", v1: "true", v2: "false"}, "true"},
516 {"f", args{cond: "", v1: "true", v2: "false"}, "false"},
517 {"g", args{cond: []int{6, 7}, v1: "true", v2: "false"}, "true"},
518 {"h", args{cond: []int{}, v1: "true", v2: "false"}, "false"},
519 {"i", args{cond: nil, v1: "true", v2: "false"}, "false"},
520 }
521 for _, tt := range tests {
522 t.Run(tt.name, func(t *testing.T) {
523 c.Assert(ns.Conditional(tt.args.cond, tt.args.v1, tt.args.v2), qt.Equals, tt.want)
524 })
525 }
526 }
527
528 // Issue 9462
529 func TestComparisonArgCount(t *testing.T) {
530 t.Parallel()
531 c := qt.New(t)
532
533 ns := New(time.UTC, false)
534
535 panicMsg := "missing arguments for comparison"
536
537 c.Assert(func() { ns.Eq(1) }, qt.PanicMatches, panicMsg)
538 c.Assert(func() { ns.Ge(1) }, qt.PanicMatches, panicMsg)
539 c.Assert(func() { ns.Gt(1) }, qt.PanicMatches, panicMsg)
540 c.Assert(func() { ns.Le(1) }, qt.PanicMatches, panicMsg)
541 c.Assert(func() { ns.Lt(1) }, qt.PanicMatches, panicMsg)
542 c.Assert(func() { ns.Ne(1) }, qt.PanicMatches, panicMsg)
543 }