matchers

package module
v0.0.0-...-3b363e0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2022 License: Apache-2.0 Imports: 6 Imported by: 0

README

Go Matchers

This library provides googletest inspired matchers in Golang.

It uses generics to provide a typesafe matchers so trying to match non-compatible types will fail at compile time.

Example

import m "github.com/ntnj/go-generics/matchers"

func TestM(t *testing.T) {
  got := 42
  m.Expect(t, got, m.Eq(42))

  list := []int{2,4,6}
  m.Expect(t, list, m.Contains(Eq(4)))
  m.Expect(t, list, m.IsPermutationOf(Eq(4), Eq(6), Eq(2)))
}

In case of match failure, the test will fail with a nicely formatted error message, making both the tests and errors easier to read.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Assert

func Assert[T any](t testing.TB, v T, m Matcher[T])

func Expect

func Expect[T any](t testing.TB, v T, m Matcher[T])

Types

type Matcher

type Matcher[T any] interface {
	fmt.Stringer
	Match(v T) error
}

func AllOf

func AllOf[T any](ms ...Matcher[T]) Matcher[T]

func AnyOf

func AnyOf[T any](ms ...Matcher[T]) Matcher[T]

func Contains

func Contains[T any](m Matcher[T]) Matcher[[]T]

Contains checks if the list contains an element matching m.

func ContainsEq

func ContainsEq[T comparable](v T) Matcher[[]T]

ContainsEq checks if the list argument contains an element equal to v.

func ContainsRegex

func ContainsRegex(r string) Matcher[string]

func ContainsRegexPOSIX

func ContainsRegexPOSIX(r string) Matcher[string]

func Each

func Each[T any](m Matcher[T]) Matcher[[]T]

Each checks if each element in list matches m.

func ElementsAre

func ElementsAre[T any](m ...Matcher[T]) Matcher[[]T]

ElementsAre checks each element in list matches in order.

func ElementsAreEq

func ElementsAreEq[T comparable](vs ...T) Matcher[[]T]

func EndsWith

func EndsWith(s string) Matcher[string]

func Eq

func Eq[T comparable](v T) Matcher[T]

func Ge

func Ge[T constraints.Ordered](v T) Matcher[T]

func Gt

func Gt[T constraints.Ordered](v T) Matcher[T]

func HasSubstr

func HasSubstr(s string) Matcher[string]

func IsPermutationOf

func IsPermutationOf[T any](m ...Matcher[T]) Matcher[[]T]

func IsPermutationOfEq

func IsPermutationOfEq[T comparable](vs ...T) Matcher[[]T]

func IsSubsetOf

func IsSubsetOf[T any](m ...Matcher[T]) Matcher[[]T]

func IsSubsetOfEq

func IsSubsetOfEq[T comparable](vs ...T) Matcher[[]T]

func IsSupersetOf

func IsSupersetOf[T any](m ...Matcher[T]) Matcher[[]T]

func IsSupersetOfEq

func IsSupersetOfEq[T comparable](vs ...T) Matcher[[]T]

func Le

func Le[T constraints.Ordered](v T) Matcher[T]

func Lt

func Lt[T constraints.Ordered](v T) Matcher[T]

func Not

func Not[T any](m Matcher[T]) Matcher[T]

func StartsWith

func StartsWith(s string) Matcher[string]

func UnorderedElementsAre

func UnorderedElementsAre[T any](m ...Matcher[T]) Matcher[[]T]

func UnorderedElementsAreEq

func UnorderedElementsAreEq[T comparable](vs ...T) Matcher[[]T]

func WrapsError

func WrapsError(err error) Matcher[error]

Jump to

Keyboard shortcuts

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