gtest

package
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: May 15, 2022 License: Unlicense Imports: 5 Imported by: 0

Documentation

Overview

Missing feature of the standard library: terse, expressive test assertions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Catch

func Catch(t testing.TB)

Must be deferred. Usage:

func TestSomething(t *testing.T) {
	// Catches panics and uses `t.Fatalf`.
	defer gtest.Catch(t)

	// Test assertion that panics.
	gtest.Eq(10, 20)
}

func Empty

func Empty[A ~[]B, B any](src A, opt ...any)

Asserts that the given slice is empty, or fails the test, printing the optional additional messages and the stack trace.

func Eq

func Eq[A comparable](act, exp A, opt ...any)

Asserts that the inputs are equal via "==", or fails the test, printing the optional additional messages and the stack trace.

func EqAny

func EqAny[A any](act, exp A, opt ...any)

Asserts that the inputs are equal via "==", or fails the test, printing the optional additional messages and the stack trace. Doesn't statically require the inputs to be comparable, but may panic if they aren't.

func Equal

func Equal[A any](act, exp A, opt ...any)

Asserts that the inputs are deeply equal, or fails the test, printing the optional additional messages and the stack trace.

func EqualSet

func EqualSet[A ~[]B, B comparable](act, exp A, opt ...any)

Asserts that the input slices have the same set of elements, or fails the test, printing the optional additional messages and the stack trace.

func Error

func Error(test func(error) bool, err error, opt ...any)

Asserts that the given error is non-nil AND satisfies the given error-testing function. Otherwise fails the test, printing the optional additional messages and the stack trace.

func ErrorAny

func ErrorAny(err error, opt ...any)

Asserts that the given error is non-nil, or fails the test, printing the optional additional messages and the stack trace.

func False

func False(val bool, opt ...any)

Asserts that the input is `false`, or fails the test, printing the optional additional messages and the stack trace.

func Has

func Has[A ~[]B, B comparable](src A, val B, opt ...any)

Asserts that the given slice contains the given value, or fails the test, printing the optional additional messages and the stack trace.

func HasEvery added in v0.0.5

func HasEvery[A ~[]B, B comparable](src, exp A, opt ...any)

Asserts that the first slice contains all elements from the second slice. In other words, asserts that the first slice is a strict superset of the second. Otherwise fails the test, printing the optional additional messages and the stack trace.

func HasNone

func HasNone[A ~[]B, B comparable](src, exp A, opt ...any)

Asserts that the first slice does not contain any from the second slice. In other words, asserts that the element sets are disjoint. Otherwise fails the test, printing the optional additional messages and the stack trace.

func HasSome added in v0.0.5

func HasSome[A ~[]B, B comparable](src, exp A, opt ...any)

Asserts that the first slice contains some elements from the second slice. In other words, asserts that the element sets have an intersection. Otherwise fails the test, printing the optional additional messages and the stack trace.

func Len

func Len[A ~[]B, B any](src A, exp int, opt ...any)

Asserts that the given slice has exactly the given length, or fails the test, printing the optional additional messages and the stack trace.

func Less

func Less[A gg.Lesser[A]](one, two A, opt ...any)

Asserts `one < two`, or fails the test, printing the optional additional messages and the stack trace. For primitives, see `LessPrim`.

func LessEq

func LessEq[A interface {
	gg.Lesser[A]
	comparable
}](one, two A, opt ...any)

Asserts `one <= two`, or fails the test, printing the optional additional messages and the stack trace. For primitives, see `LessEqPrim`. Also see `Less`.

func LessEqPrim

func LessEqPrim[A gg.LesserPrim](one, two A, opt ...any)

Asserts `one <= two`, or fails the test, printing the optional additional messages and the stack trace. For non-primitives that implement `gg.Lesser`, see `LessEq`. Also see `LessPrim`.

func LessPrim

func LessPrim[A gg.LesserPrim](one, two A, opt ...any)

Asserts `one < two`, or fails the test, printing the optional additional messages and the stack trace. For non-primitives that implement `gg.Lesser`, see `Less`. Also see `LessEqPrim`.

func MapNotEmpty

func MapNotEmpty[Src ~map[Key]Val, Key comparable, Val any](src Src, opt ...any)

Asserts that the given slice is not empty, or fails the test, printing the optional additional messages and the stack trace.

func Neg

func Neg[A gg.Signed](src A, opt ...any)

Asserts that the given number is < 0, or fails the test, printing the optional additional messages and the stack trace.

func NoError

func NoError(err error, opt ...any)

Asserts that the given error is nil, or fails the test, printing the error's trace if possible, the optional additional messages, and the stack trace.

func NoPanic

func NoPanic(fun func(), opt ...any)

Asserts that the given function doesn't panic, or fails the test, printing the error's trace if possible, the optional additional messages, and the stack trace.

func NotEmpty

func NotEmpty[A ~[]B, B any](src A, opt ...any)

Asserts that the given slice is not empty, or fails the test, printing the optional additional messages and the stack trace.

func NotEq

func NotEq[A any](act, nom A, opt ...any)

Asserts that the inputs are not equal via "!=", or fails the test, printing the optional additional messages and the stack trace.

func NotEqual

func NotEqual[A any](act, nom A, opt ...any)

Asserts that the inputs are not deeply equal, or fails the test, printing the optional additional messages and the stack trace.

func NotHas

func NotHas[A ~[]B, B comparable](src A, val B, opt ...any)

Asserts that the given slice does not contain the given value, or fails the test, printing the optional additional messages and the stack trace.

func NotSliceIs added in v0.0.2

func NotSliceIs[A ~[]B, B any](act, nom A, opt ...any)

Asserts that the given slice headers (not their elements) are distinct. This means at least one of the following fields is different: data pointer, length, capacity. Does NOT compare individual elements, unlike `NotEqual`. Otherwise fails the test, printing the optional additional messages and the stack trace.

func NotZero

func NotZero[A any](val A, opt ...any)

Asserts that the input is not the zero value of its type, or fails the test, printing the optional additional messages and the stack trace.

func Panic

func Panic(test func(error) bool, fun func(), opt ...any)

Asserts that the given function panics AND that the resulting error satisfies the given error-testing function. Otherwise fails the test, printing the optional additional messages and the stack trace.

func PanicAny

func PanicAny(fun func(), opt ...any)

Asserts that the given function panics, or fails the test, printing the optional additional messages and the stack trace.

func PanicStr

func PanicStr(exp string, fun func(), opt ...any)

Asserts that the given function panics with an error whose message contains the given substring, or fails the test, printing the optional additional messages and the stack trace.

func Pos

func Pos[A gg.Signed](src A, opt ...any)

Asserts that the given number is > 0, or fails the test, printing the optional additional messages and the stack trace.

func SliceIs added in v0.0.2

func SliceIs[A ~[]B, B any](act, exp A, opt ...any)

Asserts that the given slice headers (not their elements) are equal via `gg.SliceIs`. This means they have the same data pointer, length, capacity. Does NOT compare individual elements, unlike `Equal`. Otherwise fails the test, printing the optional additional messages and the stack trace.

func Str

func Str[A fmt.Stringer](src A, exp string, opt ...any)

Asserts that `.String` of the input matches the expected string, or fails the test, printing the optional additional messages and the stack trace.

func TextHas

func TextHas[A, B gg.Text](src A, exp B, opt ...any)

Asserts that the given chunk of text contains the given substring, or fails the test, printing the optional additional messages and the stack trace.

func TextLen

func TextLen[A gg.Text](src A, exp int, opt ...any)

Asserts that the given text has exactly the given length, or fails the test, printing the optional additional messages and the stack trace.

func True

func True(val bool, opt ...any)

Asserts that the input is `true`, or fails the test, printing the optional additional messages and the stack trace.

func Uniq

func Uniq[A ~[]B, B comparable](src A, opt ...any)

Asserts that the given slice contains no duplicates, or fails the test, printing the optional additional messages and the stack trace.

func Zero

func Zero[A any](val A, opt ...any)

Asserts that the input is the zero value of its type, or fails the test, printing the optional additional messages and the stack trace.

Types

type Err

type Err struct{ gg.Err }

Used internally by assertion utils. Error wrapper whose default stringing uses "%+v" formatting on the inner error, causing it to be ALWAYS formatted with a stack trace, which is useful when panics are not caught.

func ToErr

func ToErr(skip int, msg string) Err

func (Err) Error

func (self Err) Error() string

Implement `error` by using full formatting on the inner error: multiline with a stack trace.

func (Err) String

func (self Err) String() string

Implement `fmt.Stringer` by using full formatting on the inner error: multiline with a stack trace.

type ErrMsgTest

type ErrMsgTest string

Shortcut for error testing.

func (ErrMsgTest) Is

func (self ErrMsgTest) Is(err error) bool

Tests that the given error has the given message.

Jump to

Keyboard shortcuts

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