|
| 1 | +package assert |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +func Equals[T comparable](t *testing.T, a T, b T, name string) bool { |
| 9 | + t.Helper() |
| 10 | + |
| 11 | + equals := a == b |
| 12 | + if !equals { |
| 13 | + t.Errorf("%v is incorrect. Recieved: %v, Expected: %v", name, b, a) |
| 14 | + } |
| 15 | + |
| 16 | + return equals |
| 17 | +} |
| 18 | + |
| 19 | +func Getter[T comparable](t *testing.T, a func() T, b T, name string) bool { |
| 20 | + t.Helper() |
| 21 | + |
| 22 | + value := a() |
| 23 | + equals := value == b |
| 24 | + if !equals { |
| 25 | + t.Errorf("%v is incorrect. Recieved: %v, Expected: %v", name, b, value) |
| 26 | + } |
| 27 | + |
| 28 | + return equals |
| 29 | +} |
| 30 | + |
| 31 | +func True(t *testing.T, value bool, message string) bool { |
| 32 | + t.Helper() |
| 33 | + |
| 34 | + if !value { |
| 35 | + t.Error(message) |
| 36 | + } |
| 37 | + |
| 38 | + return value |
| 39 | +} |
| 40 | + |
| 41 | +func False(t *testing.T, value bool, message string) bool { |
| 42 | + t.Helper() |
| 43 | + |
| 44 | + if value { |
| 45 | + t.Error(message) |
| 46 | + } |
| 47 | + |
| 48 | + return !value |
| 49 | +} |
| 50 | + |
| 51 | +func NoError(t *testing.T, err error) { |
| 52 | + t.Helper() |
| 53 | + |
| 54 | + if err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +func MustError(t *testing.T, err error, message string) { |
| 60 | + t.Helper() |
| 61 | + |
| 62 | + if err == nil { |
| 63 | + t.Fatal(message) |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func ErrorIs(t *testing.T, err error, target error, message string) bool { |
| 68 | + t.Helper() |
| 69 | + |
| 70 | + equals := errors.Is(err, target) |
| 71 | + if !equals { |
| 72 | + t.Error(message) |
| 73 | + } |
| 74 | + |
| 75 | + return equals |
| 76 | +} |
| 77 | + |
| 78 | +func NotEquals[T comparable](t *testing.T, a T, b T, name string) bool { |
| 79 | + t.Helper() |
| 80 | + |
| 81 | + notEquals := a != b |
| 82 | + if !notEquals { |
| 83 | + t.Errorf("%v is incorrect. Recieved: %v, Expected: %v", name, b, a) |
| 84 | + } |
| 85 | + |
| 86 | + return notEquals |
| 87 | +} |
| 88 | + |
| 89 | +func NotEqualsEval[T comparable](t *testing.T, a func() T, b T, name string) bool { |
| 90 | + t.Helper() |
| 91 | + |
| 92 | + value := a() |
| 93 | + equals := value == b |
| 94 | + if equals { |
| 95 | + t.Errorf("%v is incorrect. Recieved: %v, Expected: %v", name, b, value) |
| 96 | + } |
| 97 | + |
| 98 | + return !equals |
| 99 | +} |
0 commit comments