should

package
v0.21.5 Latest Latest
Warning

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

Go to latest
Published: Apr 11, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Package should

This package strives to make it easy, even fun, for software developers to produce > a quick, sure, and repeatable proof that every element of the code works as it should. (See [The Programmer's Oath](http://blog.cleancoder.com/uncle-bob/2015/11/18/TheProgrammersOath.html))

The simplest way is by combining the So function with the many provided assertions, such as should.Equal:

package whatever

import (
	"log"
	"testing"

	"github.com/mdwhatcott/testing/should"
)

func Test(t *testing.T) {
	should.So(t, 1, should.Equal, 1)
}

This package also implement an xUnit-style test runner, which is based on the following packages:

For those using an IDE by JetBrains, you may find the following "live template" helpful:

func Test$NAME$Suite(t *testing.T) {
	should.Run(&$NAME$Suite{T: should.New(t)}, should.Options.UnitTests())
}

type $NAME$Suite struct {
	*should.T
}

func (this *$NAME$Suite) Setup() {
}

func (this *$NAME$Suite) Test$END$() {
}

From a test method like the one in the template above, simply use the embedded So method:

func (this TheSuite) TestSomething() {
	this.So(1, should.Equal, 1)
}

Happy testing!

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrExpectedCountInvalid = errors.New("expected count invalid")
	ErrTypeMismatch         = errors.New("type mismatch")
	ErrKindMismatch         = errors.New("kind mismatch")
	ErrAssertionFailure     = errors.New("assertion failure")
)
View Source
var NOT negated

NOT (a singleton) constrains all negated assertions to their own namespace.

Functions

func BeChronological added in v0.19.0

func BeChronological(actual any, expected ...any) error

BeChronological asserts whether actual is a []time.Time and whether the values are in chronological order.

func BeEmpty

func BeEmpty(actual any, expected ...any) error

BeEmpty uses reflection to verify that len(actual) == 0.

func BeFalse

func BeFalse(actual any, expected ...any) error

BeFalse verifies that actual is the boolean false value.

func BeGreaterThan added in v0.15.0

func BeGreaterThan(actual any, EXPECTED ...any) error

BeGreaterThan verifies that actual is greater than expected. Both actual and expected must be strings or numeric in type.

func BeGreaterThanOrEqualTo added in v0.15.0

func BeGreaterThanOrEqualTo(actual any, expected ...any) error

BeGreaterThanOrEqualTo verifies that actual is less than or equal to expected. Both actual and expected must be strings or numeric in type.

func BeIn added in v0.8.0

func BeIn(actual any, expected ...any) error

BeIn determines whether actual is a member of expected[0]. It defers to Contain.

func BeLessThan added in v0.15.0

func BeLessThan(actual any, EXPECTED ...any) error

BeLessThan verifies that actual is less than expected. Both actual and expected must be strings or numeric in type.

func BeLessThanOrEqualTo added in v0.15.0

func BeLessThanOrEqualTo(actual any, expected ...any) error

BeLessThanOrEqualTo verifies that actual is less than or equal to expected. Both actual and expected must be strings or numeric in type.

func BeNil

func BeNil(actual any, expected ...any) error

BeNil verifies that actual is the nil value.

func BeTrue

func BeTrue(actual any, expected ...any) error

BeTrue verifies that actual is the boolean true value.

func Contain added in v0.7.0

func Contain(actual any, expected ...any) error

Contain determines whether actual contains expected[0]. The actual value may be a map, array, slice, or string:

  • In the case of maps the expected value is assumed to be a map key.
  • In the case of slices and arrays the expected value is assumed to be a member.
  • In the case of strings the expected value may be a rune or substring.

func EndWith added in v0.8.0

func EndWith(actual any, expected ...any) error

EndWith verifies that actual ends with expected[0]. The actual value may be an array, slice, or string.

func Equal

func Equal(actual any, EXPECTED ...any) error

Equal verifies that the actual value is equal to the expected value. It uses reflect.DeepEqual in most cases, but also compares numerics regardless of specific type and compares time.Time values using the time.Equal method.

func HappenAfter added in v0.19.0

func HappenAfter(actual any, expected ...any) error

HappenAfter ensures that the first time value happens after the second.

func HappenBefore added in v0.19.0

func HappenBefore(actual any, expected ...any) error

HappenBefore ensures that the first time value happens before the second.

func HappenOn added in v0.19.0

func HappenOn(actual any, expected ...any) error

HappenOn ensures that two time values happen at the same instant. See the time.Time.Equal method for the details. This function defers to Equal to do the work.

func HaveLength added in v0.4.0

func HaveLength(actual any, expected ...any) error

HaveLength uses reflection to verify that len(actual) == 0.

func Panic added in v0.5.0

func Panic(actual any, expected ...any) (err error)

Panic invokes the func() provided as actual and recovers from any panic. It returns an error if actual() does not result in a panic.

func Run added in v0.18.0

func Run(fixture any, options ...Option)

Run accepts a fixture with Test* methods and optional setup/teardown methods and executes the suite. Fixtures must be struct types which embed a *suite.T. Assuming a fixture struct with test methods 'Test1' and 'Test2' execution would proceed as follows:

  1. fixture.SetupSuite()
  2. fixture.Setup()
  3. fixture.Test1()
  4. fixture.Teardown()
  5. fixture.Setup()
  6. fixture.Test2()
  7. fixture.Teardown()
  8. fixture.TeardownSuite()

The methods provided by Options may be supplied to this function to tweak the execution.

func So added in v0.18.0

func So(t *testing.T, actual any, assertion Func, expected ...any)

func StartWith added in v0.8.0

func StartWith(actual any, expected ...any) error

StartWith verified that actual starts with expected[0]. The actual value may be an array, slice, or string.

func WrapError added in v0.4.0

func WrapError(actual any, expected ...any) error

WrapError uses errors.Is to verify that actual is an error value that wraps expected[0] (also an error value).

Types

type CompositeReporter added in v0.20.0

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

func NewCompositeReporter added in v0.20.0

func NewCompositeReporter(reporters ...Reporter) *CompositeReporter

func (*CompositeReporter) Helper added in v0.21.1

func (this *CompositeReporter) Helper()

func (*CompositeReporter) Report added in v0.20.0

func (this *CompositeReporter) Report(err error)

func (*CompositeReporter) Write added in v0.20.0

func (this *CompositeReporter) Write(p []byte) (n int, err error)

type Func added in v0.18.0

type Func func(actual any, expected ...any) error

type LogReporter added in v0.20.0

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

func NewLogReporter added in v0.20.0

func NewLogReporter(logger *log.Logger) *LogReporter

func (LogReporter) Helper added in v0.21.3

func (this LogReporter) Helper()

func (LogReporter) Report added in v0.20.0

func (this LogReporter) Report(err error)

func (LogReporter) Write added in v0.20.0

func (this LogReporter) Write(p []byte) (n int, err error)

type Opt added in v0.18.0

type Opt struct{}
var Options Opt

Options provides the sole entrypoint to the option functions provided by this package.

func (Opt) FreshFixture added in v0.18.0

func (Opt) FreshFixture() Option

FreshFixture signals to Run that the new instances of the provided fixture are to be instantiated for each and every test case. The Setup and Teardown methods are also executed on the specifically instantiated fixtures. NOTE: the SetupSuite and TeardownSuite methods are always run on the provided fixture instance, regardless of this options having been provided.

func (Opt) IntegrationTests added in v0.18.0

func (Opt) IntegrationTests() Option

IntegrationTests is a composite option that signals to Run that the test suite should be treated as an integration test suite, avoiding parallelism and utilizing shared fixtures to allow reuse of potentially expensive resources.

func (Opt) ParallelFixture added in v0.18.0

func (Opt) ParallelFixture() Option

ParallelFixture signals to Run that the provided fixture instance can be executed in parallel with other go test functions. This option assumes that `go test` was invoked with the -parallel flag.

func (Opt) ParallelTests added in v0.18.0

func (Opt) ParallelTests() Option

ParallelTests signals to Run that the test methods on the provided fixture instance can be executed in parallel with each other. This option assumes that `go test` was invoked with the -parallel flag.

func (Opt) SharedFixture added in v0.18.0

func (Opt) SharedFixture() Option

SharedFixture signals to Run that the provided fixture instance is to be used to run all test methods. This mode is not compatible with ParallelFixture or ParallelTests and disables them.

func (Opt) UnitTests added in v0.18.0

func (Opt) UnitTests() Option

UnitTests is a composite option that signals to Run that the test suite can be treated as a unit-test suite by employing parallelism and fresh fixtures to maximize the chances of exposing unwanted coupling between tests.

type Option added in v0.18.0

type Option func(*config)

Option is a function that modifies a config. See Options for provided behaviors.

type Reporter added in v0.20.0

type Reporter interface {
	Helper()
	Report(error)
	io.Writer
}

type T added in v0.18.0

type T struct{ Reporter }

func New added in v0.18.0

func New(t *testing.T) *T

func Report added in v0.20.0

func Report(reporters ...Reporter) *T

func (*T) Print added in v0.21.0

func (this *T) Print(v ...any)

func (*T) Printf added in v0.21.0

func (this *T) Printf(f string, v ...any)

func (*T) Println added in v0.21.0

func (this *T) Println(v ...any)

func (*T) So added in v0.18.0

func (this *T) So(actual any, assertion Func, expected ...any) (ok bool)

type TestingReporter added in v0.20.0

type TestingReporter struct{ *testing.T }

func NewTestingReporter added in v0.20.0

func NewTestingReporter(t *testing.T) *TestingReporter

func (*TestingReporter) Report added in v0.20.0

func (this *TestingReporter) Report(err error)

func (*TestingReporter) Write added in v0.20.0

func (this *TestingReporter) Write(p []byte) (n int, err error)

type WriterReporter added in v0.20.0

type WriterReporter struct{ io.Writer }

func NewWriterReporter added in v0.20.0

func NewWriterReporter(writer io.Writer) *WriterReporter

func (*WriterReporter) Helper added in v0.21.1

func (this *WriterReporter) Helper()

func (*WriterReporter) Report added in v0.20.0

func (this *WriterReporter) Report(err error)

Jump to

Keyboard shortcuts

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