Documentation
¶
Overview ¶
Package imptest provides test mocking infrastructure for Go. It generates type-safe mocks from interfaces with interactive test control.
User API ¶
These are meant to be used directly in test code:
- TestReporter - interface for test frameworks (usually *testing.T)
- GetOrCreateImp - get/create shared coordinator for a test (used by generated code)
- Wait - block until all async expectations for a test are satisfied
- SetTimeout - configure timeout for blocking operations
For matchers (BeAny, Satisfy), import the match package:
import . "github.com/toejough/imptest/match"
Generated Code API ¶
These are used by code generated via `impgen`. Users interact with them indirectly through the generated type-safe wrappers:
- Imp - coordinator for dependency mocks
- Controller - manages call queue and synchronization
- DependencyMethod, DependencyCall, DependencyArgs - mock internals
- CallableController, TargetController - wrapper internals
- GenericCall, GenericResponse - low-level call/response types
- PendingExpectation, PendingCompletion - async expectation internals
- Matcher, Timer, Call - supporting interfaces and types
Index ¶
- func MatchValue(actual, expected any) (bool, string)
- func SetTimeout(t TestReporter, d time.Duration)
- func Wait(t TestReporter)
- type Call
- type CallableController
- type Controller
- type DependencyArgs
- type DependencyCall
- type DependencyMethod
- type GenericCall
- type GenericResponse
- type Imp
- type Matcher
- type PendingCompletion
- type PendingExpectation
- type TargetController
- type TestReporter
- type Timer
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MatchValue ¶
MatchValue checks if actual matches expected.
func SetTimeout ¶
func SetTimeout(t TestReporter, d time.Duration)
SetTimeout configures the timeout for all blocking operations in the test. A duration of 0 means no timeout (block forever).
If no Imp has been created for t yet, one is created.
func Wait ¶
func Wait(t TestReporter)
Wait blocks until all async expectations registered under t are satisfied. This is the package-level wait that coordinates across all mocks/wrappers sharing the same TestReporter.
If no Imp has been created for t yet, Wait returns immediately.
Types ¶
type CallableController ¶
type CallableController[T any] = core.CallableController[T]
func NewCallableController ¶
func NewCallableController[T any](t TestReporter) *CallableController[T]
NewCallableController creates a new callable controller.
type Controller ¶
type Controller[T Call] = core.Controller[T]
type DependencyArgs ¶
type DependencyArgs = core.DependencyArgs
type DependencyCall ¶
type DependencyCall = core.DependencyCall
type DependencyMethod ¶
type DependencyMethod = core.DependencyMethod
func NewDependencyMethod ¶
func NewDependencyMethod(imp *Imp, methodName string) *DependencyMethod
NewDependencyMethod creates a new DependencyMethod.
type GenericCall ¶
type GenericCall = core.GenericCall
type GenericResponse ¶
type GenericResponse = core.GenericResponse
type Imp ¶
func GetOrCreateImp ¶
func GetOrCreateImp(t TestReporter) *Imp
GetOrCreateImp returns the Imp for the given test, creating one if needed. Multiple calls with the same TestReporter return the same Imp instance. This enables coordination between mocks and wrappers in the same test.
If the TestReporter supports Cleanup (like *testing.T), the Imp is automatically removed from the registry when the test completes.
type PendingCompletion ¶
type PendingCompletion = core.PendingCompletion
type PendingExpectation ¶
type PendingExpectation = core.PendingExpectation
type TargetController ¶
type TargetController = core.TargetController
func NewTargetController ¶
func NewTargetController(t TestReporter) *TargetController
NewTargetController creates a new target controller.
type TestReporter ¶
type TestReporter = core.TestReporter
Directories
¶
| Path | Synopsis |
|---|---|
|
UAT
|
|
|
core/mock-function
Package mockfunction contains example functions to demonstrate mocking package-level functions with --dependency flag.
|
Package mockfunction contains example functions to demonstrate mocking package-level functions with --dependency flag. |
|
core/mock-functype
Package handlers contains function type definitions for testing direct function type mocking with --dependency flag.
|
Package handlers contains function type definitions for testing direct function type mocking with --dependency flag. |
|
core/mock-interface
Package basic demonstrates the core mocking features of imptest.
|
Package basic demonstrates the core mocking features of imptest. |
|
core/mock-method
Package mockmethod demonstrates mocking individual struct methods.
|
Package mockmethod demonstrates mocking individual struct methods. |
|
core/mock-struct
Package mockstruct demonstrates mocking a struct type by wrapping its methods into an interface that can be mocked.
|
Package mockstruct demonstrates mocking a struct type by wrapping its methods into an interface that can be mocked. |
|
core/wrapper-function
Package callable demonstrates wrapping functions and struct methods for testing.
|
Package callable demonstrates wrapping functions and struct methods for testing. |
|
core/wrapper-functype
Package functype demonstrates wrapping named function types for testing.
|
Package functype demonstrates wrapping named function types for testing. |
|
core/wrapper-interface
Package handlers demonstrates wrapping interface implementations.
|
Package handlers demonstrates wrapping interface implementations. |
|
core/wrapper-struct
Package calculator demonstrates wrapping struct method implementations.
|
Package calculator demonstrates wrapping struct method implementations. |
|
variations/behavior/callbacks
Package visitor demonstrates mocking interfaces with callback parameters.
|
Package visitor demonstrates mocking interfaces with callback parameters. |
|
variations/behavior/embedded-interfaces
Package embedded demonstrates mocking interfaces that embed other interfaces.
|
Package embedded demonstrates mocking interfaces that embed other interfaces. |
|
variations/behavior/embedded-structs
Package embeddedstructs demonstrates mocking interfaces with embedded structs.
|
Package embeddedstructs demonstrates mocking interfaces with embedded structs. |
|
variations/behavior/matching
Package matching demonstrates mocking interfaces with complex struct parameters and using matchers for partial field validation.
|
Package matching demonstrates mocking interfaces with complex struct parameters and using matchers for partial field validation. |
|
variations/behavior/panic-handling
Package safety demonstrates testing code that handles panics from dependencies.
|
Package safety demonstrates testing code that handles panics from dependencies. |
|
variations/behavior/typesafe-getargs
Package typesafeargs demonstrates type-safe GetArgs() for dependency mocks.
|
Package typesafeargs demonstrates type-safe GetArgs() for dependency mocks. |
|
variations/concurrency/eventually
Package concurrency demonstrates testing code that calls dependencies concurrently.
|
Package concurrency demonstrates testing code that calls dependencies concurrently. |
|
variations/concurrency/ordered
Package orderedvsmode demonstrates ordered vs eventually call matching modes.
|
Package orderedvsmode demonstrates ordered vs eventually call matching modes. |
|
variations/package/dot-imports/business/service
Package service demonstrates business logic using dot-imported interfaces.
|
Package service demonstrates business logic using dot-imported interfaces. |
|
variations/package/dot-imports/business/storage
Package storage provides storage interfaces for dot-import testing.
|
Package storage provides storage interfaces for dot-import testing. |
|
variations/package/dot-imports/helpers
Package helpers provides helper interfaces for dot-import testing.
|
Package helpers provides helper interfaces for dot-import testing. |
|
variations/package/same-package/interface-refs
Package samepackage demonstrates interfaces that reference other interfaces from the same package in their method signatures.
|
Package samepackage demonstrates interfaces that reference other interfaces from the same package in their method signatures. |
|
variations/package/same-package/whitebox
Package whitebox demonstrates whitebox testing where tests are in the same package.
|
Package whitebox demonstrates whitebox testing where tests are in the same package. |
|
variations/package/shadowing
Package timeconflict demonstrates using an interface from a package that shadows a stdlib package.
|
Package timeconflict demonstrates using an interface from a package that shadows a stdlib package. |
|
variations/package/shadowing/time
Package time demonstrates a local package that shadows the stdlib time package.
|
Package time demonstrates a local package that shadows the stdlib time package. |
|
variations/package/test-package
Package testpkgimport demonstrates resolving unqualified interface names from test packages to their non-test package equivalents.
|
Package testpkgimport demonstrates resolving unqualified interface names from test packages to their non-test package equivalents. |
|
variations/signature/channels
Package channels demonstrates mocking interfaces with channel types.
|
Package channels demonstrates mocking interfaces with channel types. |
|
variations/signature/cross-file-external
Package crossfile demonstrates mocking interfaces that reference external types from other files.
|
Package crossfile demonstrates mocking interfaces that reference external types from other files. |
|
variations/signature/edge-many-params
Package manyparams demonstrates mock generation for interfaces with many parameters.
|
Package manyparams demonstrates mock generation for interfaces with many parameters. |
|
variations/signature/edge-zero-returns
Package zeroreturns demonstrates mock generation for functions with zero return values.
|
Package zeroreturns demonstrates mock generation for functions with zero return values. |
|
variations/signature/external-functype
Package middleware demonstrates mocking interfaces with external function types.
|
Package middleware demonstrates mocking interfaces with external function types. |
|
variations/signature/external-types
Package externalimports demonstrates mocking interfaces with external types.
|
Package externalimports demonstrates mocking interfaces with external types. |
|
variations/signature/function-literal
Package funclit demonstrates mocking interfaces with function literal parameters.
|
Package funclit demonstrates mocking interfaces with function literal parameters. |
|
variations/signature/generics
Package generics demonstrates mocking generic interfaces.
|
Package generics demonstrates mocking generic interfaces. |
|
variations/signature/interface-literal
Package interfaceliteral demonstrates mocking interfaces that use anonymous interface literal types in method signatures.
|
Package interfaceliteral demonstrates mocking interfaces that use anonymous interface literal types in method signatures. |
|
variations/signature/named-params
Package named demonstrates mocking interfaces and functions with named parameters and return values.
|
Package named demonstrates mocking interfaces and functions with named parameters and return values. |
|
variations/signature/non-comparable
Package noncomparable demonstrates mocking interfaces with non-comparable parameter types like slices and maps.
|
Package noncomparable demonstrates mocking interfaces with non-comparable parameter types like slices and maps. |
|
variations/signature/parameterized
Package parameterized demonstrates mocking interfaces that use instantiated generic types like Container[string] in method signatures.
|
Package parameterized demonstrates mocking interfaces that use instantiated generic types like Container[string] in method signatures. |
|
variations/signature/struct-literal
Package structlit demonstrates mocking interfaces and functions with anonymous struct literal parameters and return types.
|
Package structlit demonstrates mocking interfaces and functions with anonymous struct literal parameters and return types. |
|
imptest/impgen is a tool to generate test mocks for Go interfaces.
|
imptest/impgen is a tool to generate test mocks for Go interfaces. |
|
internal
|
|
|
core
Package core provides the internal implementation of imptest's mock and target controller infrastructure.
|
Package core provides the internal implementation of imptest's mock and target controller infrastructure. |
|
run
Package run implements the main logic for the impgen tool in a testable way.
|
Package run implements the main logic for the impgen tool in a testable way. |
|
run/0_util
Package astutil provides shared utilities for AST manipulation and string formatting.
|
Package astutil provides shared utilities for AST manipulation and string formatting. |
|
run/1_cache
Package cache provides caching for generated code signatures to support incremental regeneration in the impgen tool.
|
Package cache provides caching for generated code signatures to support incremental regeneration in the impgen tool. |
|
run/2_load
Package load provides package loading functionality using DST parsing.
|
Package load provides package loading functionality using DST parsing. |
|
run/3_detect
Package detect provides symbol detection and type resolution for Go packages.
|
Package detect provides symbol detection and type resolution for Go packages. |
|
Package match provides matchers for use with imptest's ArgsShould, ReturnsShould, and PanicShould.
|
Package match provides matchers for use with imptest's ArgsShould, ReturnsShould, and PanicShould. |