test

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2024 License: MIT Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrNilReceiver occurs when a method is called on a receiver that is nil. This
	// error can be checked with the == operator.
	//
	// Format:
	// 	"receiver must not be nil"
	ErrNilReceiver error

	// ErrTestNotImpl occurs when a test is not implemented. This can be checked
	// with the == operator.
	//
	// Format:
	// 	"test not implemented"
	ErrTestNotImpl error
)
View Source
var (
	// FAIL is the namespace for creating ErrTest errors.
	FAIL failT
)

Functions

func NewErrPanic

func NewErrPanic(value any) error

NewErrPanic creates a new error that represents a panic.

Parameters:

  • value: The value of the panic.

Returns:

  • error: The new error. Never returns nil.

Format:

"panic: <value>"

where <value> is the value of the panic.

func NewErrTest added in v0.1.3

func NewErrTest(want, got string) error

NewErrTest creates and returns a new ErrTest error with the given expected and actual values.

Parameters:

  • want: The expected value.
  • got: The actual value.

Returns:

  • error: A pointer to the newly created ErrTest. Never returns nil.

WARNING: Constructors that uses the FAIL namespaces are recommended to be used instead of this. However, if the precise use case is not found in the FAIL namespaces, this can be used.

Format:

"want <want>, got <got>"

Where:

  • <want> is the expected value.
  • <got> is the actual value.

func Try

func Try(fn func()) error

Try executes the given function and returns any paniced error. If the given function does not panic, nil is returned.

Parameters:

  • fn: The function to execute. If nil, nil is returned.

Returns:

  • error: The paniced error, if any. Otherwise, nil.

Errors:

  • *ErrPanic: If the panic value is not an error.
  • any other error: If the panic value is an error.

If the panic value is an error, it is returned as-is. If the panic value is a string, it is converted to an error. In any other case, a new ErrPanic is created.

Example:

err := Try(func() {
	panic("something went wrong")
})

fmt.Println(err) // Prints: "something went wrong"

Types

type ErrPanic

type ErrPanic struct {
	// Value is the value of the panic.
	Value any
}

ErrPanic is an error that represents a panic.

func (ErrPanic) Error

func (e ErrPanic) Error() string

Error implements the error interface.

type ErrTest added in v0.1.3

type ErrTest struct {
	// Want is the expected value.
	Want string

	// Got is the actual value.
	Got string
}

ErrTest occurs when a test failed.

func (ErrTest) Error added in v0.1.3

func (e ErrTest) Error() string

Error implements error.

type Instance

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

Instance is an instance of a test.

type MakeFn added in v0.1.3

type MakeFn[T any] func(args T) TestingFn

MakeFn is a function that is used to create TestingFn instances.

Parameters:

  • args: The arguments to pass to the testing function.

Returns:

  • TestingFn: The function that is used to run the test. Never returns nil.

type TestSet added in v0.1.3

type TestSet[T any] struct {
	// contains filtered or unexported fields
}

TestSet is a collection of tests.

func NewTestSet added in v0.1.3

func NewTestSet[T any](makeFn MakeFn[T]) TestSet[T]

NewTestSet creates and returns a new Tests instance with a specified function to create TestingFn instances. If no function is provided, a default testing function is used.

Parameters:

  • makeFn: A function that returns a TestingFn. If nil, a default testing function is used.

Returns:

  • Tests: A new Tests instance with the provided or default makeFn.

func (*TestSet[T]) Add added in v0.1.3

func (tt *TestSet[T]) Add(name string, args T) error

Add adds a new test to the collection of tests.

Parameters:

  • name: The name of the test.
  • args: The arguments to pass to the testing function.

Returns:

  • error: An error if the test could not be added.

Errors:

  • ErrNilReceiver: If the receiver is nil.

func (TestSet[T]) Run added in v0.1.3

func (tt TestSet[T]) Run(t *testing.T) uint

Run runs all tests in the collection. Does nothing if there are no tests.

Parameters:

  • t: The testing.T instance to use for reporting.

Returns:

  • uint: The number of tests that are failed.

Panics:

  • "parameter (t) must not be nil": If t is nil.

type TestingFn added in v0.1.3

type TestingFn func() error

TestingFn is a function that is used to run a test.

Parameters:

  • error: An error if the test failed.
var (
	// DefaultTestingFn is the default function for when no testing function is provided.
	//
	// It just returns ErrTestNotImpl.
	DefaultTestingFn TestingFn
)

Jump to

Keyboard shortcuts

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