hamcrest

package module
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2021 License: MIT Imports: 4 Imported by: 0

README

GO Hamcrest

Code Coverage Go Report Card Go Reference GitHub license

I am about to do my first steps with Go, so please don't expect this to be idiomatic. Coming from the Java Enterprise world(T_x). I am used to working with the hamcrest matcher framework, which makes it easy (at least for me) to write readable assertions. Maybe someone else finds this useful too.

Contributions and Feedback are welcome.

Assertions / Requirements

assert := hamcrest.NewAssertion(t)

require := hamcrest.NewRequirement(t)

Can be used interchangeably - the only difference is that a requirement stops the test case in case of a failing matcher, whereas an assertion continues with the test execution.

Matcher

General
assert := hamcrest.NewAssertion(t)

assert.That(actual, is.EqualTo(10))
assert.That(actual, is.NotEqualTo(10))

assert.That(actual, is.True())
assert.That(actual, is.Ok())
assert.That(actual, is.False())

assert.That(actual, is.Nil())
assert.That(actual, is.NotNil())
assert.That(actual, is.PointingTo(givenPtr))
Numeric
assert := hamcrest.NewAssertion(t)

assert.That(actual, is.GreaterThan(10))
assert.That(actual, is.GreaterThanEqual(10))
assert.That(actual, is.LessThan(10))
assert.That(actual, is.LessThanEqual(10))

assert.That(actual, is.Between(10, 100))
assert.That(actual, is.BetweenOrEqual(10, 100))

assert.That(actual, is.NotBetween(10, 100))
assert.That(actual, is.NotBetweenOrEqual(10, 100))

assert.That(actual, is.CloseTo(9.5, 0.001))
assert.That(actual, is.NotCloseTo(9.5, 0.001))

Strings
assert := hamcrest.NewAssertion(t)

assert.That(acutal, has.Prefix("abc"))
assert.That(acutal, has.NotPrefix("abc"))

assert.That(acutal, has.Suffix("xyz"))
assert.That(acutal, has.NotSuffix("xyz"))

assert.That(actual, is.MatchingPattern(pattern))
assert.That(actual, is.NotMatchingPattern(pattern))

assert.That(actual, has.Length(3))

assert.That(actual, is.Empty())
assert.That(actual, is.NotEmpty())
Collections
assert := hamcrest.NewAssertion(t)

assert.That(actual, is.Empty())
assert.That(actual, is.NotEmpty())
assert.That(actual, has.Item(10))
assert.That(actual, has.NotItem(10))
assert.That(actual, has.Items(10,20))
assert.That(actual, has.NotItems(10,20))

assert.That(actual, has.Key("someKey"))
assert.That(actual, has.NotKey("someKey"))
assert.That(actual, has.Keys("K1", "K2"))
assert.That(actual, has.NotKeys("K1", "K2"))
Boolean

assert := hamcrest.NewAssertion(t)

assert.That(actual, is.EqualTo(true))
assert.That(actual, is.NotEqualTo(false))

assert.That(actual, is.True())
assert.That(actual, is.False())
assert.That(actual, is.Ok())
assert.That(actual, is.NotOk())

Time
assert := hamcrest.NewAssertion(t)

assert.That(actual, is.Before(given))
assert.That(actual, is.NotBefore(given))
assert.That(actual, is.BeforeOrEqual(given))

assert.That(actual, is.After(given))
assert.That(actual, is.NotAfter(given))
assert.That(actual, is.AfterOrEqual(given))

Type
assert := hamcrest.NewAssertion(t)

assert.That(actual, has.SameTypeAs(given))
assert.That(actual, has.NotSameTypeAs(given))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Assertion

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

func NewAssertion

func NewAssertion(t *testing.T) *Assertion

func (*Assertion) That

func (a *Assertion) That(actual interface{}, matcher matcher.Matcher, messages ...MessageInterface) matcher.Matcher

type MessageInterface

type MessageInterface interface {
	String() string
}

func Message

func Message(m string) MessageInterface

func MessageF

func MessageF(format string, args ...interface{}) MessageInterface

type Requirement

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

func NewRequirement

func NewRequirement(t *testing.T) *Requirement

func (*Requirement) That

func (r *Requirement) That(actual interface{}, matcher matcher.Matcher, messages ...MessageInterface) matcher.Matcher

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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