tutil

package
v0.24.2 Latest Latest
Warning

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

Go to latest
Published: Mar 14, 2023 License: MIT Imports: 6 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 InterfaceSlice

func InterfaceSlice(slice any) []any

InterfaceSlice converts a typed slice (such as []string) to []interface{}. If slice is already of type []interface{}, it is returned unmodified. Otherwise a new []interface{} 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 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.T, skip bool)

SkipShort invokes t.Skip if testing.Short and arg skip are both 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 StringSlice

func StringSlice(slice any) []string

StringSlice accepts a slice of arbitrary type (e.g. []int64 or []interface{}) and returns a slice of string.

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 Val

func Val(i any) any

Val returns the fully dereferenced value of i. If i is nil, nil is returned. If i has type *(*string), Val(i) returns string. Useful for testing.

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