utils

package
v0.12.6 Latest Latest
Warning

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

Go to latest
Published: Oct 16, 2023 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package utils contains a collection of utility functions that can be used throughout the Go application.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddInfoIfErr

func AddInfoIfErr(err error, info ...string) error

AddInfoIfErr adds info to the error that is passed in.

func Panics

func Panics(doesItPanic func()) (panics bool)

Panics returns true if the provided function panics. Source: https://www.reddit.com/r/golang/comments/2tj8b3/comment/cnzy3kc

func RunSingleArgumentTestCases

func RunSingleArgumentTestCases[T, R any](
	t *testing.T,
	testName string,
	runFn func(T) R,
	testCases []GenericTestCase[SingleArgumentTestCasesArgsType[T], R],
)

RunSingleArgumentTestCases runs a sets of test cases for a function that takes a single argument.

func RunThreeArgumentTestCases

func RunThreeArgumentTestCases[T, U, V, R any](
	t *testing.T,
	testName string,
	runFn func(T, U, V) R,
	testCases []GenericTestCase[ThreeArgumentTestCasesArgsType[T, U, V], R],
)

RunThreeArgumentTestCases runs a sets of test cases for a function that takes three arguments.

func RunTwoArgumentTestCases

func RunTwoArgumentTestCases[T, U, R any](
	t *testing.T,
	testName string,
	runFn func(T, U) R,
	testCases []GenericTestCase[TwoArgumentTestCasesArgsType[T, U], R],
)

RunTwoArgumentTestCases runs a sets of test cases for a function that takes two arguments.

func TestingSlicesEqualWithoutOrder added in v0.12.1

func TestingSlicesEqualWithoutOrder[R any](a, b []R) bool

TestingSlicesEqualWithoutOrder checks whether two slices are equal without order. Used for slices where order is not important or not defined, e.g. maps, sets, random samples.

Types

type GenericTestCase

type GenericTestCase[A any, W any] struct {
	Name string
	Args A
	Want W
}

GenericTestCase is a generic test case.

type GenericTestCaseWithNoWant

type GenericTestCaseWithNoWant[A any] struct {
	Name string
	Args A
}

GenericTestCaseWithNoWant is a generic test case with an option want.

type Option added in v0.9.0

type Option[V any] struct {
	Value *V
}

Option is a wrapper around a value that can be nil.

func NewOption added in v0.9.0

func NewOption[V any](value V) Option[V]

NewOption returns a new Option.

func (Option[V]) IsNil added in v0.9.0

func (o Option[V]) IsNil() bool

IsNil returns true if the Option is nil.

func (Option[V]) IsSome added in v0.9.0

func (o Option[V]) IsSome() bool

IsSome returns true if the Option is not nil.

func (Option[V]) Unwrap added in v0.9.0

func (o Option[V]) Unwrap() V

Unwrap returns the value of the Option or panics.

func (Option[V]) UnwrapOr added in v0.9.0

func (o Option[V]) UnwrapOr(defaultValue V) V

UnwrapOr returns the value of the Option or the provided default value.

func (Option[V]) UnwrapOrElse added in v0.9.0

func (o Option[V]) UnwrapOrElse(f func() V) V

UnwrapOrElse returns the value of the Option or the result of the provided function.

type Result added in v0.9.0

type Result[V any] struct {
	Value V
	Error error
	// contains filtered or unexported fields
}

func NewResult added in v0.9.0

func NewResult[V any](value V, err error) *Result[V]

NewResult returns a new Result with the given value and error.

func NewResultWithIgnoredErrs added in v0.9.0

func NewResultWithIgnoredErrs[V any](value V, err error, ignoredErrs ...error) *Result[V]

NewResultWithIgnoredErrs returns a new Result with the given value and error.

func (*Result[V]) AddInfoIfErr added in v0.9.0

func (w *Result[V]) AddInfoIfErr(info ...string) bool

AddInfoIfErr adds info to the error if Error is not nil. Returns true if the result contained an error.

func (*Result[V]) Destruct added in v0.9.0

func (w *Result[V]) Destruct() (V, error)

Destruct returns the value and error of the Result.

func (*Result[V]) FailTestIfErr added in v0.9.0

func (w *Result[V]) FailTestIfErr(t *testing.T)

FailTestIfErr fails the test if the Result is an error.

func (*Result[V]) FailTestIfErrf added in v0.9.0

func (w *Result[V]) FailTestIfErrf(t *testing.T, format string, args ...interface{})

func (*Result[V]) FailTestIfOk added in v0.9.0

func (w *Result[V]) FailTestIfOk(t *testing.T)

FailTestIfOk fails the test if the Result is ok.

func (*Result[V]) FailTestIfOkf added in v0.9.0

func (w *Result[V]) FailTestIfOkf(t *testing.T, format string, args ...interface{})

func (*Result[V]) IsErr added in v0.9.0

func (w *Result[V]) IsErr() bool

IsErr returns true if the Result is nil.

func (*Result[V]) IsOk added in v0.9.0

func (w *Result[V]) IsOk() bool

IsOk returns true if the Result is not nil.

func (*Result[V]) Msg added in v0.9.0

func (w *Result[V]) Msg() string

Msg returns the error message of the Result.

func (*Result[V]) Unwrap added in v0.9.0

func (w *Result[V]) Unwrap() V

Unwrap returns the value of the Result or panics with the error.

func (*Result[V]) UnwrapOr added in v0.9.0

func (w *Result[V]) UnwrapOr(defaultValue V) V

UnwrapOr returns the value of the Result or the provided default value.

type SingleArgumentTestCasesArgsType

type SingleArgumentTestCasesArgsType[T any] struct {
	A T
}

SingleArgumentTestCasesArgsType is the type of the arguments for a single argument test case.

type ThreeArgumentTestCasesArgsType

type ThreeArgumentTestCasesArgsType[T, U, V any] struct {
	A T
	B U
	C V
}

ThreeArgumentTestCasesArgsType is the type of the arguments for a three argument test case.

type TwoArgumentTestCasesArgsType

type TwoArgumentTestCasesArgsType[T, U any] struct {
	A T
	B U
}

TwoArgumentTestCasesArgsType is the type of the arguments for a two argument test case.

Jump to

Keyboard shortcuts

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