Skip to content

Commit

Permalink
[*] add documentation for CallModifier interface (#183)
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub authored Dec 21, 2023
1 parent 7447a72 commit a80a73d
Showing 1 changed file with 16 additions and 15 deletions.
31 changes: 16 additions & 15 deletions expectations.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@ type expectation interface {
fmt.Stringer
}

type CallModifyer interface {
Maybe() CallModifyer
Times(n uint) CallModifyer
WillDelayFor(duration time.Duration) CallModifyer
// CallModifier interface represents common interface for all expectations supported
type CallModifier interface {
// Maybe allows the expected method call to be optional.
// Not calling an optional method will not cause an error while asserting expectations
Maybe() CallModifier
// Times indicates that that the expected method should only fire the indicated number of times.
// Zero value is ignored and means the same as one.
Times(n uint) CallModifier
// WillDelayFor allows to specify duration for which it will delay
// result. May be used together with Context
WillDelayFor(duration time.Duration) CallModifier
// WillReturnError allows to set an error for the expected method
WillReturnError(err error)
// WillPanic allows to force the expected method to panic
WillPanic(v any)
}

Expand Down Expand Up @@ -72,35 +81,27 @@ func (e *commonExpectation) waitForDelay(ctx context.Context) (err error) {
return err
}

// Maybe allows the expected method call to be optional.
// Not calling an optional method will not cause an error while asserting expectations
func (e *commonExpectation) Maybe() CallModifyer {
func (e *commonExpectation) Maybe() CallModifier {
e.optional = true
return e
}

// Times indicates that that the expected method should only fire the indicated number of times.
// Zero value is ignored and means the same as one.
func (e *commonExpectation) Times(n uint) CallModifyer {
func (e *commonExpectation) Times(n uint) CallModifier {
e.plannedCalls = n
return e
}

// WillDelayFor allows to specify duration for which it will delay
// result. May be used together with Context
func (e *commonExpectation) WillDelayFor(duration time.Duration) CallModifyer {
func (e *commonExpectation) WillDelayFor(duration time.Duration) CallModifier {
e.plannedDelay = duration
return e
}

// WillReturnError allows to set an error for the expected method
func (e *commonExpectation) WillReturnError(err error) {
e.err = err
}

var errPanic = errors.New("pgxmock panic")

// WillPanic allows to force the expected method to panic
func (e *commonExpectation) WillPanic(v any) {
e.err = errPanic
e.panicArgument = v
Expand Down

0 comments on commit a80a73d

Please sign in to comment.