package
0.0.0-20230101183712-202847b4b89b
Repository: https://github.com/corestoreio/pkg.git
Documentation: pkg.go.dev

# README

Go assertion library (fork of stretchr/testify/require)

This is a fork of stretchr's assertion library that does two things:

  1. It makes spotting differences in equality much easier. It uses repr and diffmatchpatch to display structural differences in colour.
  2. Aborts tests on first assertion failure (the same behaviour as stretchr/testify/require).
  3. CoreStore: The function CallerInfo reports correctly the line number in sub tests.

Example

Given the following test:

type Person struct {
  Name string
  Age  int
}

// Use github.com/alecthomas/assert
func TestDiff(t *testing.T) {
  expected := []*Person{{"Alec", 20}, {"Bob", 21}, {"Sally", 22}}
  actual := []*Person{{"Alex", 20}, {"Bob", 22}, {"Sally", 22}}
  assert.Equal(t, expected, actual)
}

// Use github.com/stretchr/testify/require
func TestTestifyDiff(t *testing.T) {
  expected := []*Person{{"Alec", 20}, {"Bob", 21}, {"Sally", 22}}
  actual := []*Person{{"Alex", 20}, {"Bob", 22}, {"Sally", 22}}
  require.Equal(t, expected, actual)
}

The following output illustrates the problems this solves. Firstly, it shows nested structures correctly, and secondly it highlights the differences between actual and expected text.

# Functions

CallerInfo returns an array of strings containing the file and line number of each stack frame leading from the current test to the assert call that failed.
Condition uses a Comparison to assert a complex condition.
Contains asserts that the specified string, list(array, slice...) or map contains the specified substring or element.
No description provided by the author
No description provided by the author
DirExists checks whether a directory exists in the given path.
ElementsMatch asserts that the specified listA(array, slice...) is equal to specified listB(array, slice...) ignoring the order of the elements.
Empty asserts that the specified object is empty.
Equal asserts that two objects are equal.
EqualError asserts that a function returned an error (i.e.
EqualValues asserts that two objects are equal or convertable to the same types and equal.
Error asserts that a function returned an error (i.e.
ErrorIsKind asserts that an error matches the expected Kind.
Eventually asserts that given condition will be met in waitFor time, periodically checking target function each tick.
Exactly asserts that two objects are equal in value and type.
ExactlyLength asserts that two objects with maximum length of their string fields are equal in value and type.
Fail reports a failure through.
False asserts that the specified value is false.
FileExists checks whether a file exists in the given path.
Implements asserts that an object is implemented by the specified interface.
InDelta asserts that the two numerals are within delta of each other.
InDeltaMapValues is the same as InDelta, but it compares all values between two maps.
InDeltaSlice is the same as InDelta, except it compares two slices.
InEpsilon asserts that expected and actual have a relative error less than epsilon.
InEpsilonSlice is the same as InEpsilon, except it compares each value from two slices.
IsType asserts that the specified objects are of the same type.
JSONEq asserts that two JSON strings are equivalent.
Len asserts that the specified object has specific length.
LenBetween asserts that the specified object has a length between a lower and an upper bound.
MatchesGolden compares haveData with the file content of pathGoldenFile.
Never asserts that the given condition doesn't satisfy in waitFor time, periodically checking the target function each tick.
Nil asserts that the specified object is nil.
NoDirExists checks whether a directory does not exist in the given path.
NoError asserts that a function returned no error (i.e.
NoFileExists checks whether a file does not exist in a given path.
NotContains asserts that the specified string, list(array, slice...) or map does NOT contain the specified substring or element.
NotEmpty asserts that the specified object is NOT empty.
NotEqual asserts that the specified values are NOT equal.
NotNil asserts that the specified object is not nil.
NotPanics asserts that the code inside the specified PanicTestFunc does NOT panic.
NotRegexp asserts that a specified regexp does not match a string.
NotSame asserts that two pointers do not reference the same object.
NotSubset asserts that the specified list(array, slice...) contains not all elements given in the specified subset(array, slice...).
NotZero asserts that i is not the zero value for its type.
ObjectsAreEqual determines if two objects are considered equal.
ObjectsAreEqualValues gets whether two objects are equal, or if their values are equal.
Panics asserts that the code inside the specified PanicTestFunc panics.
PanicsWithError asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value is an error that satisfies the EqualError comparison.
PanicsWithValue asserts that the code inside the specified PanicTestFunc panics, and that the recovered panic value equals the expected panic value.
Regexp asserts that a specified regexp matches a string.
Same asserts that two pointers reference the same object.
Subset asserts that the specified list(array, slice...) contains all elements given in the specified subset(array, slice...).
True asserts that the specified value is true.
WithinDuration asserts that the two times are within duration delta of each other.
Zero asserts that i is the zero value for its type.

# Variables

AnError is an error instance useful for testing.

# Interfaces

TestingT is an interface wrapper around *testing.T.

# Type aliases

Comparison a custom function that returns true on success and false on failure.
PanicTestFunc defines a func that should be passed to the assert.Panics and assert.NotPanics methods, and represents a simple func that takes no arguments, and returns nothing.