testutils

package module
v0.1.42 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2021 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package testutils provides a framework and helper functions for use during unit testing.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BoolPtr

func BoolPtr(b bool) *bool

BoolPtr returns a pointer to a bool.

func CallMethod

func CallMethod(t *testing.T, obj interface{}, methodName string, params []interface{}) []interface{}

CallMethod calls a method on an object and returns the results.

func CastToMapStringString

func CastToMapStringString(t *testing.T, data interface{}) map[string]string

CastToMapStringString casts an interface to a map[string]string.

func CastToStringList

func CastToStringList(t *testing.T, data interface{}) []string

CastToStringList casts an interface to a []string.

func CheckError

func CheckError(u TestUtil) bool

CheckError implements the CheckTestI interface, verifying that a error contains expected text strings. It then calls the default check function to verify other results, if any.

func CheckFieldGetter

func CheckFieldGetter(u TestUtil, fieldName string, fieldInfo FieldInfo) bool

CheckFieldGetter will the getter function of a field and check the value is equal to the expected value and set the test to failed if not.

func CheckFieldValue

func CheckFieldValue(u TestUtil, fieldName string, fieldInfo FieldInfo) bool

CheckFieldValue will check if a field value is equal to the expected value and set the test to failed if not.

func CheckFieldsGetter

func CheckFieldsGetter(u TestUtil) bool

CheckFieldsGetter will call the getter functions on an object to check the expected values are equal to the actual value and set the test to failed if not.

func CheckFieldsValue

func CheckFieldsValue(u TestUtil) bool

CheckFieldsValue will get the value of object fields and check the expected values are equal to the actual value and set the test to failed if not.

func CheckNotNil added in v0.0.39

func CheckNotNil(u TestUtil, name string, a, e interface{}) bool

CheckNotNil is passed two interfaces and checks if both or neither are nil. If both are nil it returns true, if neither are nil it returns false. If one is nil and the other is not it reports the difference and returns nil.

func CheckObjStatusFunc

func CheckObjStatusFunc(u TestUtil) bool

CheckObjStatusFunc checks object fields values against expected and report if different.

func CompareAsJSON

func CompareAsJSON(t *testing.T, one, two interface{}) bool

CompareAsJSON compares two interfaces as JSON.

func CompareJSON added in v0.0.42

func CompareJSON(u TestUtil, name string, actual, expected interface{}) bool

CompareJSON compares values by converting to json.

func CompareReflectDeepEqual added in v0.0.39

func CompareReflectDeepEqual(u TestUtil, name string, actual, expected interface{}) bool

CompareReflectDeepEqual compares values using reflect.DeepEqual().

func ContainsStringArray

func ContainsStringArray(one, two []string, first bool) bool

ContainsStringArray checks items in two string arrays verifying that the second string array is contained in the first.

func ContainsStrings

func ContainsStrings(expected []string, result string) bool

ContainsStrings checks that all elements in a list of strings are contained in another string.

func DefaultCheckFunc

func DefaultCheckFunc(u TestUtil) bool

DefaultCheckFunc is the default check test function that compares actual and expected.

func DefaultPrepFunc

func DefaultPrepFunc(u TestUtil)

DefaultPrepFunc is the default pre test function that prints the test number and name.

func DisplayStrings

func DisplayStrings(strs []string) string

DisplayStrings returns a []string as a single string, one element per line.

func GetPointerValue

func GetPointerValue(i interface{}) interface{}

GetPointerValue returns what a pointer points to.

func HandlePanic

func HandlePanic(t *testing.T)

HandlePanic handles a panic in test code calling testing.T.Fatal() with interface returned by recover().

func IsPointer

func IsPointer(i interface{}) bool

IsPointer returns true if a variable is a pointer.

func MapKeysToList

func MapKeysToList(envsMap map[string]string) []string

MapKeysToList returns the keys of a map[string]string as a list.

func ReadBuf

func ReadBuf(out io.Reader) *[]string

ReadBuf reads lines from a buffer into a string array.

func ReportCallJSON added in v0.0.39

func ReportCallJSON(u TestUtil)

ReportCallJSON uses json to report on result of a function or method call. It displays the input, result and expected result as well as the differences between the actual and expected results.

func ReportCallSpew added in v0.0.39

func ReportCallSpew(u TestUtil)

ReportCallSpew uses spew to report on result of a function or method call. It displays the input, result and expected result as well as the differences between the actual and expected results.

func ReportJSON

func ReportJSON(u TestUtil, name string, actual, expected interface{})

ReportJSON uses json to report the differences between two values. It displays the actual and expected values as well as the differences between them.

func ReportSpew

func ReportSpew(u TestUtil, name string, actual, expected interface{})

ReportSpew uses spew to report the differences between two values. It displays the actual and expected values as well as the differences between them.

func SetEnv

func SetEnv(t *testing.T, envName, envValue string)

SetEnv sets an environmental variable.

func SetEnvs

func SetEnvs(t *testing.T, envSettings map[string]string)

SetEnvs sets a number of environmental variables.

func ToJSON

func ToJSON(t *testing.T, data interface{}) string

ToJSON is used get an interface in JSON format.

func UnsetEnv

func UnsetEnv(t *testing.T, envName string)

UnsetEnv unsets an environmental variable.

func UnsetEnvs

func UnsetEnvs(t *testing.T, names []string)

UnsetEnvs unsets a list of environmental variables.

Types

type CallMethodFunc

type CallMethodFunc func(t *testing.T, obj interface{}, methodName string, values []interface{}) []interface{}

CallMethodFunc is the function to call a method on an object.

type CheckTestI

type CheckTestI func(u TestUtil) bool

CheckTestI defines function to be called after test to check result.

type ComparerI added in v0.0.39

type ComparerI func(u TestUtil, name string, actual, expected interface{}) bool

ComparerI defines the comparer function interface.

type DefTest

type DefTest struct {
	Number      int           // Test number.
	Description string        // Test description.
	EnvVars     []string      // List of environmental variable to be reset at the start of each test
	Config      interface{}   // Test configuration information to be used by test function or custom pre/post test functions.
	Inputs      []interface{} // Test inputs.
	Expected    []interface{} // Test expected results.
	Results     []interface{} // Test results.
	ObjStatus   *ObjectStatus // Details of object under test including field names and expected values, used by CheckFunc to verify values.
	// PrepFunc is function to be called before a test, leave unset to call default - which prints the test number and name.
	PrepFunc PrepTestI
	// CheckFunc is function to be called to check a test results, leave unset to call default.
	// Default compares actual results with expected results and verifies object status.
	CheckFunc CheckTestI
	// ResultsCompareFunc is function to be called to compare a test results, leave unset to call default.
	// Default compares actual results with expected results using reflect.DeepEqual().
	ResultsCompareFunc ComparerI
	// ResultsReportFunc is function to be called to report difference in test results, leave unset to call default - which uses spew.Sdump().
	ResultsReportFunc ReportDiffI
	// FieldCompareFunc is function to be called to compare a field values, leave unset to call default.
	// Default compares actual results with expected results using reflect.DeepEqual().
	FieldCompareFunc ComparerI
	// FieldCompareFunc is function to be called to report difference in field values, leave unset to call default - which uses spew.Sdump().
	FieldReportFunc ReportDiffI
}

DefTest generic test data structure.

type FieldInfo

type FieldInfo struct {
	Reporter     ReportDiffI `json:"reporter,omitempty"` // Function to do field specific reporting of differences, nil if not set.
	Comparer     ComparerI   `json:"comparer,omitempty"` // Function to do field specific compare, nil if not set.
	GetterMethod string      `json:"getter,omitempty"`   // The method to get the value, nil if no getter method.
	SetterMethod string      `json:"setter,omitempty"`   // The method to get the value, nil if no setter method.
	FieldValue   interface{} `json:"value"`              // The value to set or expected value to verify.
}

FieldInfo holds information about a field of a struct.

type Fields

type Fields map[string]FieldInfo

Fields is a map of field names to information about the field.

type GetFieldFunc

type GetFieldFunc func(t *testing.T, obj interface{}, fieldName string) interface{}

GetFieldFunc is the function to call to get the value of a field of an object.

type ObjectStatus

type ObjectStatus struct {
	Object     interface{}    // The object or interface under test, this needs to be set during test before calling post test actions.
	GetField   GetFieldFunc   // The function to call to get a field value.
	SetField   SetFieldFunc   // The function to call to set a field value.
	CallMethod CallMethodFunc // The function to call a method on an object.
	Fields     Fields         // The fields of an object.
}

ObjectStatus hold details of the object under test. This can be used to verify the internal state of an object after a test.

func CopyObjectStatus

func CopyObjectStatus(objStatus *ObjectStatus) *ObjectStatus

CopyObjectStatus copys an ObjectStatus object leaving Fields element empty.

func SetObjStatusFields

func SetObjStatusFields(t *testing.T, objStatus *ObjectStatus, fieldValues map[string]interface{}, retain bool) *ObjectStatus

SetObjStatusFields sets the values in the FieldInfo struct, used to update a template ObjectStatus with values for a given test.

type PrepTestI

type PrepTestI func(u TestUtil)

PrepTestI defines function to be called before running a test.

type ReportDiffI added in v0.0.39

type ReportDiffI func(u TestUtil, name string, actual, expected interface{})

ReportDiffI defines the report difference function interface.

type SetFieldFunc

type SetFieldFunc func(t *testing.T, obj interface{}, fieldName string, value interface{})

SetFieldFunc is the function to call to set the value of a field of an object.

type TestUtil

type TestUtil interface {
	CallPrepFunc()                 // Call the custom or default test preparation function.
	CallCheckFunc() bool           // Call the custom or default test checking function.
	Testing() *testing.T           // testing object.
	SetFailTests(value bool)       // Set the fail test setting to verify test check reporting.
	FailTests() bool               // Get the fail test setting.
	SetVerbose(value bool)         // Set the verbose setting.
	Verbose() bool                 // Get the verbose setting.
	SetTestData(testData *DefTest) // Set the test data.
	TestData() *DefTest            // Get the test data.
	// ResultsComparer calls the specified comparer, default checking function calls this to call test data's CompareFunc or CompareReflectDeepEqual if not set.
	ResultsComparer() bool
	// FieldComparer calls the field comparer, default checking function calls this to call test data's CompareFunc or CompareReflectDeepEqual if not set.
	FieldComparer(name string, actual, expected interface{}) bool
	// ResultReporter calls the specified reporter, default checking function calls this to call test data's ResultsReportFunc or ReportSpew if not set.
	ResultsReporter()
	// FieldReporter calls the specified reporter, default checking function calls this to call test data's ReportFieldsFunc or ReportSpew if not set.
	FieldReporter(name string, actual, expected interface{})
}

TestUtil the interface used to provide testing utilities.

func NewTestUtil

func NewTestUtil(t *testing.T, testData *DefTest) TestUtil

NewTestUtil retruns a new TestUtil interface.

Jump to

Keyboard shortcuts

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