-
Notifications
You must be signed in to change notification settings - Fork 2
Comparers
Jeremiah edited this page Nov 16, 2023
·
3 revisions
Table of Contents
The default comparer used by trial, it is a wrapping for cmp.Equal with the AllowUnexported and EquateEmpty option set for all structs. This allows trial to check all fields (public and private) in a struct to be compared, and that an empty slice/map is the same as a nil slice/map. (see https://github.com/google/go-cmp)
Customize the use of cmp.Equal with options:
-
AllowAllUnexported
- [default: Equal] compare all unexported (private) variables within a struct. This is useful when testing a struct inside its own package. -
IgnoreAllUnexported
- ignore all unexported (private) variables within a struct. This is useful when dealing with a struct outside the project. -
IgnoreFields(fields ...string)
- define a list of variables to exclude for the comparer, the field name are case sensitize and can be dot-delimited ("Field", "Parent.child") -
EquateEmpty
- [default: Equal] a nil map or slice is equal to an empty one (len is zero) -
IgnoreTypes(values ...interface{})
- ignore all types of the values passed in. Ex: IgnoreTypes(int64(0), float32(0.0)) ignore int64 and float32 -
ApproxTime(d time.Duration)
- approximates time values to to the nearest duration.
// example struct that is compared to expected
type Example struct {
Field1 int
Field2 int
ignoreMe string // ignored unexported
Timestamp time.Time // ignored field
LastUpdate time.Time // ignored field
}
trial.New(fn, cases).Comparer(
trial.EqualOpt(
trial.IgnoreAllUnexported,
trial.IgnoreFields("Timestamp","LastUpdate")),
).SubTest(t)
Checks if the expected value is contained in the actual value. The symbol ⊇ is used to donate a subset. ∈ is used to show that a value exists in a slice. Contains checks the following relationships
-
string ⊇ string
- is the expected string contained in the actual string (strings.Contains)
-
string ⊇ []string
- are the expected substrings contained in the actual string
-
[]interface{} ⊇ interface{}
- is the expected value found in the slice or array
-
[]interface{} ⊇ []interface{}
- is the expected slice a subset of the actual slice. all values in expected exist and are contained in actual.
-
map[key]interface{} ⊇ map[key]interface{}
- is the expected map a subset of the actual map. all keys in expected are in actual and all values under that key are contained in actual