github.com/moitias/moq@v0.0.0-20240223074357-5eb0f0ba4054/internal/template/template_test.go (about) 1 package template 2 3 import ( 4 "go/types" 5 "testing" 6 7 "github.com/moitias/moq/internal/registry" 8 ) 9 10 func TestTemplateFuncs(t *testing.T) { 11 t.Run("Exported", func(t *testing.T) { 12 f := templateFuncs["Exported"].(func(string) string) 13 if f("") != "" { 14 t.Errorf("Exported(...) want: ``; got: `%s`", f("")) 15 } 16 if f("var") != "Var" { 17 t.Errorf("Exported(...) want: `Var`; got: `%s`", f("var")) 18 } 19 }) 20 21 t.Run("ImportStatement", func(t *testing.T) { 22 f := templateFuncs["ImportStatement"].(func(*registry.Package) string) 23 pkg := registry.NewPackage(types.NewPackage("xyz", "xyz")) 24 if f(pkg) != `"xyz"` { 25 t.Errorf("ImportStatement(...): want: `\"xyz\"`; got: `%s`", f(pkg)) 26 } 27 28 pkg.Alias = "x" 29 if f(pkg) != `x "xyz"` { 30 t.Errorf("ImportStatement(...): want: `x \"xyz\"`; got: `%s`", f(pkg)) 31 } 32 }) 33 34 t.Run("SyncPkgQualifier", func(t *testing.T) { 35 f := templateFuncs["SyncPkgQualifier"].(func([]*registry.Package) string) 36 if f(nil) != "sync" { 37 t.Errorf("SyncPkgQualifier(...): want: `sync`; got: `%s`", f(nil)) 38 } 39 imports := []*registry.Package{ 40 registry.NewPackage(types.NewPackage("sync", "sync")), 41 registry.NewPackage(types.NewPackage("github.com/some/module", "module")), 42 } 43 if f(imports) != "sync" { 44 t.Errorf("SyncPkgQualifier(...): want: `sync`; got: `%s`", f(imports)) 45 } 46 47 syncPkg := registry.NewPackage(types.NewPackage("sync", "sync")) 48 syncPkg.Alias = "stdsync" 49 otherSyncPkg := registry.NewPackage(types.NewPackage("github.com/someother/sync", "sync")) 50 imports = []*registry.Package{otherSyncPkg, syncPkg} 51 if f(imports) != "stdsync" { 52 t.Errorf("SyncPkgQualifier(...): want: `stdsync`; got: `%s`", f(imports)) 53 } 54 }) 55 }