matchers

package module
v0.0.0-...-c279145 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2018 License: MIT Imports: 2 Imported by: 0

README

matchers

Have you ever been in this situation, scratching your head trying to compare some complex data structures?

Expected
    <main_test.User>: {
        Name: "david duchovny",
        Address: "santa monica",
        Age: 56,
        Badge: {ID: 666, Color: "shiny"},
    }
to equal
    <main_test.User>: {
        Name: "david duchovny",
        Address: "santa monica",
        Age: 56,
        Badge: {ID: 666, Color: "shiny"},
    }

Well, you've come to the right place. The place that can answer ALL your questions (so long as they have to do with figuring out why values aren't equal). Because now you can get actually helpful errors, like this one:

Expected
    <main_test.User>: {
        Name: "david duchovny",
        Address: "santa monica",
        Age: 56,
        Badge: {ID: 666, Color: "shiny"},
    }
to deep equal
    <main_test.User>: {
        Name: "david duchovny",
        Address: "santa monica",
        Age: 56,
        Badge: {ID: 666, Color: "shiny"},
    }
mismatch at .Badge: type mismatch main_test.FBIBadge vs *interface {}; obtained main_test.FBI{ID:666, Color:"shiny"}; expected (*interface {})(0xc420130728)

D'oh! I hate it when I use a FBIBadge when I mean to use an *interface{}! (This is actually one of the more cryptic explanations we can get, but basically one instance has a pointer reference for its Badge slot, and the other has a value reference. This happened to me recently, and it sucked.)

That's right, this library provides only a single Gomega matcher, but it's going to be the last matcher you ever need.

To get started:

go get -u github.com/benmoss/matchers

and just swap your Expect(...).To(Equal(...)) with a lil Expect(...).To(matchers.DeepEqual(...)), and you're off to the races!

It adapts the DeepEqual function from juju/testing so has all the same caveats as their implementation.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DeepEqual

func DeepEqual(expected interface{}) *deepMatcher

DeepEqual uses github.com/juju/testing's reimplementation of reflect.DeepEqual. Their docstring:

DeepEqual tests for deep equality. It uses normal == equality where possible but will scan elements of arrays, slices, maps, and fields of structs. In maps, keys are compared with == but elements use deep equality. DeepEqual correctly handles recursive types. Functions are equal only if they are both nil.

DeepEqual differs from reflect.DeepEqual in two ways: - an empty slice is considered equal to a nil slice. - two time.Time values that represent the same instant but with different time zones are considered equal.

If the two values compare unequal, the resulting error holds the first difference encountered.

Types

This section is empty.

Jump to

Keyboard shortcuts

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