pcmp

package
v0.0.0-...-034b59c Latest Latest
Warning

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

Go to latest
Published: Dec 11, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Package pcmp

pcmp and the related passert and prequire provide equality checking to replace reflect.DeepEqual, which no longer works for protocol buffer messages.

tl;dr: if you're using assert.Equal(t, expectedMessage, actualMessage) in your tests and it doesn't work, using passert.Equal(...) instead should fix it.

Longer Version

The newest generated code for protobufs a function pointer in the proto message structs in order to make all comparisons between the objects fail, and that breaks reflect.DeepEqual. Equal in the testify packages is implemented with DeepEqual, so it no longer works for proto messages. There is github.com/google/go-cmp/cmp as an alternative for DeepEqual use cases but it doesn't provide any useful functionality out of the box, you have to provide some magic incantations to make it fix the proto comparison breakage. And to use that in tests you need to reimplement some of testify's code. That's what this package does.

There is also "google.golang.org/protobuf/testing/protocmp" which is supposed to help with this but I didn't find it helpful. You could maybe reimplement the functionality here by using that package if needed.

Documentation

Index

Constants

This section is empty.

Variables

compareOptsForProtos is a slice of options for the https://github.com/google/go-cmp/cmp object comparing package. As of the latest implementation of the protocompiler for go, the messages compile to something like:

type YourMessage struct {
	state         protoimpl.MessageState
	sizeCache     protoimpl.SizeCache
	unknownFields protoimpl.UnknownFields

// Your fields here
}

Comparisons on the private fields often/always fail (this seems intentional on the part of the protobuf devs). Excluding these types from comparison allows us to do deep equality tests on just the public fields we defined and skip internal proto stuff.

We also use cmpopts.EquateEmpty, which makes a nil map and an empty one equal, because the other packages we have been using for deep equality comparisons behave this way.

Functions

func DeepEqual

func DeepEqual(x, y interface{}) bool

func FormatUnequalValues

func FormatUnequalValues(expected, actual interface{}) (e string, a string)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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