moq/internal/template/template_test.go
Suhas Karanth 2ae606f132
Internal registry for disambiguated imports, vars (#141)
* Internal registry for disambiguated imports, vars
- Move functionality in the moq package partially into
  internal/{registry,template}.
- Leverage registry to assign unique package and variable/method
  parameter names. Use import aliases if present in interface source
  package.
BREAKING CHANGE: When the interface definition does not mention the
parameter names, the field names in call info anonymous struct will be
different.
The new field names are generated using the type info (string -> s,
int -> n, chan int -> intCh, []MyType -> myTypes, map[string]int ->
stringToInt etc.).
For example, for a string parameter previously if the field name was
'In1', the new field could be 'S' or 'S1' (depends on number of
string method parameters).
* Refactor golden file tests to be table-driven
* Fix sync pkg alias handling for moq generation
* Improve, add tests (increase coverage)
* Use $.Foo in template, avoid declaring variables
$ is set to the data argument passed to Execute, that is, to the
starting value of dot.
Variables were declared to be able to refer to the parent context.
* Consistent template field formatting
* Use tabs in generated Godoc comments' example code
* Minor simplification
* go generate
* Fix conflict for generated param name of pointer type

Excellent work by @sudo-suhas.
2021-02-01 19:20:20 +00:00

56 lines
1.6 KiB
Go

package template
import (
"go/types"
"testing"
"github.com/matryer/moq/internal/registry"
)
func TestTemplateFuncs(t *testing.T) {
t.Run("Exported", func(t *testing.T) {
f := templateFuncs["Exported"].(func(string) string)
if f("") != "" {
t.Errorf("Exported(...) want: ``; got: `%s`", f(""))
}
if f("var") != "Var" {
t.Errorf("Exported(...) want: `Var`; got: `%s`", f("var"))
}
})
t.Run("ImportStatement", func(t *testing.T) {
f := templateFuncs["ImportStatement"].(func(*registry.Package) string)
pkg := registry.NewPackage(types.NewPackage("xyz", "xyz"))
if f(pkg) != `"xyz"` {
t.Errorf("ImportStatement(...): want: `\"xyz\"`; got: `%s`", f(pkg))
}
pkg.Alias = "x"
if f(pkg) != `x "xyz"` {
t.Errorf("ImportStatement(...): want: `x \"xyz\"`; got: `%s`", f(pkg))
}
})
t.Run("SyncPkgQualifier", func(t *testing.T) {
f := templateFuncs["SyncPkgQualifier"].(func([]*registry.Package) string)
if f(nil) != "sync" {
t.Errorf("SyncPkgQualifier(...): want: `sync`; got: `%s`", f(nil))
}
imports := []*registry.Package{
registry.NewPackage(types.NewPackage("sync", "sync")),
registry.NewPackage(types.NewPackage("github.com/some/module", "module")),
}
if f(imports) != "sync" {
t.Errorf("SyncPkgQualifier(...): want: `sync`; got: `%s`", f(imports))
}
syncPkg := registry.NewPackage(types.NewPackage("sync", "sync"))
syncPkg.Alias = "stdsync"
otherSyncPkg := registry.NewPackage(types.NewPackage("github.com/someother/sync", "sync"))
imports = []*registry.Package{otherSyncPkg, syncPkg}
if f(imports) != "stdsync" {
t.Errorf("SyncPkgQualifier(...): want: `stdsync`; got: `%s`", f(imports))
}
})
}