expect

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Feb 6, 2026 License: MIT Imports: 6 Imported by: 0

README

expect

Build Status Go Reference Go Report Card

An assertion library to simplify writing tests in Go.

Features

  • Zero dependencies
  • Generic functions for type-safe comparisons
  • Support for slices and maps
  • Works with *testing.T and *testing.B
  • Clear failure messages

Installation

go get github.com/lumertzg/expect

Usage

package mypackage

import (
    "testing"

    "github.com/lumertzg/expect"
)

func TestExample(t *testing.T) {
    expect.Equal(t, 42, Calculate())
    expect.True(t, IsValid())
    expect.NoError(t, DoSomething())
}

See the examples folder for more usage examples.

Documentation

Overview

Package expect provides simple test assertions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ContainsMapKey added in v0.3.0

func ContainsMapKey[M ~map[K]V, K comparable, V any](t T, m M, key K)

ContainsMapKey asserts that m contains key.

func ContainsSlice added in v0.3.0

func ContainsSlice[S ~[]E, E comparable](t T, values S, item E)

ContainsSlice asserts that values contains item.

func ContainsString added in v0.3.0

func ContainsString(t T, s, substr string)

ContainsString asserts that s contains substr.

func Empty added in v0.3.0

func Empty(t T, value any)

Empty asserts that value is empty.

func Equal

func Equal[V comparable](t T, expected, actual V)

Equal asserts that expected and actual are equal.

func EqualMap

func EqualMap[M ~map[K]V, K, V comparable](t T, expected, actual M)

EqualMap asserts that expected and actual maps are equal.

func EqualSlice

func EqualSlice[S ~[]E, E comparable](t T, expected, actual S)

EqualSlice asserts that expected and actual slices are equal.

func Error

func Error(t T, err error)

Error asserts that err is not nil.

func ErrorAs added in v0.3.0

func ErrorAs(t T, err error, target any)

ErrorAs asserts that err matches target using errors.As.

func ErrorIs added in v0.3.0

func ErrorIs(t T, err, target error)

ErrorIs asserts that err matches target using errors.Is.

func False

func False(t T, value bool)

False asserts that value is false.

func Greater added in v0.2.0

func Greater[V cmp.Ordered](t T, a, b V)

Greater asserts that a > b.

func GreaterOrEqual added in v0.2.0

func GreaterOrEqual[V cmp.Ordered](t T, a, b V)

GreaterOrEqual asserts that a >= b.

func Len added in v0.3.0

func Len(t T, value any, expected int)

Len asserts that value has the expected length.

func Less added in v0.2.0

func Less[V cmp.Ordered](t T, a, b V)

Less asserts that a < b.

func LessOrEqual added in v0.2.0

func LessOrEqual[V cmp.Ordered](t T, a, b V)

LessOrEqual asserts that a <= b.

func Nil

func Nil(t T, value any)

Nil asserts that value is nil.

func NoError

func NoError(t T, err error)

NoError asserts that err is nil.

func NotContainsSlice added in v0.3.0

func NotContainsSlice[S ~[]E, E comparable](t T, values S, item E)

NotContainsSlice asserts that values does not contain item.

func NotContainsString added in v0.3.0

func NotContainsString(t T, s, substr string)

NotContainsString asserts that s does not contain substr.

func NotEmpty added in v0.3.0

func NotEmpty(t T, value any)

NotEmpty asserts that value is not empty.

func NotEqual

func NotEqual[V comparable](t T, unexpected, actual V)

NotEqual asserts that unexpected and actual are not equal.

func NotEqualMap

func NotEqualMap[M ~map[K]V, K, V comparable](t T, unexpected, actual M)

NotEqualMap asserts that unexpected and actual maps are not equal.

func NotEqualSlice

func NotEqualSlice[S ~[]E, E comparable](t T, unexpected, actual S)

NotEqualSlice asserts that unexpected and actual slices are not equal.

func NotErrorIs added in v0.3.0

func NotErrorIs(t T, err, target error)

NotErrorIs asserts that err does not match target using errors.Is.

func NotNil

func NotNil(t T, value any)

NotNil asserts that value is not nil.

func True

func True(t T, value bool)

True asserts that value is true.

Types

type T

type T interface {
	Helper()
	Errorf(format string, args ...any)
}

T is the interface required for test assertions. Both *testing.T and *testing.B satisfy it.

Jump to

Keyboard shortcuts

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