template

package
v3.0.0-alpha.10 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2025 License: BSD-3-Clause Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
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 },
}
View Source
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

func (d Data) MocksSomeMethod() bool

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 (m MockData) TypeConstraint() string

func (MockData) TypeInstantiation

func (m MockData) TypeInstantiation() string

type ParamData

type ParamData struct {
	Var      *registry.Var
	Variadic bool
}

ParamData is the data which represents a parameter to some method of an interface.

func (ParamData) CallName

func (p ParamData) CallName(ellipsis bool) string

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

func (p ParamData) MethodArg() string

MethodArg is the representation of the parameter in the function signature, ex: 'name a.Type'.

func (ParamData) Name

func (p ParamData) Name() string

Name returns the name of the parameter.

func (ParamData) TypeString

func (p ParamData) TypeString() string

TypeString returns the string representation of the type of the parameter.

func (ParamData) TypeStringEllipsis

func (p ParamData) TypeStringEllipsis() string

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

func (p ParamData) TypeStringVariadicUnderlying() string

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.

func New

func New(templateString string, name string) (Template, error)

New returns a new instance of Template.

func (Template) Execute

func (t Template) Execute(w io.Writer, data Data) error

Execute generates and writes the Moq implementation for the given data.

type TypeParamData

type TypeParamData struct {
	ParamData
	Constraint types.Type
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL