| 1 |
// Copyright (c) 2018, 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 test |
| 8 |
|
| 9 |
import ( |
| 10 |
"regexp" |
| 11 |
"strings" |
| 12 |
"testing" |
| 13 |
"unicode" |
| 14 |
|
| 15 |
"github.com/maxatome/go-testdeep/helpers/tdutil" |
| 16 |
"github.com/maxatome/go-testdeep/internal/spew" |
| 17 |
) |
| 18 |
|
| 19 |
// EqualErrorMessage prints a test error message of the form: |
| 20 |
// |
| 21 |
// Message |
| 22 |
// Failed test |
| 23 |
// got: got_value |
| 24 |
// expected: expected_value |
| 25 |
func EqualErrorMessage(t *testing.T, got, expected any, |
| 26 |
args ...any, |
| 27 |
) { |
| 28 |
t.Helper() |
| 29 |
|
| 30 |
testName := tdutil.BuildTestName(args...) |
| 31 |
if testName != "" { |
| 32 |
testName = " '" + testName + "'" |
| 33 |
} |
| 34 |
|
| 35 |
t.Errorf(`Failed test%s |
| 36 |
got: %v |
| 37 |
expected: %v`, |
| 38 |
testName, got, expected) |
| 39 |
} |
| 40 |
|
| 41 |
func spewIfNeeded(s string) string { |
| 42 |
for _, chr := range s { |
| 43 |
if !unicode.IsPrint(chr) { |
| 44 |
return spew.Sdump(s) |
| 45 |
} |
| 46 |
} |
| 47 |
return s |
| 48 |
} |
| 49 |
|
| 50 |
// EqualStr checks that got equals expected. |
| 51 |
func EqualStr(t *testing.T, got, expected string, args ...any) bool { |
| 52 |
if got == expected { |
| 53 |
return true |
| 54 |
} |
| 55 |
|
| 56 |
t.Helper() |
| 57 |
EqualErrorMessage(t, spewIfNeeded(got), spewIfNeeded(expected), args...) |
| 58 |
return false |
| 59 |
} |
| 60 |
|
| 61 |
// MatchStr checks that got matches expected regexp. |
| 62 |
func MatchStr(t *testing.T, got, expectedRe string, args ...any) bool { |
| 63 |
re := regexp.MustCompile(expectedRe) |
| 64 |
if re.MatchString(got) { |
| 65 |
return true |
| 66 |
} |
| 67 |
|
| 68 |
t.Helper() |
| 69 |
EqualErrorMessage(t, spewIfNeeded(got), spewIfNeeded(expectedRe), args...) |
| 70 |
return false |
| 71 |
} |
| 72 |
|
| 73 |
// EqualInt checks that got equals expected. |
| 74 |
func EqualInt(t *testing.T, got, expected int, args ...any) bool { |
| 75 |
if got == expected { |
| 76 |
return true |
| 77 |
} |
| 78 |
|
| 79 |
t.Helper() |
| 80 |
EqualErrorMessage(t, got, expected, args...) |
| 81 |
return false |
| 82 |
} |
| 83 |
|
| 84 |
// EqualBool checks that got equals expected. |
| 85 |
func EqualBool(t *testing.T, got, expected bool, args ...any) bool { |
| 86 |
if got == expected { |
| 87 |
return true |
| 88 |
} |
| 89 |
|
| 90 |
t.Helper() |
| 91 |
EqualErrorMessage(t, got, expected, args...) |
| 92 |
return false |
| 93 |
} |
| 94 |
|
| 95 |
// IsTrue checks that got is true. |
| 96 |
func IsTrue(t *testing.T, got bool, args ...any) bool { |
| 97 |
if got { |
| 98 |
return true |
| 99 |
} |
| 100 |
|
| 101 |
t.Helper() |
| 102 |
EqualErrorMessage(t, false, true, args...) |
| 103 |
return false |
| 104 |
} |
| 105 |
|
| 106 |
// IsFalse checks that got is false. |
| 107 |
func IsFalse(t *testing.T, got bool, args ...any) bool { |
| 108 |
if !got { |
| 109 |
return true |
| 110 |
} |
| 111 |
|
| 112 |
t.Helper() |
| 113 |
EqualErrorMessage(t, true, false, args...) |
| 114 |
return false |
| 115 |
} |
| 116 |
|
| 117 |
// CheckPanic checks that fn() panics and that the panic() arg is a |
| 118 |
// string that contains contains. |
| 119 |
func CheckPanic(t *testing.T, fn func(), contains string) bool { |
| 120 |
t.Helper() |
| 121 |
|
| 122 |
var ( |
| 123 |
panicked bool |
| 124 |
panicParam any |
| 125 |
) |
| 126 |
|
| 127 |
func() { |
| 128 |
defer func() { panicParam = recover() }() |
| 129 |
panicked = true |
| 130 |
fn() |
| 131 |
panicked = false |
| 132 |
}() |
| 133 |
|
| 134 |
if !panicked { |
| 135 |
t.Error("panic() did not occur") |
| 136 |
return false |
| 137 |
} |
| 138 |
|
| 139 |
panicStr, ok := panicParam.(string) |
| 140 |
if !ok { |
| 141 |
t.Errorf("panic() occurred but recover()d %T type (%v) instead of string", |
| 142 |
panicParam, panicParam) |
| 143 |
return false |
| 144 |
} |
| 145 |
|
| 146 |
if !strings.Contains(panicStr, contains) { |
| 147 |
t.Errorf("panic() string `%s'\ndoes not contain `%s'", panicStr, contains) |
| 148 |
return false |
| 149 |
} |
| 150 |
return true |
| 151 |
} |
| 152 |
|
| 153 |
// NoError checks that err is nil. |
| 154 |
func NoError(t *testing.T, err error, args ...any) bool { |
| 155 |
if err == nil { |
| 156 |
return true |
| 157 |
} |
| 158 |
|
| 159 |
t.Helper() |
| 160 |
EqualErrorMessage(t, err, nil, args...) |
| 161 |
return false |
| 162 |
} |
| 163 |
|
| 164 |
// Error checks that err is non-nil. |
| 165 |
func Error(t *testing.T, err error, args ...any) bool { |
| 166 |
if err != nil { |
| 167 |
return true |
| 168 |
} |
| 169 |
|
| 170 |
t.Helper() |
| 171 |
EqualErrorMessage(t, err, "<non-nil>", args...) |
| 172 |
return false |
| 173 |
} |