testmarshal

package
v0.0.0-...-9649366 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2019 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package testmarshal provides some assertions around marshalling/unmarshalling (serialization/deserialization) behavior for types. It is intended to reduce boilerplate in tests of the form:

func TestMyTypeUnmarshals(t *testing.T) {
		type MyType struct{}
		var mt MyType
		require.NoError(t, json.Unmarshal([]byte("{}"), &mt))
		assert.Equal(t, MyType{}, mt)
}

with assertion calls:

func TestMyTypeUnmarshals(t *testing.T) {
		type MyType struct{}
     testmarshal.AssertUnmarshals(t, testmarshal.JSONMarshaler, MyType{}, []byte("{}"))
}

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AssertMarshalingRoundtrips

func AssertMarshalingRoundtrips(t *testing.T, marshaller Marshaler, example interface{}) bool
func TestMyTypeRoundtrips(t *testing.T) {
	type MyType struct{}
	testmarshal.AssertMarshalingRoundtrips(t, testmarshal.JSONMarshaler, MyType{})
}

func AssertMarshals

func AssertMarshals(t *testing.T, marshaller Marshaler, toMarshal interface{}, expectedData []byte) bool

AssertMarshals checks that the given value marshals into data equal to expectedData.

It is intended to replace tests of the form:
func TestMyTypeMarshals(t *testing.T) {
	type MyType struct{}
   mt := MyType{}
   d, err := json.Marshal(mt)
   require.NoError(t, err)
   assert.Equal(t, d, []byte("{}"))
}

with:

func TestMyTypeUnmarshals(t *testing.T) {
	 type MyType struct{}
	 testmarshal.AssertMarshals(t, testmarshal.JSONMarshaler, MyType{}, []byte("{}"))
}

func AssertUnmarshals

func AssertUnmarshals(t *testing.T, marshaller Marshaler, expected interface{}, data []byte) bool

func Require

func Require(t *testing.T, b bool)

Require wraps an Assert call and turns it into a require.* call (fails if the assert fails).

func TestMarshalersRoundtrip

func TestMarshalersRoundtrip(t *testing.T, examples interface{}, marshallers []Marshaler)

TestMarshalersRoundtrip is a helper which runs a test for each provided marshaller on each example in examples (a slice of any type). The test checks that the marshaler "roundtrips" for each example, i.e.: marshaler.Unmarshal(marshaler.Marshal(example)) == example.

Types

type Marshaler

type Marshaler interface {
	// Marshal converts a Go type into bytes
	Marshal(interface{}) ([]byte, error)

	// Unmarshal converts bytes into a Go type.
	Unmarshal([]byte, interface{}) error

	// ID identifies the protocol, mostly for use in test naming.
	ID() string
}

Marshaler represents a serialization protocol, e.g. JSON or YAML

var (
	// JSONMarshaler uses the encoding/json package to marshal types
	JSONMarshaler Marshaler = simpleMarshaler{
					// contains filtered or unexported fields
	}

	// YAMLMarshaler uses the gopkg.in/yaml.v2 package to marshal types
	YAMLMarshaler Marshaler = simpleMarshaler{
					// contains filtered or unexported fields
	}

	// TextMarshaler marshals types which implement both encoding.TextMarshaler
	// and encoding.TextUnmarshaler
	TextMarshaler Marshaler = simpleMarshaler{
					// contains filtered or unexported fields
	}
)

Jump to

Keyboard shortcuts

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