tutil

package
v0.42.1 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2023 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package tutil contains basic generic test utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AnySlice added in v0.41.0

func AnySlice(slice any) []any

AnySlice converts a typed slice (such as []string) to []any. If slice is already of type []any, it is returned unmodified. Otherwise a new []any is constructed. If slice is nil, nil is returned. The function panics if slice is not a slice.

Note that this function uses reflection, and may panic. It is only to be used by test code.

func Chdir added in v0.37.0

func Chdir(t testing.TB, dir string) (absDir string)

Chdir changes the working directory to dir, or if dir is empty, to a temp dir. On test end, the original working dir is restored, and the temp dir deleted (if applicable). The absolute path of the changed working dir is returned.

func DirCopy added in v0.34.0

func DirCopy(t testing.TB, sourceDir string, keep bool) (tmpDir string)

DirCopy copies the contents of sourceDir to a temp dir. If keep is false, temp dir will be cleaned up on test exit.

func MustAbsFilepath added in v0.42.0

func MustAbsFilepath(elems ...string) string

MustAbsFilepath invokes filepath.Join on elems, and then filepath.Abs on the result. It panics on error.

func Name

func Name(args ...any) string

Name is a convenience function for building a test name to pass to t.Run.

t.Run(testh.Name("my_test", 1), func(t *testing.T) {

The most common usage is with test names that are file paths.

testh.Name("path/to/file") --> "path_to_file"

Any element of arg that prints to empty string is skipped.

func SkipIff

func SkipIff(t testing.TB, b bool, format string, args ...any)

SkipIff skips t if b is true. If msgAndArgs is non-empty, its first element must be a string, which can be a format string if there are additional elements.

Examples:

tutil.SkipIff(t, a == b)
tutil.SkipIff(t, a == b, "skipping because a == b")
tutil.SkipIff(t, a == b, "skipping because a is %v and b is %v", a, b)

func SkipShort

func SkipShort(t testing.TB, skip bool)

SkipShort invokes t.Skip if testing.Short and arg skip are both true.

func SkipWindows added in v0.37.0

func SkipWindows(t testing.TB, format string, args ...any)

SkipWindows skips t if running on Windows.

func SkipWindowsIf added in v0.39.0

func SkipWindowsIf(t testing.TB, b bool, format string, args ...any)

SkipWindowsIf skips t if running on Windows and b is true.

func SliceFieldKeyValues

func SliceFieldKeyValues(keyFieldName, valFieldName string, slice any) map[any]any

SliceFieldKeyValues is similar to SliceFieldValues, but instead of returning a slice of field values, it returns a map containing two field values, a "key" and a "value". For example:

persons := []*person{
  {Name: "Alice", Age: 42},
  {Name: "Bob", Age: 27},
}

m := SliceFieldKeyValues("Name", "Age", persons)
// map[Alice:42 Bob:27]

Note that this function uses reflection, and may panic. It is only to be used by test code.

See also: StructFieldValue, SliceFieldValues.

func SliceFieldValues

func SliceFieldValues(fieldName string, slice any) []any

SliceFieldValues takes a slice of structs, and returns a slice containing the value of fieldName for each element of slice.

Note that slice can be []interface{}, or a typed slice (e.g. []*Person). If slice is nil, nil is returned. If slice has len zero, an empty slice is returned. The function panics if slice is not a slice, or if any element of slice is not a struct (excepting nil elements).

Note that this function uses reflection, and may panic. It is only to be used by test code.

See also: StructFieldValue, SliceFieldKeyValues.

func StructFieldValue

func StructFieldValue(fieldName string, strct any) any

StructFieldValue extracts the value of fieldName from arg strct. If strct is nil, nil is returned. The function will panic if strct is not a struct (or pointer to struct), or if the struct does not have fieldName. The returned value may be nil if the field is a pointer and is nil.

Note that this function uses reflection, and may panic. It is only to be used by test code.

See also: SliceFieldValues, SliceFieldKeyValues.

func WriteTemp added in v0.41.0

func WriteTemp(t testing.TB, pattern string, b []byte, cleanup bool) (fpath string)

WriteTemp writes b to a temporary file. The pattern arg is used to generate the file name, per os.CreateTemp. If cleanup is true, the file is deleted on test cleanup.

func Writer added in v0.34.0

func Writer(t testing.TB) io.Writer

Writer returns an io.Writer whose Write method invokes t.Log. A newline is prepended to the log output.

Types

type AssertCompareFunc

type AssertCompareFunc func(require.TestingT, any, any, ...any)

AssertCompareFunc matches several of the testify/require funcs. It can be used to choose assertion comparison funcs in test cases.

Jump to

Keyboard shortcuts

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