| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package assert |
| 10 |
|
| 11 |
import ( |
| 12 |
"errors" |
| 13 |
"fmt" |
| 14 |
"reflect" |
| 15 |
"regexp" |
| 16 |
) |
| 17 |
|
| 18 |
|
| 19 |
type miniTB interface { |
| 20 |
Helper() |
| 21 |
Fatalf(string, ...any) |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
func formatSuffix(msgFormat string, args ...any) string { |
| 27 |
if msgFormat == "" { |
| 28 |
return "" |
| 29 |
} |
| 30 |
return " - " + fmt.Sprintf(msgFormat, args...) |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
func Equal(tb miniTB, want, got any) { |
| 39 |
tb.Helper() |
| 40 |
Equalf(tb, want, got, "") |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func Equalf(tb miniTB, want, got any, msgFormat string, args ...any) { |
| 45 |
tb.Helper() |
| 46 |
if got != want { |
| 47 |
suffix := formatSuffix(msgFormat, args...) |
| 48 |
tb.Fatalf("got %v; want %v%s", got, want, suffix) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
func DeepEqual(tb miniTB, want, got any) { |
| 58 |
tb.Helper() |
| 59 |
DeepEqualf(tb, want, got, "") |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
func DeepEqualf(tb miniTB, want, got any, msgFormat string, args ...any) { |
| 64 |
tb.Helper() |
| 65 |
if !reflect.DeepEqual(got, want) { |
| 66 |
suffix := formatSuffix(msgFormat, args...) |
| 67 |
tb.Fatalf("got %+v; want %+v%s", got, want, suffix) |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
func ErrorMatches(tb miniTB, pattern string, err error) { |
| 73 |
tb.Helper() |
| 74 |
ErrorMatchesf(tb, pattern, err, "") |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
func ErrorMatchesf(tb miniTB, pattern string, err error, msgFormat string, args ...any) { |
| 79 |
tb.Helper() |
| 80 |
if err == nil { |
| 81 |
suffix := formatSuffix(msgFormat, args...) |
| 82 |
tb.Fatalf("got nil; want error matching %q%s", pattern, suffix) |
| 83 |
return |
| 84 |
} |
| 85 |
re, reErr := regexp.Compile(pattern) |
| 86 |
if reErr != nil { |
| 87 |
suffix := formatSuffix(msgFormat, args...) |
| 88 |
tb.Fatalf("invalid regexp %q: %v%s", pattern, reErr, suffix) |
| 89 |
return |
| 90 |
} |
| 91 |
if !re.MatchString(err.Error()) { |
| 92 |
suffix := formatSuffix(msgFormat, args...) |
| 93 |
tb.Fatalf("error %q does not match %q%s", err.Error(), pattern, suffix) |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
|
| 98 |
func ErrorIs(tb miniTB, got, want error) { |
| 99 |
tb.Helper() |
| 100 |
if !errors.Is(got, want) { |
| 101 |
tb.Fatalf("got %#v; want %#v", got, want) |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
func errorAsNoPanic(tb miniTB, err error, target any) (ok bool, panic error) { |
| 107 |
defer func() { |
| 108 |
if r := recover(); r != nil { |
| 109 |
ok = false |
| 110 |
panic = fmt.Errorf("panic: %v", r) |
| 111 |
return |
| 112 |
} |
| 113 |
}() |
| 114 |
|
| 115 |
return errors.As(err, target), nil |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func ErrorAs(tb miniTB, err error, target any) { |
| 120 |
tb.Helper() |
| 121 |
|
| 122 |
ok, panicErr := errorAsNoPanic(tb, err, target) |
| 123 |
if panicErr != nil { |
| 124 |
tb.Fatalf("%s", panicErr) |
| 125 |
return |
| 126 |
} |
| 127 |
if ok { |
| 128 |
return |
| 129 |
} |
| 130 |
|
| 131 |
reflectedType := reflect.TypeOf(target) |
| 132 |
if reflectedType.Kind() != reflect.Pointer { |
| 133 |
|
| 134 |
tb.Fatalf("a pointer was expected: got: %s; want: ptr", reflectedType.Kind()) |
| 135 |
return |
| 136 |
} |
| 137 |
|
| 138 |
tb.Fatalf("got %#v; want %s", err, reflectedType.Elem()) |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
func NoError(tb miniTB, err error) { |
| 143 |
tb.Helper() |
| 144 |
NoErrorf(tb, err, "") |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
func NoErrorf(tb miniTB, err error, msgFormat string, args ...any) { |
| 149 |
tb.Helper() |
| 150 |
if err != nil { |
| 151 |
suffix := formatSuffix(msgFormat, args...) |
| 152 |
tb.Fatalf("unexpected error: %v%s", err, suffix) |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
func IsNil(tb miniTB, v any) { |
| 158 |
tb.Helper() |
| 159 |
IsNilf(tb, v, "") |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
func IsNilf(tb miniTB, v any, msgFormat string, args ...any) { |
| 164 |
tb.Helper() |
| 165 |
if !isNil(v) { |
| 166 |
suffix := formatSuffix(msgFormat, args...) |
| 167 |
tb.Fatalf("got non-nil (type %T): %#v%s", v, v, suffix) |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
func NotNil(tb miniTB, v any) { |
| 173 |
tb.Helper() |
| 174 |
NotNilf(tb, v, "") |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
func NotNilf(tb miniTB, v any, msgFormat string, args ...any) { |
| 179 |
tb.Helper() |
| 180 |
if isNil(v) { |
| 181 |
suffix := formatSuffix(msgFormat, args...) |
| 182 |
tb.Fatalf("got nil; want non-nil%s", suffix) |
| 183 |
} |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
func True(tb miniTB, got bool) { |
| 188 |
tb.Helper() |
| 189 |
Truef(tb, got, "") |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
func Truef(tb miniTB, got bool, msgFormat string, args ...any) { |
| 194 |
tb.Helper() |
| 195 |
if !got { |
| 196 |
suffix := formatSuffix(msgFormat, args...) |
| 197 |
tb.Fatalf("got false; want true%s", suffix) |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
func False(tb miniTB, got bool) { |
| 203 |
tb.Helper() |
| 204 |
Falsef(tb, got, "") |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
func Falsef(tb miniTB, got bool, msgFormat string, args ...any) { |
| 209 |
tb.Helper() |
| 210 |
if got { |
| 211 |
suffix := formatSuffix(msgFormat, args...) |
| 212 |
tb.Fatalf("got true; want false%s", suffix) |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
func PanicMatches(tb miniTB, pattern string, f func()) { |
| 218 |
tb.Helper() |
| 219 |
PanicMatchesf(tb, pattern, f, "") |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
func PanicMatchesf(tb miniTB, pattern string, f func(), msgFormat string, args ...any) { |
| 225 |
tb.Helper() |
| 226 |
var pan any |
| 227 |
func() { |
| 228 |
defer func() { pan = recover() }() |
| 229 |
f() |
| 230 |
}() |
| 231 |
if pan == nil { |
| 232 |
suffix := formatSuffix(msgFormat, args...) |
| 233 |
tb.Fatalf("function did not panic; want panic matching %q%s", pattern, suffix) |
| 234 |
return |
| 235 |
} |
| 236 |
var pmsg string |
| 237 |
switch x := pan.(type) { |
| 238 |
case error: |
| 239 |
pmsg = x.Error() |
| 240 |
case string: |
| 241 |
pmsg = x |
| 242 |
default: |
| 243 |
pmsg = fmt.Sprint(x) |
| 244 |
} |
| 245 |
re, reErr := regexp.Compile(pattern) |
| 246 |
if reErr != nil { |
| 247 |
suffix := formatSuffix(msgFormat, args...) |
| 248 |
tb.Fatalf("invalid regexp %q: %v%s", pattern, reErr, suffix) |
| 249 |
return |
| 250 |
} |
| 251 |
if !re.MatchString(pmsg) { |
| 252 |
suffix := formatSuffix(msgFormat, args...) |
| 253 |
tb.Fatalf("panic %q does not match %q%s", pmsg, pattern, suffix) |
| 254 |
} |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
func isNil(v any) bool { |
| 259 |
if v == nil { |
| 260 |
return true |
| 261 |
} |
| 262 |
rv := reflect.ValueOf(v) |
| 263 |
switch rv.Kind() { |
| 264 |
case reflect.Chan, reflect.Func, reflect.Map, reflect.Pointer, reflect.Slice, reflect.Interface, reflect.UnsafePointer: |
| 265 |
return rv.IsNil() |
| 266 |
default: |
| 267 |
return false |
| 268 |
} |
| 269 |
} |