Files
relspecgo/pkg/writers/template/errors_test.go
T
warkanum 495a21b67b test: expand coverage across readers, writers, cmd, ui, diff and merge
Implements tests/_plans and previously deferred packages; updates plan
README with new coverage numbers.
2026-10-03 21:33:59 +02:00

49 lines
1.2 KiB
Go

package template
import (
"errors"
"strings"
"testing"
)
func TestTemplateError(t *testing.T) {
cause := errors.New("boom")
tests := []struct {
name string
err *TemplateError
phase string
}{
{"load", NewTemplateLoadError("cannot read", cause), "load"},
{"parse", NewTemplateParseError("bad syntax", cause), "parse"},
{"execute", NewTemplateExecuteError("failed render", cause), "execute"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.err.Phase != tt.phase {
t.Errorf("phase = %q", tt.err.Phase)
}
msg := tt.err.Error()
if !strings.Contains(msg, "template "+tt.phase+" error") || !strings.Contains(msg, "boom") {
t.Errorf("message = %q", msg)
}
if !errors.Is(tt.err, cause) {
t.Error("errors.Is must reach cause")
}
var te *TemplateError
if !errors.As(error(tt.err), &te) || te != tt.err {
t.Error("errors.As failed")
}
})
}
}
func TestTemplateErrorWithoutCause(t *testing.T) {
e := NewTemplateParseError("only message", nil)
if got := e.Error(); got != "template parse error: only message" {
t.Errorf("got %q", got)
}
if e.Unwrap() != nil {
t.Error("Unwrap must be nil")
}
}