test

package
v0.29.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 7, 2026 License: MIT Imports: 26 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccessKey added in v0.20.3

func AccessKey(
	t *testing.T, key *ecdsa.PrivateKey, claims elephantine.JWTClaims,
) string

AccessKey creates a signed access key from the signing key and claims.

func AgainstGolden added in v0.27.2

func AgainstGolden[T any](
	t *testing.T,
	regenerate bool,
	got T,
	goldenPath string,
	helpers ...GoldenHelper,
)

AgainstGolden compares a result against the contents of the file at the goldenPath. Run with regenerate set to true to create or update the file.

func Claims added in v0.20.3

func Claims(
	t *testing.T, user string, scope string, units ...string,
) elephantine.JWTClaims

Claims creates an elephantine.JWTClaims struct for use in testing.

func CloneMessage

func CloneMessage[T proto.Message](msg T) T

CloneMessage allows for type safe cloning of protobuf messages.

func CommonTimeFields added in v0.17.10

func CommonTimeFields() []string

func Context deprecated

func Context(c Cleaner) context.Context

Deprecated: Use testing.T.Context() instead.

func Equal deprecated

func Equal[T comparable](t TestingT, want T, got T, format string, a ...any)

Equal is a shim for Equalf.

Deprecated: use Equalf instead.

func EqualDiff added in v0.3.0

func EqualDiff[T any](t TestingT,
	want T, got T,
	format string, a ...any,
)

EqualDiff runs a cmp.Diff to do a deep equal check with readable diff output.

func EqualDiffWithOptions deprecated added in v0.21.1

func EqualDiffWithOptions[T any](
	t TestingT,
	want T, got T,
	opts cmp.Options,
	format string, a ...any,
)

EqualDiffWithOptions is a shim for EqualDiffWithOptionsf.

Deprecated: use EqualDiffWithOptionsf instead.

func EqualDiffWithOptionsf added in v0.27.2

func EqualDiffWithOptionsf[T any](
	t TestingT,
	want T, got T,
	opts cmp.Options,
	format string, a ...any,
)

func EqualMessage deprecated

func EqualMessage(t TestingT,
	want proto.Message, got proto.Message,
	format string, a ...any,
)

EqualMessage is a shim for EqualMessagef.

Deprecated: use EqualMessagef instead.

func EqualMessageWithOptions deprecated added in v0.17.2

func EqualMessageWithOptions(t TestingT,
	want proto.Message, got proto.Message,
	opts cmp.Options,
	format string, a ...any,
)

EqualMessageWithOptions is a shim for EqualMessageWithOptionsf.

Deprecated: use EqualMessageWithOptionsf instead.

func EqualMessageWithOptionsf added in v0.27.2

func EqualMessageWithOptionsf(t TestingT,
	want proto.Message, got proto.Message,
	opts cmp.Options,
	format string, a ...any,
)

EqualMessageWithOptionsf runs a cmp.Diff with protobuf-specific options plus the additional cmp.Options provided by the caller.

func EqualMessagef added in v0.27.2

func EqualMessagef(t TestingT,
	want proto.Message, got proto.Message,
	format string, a ...any,
)

EqualMessagef runs a cmp.Diff with protobuf-specific options.

func Equalf added in v0.27.2

func Equalf[T comparable](t TestingT, want T, got T, format string, a ...any)

func ErrorParity added in v0.29.0

func ErrorParity(t TestingT, twirpErr error, connectErr error)

ErrorParity checks that a Twirp error and a Connect error describe the same failure: the same code, the same message and the same error metadata. It is what makes a service's move to the Connect error vocabulary checkable, by running the same call against both stacks and comparing the two errors.

func IsRPCError added in v0.29.0

func IsRPCError(t TestingT, err error, code connect.Code)

IsRPCError checks that the error is an RPC error with the given code. Both *connect.Error and twirp.Error are accepted, so a test does not have to be changed at the same time as the client constructor that produces its errors.

func IsTwirpError deprecated

func IsTwirpError(
	t TestingT, err error, code twirp.ErrorCode,
)

IsTwirpError checks that the error is a Twirp error with the given code.

Deprecated: use IsRPCError, which takes a connect.Code and accepts both error types, so the check does not have to change with the client constructor.

func MessageAgainstGolden added in v0.27.2

func MessageAgainstGolden(
	t *testing.T,
	regenerate bool,
	got proto.Message,
	goldenPath string,
	helpers ...GoldenHelper,
)

MessageAgainstGolden compares a protobuf message against the contents of the file at the goldenPath. Run with regenerate set to true to create or update the file.

func MessagesAgainstGolden added in v0.27.2

func MessagesAgainstGolden[T proto.Message](
	t *testing.T,
	regenerate bool,
	gotSlice []T,
	goldenPath string,
	helpers ...GoldenHelper,
)

MessagesAgainstGolden compares a slice of protobuf messages against the contents of the file at the goldenPath. Run with regenerate set to true to create or update the file.

func Must deprecated

func Must(t TestingT, err error, format string, a ...any)

Must is a shim for Mustf.

Deprecated: use Mustf instead.

func MustNot deprecated

func MustNot(t TestingT, err error, format string, a ...any)

MustNot is a shim for MustNotf.

Deprecated: use MustNotf instead.

func MustNotf added in v0.27.2

func MustNotf(t TestingT, err error, format string, a ...any)

func Mustf added in v0.27.2

func Mustf(t TestingT, err error, format string, a ...any)

func NewLogHandler

func NewLogHandler(t TestingLogger, level slog.Level) slog.Handler

NewLogHandler returns a slog.Handler that logs through t. Once t's cleanup has run the handler silently drops further records: the cleanup and the write into t.Log are serialised, so a record either reaches t.Log before the cleanup returns, while logging is still legal, or is dropped.

func NewSigningKey added in v0.20.3

func NewSigningKey(t *testing.T) *ecdsa.PrivateKey

NewSigningKey creates a signing key for use with a elephantine.NewStaticAuthInfoParser.

func NotNil deprecated

func NotNil[T any](t TestingT, v *T, format string, a ...any)

NotNil is a shim for NotNilf.

Deprecated: use NotNilf instead.

func NotNilf added in v0.27.2

func NotNilf[T any](t TestingT, v *T, format string, a ...any)

func Regenerate added in v0.23.5

func Regenerate() bool

Regenerate returns true if the environment variable REGENERATE is set to "true", signalling that golden test fixtures should be regenerated.

func StandardClaims added in v0.20.3

func StandardClaims(
	t *testing.T, scope string, units ...string,
) elephantine.JWTClaims

StandardClaims is a helper function that creates standard claims for a test.

func TestAgainstGolden deprecated added in v0.21.1

func TestAgainstGolden[T any](
	t *testing.T,
	regenerate bool,
	got T,
	goldenPath string,
	helpers ...GoldenHelper,
)

TestAgainstGolden compares a result against the contents of the file at the goldenPath. Run with regenerate set to true to create or update the file.

Deprecated: use AgainstGolden instead.

func TestMessageAgainstGolden deprecated added in v0.17.2

func TestMessageAgainstGolden(
	t *testing.T,
	regenerate bool,
	got proto.Message,
	goldenPath string,
	helpers ...GoldenHelper,
)

TestMessageAgainstGolden compares a protobuf message against the contents of the file at the goldenPath. Run with regenerate set to true to create or update the file.

Deprecated: use MessageAgainstGolden instead.

func TestMessagesAgainstGolden deprecated added in v0.22.1

func TestMessagesAgainstGolden[T proto.Message](
	t *testing.T,
	regenerate bool,
	gotSlice []T,
	goldenPath string,
	helpers ...GoldenHelper,
)

TestMessagesAgainstGolden compares a slice of protobuf messages against the contents of the file at the goldenPath. Run with regenerate set to true to create or update the file.

Deprecated: use MessagesAgainstGolden instead.

Types

type Cleaner

type Cleaner interface {
	Cleanup(fn func())
}

Cleaner is the subset of testing.TB used to register cleanup callbacks, satisfied by *testing.T.

type GoldenHelper added in v0.17.10

type GoldenHelper interface {
	CmpOpts() cmp.Options
	JSONTransform(value map[string]any) error
}

GoldenHelper customises golden-file comparisons: CmpOpts contributes cmp.Options used when diffing, and JSONTransform rewrites the decoded JSON before it is stored, typically to mask volatile fields. See IgnoreField and IgnoreTimestamps.

type GoldenHelperForAny added in v0.21.1

type GoldenHelperForAny interface {
	JSONTransformAny(value any) error
}

GoldenHelperForAny is an optional extension of GoldenHelper for helpers that can also transform non-object JSON values, such as a top-level array.

type IgnoreField added in v0.20.1

type IgnoreField[T any] struct {
	Name       string
	DummyValue T
	Validator  func(v T) error
}

IgnoreField is a GoldenHelper that ignores a named field during comparison and replaces its value with DummyValue when storing the golden file. An optional Validator can assert something about the original value before it is masked.

func (IgnoreField[T]) CmpOpts added in v0.20.1

func (fi IgnoreField[T]) CmpOpts() cmp.Options

CmpOpts implements GoldenHelper.

func (IgnoreField[T]) JSONTransform added in v0.20.1

func (fi IgnoreField[T]) JSONTransform(value map[string]any) error

JSONTransform implements GoldenHelper.

type IgnoreTimestamps added in v0.17.10

type IgnoreTimestamps struct {
	Fields     []string
	DummyValue time.Time
	Format     string
}

IgnoreTimestamps is a GoldenHelper that masks timestamp fields during comparison and replaces them with a fixed value when storing the golden file. When Fields is empty CommonTimeFields is used, and Format defaults to time.RFC3339.

func (IgnoreTimestamps) CmpOpts added in v0.17.10

func (it IgnoreTimestamps) CmpOpts() cmp.Options

func (IgnoreTimestamps) JSONTransform added in v0.17.10

func (it IgnoreTimestamps) JSONTransform(value map[string]any) error

type LogHandler

type LogHandler struct {
	// contains filtered or unexported fields
}

LogHandler is a slog.Handler that writes log records through a TestingLogger's Log method, so log output is attributed to the running test. Once the test's cleanup has run it silently drops further records. Create it with NewLogHandler.

Every record, including those from handlers derived with WithAttrs and WithGroup, is written through Write, which is where the guard lives.

func (*LogHandler) Enabled

func (h *LogHandler) Enabled(ctx context.Context, level slog.Level) bool

func (*LogHandler) Handle

func (h *LogHandler) Handle(ctx context.Context, r slog.Record) error

func (*LogHandler) WithAttrs

func (h *LogHandler) WithAttrs(attrs []slog.Attr) slog.Handler

func (*LogHandler) WithGroup

func (h *LogHandler) WithGroup(name string) slog.Handler

func (*LogHandler) Write

func (h *LogHandler) Write(data []byte) (int, error)

Write is the io.Writer the TextHandler and all its clones write to. It must not be called while holding h.mu.

type Logger deprecated

type Logger = TestingLogger

Logger is the previous interface for NewLogHandler. Use TestingLogger instead.

Deprecated: use TestingLogger.

type TestingLogger added in v0.24.0

type TestingLogger interface {
	Log(args ...any)
	Cleanup(fn func())
}

TestingLogger is satisfied by *testing.T and *testing.B.

type TestingT

type TestingT interface {
	Helper()
	Fatalf(format string, args ...any)
	Logf(format string, args ...any)
}

TestingT is the subset of *testing.T used by the assertion helpers in this package, satisfied by both *testing.T and *testing.B.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL