go-cmp: github.com/google/go-cmp/cmp/cmpopts Index | Files

package cmpopts

import "github.com/google/go-cmp/cmp/cmpopts"

Package cmpopts provides common options for the cmp package.

Index

Package Files

equate.go ignore.go sort.go struct_filter.go xform.go

func AcyclicTransformer Uses

func AcyclicTransformer(name string, f interface{}) cmp.Option

AcyclicTransformer returns a Transformer with a filter applied that ensures that the transformer cannot be recursively applied upon its own output.

An example use case is a transformer that splits a string by lines:

AcyclicTransformer("SplitLines", func(s string) []string{
	return strings.Split(s, "\n")
})

Had this been an unfiltered Transformer instead, this would result in an infinite cycle converting a string to []string to [][]string and so on.

func EquateApprox Uses

func EquateApprox(fraction, margin float64) cmp.Option

EquateApprox returns a Comparer option that determines float32 or float64 values to be equal if they are within a relative fraction or absolute margin. This option is not used when either x or y is NaN or infinite.

The fraction determines that the difference of two values must be within the smaller fraction of the two values, while the margin determines that the two values must be within some absolute margin. To express only a fraction or only a margin, use 0 for the other parameter. The fraction and margin must be non-negative.

The mathematical expression used is equivalent to:

|x-y| ≤ max(fraction*min(|x|, |y|), margin)

EquateApprox can be used in conjunction with EquateNaNs.

func EquateEmpty Uses

func EquateEmpty() cmp.Option

EquateEmpty returns a Comparer option that determines all maps and slices with a length of zero to be equal, regardless of whether they are nil.

EquateEmpty can be used in conjunction with SortSlices and SortMaps.

func EquateNaNs Uses

func EquateNaNs() cmp.Option

EquateNaNs returns a Comparer option that determines float32 and float64 NaN values to be equal.

EquateNaNs can be used in conjunction with EquateApprox.

func IgnoreFields Uses

func IgnoreFields(typ interface{}, names ...string) cmp.Option

IgnoreFields returns an Option that ignores exported fields of the given names on a single struct type. The struct type is specified by passing in a value of that type.

The name may be a dot-delimited string (e.g., "Foo.Bar") to ignore a specific sub-field that is embedded or nested within the parent struct.

This does not handle unexported fields; use IgnoreUnexported instead.

func IgnoreInterfaces Uses

func IgnoreInterfaces(ifaces interface{}) cmp.Option

IgnoreInterfaces returns an Option that ignores all values or references of values assignable to certain interface types. These interfaces are specified by passing in an anonymous struct with the interface types embedded in it. For example, to ignore sync.Locker, pass in struct{sync.Locker}{}.

func IgnoreTypes Uses

func IgnoreTypes(typs ...interface{}) cmp.Option

IgnoreTypes returns an Option that ignores all values assignable to certain types, which are specified by passing in a value of each type.

func IgnoreUnexported Uses

func IgnoreUnexported(typs ...interface{}) cmp.Option

IgnoreUnexported returns an Option that only ignores the immediate unexported fields of a struct, including anonymous fields of unexported types. In particular, unexported fields within the struct's exported fields of struct types, including anonymous fields, will not be ignored unless the type of the field itself is also passed to IgnoreUnexported.

func SortMaps Uses

func SortMaps(less interface{}) cmp.Option

SortMaps returns a Transformer option that flattens map[K]V types to be a sorted []struct{K, V}. The less function must be of the form "func(T, T) bool" which is used to sort any map with key K that is assignable to T.

Flattening the map into a slice has the property that cmp.Equal is able to use Comparers on K or the K.Equal method if it exists.

The less function must be:

• Deterministic: less(x, y) == less(x, y)
• Irreflexive: !less(x, x)
• Transitive: if !less(x, y) and !less(y, z), then !less(x, z)
• Total: if x != y, then either less(x, y) or less(y, x)

SortMaps can be used in conjunction with EquateEmpty.

func SortSlices Uses

func SortSlices(less interface{}) cmp.Option

SortSlices returns a Transformer option that sorts all []V. The less function must be of the form "func(T, T) bool" which is used to sort any slice with element type V that is assignable to T.

The less function must be:

• Deterministic: less(x, y) == less(x, y)
• Irreflexive: !less(x, x)
• Transitive: if !less(x, y) and !less(y, z), then !less(x, z)

The less function does not have to be "total". That is, if !less(x, y) and !less(y, x) for two elements x and y, their relative order is maintained.

SortSlices can be used in conjunction with EquateEmpty.

Package cmpopts imports 9 packages (graph) and is imported by 29 packages. Updated 2018-11-01. Refresh now. Tools for package owners.