test

package
v0.0.0-...-0dabb98 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2019 License: Apache-2.0 Imports: 10 Imported by: 0

README

Test

The core/test package contains helpers for aiding testing.

Examples

Equals
t.Run("foo equals foo", func(t *testing.T) {
    test.Equals(t, "foo", "foo")
})
Not equals
t.Run("foo does not equal foo", func(t *testing.T) {
    test.NotEquals(t, "foo", "bar")
})
Using the ErrorTypeComparer with cmp

This comparer relies on the use of the excellent go-cmp library.

As it's documentation states, it is intended to be a more powerful and safer alternative to reflect.DeepEqual for comparing whether two values are semantically equal.

See GoDoc documentation for more information.

t.Run("some error is not a test error", func(t *testing.T) {
	// some fake errors, for the purpose of this example.
	testErr := TestError{errors.New("oops")}
	someErr := errors.New("ouch")

	var e test.ErrorReporter
	opts := cmp.Options{
		test.ErrorTypeComparer,
		cmp.Reporter(&e),
	}
	if !cmp.Equal(testErr, someErr, opts) {
		fmt.Println(e.String())
	}
	// Output:
	// error type mismatch:
	// 	expected: test_test.TestError
	// 	got: *errors.errorString
})

Documentation

Overview

Package test contains helpers for aiding testing.

Index

Constants

This section is empty.

Variables

View Source
var ErrorTypeComparer = cmp.Comparer(func(x error, y error) bool {
	return reflect.TypeOf(x) == reflect.TypeOf(y)
})

ErrorTypeComparer defines an error type comparator for use with go-cmp Example:

opts := cmp.Options{
	 test.ErrorTypeComparer,
}

if !cmp.Equal(err, MyCustomError{}, opts) {
	t.Fatal(e.String())
}

Functions

func BindJSON

func BindJSON(r io.Reader, target interface{}) error

BindJSON takes a json stream and binds it to a struct.

func DialGRPC

func DialGRPC(addr string, opts ...grpc.DialOption) *grpc.ClientConn

DialGRPC will connect to a grpc server on a specific port.

func Equals

func Equals(tb testing.TB, expected, actual interface{})

Equals performs a deep equal comparison against two values and fails if they are not the same.

func NotEquals

func NotEquals(tb testing.TB, expected, actual interface{})

NotEquals performs a deep equal comparison against two values and fails if they are the same.

func ToJSONBody

func ToJSONBody(tb testing.TB, i interface{}) io.Reader

ToJSONBody turns a struct into a json body.

Types

type ErrorReporter

type ErrorReporter struct {
	// contains filtered or unexported fields
}

ErrorReporter defines a reporter for use with go-cmp. This enables producing elegant diff output when the ErrorTypeComparator reports unequal.

func (*ErrorReporter) PopStep

func (e *ErrorReporter) PopStep()

PopStep is always called after Report. This method is defined in the reporterIface interface for go-cmp.

func (*ErrorReporter) PushStep

func (e *ErrorReporter) PushStep(ps cmp.PathStep)

PushStep is always called before report. This method is defined in the reporterIface interface for go-cmp.

func (*ErrorReporter) Report

func (e *ErrorReporter) Report(rs cmp.Result)

Report is always called after PushStep and before PopStep. It is called wether the comparison reports equal, unequal or is ignored. This method is defined in the reporterIface interface for go-cmp.

func (ErrorReporter) String

func (e ErrorReporter) String() string

String implements the Stringer interface.

Jump to

Keyboard shortcuts

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