| 1 |
// Copyright (c) 2021 Klaus Post. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package gzhttp |
| 6 |
|
| 7 |
import ( |
| 8 |
"reflect" |
| 9 |
"slices" |
| 10 |
"testing" |
| 11 |
) |
| 12 |
|
| 13 |
func assertEqual(t testing.TB, want, got any) { |
| 14 |
t.Helper() |
| 15 |
if !reflect.DeepEqual(want, got) { |
| 16 |
t.Fatalf("want %#v, got %#v", want, got) |
| 17 |
} |
| 18 |
} |
| 19 |
|
| 20 |
func assertNotEqual(t testing.TB, want, got any) { |
| 21 |
t.Helper() |
| 22 |
if reflect.DeepEqual(want, got) { |
| 23 |
t.Fatalf("did not want %#v, got %#v", want, got) |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
func assertNil(t testing.TB, object any) { |
| 28 |
if isNil(object) { |
| 29 |
return |
| 30 |
} |
| 31 |
t.Helper() |
| 32 |
t.Fatalf("Expected value to be nil.") |
| 33 |
} |
| 34 |
|
| 35 |
func assertNotNil(t testing.TB, object any) { |
| 36 |
if !isNil(object) { |
| 37 |
return |
| 38 |
} |
| 39 |
t.Helper() |
| 40 |
t.Fatalf("Expected value not to be nil.") |
| 41 |
} |
| 42 |
|
| 43 |
// isNil checks if a specified object is nil or not, without Failing. |
| 44 |
func isNil(object any) bool { |
| 45 |
if object == nil { |
| 46 |
return true |
| 47 |
} |
| 48 |
|
| 49 |
value := reflect.ValueOf(object) |
| 50 |
kind := value.Kind() |
| 51 |
isNilableKind := containsKind( |
| 52 |
[]reflect.Kind{ |
| 53 |
reflect.Chan, reflect.Func, |
| 54 |
reflect.Interface, reflect.Map, |
| 55 |
reflect.Pointer, reflect.Slice}, |
| 56 |
kind) |
| 57 |
|
| 58 |
if isNilableKind && value.IsNil() { |
| 59 |
return true |
| 60 |
} |
| 61 |
|
| 62 |
return false |
| 63 |
} |
| 64 |
|
| 65 |
// containsKind checks if a specified kind in the slice of kinds. |
| 66 |
func containsKind(kinds []reflect.Kind, kind reflect.Kind) bool { |
| 67 |
return slices.Contains(kinds, kind) |
| 68 |
} |