Debian Codegraph @

model/value_comparison_test.go

raw symbols SCIP
1 package model_test
2
3 import (
4 "testing"
5
6 "github.com/tomwright/dasel/v3/model"
7 )
8
9 type compareTestCase struct {
10 a *model.Value
11 b *model.Value
12 exp bool
13 }
14
15 func TestValue_Equal(t *testing.T) {
16 run := func(tc compareTestCase) func(t *testing.T) {
17 return func(t *testing.T) {
18 got, err := tc.a.Equal(tc.b)
19 if err != nil {
20 t.Errorf("unexpected error: %s", err)
21 return
22 }
23 gotBool, err := got.BoolValue()
24 if err != nil {
25 t.Errorf("unexpected error: %s", err)
26 return
27 }
28 if gotBool != tc.exp {
29 t.Errorf("expected %v, got %v", tc.exp, got)
30 }
31 }
32 }
33 t.Run("string", func(t *testing.T) {
34 t.Run("equal", run(compareTestCase{
35 a: model.NewStringValue("hello"),
36 b: model.NewStringValue("hello"),
37 exp: true,
38 }))
39 t.Run("not equal", run(compareTestCase{
40 a: model.NewStringValue("hello"),
41 b: model.NewStringValue("world"),
42 exp: false,
43 }))
44 })
45 t.Run("int", func(t *testing.T) {
46 t.Run("equal", run(compareTestCase{
47 a: model.NewIntValue(1),
48 b: model.NewIntValue(1),
49 exp: true,
50 }))
51 t.Run("not equal", run(compareTestCase{
52 a: model.NewIntValue(1),
53 b: model.NewIntValue(2),
54 exp: false,
55 }))
56 t.Run("equal float", run(compareTestCase{
57 a: model.NewIntValue(1),
58 b: model.NewFloatValue(1),
59 exp: true,
60 }))
61 t.Run("not equal float", run(compareTestCase{
62 a: model.NewIntValue(1),
63 b: model.NewFloatValue(2),
64 exp: false,
65 }))
66 })
67 t.Run("float", func(t *testing.T) {
68 t.Run("equal", run(compareTestCase{
69 a: model.NewFloatValue(1.1),
70 b: model.NewFloatValue(1.1),
71 exp: true,
72 }))
73 t.Run("not equal", run(compareTestCase{
74 a: model.NewFloatValue(1.1),
75 b: model.NewFloatValue(1.2),
76 exp: false,
77 }))
78 t.Run("equal int", run(compareTestCase{
79 a: model.NewFloatValue(1),
80 b: model.NewIntValue(1),
81 exp: true,
82 }))
83 t.Run("not equal int", run(compareTestCase{
84 a: model.NewFloatValue(1.1),
85 b: model.NewIntValue(1),
86 exp: false,
87 }))
88 })
89 t.Run("bool", func(t *testing.T) {
90 t.Run("equal", run(compareTestCase{
91 a: model.NewBoolValue(true),
92 b: model.NewBoolValue(true),
93 exp: true,
94 }))
95 t.Run("not equal", run(compareTestCase{
96 a: model.NewBoolValue(true),
97 b: model.NewBoolValue(false),
98 exp: false,
99 }))
100 })
101 t.Run("map", func(t *testing.T) {
102 t.Run("equal", run(compareTestCase{
103 a: model.NewValue(map[string]interface{}{
104 "hello": "world",
105 }),
106 b: model.NewValue(map[string]interface{}{
107 "hello": "world",
108 }),
109 exp: true,
110 }))
111 t.Run("not equal", run(compareTestCase{
112 a: model.NewValue(map[string]interface{}{
113 "hello": "world",
114 }),
115 b: model.NewValue(map[string]interface{}{
116 "hello": "world2",
117 }),
118 exp: false,
119 }))
120 })
121 t.Run("array", func(t *testing.T) {
122 t.Run("equal", run(compareTestCase{
123 a: model.NewValue([]interface{}{
124 "hello", "world",
125 }),
126 b: model.NewValue([]interface{}{
127 "hello", "world",
128 }),
129 exp: true,
130 }))
131 t.Run("not equal", run(compareTestCase{
132 a: model.NewValue([]interface{}{
133 "hello", "world",
134 }),
135 b: model.NewValue([]interface{}{
136 "hello", "world2",
137 }),
138 exp: false,
139 }))
140 })
141 t.Run("null", func(t *testing.T) {
142 t.Run("equal", run(compareTestCase{
143 a: model.NewValue(nil),
144 b: model.NewValue(nil),
145 exp: true,
146 }))
147 })
148 }
149
150 func TestValue_NotEqual(t *testing.T) {
151 run := func(tc compareTestCase) func(t *testing.T) {
152 return func(t *testing.T) {
153 got, err := tc.a.NotEqual(tc.b)
154 if err != nil {
155 t.Errorf("unexpected error: %s", err)
156 return
157 }
158 gotBool, err := got.BoolValue()
159 if err != nil {
160 t.Errorf("unexpected error: %s", err)
161 return
162 }
163 if gotBool != tc.exp {
164 t.Errorf("expected %v, got %v", tc.exp, got)
165 }
166 }
167 }
168 t.Run("string", func(t *testing.T) {
169 t.Run("equal", run(compareTestCase{
170 a: model.NewStringValue("hello"),
171 b: model.NewStringValue("hello"),
172 exp: false,
173 }))
174 t.Run("not equal", run(compareTestCase{
175 a: model.NewStringValue("hello"),
176 b: model.NewStringValue("world"),
177 exp: true,
178 }))
179 })
180 t.Run("int", func(t *testing.T) {
181 t.Run("equal", run(compareTestCase{
182 a: model.NewIntValue(1),
183 b: model.NewIntValue(1),
184 exp: false,
185 }))
186 t.Run("not equal", run(compareTestCase{
187 a: model.NewIntValue(1),
188 b: model.NewIntValue(2),
189 exp: true,
190 }))
191 t.Run("equal float", run(compareTestCase{
192 a: model.NewIntValue(1),
193 b: model.NewFloatValue(1),
194 exp: false,
195 }))
196 t.Run("not equal float", run(compareTestCase{
197 a: model.NewIntValue(1),
198 b: model.NewFloatValue(2),
199 exp: true,
200 }))
201 })
202 t.Run("float", func(t *testing.T) {
203 t.Run("equal", run(compareTestCase{
204 a: model.NewFloatValue(1.1),
205 b: model.NewFloatValue(1.1),
206 exp: false,
207 }))
208 t.Run("not equal", run(compareTestCase{
209 a: model.NewFloatValue(1.1),
210 b: model.NewFloatValue(1.2),
211 exp: true,
212 }))
213 t.Run("equal int", run(compareTestCase{
214 a: model.NewFloatValue(1),
215 b: model.NewIntValue(1),
216 exp: false,
217 }))
218 t.Run("not equal int", run(compareTestCase{
219 a: model.NewFloatValue(1.1),
220 b: model.NewIntValue(1),
221 exp: true,
222 }))
223 })
224 t.Run("bool", func(t *testing.T) {
225 t.Run("equal", run(compareTestCase{
226 a: model.NewBoolValue(true),
227 b: model.NewBoolValue(true),
228 exp: false,
229 }))
230 t.Run("not equal", run(compareTestCase{
231 a: model.NewBoolValue(true),
232 b: model.NewBoolValue(false),
233 exp: true,
234 }))
235 })
236 t.Run("map", func(t *testing.T) {
237 t.Run("equal", run(compareTestCase{
238 a: model.NewValue(map[string]interface{}{
239 "hello": "world",
240 }),
241 b: model.NewValue(map[string]interface{}{
242 "hello": "world",
243 }),
244 exp: false,
245 }))
246 t.Run("not equal", run(compareTestCase{
247 a: model.NewValue(map[string]interface{}{
248 "hello": "world",
249 }),
250 b: model.NewValue(map[string]interface{}{
251 "hello": "world2",
252 }),
253 exp: true,
254 }))
255 })
256 t.Run("array", func(t *testing.T) {
257 t.Run("equal", run(compareTestCase{
258 a: model.NewValue([]interface{}{
259 "hello", "world",
260 }),
261 b: model.NewValue([]interface{}{
262 "hello", "world",
263 }),
264 exp: false,
265 }))
266 t.Run("not equal", run(compareTestCase{
267 a: model.NewValue([]interface{}{
268 "hello", "world",
269 }),
270 b: model.NewValue([]interface{}{
271 "hello", "world2",
272 }),
273 exp: true,
274 }))
275 })
276 t.Run("null", func(t *testing.T) {
277 t.Run("equal", run(compareTestCase{
278 a: model.NewValue(nil),
279 b: model.NewValue(nil),
280 exp: false,
281 }))
282 })
283 }
284
285 func TestValue_LessThan(t *testing.T) {
286 run := func(tc compareTestCase) func(t *testing.T) {
287 return func(t *testing.T) {
288 got, err := tc.a.LessThan(tc.b)
289 if err != nil {
290 t.Errorf("unexpected error: %s", err)
291 return
292 }
293 gotBool, err := got.BoolValue()
294 if err != nil {
295 t.Errorf("unexpected error: %s", err)
296 return
297 }
298 if gotBool != tc.exp {
299 t.Errorf("expected %v, got %v", tc.exp, got)
300 }
301 }
302 }
303 t.Run("int", func(t *testing.T) {
304 t.Run("less", run(compareTestCase{
305 a: model.NewIntValue(1),
306 b: model.NewIntValue(2),
307 exp: true,
308 }))
309 t.Run("greater", run(compareTestCase{
310 a: model.NewIntValue(2),
311 b: model.NewIntValue(1),
312 exp: false,
313 }))
314 t.Run("equal", run(compareTestCase{
315 a: model.NewIntValue(1),
316 b: model.NewIntValue(1),
317 exp: false,
318 }))
319 })
320 t.Run("float", func(t *testing.T) {
321 t.Run("less", run(compareTestCase{
322 a: model.NewFloatValue(1.1),
323 b: model.NewFloatValue(1.2),
324 exp: true,
325 }))
326 t.Run("greater", run(compareTestCase{
327 a: model.NewFloatValue(1.2),
328 b: model.NewFloatValue(1.1),
329 exp: false,
330 }))
331 t.Run("equal", run(compareTestCase{
332 a: model.NewFloatValue(1.1),
333 b: model.NewFloatValue(1.1),
334 exp: false,
335 }))
336 })
337 t.Run("int float", func(t *testing.T) {
338 t.Run("less", run(compareTestCase{
339 a: model.NewIntValue(1),
340 b: model.NewFloatValue(2),
341 exp: true,
342 }))
343 t.Run("greater", run(compareTestCase{
344 a: model.NewIntValue(2),
345 b: model.NewFloatValue(1),
346 exp: false,
347 }))
348 t.Run("equal", run(compareTestCase{
349 a: model.NewIntValue(1),
350 b: model.NewFloatValue(1),
351 exp: false,
352 }))
353 })
354 t.Run("float int", func(t *testing.T) {
355 t.Run("less", run(compareTestCase{
356 a: model.NewFloatValue(1.1),
357 b: model.NewIntValue(2),
358 exp: true,
359 }))
360 t.Run("greater", run(compareTestCase{
361 a: model.NewFloatValue(2),
362 b: model.NewIntValue(1),
363 exp: false,
364 }))
365 t.Run("equal", run(compareTestCase{
366 a: model.NewFloatValue(1),
367 b: model.NewIntValue(1),
368 exp: false,
369 }))
370 })
371 t.Run("string", func(t *testing.T) {
372 t.Run("less", run(compareTestCase{
373 a: model.NewStringValue("a"),
374 b: model.NewStringValue("b"),
375 exp: true,
376 }))
377 t.Run("greater", run(compareTestCase{
378 a: model.NewStringValue("b"),
379 b: model.NewStringValue("a"),
380 exp: false,
381 }))
382 t.Run("equal", run(compareTestCase{
383 a: model.NewStringValue("a"),
384 b: model.NewStringValue("a"),
385 exp: false,
386 }))
387 })
388 }
389
390 func TestValue_LessThanOrEqual(t *testing.T) {
391 run := func(tc compareTestCase) func(t *testing.T) {
392 return func(t *testing.T) {
393 got, err := tc.a.LessThanOrEqual(tc.b)
394 if err != nil {
395 t.Errorf("unexpected error: %s", err)
396 return
397 }
398 gotBool, err := got.BoolValue()
399 if err != nil {
400 t.Errorf("unexpected error: %s", err)
401 return
402 }
403 if gotBool != tc.exp {
404 t.Errorf("expected %v, got %v", tc.exp, got)
405 }
406 }
407 }
408 t.Run("int", func(t *testing.T) {
409 t.Run("less", run(compareTestCase{
410 a: model.NewIntValue(1),
411 b: model.NewIntValue(2),
412 exp: true,
413 }))
414 t.Run("greater", run(compareTestCase{
415 a: model.NewIntValue(2),
416 b: model.NewIntValue(1),
417 exp: false,
418 }))
419 t.Run("equal", run(compareTestCase{
420 a: model.NewIntValue(1),
421 b: model.NewIntValue(1),
422 exp: true,
423 }))
424 })
425 t.Run("float", func(t *testing.T) {
426 t.Run("less", run(compareTestCase{
427 a: model.NewFloatValue(1.1),
428 b: model.NewFloatValue(1.2),
429 exp: true,
430 }))
431 t.Run("greater", run(compareTestCase{
432 a: model.NewFloatValue(1.2),
433 b: model.NewFloatValue(1.1),
434 exp: false,
435 }))
436 t.Run("equal", run(compareTestCase{
437 a: model.NewFloatValue(1.1),
438 b: model.NewFloatValue(1.1),
439 exp: true,
440 }))
441 })
442 t.Run("int float", func(t *testing.T) {
443 t.Run("less", run(compareTestCase{
444 a: model.NewIntValue(1),
445 b: model.NewFloatValue(2),
446 exp: true,
447 }))
448 t.Run("greater", run(compareTestCase{
449 a: model.NewIntValue(2),
450 b: model.NewFloatValue(1),
451 exp: false,
452 }))
453 t.Run("equal", run(compareTestCase{
454 a: model.NewIntValue(1),
455 b: model.NewFloatValue(1),
456 exp: true,
457 }))
458 })
459 t.Run("float int", func(t *testing.T) {
460 t.Run("less", run(compareTestCase{
461 a: model.NewFloatValue(1.1),
462 b: model.NewIntValue(2),
463 exp: true,
464 }))
465 t.Run("greater", run(compareTestCase{
466 a: model.NewFloatValue(2),
467 b: model.NewIntValue(1),
468 exp: false,
469 }))
470 t.Run("equal", run(compareTestCase{
471 a: model.NewFloatValue(1),
472 b: model.NewIntValue(1),
473 exp: true,
474 }))
475 })
476 t.Run("string", func(t *testing.T) {
477 t.Run("less", run(compareTestCase{
478 a: model.NewStringValue("a"),
479 b: model.NewStringValue("b"),
480 exp: true,
481 }))
482 t.Run("greater", run(compareTestCase{
483 a: model.NewStringValue("b"),
484 b: model.NewStringValue("a"),
485 exp: false,
486 }))
487 t.Run("equal", run(compareTestCase{
488 a: model.NewStringValue("a"),
489 b: model.NewStringValue("a"),
490 exp: true,
491 }))
492 })
493 }
494
495 func TestValue_GreaterThan(t *testing.T) {
496 run := func(tc compareTestCase) func(t *testing.T) {
497 return func(t *testing.T) {
498 got, err := tc.a.GreaterThan(tc.b)
499 if err != nil {
500 t.Errorf("unexpected error: %s", err)
501 return
502 }
503 gotBool, err := got.BoolValue()
504 if err != nil {
505 t.Errorf("unexpected error: %s", err)
506 return
507 }
508 if gotBool != tc.exp {
509 t.Errorf("expected %v, got %v", tc.exp, got)
510 }
511 }
512 }
513 t.Run("int", func(t *testing.T) {
514 t.Run("less", run(compareTestCase{
515 a: model.NewIntValue(1),
516 b: model.NewIntValue(2),
517 exp: false,
518 }))
519 t.Run("greater", run(compareTestCase{
520 a: model.NewIntValue(2),
521 b: model.NewIntValue(1),
522 exp: true,
523 }))
524 t.Run("equal", run(compareTestCase{
525 a: model.NewIntValue(1),
526 b: model.NewIntValue(1),
527 exp: false,
528 }))
529 })
530 t.Run("float", func(t *testing.T) {
531 t.Run("less", run(compareTestCase{
532 a: model.NewFloatValue(1.1),
533 b: model.NewFloatValue(1.2),
534 exp: false,
535 }))
536 t.Run("greater", run(compareTestCase{
537 a: model.NewFloatValue(1.2),
538 b: model.NewFloatValue(1.1),
539 exp: true,
540 }))
541 t.Run("equal", run(compareTestCase{
542 a: model.NewFloatValue(1.1),
543 b: model.NewFloatValue(1.1),
544 exp: false,
545 }))
546 })
547 t.Run("int float", func(t *testing.T) {
548 t.Run("less", run(compareTestCase{
549 a: model.NewIntValue(1),
550 b: model.NewFloatValue(2),
551 exp: false,
552 }))
553 t.Run("greater", run(compareTestCase{
554 a: model.NewIntValue(2),
555 b: model.NewFloatValue(1),
556 exp: true,
557 }))
558 t.Run("equal", run(compareTestCase{
559 a: model.NewIntValue(1),
560 b: model.NewFloatValue(1),
561 exp: false,
562 }))
563 })
564 t.Run("float int", func(t *testing.T) {
565 t.Run("less", run(compareTestCase{
566 a: model.NewFloatValue(1.1),
567 b: model.NewIntValue(2),
568 exp: false,
569 }))
570 t.Run("greater", run(compareTestCase{
571 a: model.NewFloatValue(2),
572 b: model.NewIntValue(1),
573 exp: true,
574 }))
575 t.Run("equal", run(compareTestCase{
576 a: model.NewFloatValue(1),
577 b: model.NewIntValue(1),
578 exp: false,
579 }))
580 })
581 t.Run("string", func(t *testing.T) {
582 t.Run("less", run(compareTestCase{
583 a: model.NewStringValue("a"),
584 b: model.NewStringValue("b"),
585 exp: false,
586 }))
587 t.Run("greater", run(compareTestCase{
588 a: model.NewStringValue("b"),
589 b: model.NewStringValue("a"),
590 exp: true,
591 }))
592 t.Run("equal", run(compareTestCase{
593 a: model.NewStringValue("a"),
594 b: model.NewStringValue("a"),
595 exp: false,
596 }))
597 })
598 }
599
600 func TestValue_GreaterThanOrEqual(t *testing.T) {
601 run := func(tc compareTestCase) func(t *testing.T) {
602 return func(t *testing.T) {
603 got, err := tc.a.GreaterThanOrEqual(tc.b)
604 if err != nil {
605 t.Errorf("unexpected error: %s", err)
606 return
607 }
608 gotBool, err := got.BoolValue()
609 if err != nil {
610 t.Errorf("unexpected error: %s", err)
611 return
612 }
613 if gotBool != tc.exp {
614 t.Errorf("expected %v, got %v", tc.exp, got)
615 }
616 }
617 }
618 t.Run("int", func(t *testing.T) {
619 t.Run("less", run(compareTestCase{
620 a: model.NewIntValue(1),
621 b: model.NewIntValue(2),
622 exp: false,
623 }))
624 t.Run("greater", run(compareTestCase{
625 a: model.NewIntValue(2),
626 b: model.NewIntValue(1),
627 exp: true,
628 }))
629 t.Run("equal", run(compareTestCase{
630 a: model.NewIntValue(1),
631 b: model.NewIntValue(1),
632 exp: true,
633 }))
634 })
635 t.Run("float", func(t *testing.T) {
636 t.Run("less", run(compareTestCase{
637 a: model.NewFloatValue(1.1),
638 b: model.NewFloatValue(1.2),
639 exp: false,
640 }))
641 t.Run("greater", run(compareTestCase{
642 a: model.NewFloatValue(1.2),
643 b: model.NewFloatValue(1.1),
644 exp: true,
645 }))
646 t.Run("equal", run(compareTestCase{
647 a: model.NewFloatValue(1.1),
648 b: model.NewFloatValue(1.1),
649 exp: true,
650 }))
651 })
652 t.Run("int float", func(t *testing.T) {
653 t.Run("less", run(compareTestCase{
654 a: model.NewIntValue(1),
655 b: model.NewFloatValue(2),
656 exp: false,
657 }))
658 t.Run("greater", run(compareTestCase{
659 a: model.NewIntValue(2),
660 b: model.NewFloatValue(1),
661 exp: true,
662 }))
663 t.Run("equal", run(compareTestCase{
664 a: model.NewIntValue(1),
665 b: model.NewFloatValue(1),
666 exp: true,
667 }))
668 })
669 t.Run("float int", func(t *testing.T) {
670 t.Run("less", run(compareTestCase{
671 a: model.NewFloatValue(1.1),
672 b: model.NewIntValue(2),
673 exp: false,
674 }))
675 t.Run("greater", run(compareTestCase{
676 a: model.NewFloatValue(2),
677 b: model.NewIntValue(1),
678 exp: true,
679 }))
680 t.Run("equal", run(compareTestCase{
681 a: model.NewFloatValue(1),
682 b: model.NewIntValue(1),
683 exp: true,
684 }))
685 })
686 t.Run("string", func(t *testing.T) {
687 t.Run("less", run(compareTestCase{
688 a: model.NewStringValue("a"),
689 b: model.NewStringValue("b"),
690 exp: false,
691 }))
692 t.Run("greater", run(compareTestCase{
693 a: model.NewStringValue("b"),
694 b: model.NewStringValue("a"),
695 exp: true,
696 }))
697 t.Run("equal", run(compareTestCase{
698 a: model.NewStringValue("a"),
699 b: model.NewStringValue("a"),
700 exp: true,
701 }))
702 })
703 }
704
705 func TestValue_Compare(t *testing.T) {
706 run := func(a *model.Value, b *model.Value, exp int) func(t *testing.T) {
707 return func(t *testing.T) {
708 got, err := a.Compare(b)
709 if err != nil {
710 t.Errorf("unexpected error: %s", err)
711 return
712 }
713 if got != exp {
714 t.Errorf("expected %d, got %d", exp, got)
715 }
716 }
717 }
718 t.Run("int", func(t *testing.T) {
719 t.Run("less", run(
720 model.NewIntValue(1),
721 model.NewIntValue(2),
722 -1,
723 ))
724 t.Run("greater", run(
725 model.NewIntValue(2),
726 model.NewIntValue(1),
727 1,
728 ))
729 t.Run("equal", run(
730 model.NewIntValue(1),
731 model.NewIntValue(1),
732 0,
733 ))
734 })
735 t.Run("float", func(t *testing.T) {
736 t.Run("less", run(
737 model.NewFloatValue(1.1),
738 model.NewFloatValue(1.2),
739 -1,
740 ))
741 t.Run("greater", run(
742 model.NewFloatValue(1.2),
743 model.NewFloatValue(1.1),
744 1,
745 ))
746 t.Run("equal", run(
747 model.NewFloatValue(1.1),
748 model.NewFloatValue(1.1),
749 0,
750 ))
751 })
752 t.Run("int float", func(t *testing.T) {
753 t.Run("less", run(
754 model.NewIntValue(1),
755 model.NewFloatValue(2),
756 -1,
757 ))
758 t.Run("greater", run(
759 model.NewIntValue(2),
760 model.NewFloatValue(1),
761 1,
762 ))
763 t.Run("equal", run(
764 model.NewIntValue(1),
765 model.NewFloatValue(1),
766 0,
767 ))
768 })
769 t.Run("float int", func(t *testing.T) {
770 t.Run("less", run(
771 model.NewFloatValue(1.1),
772 model.NewIntValue(2),
773 -1,
774 ))
775 t.Run("greater", run(
776 model.NewFloatValue(1.1),
777 model.NewIntValue(1),
778 1,
779 ))
780 t.Run("equal", run(
781 model.NewFloatValue(1),
782 model.NewIntValue(1),
783 0,
784 ))
785 })
786 t.Run("string", func(t *testing.T) {
787 t.Run("less", run(
788 model.NewStringValue("a"),
789 model.NewStringValue("b"),
790 -1,
791 ))
792 t.Run("greater", run(
793 model.NewStringValue("b"),
794 model.NewStringValue("a"),
795 1,
796 ))
797 t.Run("equal", run(
798 model.NewStringValue("a"),
799 model.NewStringValue("a"),
800 0,
801 ))
802 })
803 }