template/pkg/assert/assert_test.go
2026-07-29 18:57:39 +00:00

60 lines
1.3 KiB
Go

//go:build !assert_disable
package assert_test
import (
"strings"
"testing"
"gitea.djmil.dev/go/template/pkg/assert"
)
func TestThat_passes(t *testing.T) {
assert.That(true, "should not panic")
}
func TestThat_panics(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic, got none")
}
msg, ok := r.(string)
if !ok {
t.Fatalf("expected string panic value, got %T", r)
}
if !strings.Contains(msg, "ring must be non-empty") {
t.Errorf("panic message %q does not contain invariant text", msg)
}
if !strings.Contains(msg, "assert_test.go") {
t.Errorf("panic message %q does not contain caller file", msg)
}
}()
assert.That(false, "ring must be non-empty")
}
func TestThatf_passes(t *testing.T) {
n := 1
assert.Thatf(n > 0, "should not panic: %d", n)
}
func TestThatf_panics(t *testing.T) {
defer func() {
r := recover()
if r == nil {
t.Fatal("expected panic, got none")
}
msg, ok := r.(string)
if !ok {
t.Fatalf("expected string panic value, got %T", r)
}
if !strings.Contains(msg, "len=5 cap=3") {
t.Errorf("panic message %q does not contain formatted args", msg)
}
if !strings.Contains(msg, "assert_test.go") {
t.Errorf("panic message %q does not contain caller file", msg)
}
}()
assert.Thatf(false, "buffer overflowed: len=%d cap=%d", 5, 3)
}