testmarshal

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2026 License: Apache-2.0 Imports: 7 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

AssertMarshalingRoundtrips checks that the marshaller "roundtrips" example i.e.: marshaler.Unmarshal(marshaler.Marshal(example)) == example.

It is intended to replace tests of the form:

func TestMyTypeRoundtrips(t *testing.T) {
	type MyType struct{}
	mt := MyType{}
	d, err := json.Marshal(mt)
	require.NoError(t, err)

	var revived MyType
	require.NoError(t, json.Unmarshal(d, &revived))
	assert.Equal(t, mt, revived)
}

with:

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