sliding-sync/internal/errors_test.go
Kegan Dougal 33cf1542aa Add Assert() function which works like C assert()
Particularly as the server expands into multiple lists and
filters, having a way to quickly detect off-by-one index
errors is important, so add an assert() function which
will panic() if SYNCV3_DEBUG=1 else log an angry message.
2021-11-08 13:04:03 +00:00

47 lines
801 B
Go

package internal
import (
"os"
"testing"
)
func TestAssertion(t *testing.T) {
os.Setenv("SYNCV3_DEBUG", "1")
shouldPanic := true
shouldNotPanic := false
try(t, shouldNotPanic, func() {
Assert("true does nothing", true)
})
try(t, shouldPanic, func() {
Assert("false panics", false)
})
os.Setenv("SYNCV3_DEBUG", "0")
try(t, shouldNotPanic, func() {
Assert("true does nothing", true)
})
try(t, shouldNotPanic, func() {
Assert("false does not panic if SYNCV3_DEBUG is not 1", false)
})
}
func try(t *testing.T, shouldPanic bool, fn func()) {
t.Helper()
defer func() {
t.Helper()
err := recover()
if err != nil {
if shouldPanic {
return
}
t.Fatalf("panic: %s", err)
} else {
if shouldPanic {
t.Fatalf("function did not panic")
}
}
}()
fn()
}