test

package
v0.22.0 Latest Latest
Warning

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

Go to latest
Published: Jan 29, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Some error handling functions and types to more ergonomically assist with writing tests against functions that may return errors

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Check

func Check(t TestReporting, err error)

Check checks if a given error value is nil. If not, report the error and fail the test.

func ReportErr

func ReportErr(t TestReporting)

Reports any error encountered in a call to Try(). Should be used as part of a defer call. e.g.

defer ReportErr(t)
f := Try(os.Open(myfile))

Types

type TestReporting

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

An interface implemented by multiple types in the "testing" package

type TestableResult

type TestableResult[T any] struct {
	result.Result[T]
}

A wrapper around result.Result that supports test assertions.

func Result

func Result[T any](value T, err error) *TestableResult[T]

Creates a TestableResult from a return value and an error e.g.

r := test.Result(os.Open(myfile))

func Result0

func Result0(err error) *TestableResult[tuple.Tuple0]

Creates a TestableResult from a an error only, e.g.

r := test.Result0(os.Rename(oldfile,newfile))

func Status

func Status(err error) *TestableResult[tuple.Tuple0]

Creates a TestableResult from a an error only. Alias for Result0. e.g.

r := test.Status(os.Rename(oldfile,newfile))

func (*TestableResult[T]) Fails

func (r *TestableResult[T]) Fails(t TestReporting) *TestableResult[T]

Fails expects an error; it marks the test as failed if the result is not an error

func (*TestableResult[T]) FailsContaining

func (r *TestableResult[T]) FailsContaining(t TestReporting, expected string) *TestableResult[T]

Expects a specific error; marks the test as failed if the result is not an error whose Error() return contains the string provided in expected.

func (*TestableResult[T]) FailsWith

func (r *TestableResult[T]) FailsWith(t TestReporting, expected error) *TestableResult[T]

FailsWith expects a specific error; it marks the test as failed if the result is not, or does not wrap, the expected error.

func (*TestableResult[T]) Must

func (r *TestableResult[T]) Must(t TestReporting) T

Must either returns the value of a TestableResult, or if an error has occurred, report it and fail the test.

type TestableResult2

type TestableResult2[T1 any, T2 any] struct {
	TestableResult[tuple.Tuple2[T1, T2]]
}

A wrapper type for a TestableResult that contains a value of type type.Tuple2

func Result2

func Result2[T1 any, T2 any](v1 T1, v2 T2, err error) *TestableResult2[T1, T2]

A constructor for TestableResult2 from the values returned by a function that returns 2 values plus an error.

func (*TestableResult2[T1, T2]) Must2

func (r *TestableResult2[T1, T2]) Must2(t TestReporting) (T1, T2)

A variation of Must that returns 2 non-error values

func (*TestableResult2[T1, T2]) Try2

func (r *TestableResult2[T1, T2]) Try2() (T1, T2)

A variation of Try that returns 2 non-error values

type TestableResult3

type TestableResult3[T1 any, T2 any, T3 any] struct {
	TestableResult[tuple.Tuple3[T1, T2, T3]]
}

A wrapper type for a TestableResult that contains a value of type type.Tuple3

func Result3

func Result3[T1 any, T2 any, T3 any](v1 T1, v2 T2, v3 T3, err error) *TestableResult3[T1, T2, T3]

A constructor for TestableResult3 from the values returned by a function that returns 3 values plus an error.

func (*TestableResult3[T1, T2, T3]) Must3

func (r *TestableResult3[T1, T2, T3]) Must3(t TestReporting) (T1, T2, T3)

A variation of Must that returns 3 non-error values

func (*TestableResult3[T1, T2, T3]) Try3

func (r *TestableResult3[T1, T2, T3]) Try3() (T1, T2, T3)

A variation of Try that returns 3 non-error values

type TestableResult4

type TestableResult4[T1 any, T2 any, T3 any, T4 any] struct {
	TestableResult[tuple.Tuple4[T1, T2, T3, T4]]
}

A wrapper type for a TestableResult that contains a value of type type.Tuple4

func Result4

func Result4[T1 any, T2 any, T3 any, T4 any](v1 T1, v2 T2, v3 T3, v4 T4, err error) *TestableResult4[T1, T2, T3, T4]

A constructor for TestableResult4 from the values returned by a function that returns 4 values plus an error.

func (*TestableResult4[T1, T2, T3, T4]) Must4

func (r *TestableResult4[T1, T2, T3, T4]) Must4(t TestReporting) (T1, T2, T3, T4)

A variation of Must that returns 4 non-error values

func (*TestableResult4[T1, T2, T3, T4]) Try4

func (r *TestableResult4[T1, T2, T3, T4]) Try4() (T1, T2, T3, T4)

A variation of Try that returns 4 non-error values

type TestableResult5

type TestableResult5[T1 any, T2 any, T3 any, T4 any, T5 any] struct {
	TestableResult[tuple.Tuple5[T1, T2, T3, T4, T5]]
}

A wrapper type for a TestableResult that contains a value of type type.Tuple5

func Result5

func Result5[T1 any, T2 any, T3 any, T4 any, T5 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, err error) *TestableResult5[T1, T2, T3, T4, T5]

A constructor for TestableResult5 from the values returned by a function that returns 5 values plus an error.

func (*TestableResult5[T1, T2, T3, T4, T5]) Must5

func (r *TestableResult5[T1, T2, T3, T4, T5]) Must5(t TestReporting) (T1, T2, T3, T4, T5)

A variation of Must that returns 5 non-error values

func (*TestableResult5[T1, T2, T3, T4, T5]) Try5

func (r *TestableResult5[T1, T2, T3, T4, T5]) Try5() (T1, T2, T3, T4, T5)

A variation of Try that returns 5 non-error values

type TestableResult6

type TestableResult6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any] struct {
	TestableResult[tuple.Tuple6[T1, T2, T3, T4, T5, T6]]
}

A wrapper type for a TestableResult that contains a value of type type.Tuple6

func Result6

func Result6[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, err error) *TestableResult6[T1, T2, T3, T4, T5, T6]

A constructor for TestableResult6 from the values returned by a function that returns 6 values plus an error.

func (*TestableResult6[T1, T2, T3, T4, T5, T6]) Must6

func (r *TestableResult6[T1, T2, T3, T4, T5, T6]) Must6(t TestReporting) (T1, T2, T3, T4, T5, T6)

A variation of Must that returns 6 non-error values

func (*TestableResult6[T1, T2, T3, T4, T5, T6]) Try6

func (r *TestableResult6[T1, T2, T3, T4, T5, T6]) Try6() (T1, T2, T3, T4, T5, T6)

A variation of Try that returns 6 non-error values

type TestableResult7

type TestableResult7[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any] struct {
	TestableResult[tuple.Tuple7[T1, T2, T3, T4, T5, T6, T7]]
}

A wrapper type for a TestableResult that contains a value of type type.Tuple7

func Result7

func Result7[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, err error) *TestableResult7[T1, T2, T3, T4, T5, T6, T7]

A constructor for TestableResult7 from the values returned by a function that returns 7 values plus an error.

func (*TestableResult7[T1, T2, T3, T4, T5, T6, T7]) Must7

func (r *TestableResult7[T1, T2, T3, T4, T5, T6, T7]) Must7(t TestReporting) (T1, T2, T3, T4, T5, T6, T7)

A variation of Must that returns 7 non-error values

func (*TestableResult7[T1, T2, T3, T4, T5, T6, T7]) Try7

func (r *TestableResult7[T1, T2, T3, T4, T5, T6, T7]) Try7() (T1, T2, T3, T4, T5, T6, T7)

A variation of Try that returns 7 non-error values

type TestableResult8

type TestableResult8[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any, T8 any] struct {
	TestableResult[tuple.Tuple8[T1, T2, T3, T4, T5, T6, T7, T8]]
}

A wrapper type for a TestableResult that contains a value of type type.Tuple8

func Result8

func Result8[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any, T8 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, err error) *TestableResult8[T1, T2, T3, T4, T5, T6, T7, T8]

A constructor for TestableResult8 from the values returned by a function that returns 8 values plus an error.

func (*TestableResult8[T1, T2, T3, T4, T5, T6, T7, T8]) Must8

func (r *TestableResult8[T1, T2, T3, T4, T5, T6, T7, T8]) Must8(t TestReporting) (T1, T2, T3, T4, T5, T6, T7, T8)

A variation of Must that returns 8 non-error values

func (*TestableResult8[T1, T2, T3, T4, T5, T6, T7, T8]) Try8

func (r *TestableResult8[T1, T2, T3, T4, T5, T6, T7, T8]) Try8() (T1, T2, T3, T4, T5, T6, T7, T8)

A variation of Try that returns 8 non-error values

type TestableResult9

type TestableResult9[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any, T8 any, T9 any] struct {
	TestableResult[tuple.Tuple9[T1, T2, T3, T4, T5, T6, T7, T8, T9]]
}

A wrapper type for a TestableResult that contains a value of type type.Tuple9

func Result9

func Result9[T1 any, T2 any, T3 any, T4 any, T5 any, T6 any, T7 any, T8 any, T9 any](v1 T1, v2 T2, v3 T3, v4 T4, v5 T5, v6 T6, v7 T7, v8 T8, v9 T9, err error) *TestableResult9[T1, T2, T3, T4, T5, T6, T7, T8, T9]

A constructor for TestableResult9 from the values returned by a function that returns 9 values plus an error.

func (*TestableResult9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) Must9

func (r *TestableResult9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) Must9(t TestReporting) (T1, T2, T3, T4, T5, T6, T7, T8, T9)

A variation of Must that returns 9 non-error values

func (*TestableResult9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) Try9

func (r *TestableResult9[T1, T2, T3, T4, T5, T6, T7, T8, T9]) Try9() (T1, T2, T3, T4, T5, T6, T7, T8, T9)

A variation of Try that returns 9 non-error values

Jump to

Keyboard shortcuts

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