deep

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2018 License: MIT Imports: 5 Imported by: 311

README

Deep Variable Equality for Humans

Go Report Card Build Status Coverage Status GoDoc

This package provides a single function: deep.Equal. It's like reflect.DeepEqual but much friendlier to humans (or any sentient being) for two reason:

  • deep.Equal returns a list of differences
  • deep.Equal does not compare unexported fields (by default)

reflect.DeepEqual is good (like all things Golang!), but it's a game of Hunt the Wumpus. For large maps, slices, and structs, finding the difference is difficult.

deep.Equal doesn't play games with you, it lists the differences:

package main_test

import (
	"testing"
	"github.com/go-test/deep"
)

type T struct {
	Name    string
	Numbers []float64
}

func TestDeepEqual(t *testing.T) {
	// Can you spot the difference?
	t1 := T{
		Name:    "Isabella",
		Numbers: []float64{1.13459, 2.29343, 3.010100010},
	}
	t2 := T{
		Name:    "Isabella",
		Numbers: []float64{1.13459, 2.29843, 3.010100010},
	}

	if diff := deep.Equal(t1, t2); diff != nil {
		t.Error(diff)
	}
}
$ go test
--- FAIL: TestDeepEqual (0.00s)
        main_test.go:25: [Numbers.slice[1]: 2.29343 != 2.29843]

The difference is in Numbers.slice[1]: the two values aren't equal using Go ==.

Documentation

Overview

Package deep provides function deep.Equal which is like reflect.DeepEqual but returns a list of differences. This is helpful when comparing complex types like structures and maps.

Index

Constants

This section is empty.

Variables

View Source
var (
	// FloatPrecision is the number of decimal places to round float values
	// to when comparing.
	FloatPrecision = 10

	// MaxDiff specifies the maximum number of differences to return.
	MaxDiff = 10

	// MaxDepth specifies the maximum levels of a struct to recurse into.
	MaxDepth = 10

	// LogErrors causes errors to be logged to STDERR when true.
	LogErrors = false

	// CompareUnexportedFields causes unexported struct fields, like s in
	// T{s int}, to be comparsed when true.
	CompareUnexportedFields = false
)
View Source
var (
	// ErrMaxRecursion is logged when MaxDepth is reached.
	ErrMaxRecursion = errors.New("recursed to MaxDepth")

	// ErrTypeMismatch is logged when Equal passed two different types of values.
	ErrTypeMismatch = errors.New("variables are different reflect.Type")

	// ErrNotHandled is logged when a primitive Go kind is not handled.
	ErrNotHandled = errors.New("cannot compare the reflect.Kind")
)

Functions

func Equal

func Equal(a, b interface{}) []string

Equal compares variables a and b, recursing into their structure up to MaxDepth levels deep, and returns a list of differences, or nil if there are none. Some differences may not be found if an error is also returned.

If a type has an Equal method, like time.Equal, it is called to check for equality.

Types

This section is empty.

Jump to

Keyboard shortcuts

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