testr

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: May 22, 2025 License: MIT Imports: 3 Imported by: 0

README

testr Go Reference main

Package testr provides a minimal extension to the standard library's testing.

Features

  • Minimal assertion API with Equal, ErrorIs, ErrorAs, and Panic.
  • Optional message using WithMessage.
  • Stop the execution using WithFailNow.
  • Ease of initialization with Must and MustV.

Documentation

Overview

Package testr provides a minimal extension to the standard library's testing.

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrIntentional = errors.New("testr: intentional error")

ErrIntentional is a sentinel error represents intentional error in the test.

Functions

func Must

func Must(err error)

Must is a helper that wraps a call to a function returning error and panics if the error is non-nil. It is intended for use in variable initializations such as

t := testr.MustV(json.Unmarshal(b, &v))
Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/smoothprogrammer/testr"
)

func main() {
	var v string
	testr.Must(json.Unmarshal([]byte(`"testr"`), &v))
	fmt.Println(v)

}
Output:

testr

func MustV

func MustV[V any](v V, err error) V

MustV is a helper that wraps a call to a function returning (V, error) and panics if the error is non-nil. It is intended for use in variable initializations such as

t := testr.MustV(json.Marshal(v))
Example
package main

import (
	"encoding/json"
	"fmt"

	"github.com/smoothprogrammer/testr"
)

func main() {
	b := testr.MustV(json.Marshal("testr"))
	fmt.Println(string(b))

}
Output:

"testr"

Types

type Assertion

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

Assertion provides assertion methods around T.

func New

func New(t T) *Assertion

New returns a new Assertion given by T.

func (*Assertion) Equal

func (assert *Assertion) Equal(actual any, expected any, options ...Option)

Equal asserts that the actual object are equal to the expected object. It can take options.

Example
assert := testr.New(t) // using *testing.T

assert.Equal(nil, nil)                    // PASS
assert.Equal(false, true)                 // FAIL
assert.Equal("nil", nil)                  // FAIL
assert.Equal([]string{"hello\nworld"}, 0) // FAIL
Output:

false != expected:true
"nil" != expected:<nil>
[]string{"hello\nworld"} != expected:0

func (*Assertion) ErrorAs

func (assert *Assertion) ErrorAs(actual error, expected any, options ...Option)

ErrorAs asserts that the actual error as the expected target. ErrorAs uses errors.As so it can use any perks that errors.As provides. It can take options.

Example
assert := testr.New(t) // using *testing.T

var e customError
assert.ErrorAs(customError("err"), &e) // PASS
assert.ErrorAs(errFoo, &e)             // FAIL
Output:

error(foo) != expected:as(*testr_test.customError)

func (*Assertion) ErrorIs

func (assert *Assertion) ErrorIs(actual error, expected error, options ...Option)

ErrorIs asserts that the actual error are equal to the expected error. ErrorIs uses errors.Is so it can use any perks that errors.Is provides. It can take options.

Example
assert := testr.New(t) // using *testing.T

assert.ErrorIs(nil, nil)           // PASS
assert.ErrorIs(errFoo, errFoo)     // PASS
assert.ErrorIs(errWrapFoo, errFoo) // PASS
assert.ErrorIs(errFoo, nil)        // FAIL
assert.ErrorIs(errFoo, errBar)     // FAIL
assert.ErrorIs(errWrapFoo, errBar) // FAIL
Output:

error(foo) != expected:<nil>
error(foo) != expected:error(bar)
error(wrap foo) != expected:error(bar)

func (*Assertion) Panic

func (assert *Assertion) Panic(f func(), options ...Option)

Panic assert that the function is panic. It can take options.

Example
assert := testr.New(t) // using *testing.T

assert.Panic(func() { panic("panic") }) // PASS
assert.Panic(func() {})                 // FAIL
Output:

func() != expected:panic()

type Option

type Option func(*option)

Option represents options for the Assertion's methods.

func WithFailNow

func WithFailNow() Option

WithFailNow marks the test state as fail and stop the execution if the assertion is fail.

Example
assert := testr.New(t) // using *testing.T

assert.ErrorIs(errFoo, nil,
	testr.WithFailNow(),
	testr.WithMessage("using t.FailNow"),
)
Output:

error(foo) != expected:<nil> // using t.FailNow

func WithMessage

func WithMessage(message string) Option

WithMessage appends the message to the end of the output if the assertion is fail.

Example
assert := testr.New(t) // using *testing.T

assert.Equal(false, true, testr.WithMessage("assert equality"))
assert.ErrorIs(errFoo, nil, testr.WithMessage("assert err is nil"))
var e customError
assert.ErrorAs(errFoo, &e, testr.WithMessage("assert err as customError"))
assert.Panic(func() {}, testr.WithMessage("assert function is panic"))
Output:

false != expected:true // assert equality
error(foo) != expected:<nil> // assert err is nil
error(foo) != expected:as(*testr_test.customError) // assert err as customError
func() != expected:panic() // assert function is panic

type T

type T interface {
	Helper()
	Logf(format string, args ...any)
	Fail()
	FailNow()
}

T represents testing.T.

Jump to

Keyboard shortcuts

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