test

package
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: May 17, 2022 License: MIT Imports: 8 Imported by: 0

README

Examples

Package allows to automatize testing of Marshal* and Unmarshal* methods.

Suppose we have the following type:

binary/byte.go

package binary

import (
	"fmt"
	"strconv"
)

type Byte byte

func (b Byte) MarshalText() ([]byte, error) {
	return []byte(fmt.Sprintf("%08b", b)), nil
}

func (b *Byte) UnmarshalText(data []byte) error {
	u, err := strconv.ParseUint(string(data), 2, 8)
	if err != nil {
		return err
	}
	*b = Byte(u)
	return nil
}

Package go.lstv.dev/util/test allowing to create tests by following way:

binary/byte_test.go

package binary

import (
	"testing"

	"go.lstv.dev/util/test"
)

var ByteTextCases = []test.CaseText[Byte]{
	{ // 0
		Data:  "00000000",
		Value: 0,
	},
	{ // 1
		Data:  "00000001",
		Value: 1,
	},
	{ // 2
		Data:  "11111111",
		Value: 255,
	},
	{ // 3
		Constraint: test.OnlyUnmarshal,
		Error:      test.Error("strconv.ParseUint: parsing \"2\": invalid syntax"),
		Data:       "2",
	},
}

func TestByteMarshalText(t *testing.T) {
	test.MarshalText(t, ByteTextCases)
}

func TestByteUnmarshalText(t *testing.T) {
	test.UnmarshalText(t, ByteTextCases, nil)
}

Documentation

Overview

Package test contains functions to support advanced testing.

Index

Constants

View Source
const (
	// OnlyMarshal limits test case only for marshal functions.
	OnlyMarshal = Constraint(iota + 1)

	// OnlyUnmarshal limits test case only for unmarshal functions.
	OnlyUnmarshal
)

Variables

This section is empty.

Functions

func MarshalBinary

func MarshalBinary[T any](t TestingT, cases []CaseBinary[T])

MarshalBinary runs all passed test cases of method with same name.

func MarshalJSON

func MarshalJSON[T any](t TestingT, cases []CaseJSON[T])

MarshalJSON runs all passed test cases of method with same name.

func MarshalText

func MarshalText[T any](t TestingT, cases []CaseText[T])

MarshalText runs all passed test cases of method with same name.

func UnmarshalBinary

func UnmarshalBinary[T any](t TestingT, cases []CaseBinary[T], helper TypeHelper[T])

UnmarshalBinary runs all passed test cases of method with same name.

func UnmarshalJSON

func UnmarshalJSON[T any](t TestingT, cases []CaseJSON[T], helper TypeHelper[T])

UnmarshalJSON runs all passed test cases of method with same name.

func UnmarshalText

func UnmarshalText[T any](t TestingT, cases []CaseText[T], helper TypeHelper[T])

UnmarshalText runs all passed test cases of method with same name.

Types

type AssertErrorFunc

type AssertErrorFunc func(t TestingT, err error, failInfo string) bool

AssertErrorFunc represents function to assert error.

var AnyError AssertErrorFunc = func(t TestingT, err error, failInfo string) bool {
	return assert.Error(t, err, failInfo)
}

AnyError is AssertErrorFunc to check if any error was passed.

func Error

func Error(text string) AssertErrorFunc

Error creates AssertErrorFunc to check if error has passed text.

func ErrorHasPrefix added in v0.8.0

func ErrorHasPrefix(prefix string) AssertErrorFunc

ErrorHasPrefix creates AssertErrorFunc to check if error has passed text prefix.

func ErrorHasSuffix added in v0.8.0

func ErrorHasSuffix(suffix string) AssertErrorFunc

ErrorHasSuffix creates AssertErrorFunc to check if error has passed text suffix.

func ErrorMatch added in v0.8.0

func ErrorMatch(regexpPattern string) AssertErrorFunc

ErrorMatch creates AssertErrorFunc to check if error text match passed regexp.

type CaseBinary

type CaseBinary[T any] struct {
	Constraint Constraint
	Before     func(index int, c *CaseBinary[T]) error
	After      func(index int, c *CaseBinary[T]) error
	Error      AssertErrorFunc
	Data       []byte
	Value      T
	Custom     any // user-defined custom value for Before and/or After function
}

CaseBinary represents one specific test case for MarshalBinary and/or UnmarshalBinary method.

type CaseJSON

type CaseJSON[T any] struct {
	Constraint Constraint
	Before     func(index int, c *CaseJSON[T]) error
	After      func(index int, c *CaseJSON[T]) error
	Error      AssertErrorFunc
	Data       string
	Value      T
	Custom     any // user-defined custom value for Before and/or After function
}

CaseJSON represents one specific test case for MarshalJSON and/or UnmarshalJSON method.

type CaseText

type CaseText[T any] struct {
	Constraint Constraint
	Before     func(index int, c *CaseText[T]) error
	After      func(index int, c *CaseText[T]) error
	Error      AssertErrorFunc
	Data       string
	Value      T
	Custom     any // user-defined custom value for Before and/or After function
}

CaseText represents one specific test case for MarshalText and/or UnmarshalText method.

type Constraint

type Constraint int

Constraint allowing limit test cases to specific use.

type TestingT

type TestingT interface {
	Errorf(format string, args ...any)
	FailNow()
	Helper()
}

TestingT is substitute for *testing.T.

type TypeHelper

type TypeHelper[T any] interface {
	New(value T) T
	AssertEmpty(t TestingT, value T, failInfo string)
	AssertEqual(t TestingT, expected, actual T, failInfo string)
}

TypeHelper represents object with type manipulation functions.

Jump to

Keyboard shortcuts

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