mirror of
https://github.com/matrix-org/sliding-sync.git
synced 2025-03-10 13:37:11 +00:00

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.
47 lines
801 B
Go
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()
|
|
}
|