test

package
v0.35.3 Latest Latest
Warning

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

Go to latest
Published: Dec 14, 2024 License: GPL-3.0 Imports: 2 Imported by: 0

Documentation

Overview

Package test contains helper functions to remove common boilerplate to make testing easier.

The ExpectEquality() is the most basic and probably the most useful function. It compares like-typed variables for equality and returns true if they match. ExpectInequality() is the inverse function.

The ExpectFailure() and ExpectSuccess() functions test for failure and success. These two functions work with bool or error and special handling for nil.

ExpectImplements() tests whether an instance implements the specified type.

ExpectFailure(), ExpectSuccess() and ExpectImplements() all return a boolean to indicate whether the test has passed. This allows the user to control larger test procedures that have several stages

The Writer type meanwhile, implements the io.Writer interface and should be used to capture output. The Writer.Compare() function can then be used to test for equality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DemandEquality added in v0.33.0

func DemandEquality[T comparable](t *testing.T, v T, expectedValue T)

DemandEquility is used to test equality between one value and another. If the test fails it is a testing fatility

This is particular useful if the values being tested are used in further tests and so must be correct. For example, testing that the lengths of two slices are equal before iterating over them in unison

func DemandFailure added in v0.35.0

func DemandFailure(t *testing.T, v any)

DemandFailure is used to test for a value which indicates an 'unsuccessful' value for the type. See ExpectFailure() for more information on failure values

func DemandImplements added in v0.35.0

func DemandImplements[T comparable](t *testing.T, instance any, implements T) bool

DemandImplements tests whether an instance is an implementation of type T

func DemandSuccess added in v0.35.0

func DemandSuccess(t *testing.T, v any)

DemandSucess is used to test for a value which indicates an 'successful' value for the type. See ExpectSucess() for more information on success values

func ExpectApproximate added in v0.35.2

func ExpectApproximate[T Approximte](t *testing.T, value T, expectedValue T, tolerance float64) bool

ExpectApproximate is used to test approximate equality between one value and another.

Tolerance represents a percentage. For example, 0.5 is tolerance of +/- 50%. If the tolerance value is negative then the positive equivalent is used.

func ExpectEquality added in v0.25.0

func ExpectEquality[T comparable](t *testing.T, value T, expectedValue T) bool

ExpectEquality is used to test equality between one value and another

func ExpectFailure added in v0.25.0

func ExpectFailure(t *testing.T, v any) bool

ExpectFailure tests for an 'unsucessful value for the value's type.

Types bool and error are treated thus:

bool == false
error != nil

If type is nil then the test will fail

func ExpectInequality added in v0.25.0

func ExpectInequality[T comparable](t *testing.T, value T, expectedValue T) bool

ExpectInequality is used to test inequality between one value and another. In other words, the test does not want to succeed if the values are equal

func ExpectSuccess added in v0.25.0

func ExpectSuccess(t *testing.T, v any) bool

ExpectSuccess tests for a 'sucessful' value for the value's type.

Types bool and error are treated thus:

bool == true
error == nil

If type is nil then the test will succeed

Types

type Approximte added in v0.35.2

type Approximte interface {
	~float32 | ~float64 | ~int
}

Approximate constraint used by ExpectApproximate() function

type Writer

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

Writer is an implementation of the io.Writer interface. It should be used to capture output and to compare with predefined strings.

func (*Writer) Clear added in v0.3.4

func (tw *Writer) Clear()

clear string empties the buffer.

func (*Writer) Compare

func (tw *Writer) Compare(s string) bool

Compare buffered output with predefined/example string.

func (*Writer) String added in v0.3.4

func (tw *Writer) String() string

implements Stringer interface.

func (*Writer) Write

func (tw *Writer) Write(p []byte) (n int, err error)

Jump to

Keyboard shortcuts

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