Back to godoc.org

Package tdutil

v1.7.0
Latest Go to latest
Published: Jul 19, 2020 | License: BSD-2-Clause | Module: github.com/maxatome/go-testdeep

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

Package Files

func BuildTestName

func BuildTestName(args ...interface{}) 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.Sprintf, else they are passed to fmt.Sprint.

func FbuildTestName

func FbuildTestName(w io.Writer, args ...interface{})

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 "" or `` and defaulting to using SpewString.

func MapEach

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

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

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

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

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 interface{}) string

SpewString uses github.com/davecgh/go-spew/spew.Sdump() to format val.

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 Run() method.

func NewT

func NewT(name string) *T

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

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

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.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier