tdutil package - github.com/maxatome/go-testdeep/helpers/tdutil - Go Packages

Package tdutil allows to write unit tests for go-testdeep helpers and so provides some helpful functions.

It is not intended to be used in tests outside go-testdeep and its helpers perimeter.

This section is empty.

This section is empty.

BuildTestName builds a string from given args.

If optional first args is a string containing at least one %, args are passed as is to fmt.Fprintf, else they are passed to fmt.Fprint.

CmpValuesFunc returns a function able to compare 2 reflect.Value values.

The sorting rules are listed in SortableValues documentation.

values := s := []reflect.Value{
  reflect.ValueOf(4),
  reflect.ValueOf(3),
  reflect.ValueOf(1),
}
slices.SortFunc(s, tdutil.CmpValuesFunc())

Cyclic references are correctly handled.

See also SortableValues.

FbuildTestName builds a string from given args.

If optional first args is a string containing at least one %, args are passed as is to fmt.Fprintf, else they are passed to fmt.Fprint.

FormatString formats s to a printable string, trying to enclose it in double-quotes or back-quotes and defaulting to using SpewString.

MapEach calls fn for each key/value pair of map m. If fn returns false, it will not be called again. MapEach returns false if fn returned false.

MapEachValue calls fn for each value of map m. If fn returns false, it will not be called again. MapEachValue returns false if fn returned false.

SortableValues is used to allow the sorting of a []reflect.Value slice. It is used with the standard sort package:

vals := []reflect.Value{a, b, c, d}
sort.Sort(SortableValues(vals))
// vals contents now sorted

Replace sort.Sort by sort.Stable for a stable sort. See sort documentation.

Sorting rules are as follows:

  • invalid value is always lower
  • nil is always lower
  • different types are sorted by their name
  • if method TYPE.Compare(TYPE) int exits, calls it
  • false is lesser than true
  • float and int numbers are sorted by their value, NaN is always lower
  • complex numbers are sorted by their real, then by their imaginary parts
  • strings are sorted by their value
  • map: shorter length is lesser, then sorted by address
  • functions, channels and unsafe pointer are sorted by their address
  • struct: comparison is spread to each field
  • pointer: comparison is spread to the pointed value
  • arrays: comparison is spread to each item
  • slice: comparison is spread to each item, then shorter length is lesser
  • interface: comparison is spread to the value

Cyclic references are correctly handled.

See also CmpValuesFunc.

T can be used in tests, to test testing.T behavior as it overrides testing.T.Run method.

NewT returns a new *T instance. name is the string returned by method Name.

func (t *T) CatchFailNow(fn func()) (failNowOccurred bool)

CatchFailNow returns true if a T.FailNow, T.Fatal or T.Fatalf call occurred during the execution of fn.

FailNow simulates the original testing.T.FailNow using panic. T.CatchFailNow should be used to properly intercept it.

LogBuf is an ugly hack allowing to access internal testing.T log buffer. Keep cool, it is only used for internal unit tests.

Name returns the name of the running test (in fact the one set by NewT).

Run is a simplified version of testing.T.Run method, without edge cases.