hammy

package
v0.0.0-...-a46c722 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2023 License: BSD-3-Clause Imports: 5 Imported by: 0

README

Hammy

GoDoc reference example

Hammy is a HamCrest inspired assertion library. The aim is to provide terse compile-time oriented type checking.

Getting Started

package adder

import (
	"github.com/gogunit/gunit/hammy"
)

func Test_calculator(t *testing.T) {
	assert := hammy.New(t)
	actual := Add(2, 3)
	assert.Is(hammy.Number(actual).EqualTo(5))
}

Map

  • EqualTo
  • IsEmpty
  • Len
  • WithItem
  • WithKeys
  • WithoutKeys
  • WithValues

Number

  • EqualTo
  • GreaterThan
  • GreaterOrEqual
  • IsZero
  • LessThan
  • LessOrEqual
  • Within

Slice

  • Contains
  • EqualTo
  • Len

String

  • EqualTo
  • ToLowerEqualTo
  • Contains
  • HasPrefix
  • HasSuffix
  • IsEmpty

Struct

  • EqualTo

Writing a Custom Matcher

package model

import "github.com/gogunit/gunit/hammy"

func Matcher(model Model) *Matchy {
	return &Matchy{
		model: model,
    }
}

type Matchy struct {
	model Model
}

func (m *Matcher) HasName(expected string) hammy.AssertionMessage {
	actual := m.model.Name
	return hammy.Assert(actual == expected, "want Name=<%v> equal to <%v>", actual, expected)
}
assert.Is(Matcher(model).HasName("bar"))

// fails with the message
// want Name=<"foo"> equal to <"bar">

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Join

func Join[T any](a []T, sep string) string

Types

type AssertionMessage

type AssertionMessage struct {
	IsSuccessful bool
	Message      string
}

func Assert

func Assert(isSuccessful bool, str string, args ...any) AssertionMessage

func Error

func Error(err error) AssertionMessage

func False

func False(actual bool) AssertionMessage

func Nil

func Nil[T any](actual *T) AssertionMessage

func NilError

func NilError(err error) AssertionMessage

func NotNil

func NotNil[T any](actual *T) AssertionMessage

func True

func True(actual bool) AssertionMessage

type Hammy

type Hammy struct {
	gunit.T
}

func New

func New(t gunit.T) *Hammy

func (*Hammy) Is

func (h *Hammy) Is(a AssertionMessage)

func (*Hammy) That

func (h *Hammy) That(msg string, a AssertionMessage)

type Mappy

type Mappy[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func Map

func Map[K comparable, V any](actual map[K]V) *Mappy[K, V]

func (Mappy[K, V]) EqualTo

func (m Mappy[K, V]) EqualTo(expected map[K]V) AssertionMessage

func (Mappy[K, V]) IsEmpty

func (m Mappy[K, V]) IsEmpty() AssertionMessage

func (Mappy[K, V]) Len

func (m Mappy[K, V]) Len(expected int) AssertionMessage

func (Mappy[K, V]) WithItem

func (m Mappy[K, V]) WithItem(k K, expected V) AssertionMessage

func (Mappy[K, V]) WithKeys

func (m Mappy[K, V]) WithKeys(expected ...K) AssertionMessage

func (Mappy[K, V]) WithValues

func (m Mappy[K, V]) WithValues(values ...V) AssertionMessage

func (Mappy[K, V]) WithoutKeys

func (m Mappy[K, V]) WithoutKeys(keys ...K) AssertionMessage

type Num

type Num[N Numeric] struct {
	// contains filtered or unexported fields
}

func Number

func Number[N Numeric](actual N) *Num[N]

func (*Num[N]) EqualTo

func (n *Num[N]) EqualTo(expected N) AssertionMessage

func (*Num[N]) GreaterOrEqual

func (n *Num[N]) GreaterOrEqual(expected N) AssertionMessage

func (*Num[N]) GreaterThan

func (n *Num[N]) GreaterThan(expected N) AssertionMessage

func (*Num[N]) IsZero

func (n *Num[N]) IsZero() AssertionMessage

func (*Num[N]) LessOrEqual

func (n *Num[N]) LessOrEqual(expected N) AssertionMessage

func (*Num[N]) LessThan

func (n *Num[N]) LessThan(expected N) AssertionMessage

func (*Num[N]) NotEqual

func (n *Num[N]) NotEqual(expected N) AssertionMessage

func (*Num[N]) Within

func (n *Num[N]) Within(expected N, error float64) AssertionMessage

type Numeric

type Numeric interface {
	~int | ~int8 | ~int16 | ~int32 | ~int64 |
		~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 |
		~float32 | ~float64
}

type Slc

type Slc[I comparable] struct {
	// contains filtered or unexported fields
}

func Slice

func Slice[I comparable](actual []I) *Slc[I]

func (*Slc[I]) Contains

func (a *Slc[I]) Contains(expected ...I) AssertionMessage

Contains asserts whether the slice contains the expected elements in any order.

func (*Slc[I]) ContainsExactly

func (a *Slc[I]) ContainsExactly(expected ...I) AssertionMessage

ContainsExactly asserts that the slice contains the exact number of elements in any order.

func (*Slc[I]) EqualTo

func (a *Slc[I]) EqualTo(expected ...I) AssertionMessage

EqualTo asserts whether the slice is equal to the expected items in both order and values.

func (*Slc[I]) IsEmpty

func (a *Slc[I]) IsEmpty() AssertionMessage

IsEmpty asserts that the slice contains no elements.

func (*Slc[I]) Len

func (a *Slc[I]) Len(expected int) AssertionMessage

Len asserts that the slice contains exactly the number of elements specified.

type St

type St[S any] struct {
	// contains filtered or unexported fields
}

func Struct

func Struct[S any](actual S) *St[S]

func (*St[S]) EqualTo

func (s *St[S]) EqualTo(expected S) AssertionMessage

type Str

type Str[S Stringy] struct {
	// contains filtered or unexported fields
}

func String

func String[S Stringy](actual S) *Str[S]

func (*Str[S]) Contains

func (s *Str[S]) Contains(expected string) AssertionMessage

func (*Str[S]) EqualTo

func (s *Str[S]) EqualTo(expected S) AssertionMessage

func (*Str[S]) HasPrefix

func (s *Str[S]) HasPrefix(expected string) AssertionMessage

func (*Str[S]) HasSuffix

func (s *Str[S]) HasSuffix(expected string) AssertionMessage

func (*Str[S]) IsEmpty

func (s *Str[S]) IsEmpty() AssertionMessage

func (*Str[S]) ToLowerEqualTo

func (s *Str[S]) ToLowerEqualTo(expected string) AssertionMessage

type Stringy

type Stringy interface {
	~string
}

Jump to

Keyboard shortcuts

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