Documentation
¶
Overview ¶
Package mocker provides a tool for generating interface mocks.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ErrUnkPkg is returned when a directory or an import path does not point // to a valid Go package. // // This error occurs in the following cases: // - The provided path cannot be resolved to a valid import path. // - The import path cannot be resolved to a valid directory. ErrUnkPkg = errors.New("package not found") // ErrUnkType is returned when a type declaration cannot be found. ErrUnkType = errors.New("type not found") // ErrUnkItf is returned when an interface cannot be found. ErrUnkItf = errors.New("interface not found") // ErrUnkMet is returned when an interface method cannot be found. ErrUnkMet = errors.New("method not found") // ErrAstParse is returned when AST parsing encounters an error. ErrAstParse = errors.New("error parsing sources") // ErrNoMethods is returned when the interface to mock has no methods. ErrNoMethods = errors.New("interface has no methods") )
Sentinel errors.
Functions ¶
func Generate ¶
Generate creates a mock implementation for the specified interface name and writes it to the configured output.
Example ¶
package main
import (
"bytes"
"fmt"
"github.com/ctx42/testing/pkg/mocker"
)
func main() {
out := &bytes.Buffer{}
err := mocker.Generate(
"Case00",
mocker.WithSrc("testdata/cases"),
mocker.WithTgt("."),
mocker.WithTgtOutput(out),
)
if err != nil {
panic(err)
}
fmt.Println(out.String())
}
Output: package mocker // Code generated by mocker. DO NOT EDIT. import ( "github.com/ctx42/testing/pkg/mock" "github.com/ctx42/testing/pkg/tester" ) type Case00Mock struct { *mock.Mock t tester.T } func NewCase00Mock(t tester.T) *Case00Mock { t.Helper() return &Case00Mock{Mock: mock.NewMock(t), t: t} } func (_mck *Case00Mock) Method00() { _mck.t.Helper() var _args []any _mck.Called(_args...) }
Example (UsingImportPaths) ¶
package main
import (
"bytes"
"fmt"
"github.com/ctx42/testing/pkg/mocker"
)
func main() {
out := &bytes.Buffer{}
err := mocker.Generate(
"Case00",
mocker.WithSrc("github.com/ctx42/testing/pkg/mocker/testdata/cases"),
mocker.WithTgt("github.com/ctx42/testing/pkg/goldy"),
mocker.WithTgtOutput(out),
)
if err != nil {
panic(err)
}
fmt.Println(out.String())
}
Output: package goldy // Code generated by mocker. DO NOT EDIT. import ( "github.com/ctx42/testing/pkg/mock" "github.com/ctx42/testing/pkg/tester" ) type Case00Mock struct { *mock.Mock t tester.T } func NewCase00Mock(t tester.T) *Case00Mock { t.Helper() return &Case00Mock{Mock: mock.NewMock(t), t: t} } func (_mck *Case00Mock) Method00() { _mck.t.Helper() var _args []any _mck.Called(_args...) }
func WithTgtOnHelpers ¶
func WithTgtOnHelpers(cfg *Config)
WithTgtOnHelpers turns on "OnXXX" helper methods generation.
Types ¶
type Config ¶
type Config struct {
// contains filtered or unexported fields
}
Config represents the configuration for the mocker.
type Mocker ¶
type Mocker struct {
// contains filtered or unexported fields
}
Mocker represents the main structure for creating interface mocks.
type Option ¶
type Option func(*Config)
Option represents a mocker option.
func WithSrc ¶
WithSrc sets the source directory or import path (package) where the interface to mock is defined.
func WithTesterAlias ¶ added in v0.30.0
WithTesterAlias sets the alias for the "github.com/ctx42/testing/pkg/tester" package. When the alias is set to empty string, it will use "_tester" as the alias.
func WithTgt ¶
WithTgt sets the target directory or import path (package) where the interface to mock should be created.
func WithTgtFilename ¶
WithTgtFilename sets the filename to write the generated interface mock to.
func WithTgtName ¶
WithTgtName sets the interface mock type name.
func WithTgtOutput ¶
WithTgtOutput configures the writer for the generated interface output. It takes precedence over the WithTgtFilename option. If the provided writer implements io.Closer, its Close method will be called after writing.