| 1 |
// Copyright (c) 2024-2025, Maxime Soulé |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// This source code is licensed under the BSD-style license found in the |
| 5 |
// LICENSE file in the root directory of this source tree. |
| 6 |
|
| 7 |
package td |
| 8 |
|
| 9 |
import ( |
| 10 |
"fmt" |
| 11 |
"reflect" |
| 12 |
"sort" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
|
| 16 |
"github.com/maxatome/go-testdeep/internal/test" |
| 17 |
"github.com/maxatome/go-testdeep/internal/types" |
| 18 |
) |
| 19 |
|
| 20 |
type sortA struct{ a, b, c int } |
| 21 |
|
| 22 |
type sortB struct { |
| 23 |
name string |
| 24 |
idx int |
| 25 |
} |
| 26 |
|
| 27 |
func (b sortB) Compare(x sortB) int { |
| 28 |
bn, xn := strings.ToLower(b.name), strings.ToLower(x.name) |
| 29 |
if bn == xn { |
| 30 |
return b.idx - x.idx |
| 31 |
} |
| 32 |
if bn < xn { |
| 33 |
return -1 |
| 34 |
} |
| 35 |
return 1 |
| 36 |
} |
| 37 |
|
| 38 |
func TestInitSortBase(t *testing.T) { |
| 39 |
newSortA := func() []sortA { |
| 40 |
return []sortA{ |
| 41 |
{2, 3, 2}, |
| 42 |
{1, 2, 3}, |
| 43 |
{3, 1, 2}, |
| 44 |
{2, 4, 2}, |
| 45 |
{1, 2, 4}, |
| 46 |
{2, 3, 1}, |
| 47 |
} |
| 48 |
} |
| 49 |
|
| 50 |
testCases := []struct { |
| 51 |
name string |
| 52 |
how any |
| 53 |
slice any |
| 54 |
got2str func(any) string |
| 55 |
expected string |
| 56 |
}{ |
| 57 |
{ |
| 58 |
name: "mkSortAsc", |
| 59 |
how: 0, |
| 60 |
slice: []int{3, 5, 2, 1, 4}, |
| 61 |
expected: "[1 2 3 4 5]", |
| 62 |
}, |
| 63 |
{ |
| 64 |
name: "mkSortDesc", |
| 65 |
how: -1, |
| 66 |
slice: []int{3, 5, 2, 1, 4}, |
| 67 |
expected: "[5 4 3 2 1]", |
| 68 |
}, |
| 69 |
{ |
| 70 |
name: "mkSortAsc-float64", |
| 71 |
how: float64(1), |
| 72 |
slice: []int{3, 5, 2, 1, 4}, |
| 73 |
expected: "[1 2 3 4 5]", |
| 74 |
}, |
| 75 |
{ |
| 76 |
name: "mkSortDesc-float64", |
| 77 |
how: float64(-1), |
| 78 |
slice: []int{3, 5, 2, 1, 4}, |
| 79 |
expected: "[5 4 3 2 1]", |
| 80 |
}, |
| 81 |
{ |
| 82 |
name: "mkSortAsc-Compare", |
| 83 |
how: 1, |
| 84 |
slice: []sortB{{"Zb", 1}, {"za", 8}, {"a", 4}, {"A", 5}}, |
| 85 |
expected: "[{a 4} {A 5} {za 8} {Zb 1}]", |
| 86 |
}, |
| 87 |
{ |
| 88 |
name: "mkSortFieldsPaths-asc1-1field", |
| 89 |
how: "b", |
| 90 |
slice: newSortA(), |
| 91 |
expected: "[{3 1 2} {1 2 3} {1 2 4} {2 3 1} {2 3 2} {2 4 2}]", |
| 92 |
}, |
| 93 |
{ |
| 94 |
name: "mkSortFieldsPaths-asc1-1field-plus", |
| 95 |
how: "+b", |
| 96 |
slice: newSortA(), |
| 97 |
expected: "[{3 1 2} {1 2 3} {1 2 4} {2 3 1} {2 3 2} {2 4 2}]", |
| 98 |
}, |
| 99 |
{ |
| 100 |
name: "mkSortFieldsPaths-desc1", |
| 101 |
how: []string{"-b"}, |
| 102 |
slice: newSortA(), |
| 103 |
expected: "[{2 4 2} {2 3 1} {2 3 2} {1 2 3} {1 2 4} {3 1 2}]", |
| 104 |
}, |
| 105 |
{ |
| 106 |
name: "mkSortFieldsPaths-multi", |
| 107 |
how: []string{"-a", "b", "-c"}, |
| 108 |
slice: newSortA(), |
| 109 |
expected: "[{3 1 2} {2 3 2} {2 3 1} {2 4 2} {1 2 4} {1 2 3}]", |
| 110 |
}, |
| 111 |
{ |
| 112 |
name: "mkSortFieldsPaths-deref", |
| 113 |
how: []string{"-b"}, |
| 114 |
slice: func() []any { |
| 115 |
sl := newSortA() |
| 116 |
var res []any //nolint: prealloc |
| 117 |
for _, v := range sl { |
| 118 |
a := v |
| 119 |
b := &a |
| 120 |
res = append(res, &b) |
| 121 |
} |
| 122 |
var ps *sortA |
| 123 |
n := 18 |
| 124 |
var pn *int |
| 125 |
return append(res, &ps, &pn, (**sortA)(nil), n, nil, &n) |
| 126 |
}(), |
| 127 |
got2str: func(got any) string { |
| 128 |
sl := got.([]any) |
| 129 |
sl2 := make([]any, len(sl)) |
| 130 |
for i, e := range sl { |
| 131 |
switch se := e.(type) { |
| 132 |
case nil: |
| 133 |
sl2[i] = "nil" |
| 134 |
case int: |
| 135 |
sl2[i] = e |
| 136 |
case *int: |
| 137 |
if se == nil { |
| 138 |
sl2[i] = "(*int)(nil)" |
| 139 |
} else { |
| 140 |
sl2[i] = fmt.Sprintf("&%d", *se) |
| 141 |
} |
| 142 |
case **int: |
| 143 |
switch { |
| 144 |
case se == nil: |
| 145 |
sl2[i] = "(**int)(nil)" |
| 146 |
case *se == nil: |
| 147 |
sl2[i] = "(**int)(&nil)" |
| 148 |
default: |
| 149 |
sl2[i] = fmt.Sprintf("&&%d", **se) |
| 150 |
} |
| 151 |
case **sortA: |
| 152 |
switch { |
| 153 |
case se == nil: |
| 154 |
sl2[i] = "(**sortA:)(nil)" |
| 155 |
case *se == nil: |
| 156 |
sl2[i] = "(**sortA:)(&nil)" |
| 157 |
default: |
| 158 |
sl2[i] = **se |
| 159 |
} |
| 160 |
default: |
| 161 |
sl2[i] = fmt.Sprintf("%[1]T(%[1]v)", se) |
| 162 |
} |
| 163 |
} |
| 164 |
return fmt.Sprintf("%v", sl2) |
| 165 |
}, |
| 166 |
expected: "[{2 4 2} {2 3 1} {2 3 2} {1 2 3} {1 2 4} {3 1 2} nil (**int)(&nil) (**sortA:)(nil) (**sortA:)(&nil) &18 18]", |
| 167 |
}, |
| 168 |
{ |
| 169 |
name: "mkSortFieldsPaths-unknown", |
| 170 |
how: []string{"a", "unknown"}, |
| 171 |
slice: newSortA(), |
| 172 |
expected: "[{1 2 3} {1 2 4} {2 3 1} {2 3 2} {2 4 2} {3 1 2}]", |
| 173 |
}, |
| 174 |
{ |
| 175 |
name: "custom func", |
| 176 |
how: func(a, b int) bool { return a > b }, |
| 177 |
slice: []int{3, 5, 2, 1, 4}, |
| 178 |
expected: "[5 4 3 2 1]", |
| 179 |
}, |
| 180 |
} |
| 181 |
for _, tc := range testCases { |
| 182 |
t.Run(tc.name, func(t *testing.T) { |
| 183 |
var sb tdSortBase |
| 184 |
err := sb.initSortBase(tc.how) |
| 185 |
test.NoError(t, err) |
| 186 |
fn, err := sb.mkSortFn(reflect.TypeOf(tc.slice).Elem()) |
| 187 |
test.NoError(t, err) |
| 188 |
sl := reflect.ValueOf(tc.slice) |
| 189 |
sort.SliceStable(tc.slice, func(i, j int) bool { |
| 190 |
return fn.Call([]reflect.Value{sl.Index(i), sl.Index(j)})[0].Bool() |
| 191 |
}) |
| 192 |
var got string |
| 193 |
if tc.got2str != nil { |
| 194 |
got = tc.got2str(tc.slice) |
| 195 |
} else { |
| 196 |
got = fmt.Sprintf("%v", tc.slice) |
| 197 |
} |
| 198 |
test.EqualStr(t, got, tc.expected) |
| 199 |
}) |
| 200 |
} |
| 201 |
|
| 202 |
t.Run("Error", func(t *testing.T) { |
| 203 |
testCases := []struct { |
| 204 |
name string |
| 205 |
how []any |
| 206 |
expectedErr string |
| 207 |
}{ |
| 208 |
{ |
| 209 |
name: "not a string", |
| 210 |
how: []any{"ok", false}, |
| 211 |
expectedErr: "string... expected but received bool as 2nd parameter", |
| 212 |
}, |
| 213 |
{ |
| 214 |
name: "any slice as used in JSON", |
| 215 |
how: []any{[]any{"zzz", true}}, |
| 216 |
expectedErr: "slice of strings expected as how, bool encountered at pos 1", |
| 217 |
}, |
| 218 |
{ |
| 219 |
name: "unknown how", |
| 220 |
how: []any{true}, |
| 221 |
expectedErr: "but received bool as 1st parameter", |
| 222 |
}, |
| 223 |
{ |
| 224 |
name: "bad func variadic", |
| 225 |
how: []any{func(...int) bool { return true }}, |
| 226 |
expectedErr: "SORT_FUNC must match func(T, T) bool signature, not func(...int) bool", |
| 227 |
}, |
| 228 |
{ |
| 229 |
name: "bad func num in1", |
| 230 |
how: []any{func(a int) bool { return true }}, |
| 231 |
expectedErr: "SORT_FUNC must match func(T, T) bool signature, not func(int) bool", |
| 232 |
}, |
| 233 |
{ |
| 234 |
name: "bad func num in3", |
| 235 |
how: []any{func(a, b, c int) bool { return true }}, |
| 236 |
expectedErr: "SORT_FUNC must match func(T, T) bool signature, not func(int, int, int) bool", |
| 237 |
}, |
| 238 |
{ |
| 239 |
name: "bad func in types", |
| 240 |
how: []any{func(a int, b bool) bool { return true }}, |
| 241 |
expectedErr: "SORT_FUNC must match func(T, T) bool signature, not func(int, bool) bool", |
| 242 |
}, |
| 243 |
{ |
| 244 |
name: "bad func num out0", |
| 245 |
how: []any{func(a, b int) {}}, |
| 246 |
expectedErr: "SORT_FUNC must match func(T, T) bool signature, not func(int, int)", |
| 247 |
}, |
| 248 |
{ |
| 249 |
name: "bad func num out2", |
| 250 |
how: []any{func(a, b int) (bool, bool) { return true, true }}, |
| 251 |
expectedErr: "SORT_FUNC must match func(T, T) bool signature, not func(int, int) (bool, bool)", |
| 252 |
}, |
| 253 |
{ |
| 254 |
name: "bad func out type", |
| 255 |
how: []any{func(a, b int) int { return 0 }}, |
| 256 |
expectedErr: "SORT_FUNC must match func(T, T) bool signature, not func(int, int) int", |
| 257 |
}, |
| 258 |
} |
| 259 |
for _, tc := range testCases { |
| 260 |
t.Run(tc.name, func(t *testing.T) { |
| 261 |
var sb tdSortBase |
| 262 |
err := sb.initSortBase(tc.how...) |
| 263 |
if sb.mkSortFn != nil { |
| 264 |
t.Error("sortFunc should return nil function") |
| 265 |
} |
| 266 |
if test.Error(t, err) { |
| 267 |
test.EqualStr(t, err.Error(), tc.expectedErr) |
| 268 |
} |
| 269 |
}) |
| 270 |
} |
| 271 |
|
| 272 |
t.Run("custom func", func(t *testing.T) { |
| 273 |
var sb tdSortBase |
| 274 |
err := sb.initSortBase(func(a, b int) bool { return a > b }) |
| 275 |
test.NoError(t, err) |
| 276 |
_, err = sb.mkSortFn(types.Bool) |
| 277 |
if test.Error(t, err) { |
| 278 |
test.EqualStr(t, err.Error(), "bool is not assignable to int") |
| 279 |
} |
| 280 |
}) |
| 281 |
}) |
| 282 |
} |