catchtest

package
v2.10.0 Latest Latest
Warning

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

Go to latest
Published: Oct 19, 2026 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Package catchtest is a shared test suite for the error channel of the monads whose error type is error: the catch-by-type combinators (CatchAs, CatchIs, MapErrorAs, TapErrorAs) and the primitives they build on (ChainLeft, OrElse, MapLeft, ChainFirstLeft, TapLeft).

A package describes its monad with a Monad value and passes the functions under test, instantiated for the fixture types of this package, in a CatchOps or LeftOps value. Go cannot pass generic functions without instantiating them, so the suite fixes the success type to int and the side effect type to Void.

The suite only depends on either, so the tests of every package above it can use it.

Index

Constants

This section is empty.

Variables

View Source
var ErrSentinel = errors.New("sentinel")

ErrSentinel is a sentinel error for errors.Is.

Functions

func TestCatch

func TestCatch[HKTInt, HKTVoid, R any](t *testing.T, ops CatchOps[HKTInt, HKTVoid, R])

TestCatch runs the tests of CatchAs, CatchIs, MapErrorAs and TapErrorAs.

func TestLeft

func TestLeft[HKTInt, HKTVoid, R any](t *testing.T, ops LeftOps[HKTInt, HKTVoid, R])

TestLeft runs the tests of the error channel primitives that ops provides.

Types

type CatchOps

type CatchOps[HKTInt, HKTVoid, R any] struct {
	Monad[HKTInt, HKTVoid, R]

	CatchAsNotFound    func(func(*NotFoundError) HKTInt) func(HKTInt) HKTInt
	CatchAsCode        func(func(CodeError) HKTInt) func(HKTInt) HKTInt
	CatchAsCodePtr     func(func(*CodeError) HKTInt) func(HKTInt) HKTInt
	CatchAsTemporary   func(func(Temporary) HKTInt) func(HKTInt) HKTInt
	CatchIs            func(error, func(error) HKTInt) func(HKTInt) HKTInt
	MapErrorAsNotFound func(func(*NotFoundError) error) func(HKTInt) HKTInt
	TapErrorAsNotFound func(func(*NotFoundError) HKTVoid) func(HKTInt) HKTInt
}

CatchOps holds the catch-by-type combinators of a monad, instantiated for the fixtures.

type CodeError

type CodeError struct{ Code int }

CodeError implements error with a value receiver.

func (CodeError) Error

func (e CodeError) Error() string

type LeftOps

type LeftOps[HKTInt, HKTVoid, R any] struct {
	Monad[HKTInt, HKTVoid, R]

	ChainFirstLeft      func(func(error) HKTVoid) func(HKTInt) HKTInt
	TapLeft             func(func(error) HKTVoid) func(HKTInt) HKTInt
	MonadChainFirstLeft func(HKTInt, func(error) HKTVoid) HKTInt
	MonadTapLeft        func(HKTInt, func(error) HKTVoid) HKTInt

	ChainLeft      func(func(error) HKTInt) func(HKTInt) HKTInt
	OrElse         func(func(error) HKTInt) func(HKTInt) HKTInt
	MonadChainLeft func(HKTInt, func(error) HKTInt) HKTInt
	MapLeft        func(func(error) error) func(HKTInt) HKTInt
	MonadMapLeft   func(HKTInt, func(error) error) HKTInt
}

LeftOps holds the error channel primitives of a monad. Primitives the monad does not have are nil, and their tests are skipped.

type Monad

type Monad[HKTInt, HKTVoid, R any] struct {
	// Of creates a successful computation.
	Of func(int) HKTInt
	// Left creates a failed computation.
	Left func(error) HKTInt
	// LeftVoid creates a failed side effect.
	LeftVoid func(error) HKTVoid
	// Observe creates a successful side effect that calls record when it runs.
	Observe func(record func()) HKTVoid
	// Run runs a computation.
	Run func(HKTInt) R
	// Want is what Run returns for a computation with the given outcome.
	Want func(ET.Either[error, int]) R
}

Monad describes how to build and run the computations of a monad.

Type Parameters:

  • HKTInt: The computation that produces an int
  • HKTVoid: The computation that produces a side effect
  • R: The value a computation is run into, compared with assert.Equal

type NotFoundError

type NotFoundError struct{ ID int }

NotFoundError implements error with a pointer receiver.

func (*NotFoundError) Error

func (e *NotFoundError) Error() string

type TempError

type TempError struct{}

TempError implements Temporary.

func (TempError) Error

func (TempError) Error() string

func (TempError) Temporary

func (TempError) Temporary() bool

type Temporary

type Temporary interface {
	error
	Temporary() bool
}

Temporary is an interface error type.

Jump to

Keyboard shortcuts

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