Files

45 lines
1.1 KiB
Go

// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Commercial
// SPDX-FileCopyrightText: 2025-2026 Christian Galo
package testkit
import (
"errors"
"fmt"
"testing"
)
// recorder stands in for a test: it records what Close reports instead of
// failing the real test.
type recorder struct {
testing.TB
reports []string
}
func (r *recorder) Helper() {}
func (r *recorder) Errorf(format string, args ...any) {
r.reports = append(r.reports, fmt.Sprintf(format, args...))
}
type closer struct{ err error }
func (c closer) Close() error { return c.err }
func TestCloseReportsOnlyAFailedClose(t *testing.T) {
var ok recorder
Close(&ok, closer{})
if len(ok.reports) != 0 {
t.Errorf("a nil Close reported %q", ok.reports)
}
var failed recorder
Close(&failed, closer{err: errors.New("boom")})
if len(failed.reports) != 1 {
t.Fatalf("a failing Close reported %d times, want 1: %q", len(failed.reports), failed.reports)
}
if want := "close testkit.closer: boom"; failed.reports[0] != want {
t.Errorf("report = %q, want %q", failed.reports[0], want)
}
}