tdutil

package
v1.14.0 Latest Latest
Warning

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

Go to latest
Published: Dec 27, 2023 License: BSD-2-Clause Imports: 10 Imported by: 0

Documentation

Overview

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BuildTestName

func BuildTestName(args ...any) string

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.

func FbuildTestName

func FbuildTestName(w io.Writer, args ...any)

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.

func FormatString

func FormatString(s string) string

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

func MapEach added in v1.1.0

func MapEach(m reflect.Value, fn func(k, v reflect.Value) bool) bool

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

func MapEachValue added in v1.1.0

func MapEachValue(m reflect.Value, fn func(k reflect.Value) bool) bool

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

func MapSortedKeys added in v1.1.0

func MapSortedKeys(m reflect.Value) []reflect.Value

MapSortedKeys returns a slice of all sorted keys of map m. It panics if m's reflect.Kind is not reflect.Map.

func MapSortedValues added in v1.1.0

func MapSortedValues(m reflect.Value) []reflect.Value

MapSortedValues returns a slice of all sorted values of map m. It panics if m's reflect.Kind is not reflect.Map.

func SortableValues added in v1.1.0

func SortableValues(s []reflect.Value) sort.Interface

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:

  • nil is always lower
  • different types are sorted by their name
  • false is lesser than true
  • float and int numbers are sorted by their value
  • 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.

func SpewString

func SpewString(val any) string

SpewString uses spew.Sdump to format val.

Types

type T

type T struct {
	testing.T
	// contains filtered or unexported fields
}

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

func NewT added in v1.2.0

func NewT(name string) *T

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

func (*T) CatchFailNow added in v1.10.0

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.

func (*T) FailNow added in v1.10.0

func (t *T) FailNow()

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

func (*T) Fatal added in v1.10.0

func (t *T) Fatal(args ...any)

Fatal simulates the original testing.T.Fatal.

func (*T) Fatalf added in v1.10.0

func (t *T) Fatalf(format string, args ...any)

Fatal simulates the original testing.T.Fatalf.

func (*T) LogBuf

func (t *T) LogBuf() string

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

func (*T) Name added in v1.2.0

func (t *T) Name() string

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

func (*T) Run

func (t *T) Run(name string, f func(*testing.T)) bool

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

Jump to

Keyboard shortcuts

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