Documentation
¶
Index ¶
- Variables
- type Data
- type MethodData
- func (m MethodData) ArgCallList() string
- func (m MethodData) ArgCallListNoEllipsis() string
- func (m MethodData) ArgCallListSlice(start, end int) string
- func (m MethodData) ArgCallListSliceNoEllipsis(start, end int) string
- func (m MethodData) ArgList() string
- func (m MethodData) ArgTypeList() string
- func (m MethodData) ArgTypeListEllipsis() string
- func (m MethodData) IsVariadic() bool
- func (m MethodData) ReturnArgList() string
- func (m MethodData) ReturnArgNameList() string
- func (m MethodData) ReturnArgTypeList() string
- type MockData
- type ParamData
- type Template
- type TypeParamData
Constants ¶
This section is empty.
Variables ¶
var StringManipulationFuncs = template.FuncMap{ "contains": func(substr string, s string) bool { return strings.Contains(s, substr) }, "hasPrefix": func(prefix string, s string) bool { return strings.HasPrefix(s, prefix) }, "hasSuffix": func(suffix string, s string) bool { return strings.HasSuffix(s, suffix) }, "join": func(sep string, elems []string) string { return strings.Join(elems, sep) }, "replace": func(old string, new string, n int, s string) string { return strings.Replace(s, old, new, n) }, "replaceAll": func(old string, new string, s string) string { return strings.ReplaceAll(s, old, new) }, "split": func(sep string, s string) []string { return strings.Split(s, sep) }, "splitAfter": func(sep string, s string) []string { return strings.SplitAfter(s, sep) }, "splitAfterN": func(sep string, n int, s string) []string { return strings.SplitAfterN(s, sep, n) }, "trim": func(cutset string, s string) string { return strings.Trim(s, cutset) }, "trimLeft": func(cutset string, s string) string { return strings.TrimLeft(s, cutset) }, "trimPrefix": func(prefix string, s string) string { return strings.TrimPrefix(s, prefix) }, "trimRight": func(cutset string, s string) string { return strings.TrimRight(s, cutset) }, "trimSpace": strings.TrimSpace, "trimSuffix": func(suffix string, s string) string { return strings.TrimSuffix(s, suffix) }, "lower": strings.ToLower, "upper": strings.ToUpper, "camelcase": xstrings.ToCamelCase, "snakecase": xstrings.ToSnakeCase, "kebabcase": xstrings.ToKebabCase, "firstLower": xstrings.FirstRuneToLower, "firstUpper": xstrings.FirstRuneToUpper, "matchString": regexp.MatchString, "quoteMeta": regexp.QuoteMeta, "base": filepath.Base, "clean": filepath.Clean, "dir": filepath.Dir, "expandEnv": os.ExpandEnv, "getenv": os.Getenv, "add": func(i1, i2 int) int { return i1 + i2 }, }
var TemplateMockFuncs = template.FuncMap{ "importStatement": func(imprt *registry.Package) string { if imprt.Alias == "" { return `"` + imprt.Path() + `"` } return imprt.Alias + ` "` + imprt.Path() + `"` }, "syncPkgQualifier": func(imports []*registry.Package) string { for _, imprt := range imports { if imprt.Path() == "sync" { return imprt.Qualifier() } } return "sync" }, "exported": exported, "mocksSomeMethod": func(mocks []MockData) bool { for _, m := range mocks { if len(m.Methods) > 0 { return true } } return false }, "typeConstraintTest": func(m MockData) string { if len(m.TypeParams) == 0 { return "" } s := "[" for idx, param := range m.TypeParams { if idx != 0 { s += ", " } s += exported(param.Name()) s += " " s += param.TypeString() } s += "]" return s }, }
Functions ¶
This section is empty.
Types ¶
type Data ¶
type Data struct { Boilerplate string BuildTags string PkgName string SrcPkgQualifier string Imports []*registry.Package Mocks []MockData StubImpl bool SkipEnsure bool WithResets bool TemplateData map[string]any }
Data is the template data used to render the Moq template.
func (Data) MocksSomeMethod ¶
MocksSomeMethod returns true of any one of the Mocks has at least 1 method.
type MethodData ¶
type MethodData struct { // Name is the method's name. Name string // Params represents all the arguments to the method. Params []ParamData // Returns represents all the return parameters of the method. Returns []ParamData // Scope represents the lexical scope of the method. Its primary function // is keeping track of all names visible in the current scope, which allows // the creation of new variables with guaranteed non-conflicting names. Scope *registry.MethodScope }
MethodData is the data which represents a method on some interface.
func (MethodData) ArgCallList ¶
func (m MethodData) ArgCallList() string
ArgCallList is the string representation of method call parameters, ex: 's, n, foo'. In case of a last variadic parameter, it will be of the format 's, n, foos...'.
func (MethodData) ArgCallListNoEllipsis ¶
func (m MethodData) ArgCallListNoEllipsis() string
ArgCallListNoEllipsis is the same as ArgCallList, except the last parameter, if variadic, will not contain an ellipsis.
func (MethodData) ArgCallListSlice ¶
func (m MethodData) ArgCallListSlice(start, end int) string
argCallListSlice is similar to ArgCallList, but it allows specification of a slice range to use for the parameter lists. Specifying an integer less than 1 for end indicates to slice to the end of the parameters. As with regular Go slicing semantics, the end value is a non-inclusive index.
func (MethodData) ArgCallListSliceNoEllipsis ¶
func (m MethodData) ArgCallListSliceNoEllipsis(start, end int) string
func (MethodData) ArgList ¶
func (m MethodData) ArgList() string
ArgList is the string representation of method parameters, ex: 's string, n int, foo bar.Baz'.
func (MethodData) ArgTypeList ¶
func (m MethodData) ArgTypeList() string
ArgTypeList returns the argument types in a comma-separated string, ex: `string, int, bar.Baz`
func (MethodData) ArgTypeListEllipsis ¶
func (m MethodData) ArgTypeListEllipsis() string
ArgTypeListEllipsis returns the argument types in a comma-separated string, ex: `string, int, bar.Baz`. If the last argument is variadic, it will contain an ellipsis as would be expected in a variadic function definition.
func (MethodData) IsVariadic ¶
func (m MethodData) IsVariadic() bool
func (MethodData) ReturnArgList ¶
func (m MethodData) ReturnArgList() string
ReturnArgList returns the name and types of the return values. For example: "foo int, bar string, err error"
func (MethodData) ReturnArgNameList ¶
func (m MethodData) ReturnArgNameList() string
ReturnArgNameList is the string representation of values being returned from the method, ex: 'foo', 's, err'.
func (MethodData) ReturnArgTypeList ¶
func (m MethodData) ReturnArgTypeList() string
ReturnArgTypeList is the string representation of method return types, ex: 'bar.Baz', '(string, error)'.
type MockData ¶
type MockData struct { InterfaceName string MockName string TypeParams []TypeParamData Methods []MethodData TemplateData map[string]any }
MockData is the data used to generate a mock for some interface.
func (MockData) TypeConstraint ¶
func (MockData) TypeInstantiation ¶
type ParamData ¶
ParamData is the data which represents a parameter to some method of an interface.
func (ParamData) CallName ¶
CallName returns the string representation of the parameter to be used for a method call. For a variadic paramter, it will be of the format 'foos...' if ellipsis is true.
func (ParamData) MethodArg ¶
MethodArg is the representation of the parameter in the function signature, ex: 'name a.Type'.
func (ParamData) TypeString ¶
TypeString returns the string representation of the type of the parameter.
func (ParamData) TypeStringEllipsis ¶
TypeStringEllipsis returns the string representation of the type of the parameter. If it is a variadic parameter, it will be represented as a variadic parameter instead of a slice. For example instead of `[]string`, it will return `...string`.
func (ParamData) TypeStringVariadicUnderlying ¶
TypeStringVariadicUnderlying returns the underlying type of a variadic parameter. For instance, if a function has a parameter defined as `foo ...int`, this function will return "int". If the parameter is not variadic, this will behave the same as `TypeString`.
type Template ¶
type Template struct {
// contains filtered or unexported fields
}
Template is the Moq template. It is capable of generating the Moq implementation for the given template.Data.