pers

package
v4.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2023 License: Unlicense Imports: 10 Imported by: 0

Documentation

Overview

Package pers (hel...pers ... get it? Yeah, I know, I have a love/hate relationship with myself too) contains a bunch of helpers for working with hel mocks. From making a mock consistently return to matchers - they'll all be here.

Index

Examples

Constants

View Source
const Any any = -1

Any is a special value to tell pers to allow any value at the position used. For example, you can assert only on the second argument with:

HaveMethodExecuted("Foo", WithArgs(Any, 22))
View Source
const VariadicAny variadicAny = -2

VariadicAny is a special value, similar to Any, but specifically to tell pers to allow any number of values for the variadic arguments. It must be passed in after all non-variadic arguments so that its position matches the position that variadic arguments would normally be.

This cannot be used to check some variadic arguments without checking the others - at least right now, you must either assert on all of the variadic arguments or none of them.

Variables

This section is empty.

Functions

func ConsistentlyReturn

func ConsistentlyReturn(t T, mock interface{}, args ...interface{}) (stop func())

ConsistentlyReturn will continue adding a given value to the channel until the test is done or the returned stop function is called, whichever happens first. When ConsistentlyReturn stops adding values to the channel(s), it will drain those channels before returning.

After the first call to stop (or after the test completes), calls to stop will be a no-op.

The value for mock may be either a channel or a struct full of channels. ConsistentlyReturn will panic otherwise.

ConsistentlyReturn will panic if:

  • args contains a different number of arguments than the number of channels on mock.
  • any of the arguments passed in are not compatible to the return types of mock.

func Return

func Return(mock interface{}, args ...interface{})

Return will add a given value to the channel or struct of channels. This isn't very useful with a single value, so it's intended more to support structs full of channels, such as the ones that hel generates for return values in its mocks.

Return panics if:

  • the passed in mock value is not a valid mock field (a channel or struct of channels).
  • the passed in args cannot be returned on the mock field.
  • the passed in mock value is already full and sending another return value would block.

Types

type HaveMethodExecutedMatcher

type HaveMethodExecutedMatcher struct {
	MethodName string
	// contains filtered or unexported fields
}

HaveMethodExecutedMatcher is a matcher to ensure that a method on a mock was executed.

func HaveMethodExecuted

func HaveMethodExecuted(name string, opts ...HaveMethodExecutedOption) *HaveMethodExecutedMatcher

HaveMethodExecuted returns a matcher that asserts that the method referenced by name was executed. Options can modify the behavior of the matcher.

HaveMethodExecuted will panic if the mock does not have a method matching name.

The HaveMethodExecutedMatcher will panic if any of the options used don't match the target mock properly. Check the documentation on the options to get more specific information about what would cause the matcher to panic.

func (HaveMethodExecutedMatcher) Match

func (m HaveMethodExecutedMatcher) Match(v interface{}) (interface{}, error)

Match checks the mock value v to see if it has a method matching m.MethodName which has been called.

func (*HaveMethodExecutedMatcher) UseDiffer

func (m *HaveMethodExecutedMatcher) UseDiffer(d matchers.Differ)

UseDiffer sets m to use d when showing a diff between actual and expected values.

type HaveMethodExecutedOption

type HaveMethodExecutedOption func(HaveMethodExecutedMatcher) HaveMethodExecutedMatcher

HaveMethodExecutedOption is an option function for the HaveMethodExecutedMatcher.

func Returning

func Returning(vals ...interface{}) HaveMethodExecutedOption

Returning returns a HaveMethodExecutedOption which will return the arguments on the mock's return channels after the method in question has been called.

Returning will cause a panic if:

  • The values provided are not ConvertibleTo the mock's return types.
  • The number of values provided does not match the number of return types in the mock.
  • For variadic methods, the matcher will use vals[len(nonVariadicArgs(mock)):] as the variadic argument.

func StoreArgs

func StoreArgs(targets ...interface{}) HaveMethodExecutedOption

StoreArgs returns a HaveMethodExecutedOption which stores the arguments passed to the method in the addresses provided. A nil value tells the matcher to skip the argument at that index.

StoreArgs will cause a panic if: - The values provided are not pointers. - The mock's arguments are not ConvertibleTo the targets' types. - The number of targets does not match the number of arguments in the function.

  • For variadic methods, a target value must be provided to store the variadic values. If the value passed in for the variadic values is nil, it will be skipped as normal. Otherwise, it must be a pointer to a slice capable of storing the variadic arguments' type.
Example
package main

import (
	"fmt"

	"git.sr.ht/~nelsam/hel/v4/pkg/pers"
)

type fakeMock struct {
	FooCalled chan struct{}
	FooInput  struct {
		Arg0 chan int
		Arg1 chan string
	}
	FooOutput struct {
		Err chan error
	}
	BarCalled chan struct{}
}

func newFakeMock() *fakeMock {
	m := &fakeMock{}
	m.FooCalled = make(chan struct{}, 100)
	m.FooInput.Arg0 = make(chan int, 100)
	m.FooInput.Arg1 = make(chan string, 100)
	m.FooOutput.Err = make(chan error, 100)
	m.BarCalled = make(chan struct{}, 100)
	return m
}

func (m *fakeMock) Foo(arg0 int, arg1 string) error {
	m.FooCalled <- struct{}{}
	m.FooInput.Arg0 <- arg0
	m.FooInput.Arg1 <- arg1
	return <-m.FooOutput.Err
}

func (m *fakeMock) Bar() {
	m.BarCalled <- struct{}{}
}

func main() {
	// Simulate calling a method on a mock
	fm := newFakeMock()
	fm.FooCalled <- struct{}{}
	fm.FooInput.Arg0 <- 42
	fm.FooInput.Arg1 <- "foobar"

	// Provide some addresses to store the arguments
	var (
		arg0 int
		arg1 string
	)
	m := pers.HaveMethodExecuted("Foo", pers.StoreArgs(&arg0, &arg1))
	_, err := m.Match(fm)
	fmt.Println(err)
	fmt.Println(arg0)
	fmt.Println(arg1)
}
Output:

<nil>
42
foobar

func WithArgs

func WithArgs(args ...interface{}) HaveMethodExecutedOption

WithArgs returns a HaveMethodExecutedOption which sets the HaveMethodExecutedMatcher to only pass if the latest execution of the method called it with the passed in arguments.

WithArgs will cause a panic if:

  • The argument passed in is not ConvertibleTo the mock's argument type at the matching index.
  • The number of arguments does not match the number of arguments in the mock.
  • For variadic methods, the number of arguments in the mock is a minimum. If len(args) is >= len(nonVariadicArgs(mock)) but len(args) != len(totalArgs(mock)), the matcher will return an error instead.

func Within

Within returns a HaveMethodExecutedOption which sets the HaveMethodExecutedMatcher to be executed within a given timeframe.

type Matcher

type Matcher interface {
	Match(actual interface{}) (interface{}, error)
}

Matcher is any type that can match values. Some code in this package supports matching against child matchers, for example:

HaveBeenExecuted("Foo", WithArgs(matchers.HaveLen(12)))

type T

type T interface {
	Fatalf(string, ...interface{})
	Cleanup(func())
}

T represents the methods we need from the testing.T or testing.B types.

Jump to

Keyboard shortcuts

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