Documentation ¶
Overview ¶
Package tutil contains basic generic test utilities.
Index ¶
- func InterfaceSlice(slice any) []any
- func Name(args ...any) string
- func SkipIff(t testing.TB, b bool, format string, args ...any)
- func SkipShort(t *testing.T, skip bool)
- func SliceFieldKeyValues(keyFieldName, valFieldName string, slice any) map[any]any
- func SliceFieldValues(fieldName string, slice any) []any
- func StringSlice(slice any) []string
- func StructFieldValue(fieldName string, strct any) any
- func Val(i any) any
- type AssertCompareFunc
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func InterfaceSlice ¶
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 ¶
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 ¶
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 SliceFieldKeyValues ¶
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 ¶
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 ¶
StringSlice accepts a slice of arbitrary type (e.g. []int64 or []interface{}) and returns a slice of string.
func StructFieldValue ¶
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.