1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
// Package assert provides programmer-error checks that abort the process.
//
// An assertion failure is never a user error: it means the program reached a
// state its author believed impossible. User errors are returned as values.
package assert
import "fmt"
// True aborts unless cond holds.
func True(cond bool, format string, args ...any) {
if !cond {
panic("assertion failed: " + fmt.Sprintf(format, args...))
}
}
// Equal aborts unless got == want.
func Equal[T comparable](got, want T, format string, args ...any) {
if got != want {
panic(fmt.Sprintf("assertion failed: %s: got %v, want %v",
fmt.Sprintf(format, args...), got, want))
}
}
// NoError aborts if err is non-nil. Use only where an error is structurally
// impossible, never to discard an error a caller could handle.
func NoError(err error, format string, args ...any) {
if err != nil {
panic(fmt.Sprintf("assertion failed: %s: %v",
fmt.Sprintf(format, args...), err))
}
}
// Unreachable aborts unconditionally.
func Unreachable(format string, args ...any) {
panic("unreachable: " + fmt.Sprintf(format, args...))
}
|