| 1 |
// Copyright (c) 2020, 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 hooks_test |
| 8 |
|
| 9 |
import ( |
| 10 |
"errors" |
| 11 |
"net" |
| 12 |
"reflect" |
| 13 |
"strconv" |
| 14 |
"strings" |
| 15 |
"testing" |
| 16 |
"time" |
| 17 |
|
| 18 |
"github.com/maxatome/go-testdeep/internal/hooks" |
| 19 |
"github.com/maxatome/go-testdeep/internal/test" |
| 20 |
) |
| 21 |
|
| 22 |
func TestAddCmpHooks(t *testing.T) { |
| 23 |
for _, tst := range []struct { |
| 24 |
name string |
| 25 |
cmp any |
| 26 |
err string |
| 27 |
}{ |
| 28 |
{ |
| 29 |
name: "not a function", |
| 30 |
cmp: "zip", |
| 31 |
err: "expects a function, not a string (@1)", |
| 32 |
}, |
| 33 |
{ |
| 34 |
name: "no variadic", |
| 35 |
cmp: func(a []byte, b ...byte) bool { return true }, |
| 36 |
err: "expects: func (T, T) bool|error not func([]uint8, ...uint8) bool (@1)", |
| 37 |
}, |
| 38 |
{ |
| 39 |
name: "in", |
| 40 |
cmp: func(a, b, c int) bool { return true }, |
| 41 |
err: "expects: func (T, T) bool|error not func(int, int, int) bool (@1)", |
| 42 |
}, |
| 43 |
{ |
| 44 |
name: "out", |
| 45 |
cmp: func(a, b int) {}, |
| 46 |
err: "expects: func (T, T) bool|error not func(int, int) (@1)", |
| 47 |
}, |
| 48 |
{ |
| 49 |
name: "type mismatch", |
| 50 |
cmp: func(a int, b bool) bool { return true }, |
| 51 |
err: "expects: func (T, T) bool|error not func(int, bool) bool (@1)", |
| 52 |
}, |
| 53 |
{ |
| 54 |
name: "interface", |
| 55 |
cmp: func(a, b any) bool { return true }, |
| 56 |
err: "expects: func (T, T) bool|error not func(interface {}, interface {}) bool (@1)", |
| 57 |
}, |
| 58 |
{ |
| 59 |
name: "bad return", |
| 60 |
cmp: func(a, b int) int { return 0 }, |
| 61 |
err: "expects: func (T, T) bool|error not func(int, int) int (@1)", |
| 62 |
}, |
| 63 |
} { |
| 64 |
i := hooks.NewInfo() |
| 65 |
|
| 66 |
err := i.AddCmpHooks([]any{ |
| 67 |
func(a, b bool) bool { return true }, |
| 68 |
tst.cmp, |
| 69 |
}) |
| 70 |
if test.Error(t, err, tst.name) { |
| 71 |
if !strings.Contains(err.Error(), tst.err) { |
| 72 |
t.Errorf("<%s> does not contain <%s> for %s", err, tst.err, tst.name) |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
func TestCmp(t *testing.T) { |
| 79 |
t.Run("bool", func(t *testing.T) { |
| 80 |
var i *hooks.Info |
| 81 |
|
| 82 |
handled, err := i.Cmp(reflect.ValueOf(12), reflect.ValueOf(12)) |
| 83 |
test.NoError(t, err) |
| 84 |
test.IsFalse(t, handled) |
| 85 |
|
| 86 |
i = hooks.NewInfo() |
| 87 |
|
| 88 |
err = i.AddCmpHooks([]any{func(a, b int) bool { return a == b }}) |
| 89 |
test.NoError(t, err) |
| 90 |
|
| 91 |
handled, err = i.Cmp(reflect.ValueOf(12), reflect.ValueOf(12)) |
| 92 |
test.NoError(t, err) |
| 93 |
test.IsTrue(t, handled) |
| 94 |
|
| 95 |
handled, err = i.Cmp(reflect.ValueOf(12), reflect.ValueOf(34)) |
| 96 |
if err != hooks.ErrBoolean { |
| 97 |
test.EqualErrorMessage(t, err, hooks.ErrBoolean) |
| 98 |
} |
| 99 |
test.IsTrue(t, handled) |
| 100 |
|
| 101 |
handled, err = i.Cmp(reflect.ValueOf(12), reflect.ValueOf("twelve")) |
| 102 |
test.NoError(t, err) |
| 103 |
test.IsFalse(t, handled) |
| 104 |
|
| 105 |
handled, err = i.Cmp(reflect.ValueOf("twelve"), reflect.ValueOf("twelve")) |
| 106 |
test.NoError(t, err) |
| 107 |
test.IsFalse(t, handled) |
| 108 |
|
| 109 |
handled, err = (*hooks.Info)(nil).Cmp(reflect.ValueOf(1), reflect.ValueOf(2)) |
| 110 |
test.NoError(t, err) |
| 111 |
test.IsFalse(t, handled) |
| 112 |
}) |
| 113 |
|
| 114 |
t.Run("error", func(t *testing.T) { |
| 115 |
i := hooks.NewInfo() |
| 116 |
|
| 117 |
diffErr := errors.New("a≠b") |
| 118 |
|
| 119 |
err := i.AddCmpHooks([]any{ |
| 120 |
func(a, b int) error { |
| 121 |
if a == b { |
| 122 |
return nil |
| 123 |
} |
| 124 |
return diffErr |
| 125 |
}, |
| 126 |
}) |
| 127 |
test.NoError(t, err) |
| 128 |
|
| 129 |
handled, err := i.Cmp(reflect.ValueOf(12), reflect.ValueOf(12)) |
| 130 |
test.NoError(t, err) |
| 131 |
test.IsTrue(t, handled) |
| 132 |
|
| 133 |
handled, err = i.Cmp(reflect.ValueOf(12), reflect.ValueOf(34)) |
| 134 |
if err != diffErr { |
| 135 |
test.EqualErrorMessage(t, err, diffErr) |
| 136 |
} |
| 137 |
test.IsTrue(t, handled) |
| 138 |
}) |
| 139 |
} |
| 140 |
|
| 141 |
func TestSmuggle(t *testing.T) { |
| 142 |
var i *hooks.Info |
| 143 |
|
| 144 |
got := reflect.ValueOf(123) |
| 145 |
handled, err := i.Smuggle(&got) |
| 146 |
test.NoError(t, err) |
| 147 |
test.IsFalse(t, handled) |
| 148 |
|
| 149 |
i = hooks.NewInfo() |
| 150 |
|
| 151 |
err = i.AddSmuggleHooks([]any{func(a int) bool { return a != 0 }}) |
| 152 |
test.NoError(t, err) |
| 153 |
|
| 154 |
got = reflect.ValueOf(123) |
| 155 |
handled, err = i.Smuggle(&got) |
| 156 |
test.NoError(t, err) |
| 157 |
test.IsTrue(t, handled) |
| 158 |
if test.EqualInt(t, int(got.Kind()), int(reflect.Bool)) { |
| 159 |
test.IsTrue(t, got.Bool()) |
| 160 |
} |
| 161 |
|
| 162 |
got = reflect.ValueOf("biz") |
| 163 |
handled, err = i.Smuggle(&got) |
| 164 |
test.NoError(t, err) |
| 165 |
test.IsFalse(t, handled) |
| 166 |
test.EqualStr(t, got.String(), "biz") |
| 167 |
|
| 168 |
err = i.AddSmuggleHooks([]any{strconv.Atoi}) |
| 169 |
test.NoError(t, err) |
| 170 |
|
| 171 |
got = reflect.ValueOf("123") |
| 172 |
handled, err = i.Smuggle(&got) |
| 173 |
test.NoError(t, err) |
| 174 |
test.IsTrue(t, handled) |
| 175 |
if test.EqualInt(t, int(got.Kind()), int(reflect.Int)) { |
| 176 |
test.EqualInt(t, int(got.Int()), 123) |
| 177 |
} |
| 178 |
|
| 179 |
got = reflect.ValueOf("NotANumber") |
| 180 |
handled, err = i.Smuggle(&got) |
| 181 |
test.Error(t, err) |
| 182 |
test.IsTrue(t, handled) |
| 183 |
} |
| 184 |
|
| 185 |
func TestAddSmuggleHooks(t *testing.T) { |
| 186 |
for _, tst := range []struct { |
| 187 |
name string |
| 188 |
smuggle any |
| 189 |
err string |
| 190 |
}{ |
| 191 |
{ |
| 192 |
name: "not a function", |
| 193 |
smuggle: "zip", |
| 194 |
err: "expects a function, not a string (@1)", |
| 195 |
}, |
| 196 |
{ |
| 197 |
name: "no variadic", |
| 198 |
smuggle: func(a ...byte) bool { return true }, |
| 199 |
err: "expects: func (A) (B[, error]) not func(...uint8) bool (@1)", |
| 200 |
}, |
| 201 |
{ |
| 202 |
name: "in", |
| 203 |
smuggle: func(a, b int) bool { return true }, |
| 204 |
err: "expects: func (A) (B[, error]) not func(int, int) bool (@1)", |
| 205 |
}, |
| 206 |
{ |
| 207 |
name: "interface", |
| 208 |
smuggle: func(a any) bool { return true }, |
| 209 |
err: "expects: func (A) (B[, error]) not func(interface {}) bool (@1)", |
| 210 |
}, |
| 211 |
{ |
| 212 |
name: "out", |
| 213 |
smuggle: func(a int) {}, |
| 214 |
err: "expects: func (A) (B[, error]) not func(int) (@1)", |
| 215 |
}, |
| 216 |
{ |
| 217 |
name: "bad return", |
| 218 |
smuggle: func(a int) (int, int) { return 0, 0 }, |
| 219 |
err: "expects: func (A) (B[, error]) not func(int) (int, int) (@1)", |
| 220 |
}, |
| 221 |
} { |
| 222 |
i := hooks.NewInfo() |
| 223 |
|
| 224 |
err := i.AddSmuggleHooks([]any{ |
| 225 |
func(a int) bool { return true }, |
| 226 |
tst.smuggle, |
| 227 |
}) |
| 228 |
if test.Error(t, err, tst.name) { |
| 229 |
if !strings.Contains(err.Error(), tst.err) { |
| 230 |
t.Errorf("<%s> does not contain <%s> for %s", err, tst.err, tst.name) |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
func TestUseEqual(t *testing.T) { |
| 237 |
var i *hooks.Info |
| 238 |
|
| 239 |
test.IsFalse(t, i.UseEqual(reflect.TypeOf(42))) |
| 240 |
|
| 241 |
i = hooks.NewInfo() |
| 242 |
test.IsFalse(t, i.UseEqual(reflect.TypeOf(42))) |
| 243 |
|
| 244 |
test.NoError(t, i.AddUseEqual([]any{})) |
| 245 |
|
| 246 |
test.NoError(t, i.AddUseEqual([]any{time.Time{}, net.IP{}})) |
| 247 |
test.IsTrue(t, i.UseEqual(reflect.TypeOf(time.Time{}))) |
| 248 |
test.IsTrue(t, i.UseEqual(reflect.TypeOf(net.IP{}))) |
| 249 |
} |
| 250 |
|
| 251 |
func TestAddUseEqual(t *testing.T) { |
| 252 |
for _, tst := range []struct { |
| 253 |
name string |
| 254 |
typ any |
| 255 |
err string |
| 256 |
}{ |
| 257 |
{ |
| 258 |
name: "no Equal() method", |
| 259 |
typ: &testing.T{}, |
| 260 |
err: "expects type *testing.T owns an Equal method (@1)", |
| 261 |
}, |
| 262 |
{ |
| 263 |
name: "variadic Equal() method", |
| 264 |
typ: badEqualVariadic{}, |
| 265 |
err: "expects type hooks_test.badEqualVariadic Equal method signature be Equal(hooks_test.badEqualVariadic) bool (@1)", |
| 266 |
}, |
| 267 |
{ |
| 268 |
name: "bad NumIn Equal() method", |
| 269 |
typ: badEqualNumIn{}, |
| 270 |
err: "expects type hooks_test.badEqualNumIn Equal method signature be Equal(hooks_test.badEqualNumIn) bool (@1)", |
| 271 |
}, |
| 272 |
{ |
| 273 |
name: "bad NumOut Equal() method", |
| 274 |
typ: badEqualNumOut{}, |
| 275 |
err: "expects type hooks_test.badEqualNumOut Equal method signature be Equal(hooks_test.badEqualNumOut) bool (@1)", |
| 276 |
}, |
| 277 |
{ |
| 278 |
name: "In(0) not assignable to In(1) Equal() method", |
| 279 |
typ: badEqualInAssign{}, |
| 280 |
err: "expects type hooks_test.badEqualInAssign Equal method signature be Equal(hooks_test.badEqualInAssign) bool (@1)", |
| 281 |
}, |
| 282 |
{ |
| 283 |
name: "Equal() method don't return bool", |
| 284 |
typ: badEqualOutType{}, |
| 285 |
err: "expects type hooks_test.badEqualOutType Equal method signature be Equal(hooks_test.badEqualOutType) bool (@1)", |
| 286 |
}, |
| 287 |
} { |
| 288 |
i := hooks.NewInfo() |
| 289 |
|
| 290 |
err := i.AddUseEqual([]any{time.Time{}, tst.typ}) |
| 291 |
if test.Error(t, err, tst.name) { |
| 292 |
if !strings.Contains(err.Error(), tst.err) { |
| 293 |
t.Errorf("<%s> does not contain <%s> for %s", err, tst.err, tst.name) |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
func TestIgnoreUnexported(t *testing.T) { |
| 300 |
var i *hooks.Info |
| 301 |
|
| 302 |
test.IsFalse(t, i.IgnoreUnexported(reflect.TypeOf(struct{}{}))) |
| 303 |
|
| 304 |
i = hooks.NewInfo() |
| 305 |
test.IsFalse(t, i.IgnoreUnexported(reflect.TypeOf(struct{}{}))) |
| 306 |
|
| 307 |
test.NoError(t, i.AddIgnoreUnexported([]any{})) |
| 308 |
|
| 309 |
test.NoError(t, i.AddIgnoreUnexported([]any{testing.T{}, time.Time{}})) |
| 310 |
test.IsTrue(t, i.IgnoreUnexported(reflect.TypeOf(time.Time{}))) |
| 311 |
test.IsTrue(t, i.IgnoreUnexported(reflect.TypeOf(testing.T{}))) |
| 312 |
} |
| 313 |
|
| 314 |
func TestAddIgnoreUnexported(t *testing.T) { |
| 315 |
i := hooks.NewInfo() |
| 316 |
|
| 317 |
err := i.AddIgnoreUnexported([]any{time.Time{}, 0}) |
| 318 |
if test.Error(t, err) { |
| 319 |
test.EqualStr(t, err.Error(), "expects type int be a struct, not a int (@1)") |
| 320 |
} |
| 321 |
} |
| 322 |
|
| 323 |
func TestCopy(t *testing.T) { |
| 324 |
var orig *hooks.Info |
| 325 |
|
| 326 |
ni := orig.Copy() |
| 327 |
if ni == nil { |
| 328 |
t.Errorf("Copy should never return nil, even for a nil instance") |
| 329 |
} |
| 330 |
|
| 331 |
orig = hooks.NewInfo() |
| 332 |
copy1 := orig.Copy() |
| 333 |
if copy1 == nil { |
| 334 |
t.Errorf("Copy should never return nil") |
| 335 |
} |
| 336 |
hookedBool := false |
| 337 |
test.NoError(t, copy1.AddSmuggleHooks([]any{ |
| 338 |
func(in bool) bool { hookedBool = true; return in }, |
| 339 |
})) |
| 340 |
|
| 341 |
gotBool := reflect.ValueOf(true) |
| 342 |
|
| 343 |
// orig instance does not have any hook |
| 344 |
handled, _ := orig.Smuggle(&gotBool) |
| 345 |
test.IsFalse(t, hookedBool) |
| 346 |
test.IsFalse(t, handled) |
| 347 |
|
| 348 |
// new bool smuggle hook OK |
| 349 |
hookedBool = false |
| 350 |
handled, _ = copy1.Smuggle(&gotBool) |
| 351 |
test.IsTrue(t, hookedBool) |
| 352 |
test.IsTrue(t, handled) |
| 353 |
|
| 354 |
copy2 := copy1.Copy() |
| 355 |
if copy2 == nil { |
| 356 |
t.Errorf("Copy should never return nil") |
| 357 |
} |
| 358 |
hookedInt := false |
| 359 |
test.NoError(t, copy2.AddSmuggleHooks([]any{ |
| 360 |
func(in int) int { hookedInt = true; return in }, |
| 361 |
})) |
| 362 |
|
| 363 |
// bool smuggle hook inherited from copy1 |
| 364 |
hookedBool = false |
| 365 |
handled, _ = copy2.Smuggle(&gotBool) |
| 366 |
test.IsTrue(t, hookedBool) |
| 367 |
test.IsTrue(t, handled) |
| 368 |
|
| 369 |
gotInt := reflect.ValueOf(123) |
| 370 |
|
| 371 |
// new int smuggle hook not available in copy1 instance |
| 372 |
hookedInt = false |
| 373 |
handled, _ = copy1.Smuggle(&gotInt) |
| 374 |
test.IsFalse(t, hookedInt) |
| 375 |
test.IsFalse(t, handled) |
| 376 |
|
| 377 |
// new int smuggle hook OK |
| 378 |
hookedInt = false |
| 379 |
handled, _ = copy2.Smuggle(&gotInt) |
| 380 |
test.IsTrue(t, hookedInt) |
| 381 |
test.IsTrue(t, handled) |
| 382 |
test.IsTrue(t, handled) |
| 383 |
} |
| 384 |
|
| 385 |
type badEqualVariadic struct{} |
| 386 |
|
| 387 |
func (badEqualVariadic) Equal(a ...badEqualVariadic) bool { return false } |
| 388 |
|
| 389 |
type badEqualNumIn struct{} |
| 390 |
|
| 391 |
func (badEqualNumIn) Equal(a, b badEqualNumIn) bool { return false } |
| 392 |
|
| 393 |
type badEqualNumOut struct{} |
| 394 |
|
| 395 |
func (badEqualNumOut) Equal(a badEqualNumOut) {} |
| 396 |
|
| 397 |
type badEqualInAssign struct{} |
| 398 |
|
| 399 |
func (badEqualInAssign) Equal(a int) bool { return false } |
| 400 |
|
| 401 |
type badEqualOutType struct{} |
| 402 |
|
| 403 |
func (badEqualOutType) Equal(a badEqualOutType) int { return 42 } |