File: compare_test.go

package info (click to toggle)
golang-github-wader-gojq 0.0~git20231105.2b6d9e2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 960 kB
  • sloc: yacc: 642; makefile: 84
file content (74 lines) | stat: -rw-r--r-- 1,899 bytes parent folder | download
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
package gojq_test

import (
	"fmt"
	"math"
	"math/big"
	"testing"

	"github.com/wader/gojq"
)

func TestCompare(t *testing.T) {
	testCases := []struct {
		l, r     any
		expected int
	}{
		{nil, nil, 0},
		{nil, false, -1},
		{false, nil, 1},
		{false, false, 0},
		{false, true, -1},
		{true, false, 1},
		{true, true, 0},
		{true, 0, -1},
		{0, true, 1},
		{0, 0, 0},
		{0, 1, -1},
		{1, 0, 1},
		{1, 1, 0},
		{0, math.NaN(), 1},
		{math.NaN(), 0, -1},
		{math.NaN(), math.NaN(), -1},
		{1, 1.00, 0},
		{1.00, 1, 0},
		{1.00, 1.01, -1},
		{1.01, 1.00, 1},
		{1.01, 1.01, 0},
		{1, big.NewInt(0), 1},
		{big.NewInt(0), 1, -1},
		{0, big.NewInt(0), 0},
		{1, "", -1},
		{"", 1, 1},
		{"", "", 0},
		{"", "abc", -1},
		{"abc", "", 1},
		{"abc", "abc", 0},
		{"", []any{}, -1},
		{[]any{}, "", 1},
		{[]any{}, []any{}, 0},
		{[]any{}, []any{nil}, -1},
		{[]any{nil}, []any{}, 1},
		{[]any{nil}, []any{nil}, 0},
		{[]any{0, 1, 2}, []any{0, 1, nil}, 1},
		{[]any{0, 1, 2}, []any{0, 1, 2, nil}, -1},
		{[]any{0, 1, 2, false, nil}, []any{0, 1, 2, nil, false}, 1},
		{[]any{}, map[string]any{}, -1},
		{map[string]any{}, []any{}, 1},
		{map[string]any{}, map[string]any{}, 0},
		{map[string]any{"a": nil}, map[string]any{"a": nil}, 0},
		{map[string]any{"a": nil}, map[string]any{"a": nil, "b": nil}, -1},
		{map[string]any{"a": nil, "b": nil}, map[string]any{"a": nil, "c": nil}, -1},
		{map[string]any{"a": 0, "b": 0, "c": 0}, map[string]any{"a": 0, "b": 0, "c": 0}, 0},
		{map[string]any{"a": 0, "b": 0, "d": 0}, map[string]any{"a": 0, "b": 1, "c": 0}, 1},
		{map[string]any{"a": 0, "b": 1, "c": 2}, map[string]any{"a": 0, "b": 2, "c": 1}, -1},
	}
	for _, tc := range testCases {
		t.Run(fmt.Sprintf("%v,%v", tc.l, tc.r), func(t *testing.T) {
			got := gojq.Compare(tc.l, tc.r)
			if got != tc.expected {
				t.Errorf("Compare(%v, %v): got %d, expected %d", tc.l, tc.r, got, tc.expected)
			}
		})
	}
}