tst

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Aug 10, 2025 License: MIT Imports: 6 Imported by: 0

README

tst

ci Go Report Card PkgGoDev Version codecov

Error asserting functions for table driven testcases.

Examples:

	err := errors.New("not found")

	tests := []struct {
		input    error
		asserter ErrorAssertionFunc
	}{
		{
			input:    nil,
			asserter: NoError(),
		},
		{
			input:    err,
			asserter: Error(),
		},
		{
			input:    err,
			asserter: ErrorIs(err),
		},
		{
			input:    fmt.Errorf("wrapped %w", err),
			asserter: ErrorIs(err),
		},
		{
			input:    &os.PathError{},
			asserter: ErrorOfType[*os.PathError](),
		},
		{
			input: &os.PathError{Op: "op", Path: "/abc", Err: os.ErrInvalid},
			asserter: ErrorOfType[*os.PathError](
				func(tt TestingT, pe *os.PathError) { assert.Equal(tt, "op", pe.Op) },
			),
		},
		{
			input: fmt.Errorf("wrapped %w", &os.PathError{Op: "op", Path: "/abc", Err: os.ErrInvalid}),
			asserter: All(
				Error(),
				ErrorIs(os.ErrInvalid),
				ErrorOfType[*os.PathError](
					func(tt TestingT, pe *os.PathError) { assert.Equal(tt, "op", pe.Op) },
				),
				ErrorStringContains("op"),
			),
		},
	}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ErrorAssertionFunc

type ErrorAssertionFunc func(t TestingT, err error) bool

func All

func All(expected ...ErrorAssertionFunc) ErrorAssertionFunc

func Error

func Error() ErrorAssertionFunc

func ErrorIs

func ErrorIs(allExpectedErrors ...error) ErrorAssertionFunc

ErrorIs returns an ErrorAssertionFunc that checks if the given error matches any of the expected errors using errors.Is. If no expected errors are provided, it simply checks that an error is present (similar to Error()). Returns false if the error is nil or doesn't match any expected errors.

func ErrorOfType

func ErrorOfType[T error](typedAsserts ...func(TestingT, T)) ErrorAssertionFunc

func ErrorStringContains

func ErrorStringContains(s string) ErrorAssertionFunc

func NoError

func NoError() ErrorAssertionFunc

func (ErrorAssertionFunc) AsAssert

func (ErrorAssertionFunc) AsRequire

type TestingT

type TestingT interface {
	Errorf(format string, args ...interface{})
	FailNow()
	Helper()
}

Jump to

Keyboard shortcuts

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