expect

package module
v0.9.9 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: LGPL-3.0 Imports: 8 Imported by: 0

README

expect

Go test expectations.

Intention of the library is to be able to write simple readable tests which produce easy to understand messages when the expectation fails.

func TestExample(t *testing.T) {
    expect.Value(t, "the guy", "Peter").ToBe("Steven")
    // will fail with error:
    //   expected the guy to be 'Steven' but it is 'Peter'
}

Expectations

ToBe

Asserts that the value is deeply equal to the provided value.

expect.Value(t, "the house", "big").ToBe("small")
// expected the house to be 'small' but it is 'big'
checking time.Time

When comparing times it can happen that two time instances with the exact same date/time can be different. This happens when one of them has location set to nil and the other to UTC. Although the documntation states that nil must be used instead of UTC some 3th party libs manage to return such instances.

checking nil

A check for nil will always be ok if the value is nil or a interface that points to a nil value. This allows that following works:

var a = *AType
expect.Value(t, "a nil", a).ToBe(nil)
checking error

For error comparison the Error strings are returned. This can lead to messages like expected Error to be 'foo' but it is 'foo'.

checking structs, slices, maps

It will print complex data types formated as yaml:

expect.Value(t, "array", []int{3, 1}).ToBe([]int{1, 3})
// expected array to be:
//   > - 1
//   > - 3
// but it is:
//   > - 3
//   > - 1

It will check for exact numbers:

expect.Value(t, "liters", 3.4500000000001).ToBe(3.45)
// expected liters to be 3.45 but it is 3.4500000000001
ToCount

Asserts that the list/map/chan/string has c elements.

expect.Value(t, "token", "F7gTr7y").ToCount(8)
// expected token to have 8 elements but it has 7 elements
ToHavePrefix/Suffix

Asserts that the string begins with the provided string or ends with it.

NotToBe

Asserts that the value is not deeply equal to the provided value.

ToBeAbout

Asserts that the number is about expected value with a margin of error of provided delta.

ToBeType

Asserts that the type of the value is the same of the value given as parameter.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	PlainOutput       = output("plain")
	ColoredDiffOutput = output("coloredDiffOutput")
)
View Source
var Default = &Expect{
	Output: PlainOutput,
}

Functions

This section is empty.

Types

type Expect

type Expect struct {
	Output output
}

func (*Expect) Error added in v0.3.0

func (e *Expect) Error(t Test, val interface{}) Val

Error wraps an error and provides expectations for this value. This is a shortcut for Value(t, "error", val).

func (*Expect) Value

func (e *Expect) Value(t Test, name string, val interface{}) Val

Value wraps a value and provides expectations for this value.

type Test

type Test interface {
	Fatalf(f string, i ...interface{})
	Errorf(f string, i ...interface{})
	Error(p ...interface{})
	Helper()
}

Test implements testing.T methods used by expect. Necessary to: - allow usage of testing.T and testing.B instances - for running tests

type Val

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

Val to call expectations on.

func Error added in v0.3.0

func Error(t Test, val interface{}) Val

Error wraps an error and provides expectations for this value. It delegates to the default instance `Default`.

func Value

func Value(t Test, name string, val interface{}) Val

Value wraps a value and provides expectations for this value. It delegates to the default instance `Default`.

func (Val) First added in v0.6.0

func (e Val) First() Val

func (Val) Last added in v0.6.0

func (e Val) Last() Val

func (Val) Message added in v0.3.0

func (e Val) Message() Val

Message creates a new value from the given errors message. If the error is nil the message wil be the empty string.

func (Val) NotToBe

func (e Val) NotToBe(unExpected interface{}) Val

NotToBe asserts that the value is not deeply equals to expected value.

func (Val) ToBe

func (e Val) ToBe(expected interface{}) Val

ToBe asserts that the value is deeply equals to expected value.

func (Val) ToBeAbout

func (e Val) ToBeAbout(expected, delta float64) Val

ToBeAbout asserts that the number is in deltas range of expected value. Only works for numbers.

func (Val) ToBeType added in v0.9.8

func (e Val) ToBeType(t any) Val

func (Val) ToCount

func (e Val) ToCount(c int) Val

ToCount asserts that the list/map/chan/string has c elements. Strings use the number of unicode chars.

func (Val) ToHavePrefix

func (e Val) ToHavePrefix(prefix string) Val

ToHavePrefix asserts that the string value starts with the provided prefix.

func (Val) ToHaveSuffix

func (e Val) ToHaveSuffix(suffix string) Val

ToHaveSuffix asserts that the string value ends with the provided sufix.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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