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.23, 0.0, 1},
225 {"5", "5", 0},
226 {"8", "5", 1},
227 {"5", "0001", 1},
228 {[]int{100, 99}, []int{1, 2, 3, 4}, -1},
229 {cast.ToTime("2015-11-20"), cast.ToTime("2015-11-20"), 0},
230 {cast.ToTime("2015-11-19"), cast.ToTime("2015-11-20"), -1},
231 {cast.ToTime("2015-11-20"), cast.ToTime("2015-11-19"), 1},
232 {"a", "a", 0},
233 {"a", "b", -1},
234 {"b", "a", 1},
235 {"infinity", "infinity", 0},
236 {"nan", "nan", 0},
237 {tstEqerType1("a"), tstEqerType1("a"), 0},
238 {tstEqerType1("a"), tstEqerType2("a"), 0},
239 {tstEqerType2("a"), tstEqerType1("a"), 0},
240 {tstEqerType2("a"), tstEqerType1("b"), -1},
241 {version.MustParseVersion("0.32.1").Version(), version.MustParseVersion("0.32").Version(), 1},
242 {version.MustParseVersion("0.35").Version(), version.MustParseVersion("0.32").Version(), 1},
243 {version.MustParseVersion("0.36").Version(), version.MustParseVersion("0.36").Version(), 0},
244 {version.MustParseVersion("0.32").Version(), version.MustParseVersion("0.36").Version(), -1},
245 {version.MustParseVersion("0.32").Version(), "0.36", -1},
246 {"0.36", version.MustParseVersion("0.32").Version(), 1},
247 {"0.36", version.MustParseVersion("0.36").Version(), 0},
248 {"0.37", version.MustParseVersion("0.37-DEV").Version(), 1},
249 {"0.37-DEV", version.MustParseVersion("0.37").Version(), -1},
250 {"0.36", version.MustParseVersion("0.37-DEV").Version(), -1},
251 {"0.37-DEV", version.MustParseVersion("0.37-DEV").Version(), 0},
252 // https://github.com/gohugoio/hugo/issues/5905
253 {nil, nil, 0},
254 {testT.NonEmptyInterfaceNil, nil, 0},
255 {testT.NonEmptyInterfaceTypedNil, nil, 0},
256 } {
257
258 result := funcUnderTest(test.left, test.right)
259 success := false
260
261 if test.expectIndicator == 0 {
262 if tstIsEq(tp) {
263 success = result
264 } else {
265 success = !result
266 }
267 }
268
269 if test.expectIndicator < 0 {
270 success = result && (tstIsLt(tp) || tp == tstNe)
271 success = success || (!result && !tstIsLt(tp))
272 }
273
274 if test.expectIndicator > 0 {
275 success = result && (tstIsGt(tp) || tp == tstNe)
276 success = success || (!result && (!tstIsGt(tp) || tp != tstNe))
277 }
278
279 if !success {
280 t.Fatalf("[%d][%s] %v compared to %v: %t", i, path.Base(runtime.FuncForPC(reflect.ValueOf(funcUnderTest).Pointer()).Name()), test.left, test.right, result)
281 }
282 }
283 }
284
285 func TestEqualExtend(t *testing.T) {
286 t.Parallel()
287 c := qt.New(t)
288
289 ns := New(time.UTC, false)
290
291 for _, test := range []struct {
292 first any
293 others []any
294 expect bool
295 }{
296 {1, []any{1, 2}, true},
297 {1, []any{2, 1}, true},
298 {1, []any{2, 3}, false},
299 {tstEqerType1("a"), []any{tstEqerType1("a"), tstEqerType1("b")}, true},
300 {tstEqerType1("a"), []any{tstEqerType1("b"), tstEqerType1("a")}, true},
301 {tstEqerType1("a"), []any{tstEqerType1("b"), tstEqerType1("c")}, false},
302 } {
303
304 result := ns.Eq(test.first, test.others...)
305
306 c.Assert(result, qt.Equals, test.expect)
307 }
308 }
309
310 func TestNotEqualExtend(t *testing.T) {
311 t.Parallel()
312 c := qt.New(t)
313
314 ns := New(time.UTC, false)
315
316 for _, test := range []struct {
317 first any
318 others []any
319 expect bool
320 }{
321 {1, []any{2, 3}, true},
322 {1, []any{2, 1}, false},
323 {1, []any{1, 2}, false},
324 } {
325 result := ns.Ne(test.first, test.others...)
326 c.Assert(result, qt.Equals, test.expect)
327 }
328 }
329
330 func TestGreaterEqualExtend(t *testing.T) {
331 t.Parallel()
332 c := qt.New(t)
333
334 ns := New(time.UTC, false)
335
336 for _, test := range []struct {
337 first any
338 others []any
339 expect bool
340 }{
341 {5, []any{2, 3}, true},
342 {5, []any{5, 5}, true},
343 {3, []any{4, 2}, false},
344 {3, []any{2, 4}, false},
345 } {
346 result := ns.Ge(test.first, test.others...)
347 c.Assert(result, qt.Equals, test.expect)
348 }
349 }
350
351 func TestGreaterThanExtend(t *testing.T) {
352 t.Parallel()
353 c := qt.New(t)
354
355 ns := New(time.UTC, false)
356
357 for _, test := range []struct {
358 first any
359 others []any
360 expect bool
361 }{
362 {5, []any{2, 3}, true},
363 {5, []any{5, 4}, false},
364 {3, []any{4, 2}, false},
365 } {
366 result := ns.Gt(test.first, test.others...)
367 c.Assert(result, qt.Equals, test.expect)
368 }
369 }
370
371 func TestLessEqualExtend(t *testing.T) {
372 t.Parallel()
373 c := qt.New(t)
374
375 ns := New(time.UTC, false)
376
377 for _, test := range []struct {
378 first any
379 others []any
380 expect bool
381 }{
382 {1, []any{2, 3}, true},
383 {1, []any{1, 2}, true},
384 {2, []any{1, 2}, false},
385 {3, []any{2, 4}, false},
386 } {
387 result := ns.Le(test.first, test.others...)
388 c.Assert(result, qt.Equals, test.expect)
389 }
390 }
391
392 func TestLessThanExtend(t *testing.T) {
393 t.Parallel()
394 c := qt.New(t)
395
396 ns := New(time.UTC, false)
397
398 for _, test := range []struct {
399 first any
400 others []any
401 expect bool
402 }{
403 {1, []any{2, 3}, true},
404 {1, []any{1, 2}, false},
405 {2, []any{1, 2}, false},
406 {3, []any{2, 4}, false},
407 } {
408 result := ns.Lt(test.first, test.others...)
409 c.Assert(result, qt.Equals, test.expect)
410 }
411 }
412
413 func TestCase(t *testing.T) {
414 c := qt.New(t)
415 n := New(time.UTC, false)
416
417 c.Assert(n.Eq("az", "az"), qt.Equals, true)
418 c.Assert(n.Eq("az", stringType("az")), qt.Equals, true)
419 }
420
421 func TestStringType(t *testing.T) {
422 c := qt.New(t)
423 n := New(time.UTC, true)
424
425 c.Assert(n.Lt("az", "Za"), qt.Equals, true)
426 c.Assert(n.Gt("ab", "Ab"), qt.Equals, true)
427 }
428
429 func TestTimeUnix(t *testing.T) {
430 t.Parallel()
431 n := New(time.UTC, false)
432 var sec int64 = 1234567890
433 tv := reflect.ValueOf(time.Unix(sec, 0))
434 i := 1
435
436 res := n.toTimeUnix(tv)
437 if sec != res {
438 t.Errorf("[%d] timeUnix got %v but expected %v", i, res, sec)
439 }
440
441 i++
442 func(t *testing.T) {
443 defer func() {
444 if err := recover(); err == nil {
445 t.Errorf("[%d] timeUnix didn't return an expected error", i)
446 }
447 }()
448 iv := reflect.ValueOf(sec)
449 n.toTimeUnix(iv)
450 }(t)
451 }
452
453 func TestConditional(t *testing.T) {
454 t.Parallel()
455 c := qt.New(t)
456 ns := New(time.UTC, false)
457
458 type args struct {
459 cond any
460 v1 any
461 v2 any
462 }
463 tests := []struct {
464 name string
465 args args
466 want any
467 }{
468 {"a", args{cond: true, v1: "true", v2: "false"}, "true"},
469 {"b", args{cond: false, v1: "true", v2: "false"}, "false"},
470 {"c", args{cond: 1, v1: "true", v2: "false"}, "true"},
471 {"d", args{cond: 0, v1: "true", v2: "false"}, "false"},
472 {"e", args{cond: "foo", v1: "true", v2: "false"}, "true"},
473 {"f", args{cond: "", v1: "true", v2: "false"}, "false"},
474 {"g", args{cond: []int{6, 7}, v1: "true", v2: "false"}, "true"},
475 {"h", args{cond: []int{}, v1: "true", v2: "false"}, "false"},
476 {"i", args{cond: nil, v1: "true", v2: "false"}, "false"},
477 }
478 for _, tt := range tests {
479 t.Run(tt.name, func(t *testing.T) {
480 c.Assert(ns.Conditional(tt.args.cond, tt.args.v1, tt.args.v2), qt.Equals, tt.want)
481 })
482 }
483 }
484
485 // Issue 9462
486 func TestComparisonArgCount(t *testing.T) {
487 t.Parallel()
488 c := qt.New(t)
489
490 ns := New(time.UTC, false)
491
492 panicMsg := "missing arguments for comparison"
493
494 c.Assert(func() { ns.Eq(1) }, qt.PanicMatches, panicMsg)
495 c.Assert(func() { ns.Ge(1) }, qt.PanicMatches, panicMsg)
496 c.Assert(func() { ns.Gt(1) }, qt.PanicMatches, panicMsg)
497 c.Assert(func() { ns.Le(1) }, qt.PanicMatches, panicMsg)
498 c.Assert(func() { ns.Lt(1) }, qt.PanicMatches, panicMsg)
499 c.Assert(func() { ns.Ne(1) }, qt.PanicMatches, panicMsg)
500 }