Documentation
¶
Overview ¶
this package contains the TestReceiver which is a process that can have message expectations set on them. These expectations match messages sent to the process inbox and execute a [TestExpectation] function. The function returns true to pass and false to fail the test.
Index ¶
- Variables
- func NewExpectationSet() *expectationSet
- type DoFun
- type ExpectArg
- type Expectation
- func (e *Expectation) After(preReq *Expectation) *Expectation
- func (e *Expectation) AnyTimes() *Expectation
- func (e *Expectation) CallCount() int
- func (e *Expectation) Do(f DoFun) *Expectation
- func (e *Expectation) Match(msg any) error
- func (e *Expectation) MaxCalls() int
- func (e *Expectation) MaxTimes(n int) *Expectation
- func (e *Expectation) MinCalls() int
- func (e *Expectation) MinTimes(n int) *Expectation
- func (e *Expectation) MustWait() bool
- func (e *Expectation) Name(name string) *Expectation
- func (e *Expectation) Reply(reply any) *Expectation
- func (e *Expectation) String() string
- func (e *Expectation) Times(n int) *Expectation
- type ExpectationFailure
- type GotFormatter
- type Matcher
- type ReceiverOpt
- type TLike
- type TestDependency
- type TestHelper
- type TestReceiver
- func (tr *TestReceiver) Expect(matchTerm any, m Matcher) *Expectation
- func (tr *TestReceiver) ExpectCall(matchTerm any, m Matcher, reply any) *Expectation
- func (tr *TestReceiver) ExpectCast(matchTerm any, m Matcher) *Expectation
- func (tr *TestReceiver) Failures() []*ExpectationFailure
- func (tr *TestReceiver) Receive(self erl.PID, inbox <-chan any) error
- func (tr *TestReceiver) Self() erl.PID
- func (tr *TestReceiver) StartSupervised(startLink func(self erl.PID) (erl.PID, error)) erl.PID
- func (tr *TestReceiver) Stop()
- func (tr *TestReceiver) String() string
- func (tr *TestReceiver) Wait()
- type TestReporter
Constants ¶
This section is empty.
Variables ¶
var ( // a signal value to indicate that the CallExpect should not reply to the caller NoCallReply any = "\x07" // DefaultReceiverTimeout time.Duration = chronos.Dur("9m59s") DefaultReceiverTimeout time.Duration = chronos.Dur("9m59s") DefaultWaitTimeout time.Duration = chronos.Dur("5s") )
Functions ¶
func NewExpectationSet ¶
func NewExpectationSet() *expectationSet
Types ¶
type Expectation ¶
type Expectation struct {
// contains filtered or unexported fields
}
func (*Expectation) After ¶
func (e *Expectation) After(preReq *Expectation) *Expectation
After declares that the call may only match after preReq has been exhausted.
func (*Expectation) AnyTimes ¶
func (e *Expectation) AnyTimes() *Expectation
AnyTimes allows the expectation to be called 0 or more times
func (*Expectation) CallCount ¶
func (e *Expectation) CallCount() int
func (*Expectation) Do ¶
func (e *Expectation) Do(f DoFun) *Expectation
Do declares the action to run when the call is matched. The function will receive an ExpectArg after the expectation is matched. If [f] panicks it will fail the test.
func (*Expectation) Match ¶
func (e *Expectation) Match(msg any) error
func (*Expectation) MaxCalls ¶
func (e *Expectation) MaxCalls() int
func (*Expectation) MaxTimes ¶
func (e *Expectation) MaxTimes(n int) *Expectation
MaxTimes limits the number of calls to n times. If AnyTimes or MinTimes have not been called or if MinTimes was previously called with 1, MaxTimes also sets the minimum number of calls to 0.
func (*Expectation) MinCalls ¶
func (e *Expectation) MinCalls() int
func (*Expectation) MinTimes ¶
func (e *Expectation) MinTimes(n int) *Expectation
MinTimes requires the call to occur at least n times. If AnyTimes or MaxTimes have not been called or if MaxTimes was previously called with 1, MinTimes also sets the maximum number of calls to infinity.
func (*Expectation) MustWait ¶
func (e *Expectation) MustWait() bool
returns true if the expectation has a max number of calls it's expecting.
func (*Expectation) Name ¶
func (e *Expectation) Name(name string) *Expectation
sets the name for the Expectation, which will be used in error msgs
func (*Expectation) Reply ¶
func (e *Expectation) Reply(reply any) *Expectation
Sets the Reply that will be sent in the event that this Expectation matches a Call message. It will not be used if it matches a Cast
func (*Expectation) String ¶
func (e *Expectation) String() string
func (*Expectation) Times ¶
func (e *Expectation) Times(n int) *Expectation
Times declares the exact number of times a function call is expected to be executed.
type ExpectationFailure ¶
type ExpectationFailure struct { // The matching term that led to the ExpectationFailure Match any // The failed expectation Exp *Expectation // The actual message that matched [Match] and was evaluated by the Expectation Msg any // Failure message Reason error }
func (*ExpectationFailure) String ¶
func (ef *ExpectationFailure) String() string
type GotFormatter ¶
type GotFormatter interface { // Got is invoked with the received value. The result is used when // printing the failure message. Got(got any) string }
GotFormatter is used to better print failure messages. If a matcher implements GotFormatter, it will use the result from Got when printing the failure message.
type Matcher ¶
type Matcher interface { // Matches returns whether x is a match. Matches(x any) bool // String describes what the matcher matches. String() string }
A Matcher is a representation of a class of values. It is used to represent the valid or expected arguments to a mocked method.
This interface is compatible with the one gomock uses, so methods like [gomock.Eq] are valid matches that can be used when creating an Expectation
type ReceiverOpt ¶
type ReceiverOpt func(ro receiverOptions) receiverOptions
func Name ¶
func Name(name string) ReceiverOpt
Set a name for the test receiver, that will be used in log messages
func NoFail ¶
func NoFail() ReceiverOpt
Set this if you do not want the TestReceiver to call testing.T.Fail in the [Wait] method.
func Parent ¶
func Parent(parent erl.PID) ReceiverOpt
Set a parent for this test process. Defaults to [erl.rootPID]
func ReceiverTimeout ¶
func ReceiverTimeout(t time.Duration) ReceiverOpt
Specify how long the test receiver should run for before stopping. this needs to be set otherwise tests will hang until exceptions are matched or the 10min Go default is reached. See DefaultReceiverTimeout If the '-timeout' option is greater or less than the DefaultReceiverTimeout, then it will be used instead. With that set, you shouldn't need to set this option unless you explicitly want to end before your test timeout, such as debugging a broken test or negative testing expect options.
func WaitTimeout ¶
func WaitTimeout(t time.Duration) ReceiverOpt
Specify the minimum amount of time that [TestReciever.Wait] will execute for before expectations like [expect.Times] or [expect.AtMost] will pass. Wait will finish before this timeout if only options like [expect.AtMost] are used. See DefaultWaitTimeout for the default. func WaitTimeout(t time.Duration) ReceiverOpt {
type TLike ¶
type TLike interface { Errorf(format string, args ...any) Logf(format string, args ...any) Failed() bool Fatalf(format string, args ...any) Log(args ...any) Helper() FailNow() Deadline() (time.Time, bool) Cleanup(func()) Error(args ...any) }
Making an interface for testing.T so that we can test the TestReceiver
type TestDependency ¶
used to inject more complex mocks based on a TestReceiver and have them checked when [Wait] is called
type TestHelper ¶
type TestHelper interface { TestReporter Helper() }
TestHelper is a TestReporter that has the Helper method. It is satisfied by the standard library's *testing.T.
type TestReceiver ¶
type TestReceiver struct {
// contains filtered or unexported fields
}
func NewReceiver ¶
func NewReceiver(t TLike, opts ...ReceiverOpt) (erl.PID, *TestReceiver)
Creates a new TestReceiver, which is a process that you can set message matching expectations on.
func (*TestReceiver) Expect ¶
func (tr *TestReceiver) Expect(matchTerm any, m Matcher) *Expectation
Set an expectation that will be matched whenever a [matchTerm] msg type is received.
func (*TestReceiver) ExpectCall ¶
func (tr *TestReceiver) ExpectCall(matchTerm any, m Matcher, reply any) *Expectation
Sets an expectation about a genserver.Call for this TestReceiver. The [reply] is the value that will be returned to the caller when a [matchTerm] msg is received.
If you want to *not* send a reply (say you're testing Call timeouts), then set [reply] to the signal value NoCallReply.
func (*TestReceiver) ExpectCast ¶
func (tr *TestReceiver) ExpectCast(matchTerm any, m Matcher) *Expectation
This is like [Expect] but is only tested against genserver.CastRequest messages.
func (*TestReceiver) Failures ¶
func (tr *TestReceiver) Failures() []*ExpectationFailure
func (*TestReceiver) Receive ¶
func (tr *TestReceiver) Receive(self erl.PID, inbox <-chan any) error
func (*TestReceiver) Self ¶
func (tr *TestReceiver) Self() erl.PID
func (*TestReceiver) StartSupervised ¶
starts the process via [startLink] with the TestReceiver as the parent. If [startLink] returns an error the test is failed. The process will be synchronously killed when calling [Stop]
func (*TestReceiver) Stop ¶
func (tr *TestReceiver) Stop()
will cause the test receiver to exit, sending signals to linked and monitoring processes. This is not needed for normal test cleanup (that is handled via [t.Cleanup()])
func (*TestReceiver) String ¶
func (tr *TestReceiver) String() string
func (*TestReceiver) Wait ¶
func (tr *TestReceiver) Wait()
Returns when the ReceiverTimeout expires or an expectation fails. It will not return *before* the WaitTimeout; this gives us a minimum amount of time for expectations to match messages.
Call this after you have sent your messages
type TestReporter ¶
type TestReporter interface { // logs an error and fails the test Errorf(format string, args ...any) // logs an error and panics the test. Be careful with this one, panicking outside // the thread where the test started is an error Fatalf(format string, args ...any) // logs that will show up only if a test fails Logf(format string, args ...any) }
A TestReporter is something that can be used to report test failures. It is satisfied by the standard library's *testing.T.
Directories
¶
Path | Synopsis |
---|---|
* This package provides "assertions" for use in [erltest.TestExpectation]s.
|
* This package provides "assertions" for use in [erltest.TestExpectation]s. |
internal
|
|
mock
Package mock is a generated GoMock package.
|
Package mock is a generated GoMock package. |
testserver provides a [GenSrv] that can be configured with different msg handlers as needed.
|
testserver provides a [GenSrv] that can be configured with different msg handlers as needed. |