Best Mock code snippet using main.newInterfaceCache
parse.go
Source:parse.go
...52 }53 p := &fileParser{54 fileSet: fs,55 imports: make(map[string]importedPackage),56 importedInterfaces: newInterfaceCache(),57 auxInterfaces: newInterfaceCache(),58 srcDir: srcDir,59 }60 // Handle -imports.61 dotImports := make(map[string]bool)62 if *imports != "" {63 for _, kv := range strings.Split(*imports, ",") {64 eq := strings.Index(kv, "=")65 k, v := kv[:eq], kv[eq+1:]66 if k == "." {67 dotImports[v] = true68 } else {69 p.imports[k] = importedPkg{path: v}70 }71 }72 }73 // Handle -aux_files.74 if err := p.parseAuxFiles(*auxFiles); err != nil {75 return nil, err76 }77 p.addAuxInterfacesFromFile(packageImport, file) // this file78 pkg, err := p.parseFile(packageImport, file)79 if err != nil {80 return nil, err81 }82 for pkgPath := range dotImports {83 pkg.DotImports = append(pkg.DotImports, pkgPath)84 }85 return pkg, nil86}87type importedPackage interface {88 Path() string89 Parser() *fileParser90}91type importedPkg struct {92 path string93 parser *fileParser94}95func (i importedPkg) Path() string { return i.path }96func (i importedPkg) Parser() *fileParser { return i.parser }97// duplicateImport is a bit of a misnomer. Currently the parser can't98// handle cases of multi-file packages importing different packages99// under the same name. Often these imports would not be problematic,100// so this type lets us defer raising an error unless the package name101// is actually used.102type duplicateImport struct {103 name string104 duplicates []string105}106func (d duplicateImport) Error() string {107 return fmt.Sprintf("%q is ambiguous because of duplicate imports: %v", d.name, d.duplicates)108}109func (d duplicateImport) Path() string { log.Fatal(d.Error()); return "" }110func (d duplicateImport) Parser() *fileParser { log.Fatal(d.Error()); return nil }111type interfaceCache struct {112 m map[string]map[string]*namedInterface113}114func newInterfaceCache() *interfaceCache {115 return &interfaceCache{116 m: make(map[string]map[string]*namedInterface),117 }118}119func (i *interfaceCache) Set(pkg, name string, it *namedInterface) {120 if _, ok := i.m[pkg]; !ok {121 i.m[pkg] = make(map[string]*namedInterface)122 }123 i.m[pkg][name] = it124}125func (i *interfaceCache) Get(pkg, name string) *namedInterface {126 if _, ok := i.m[pkg]; !ok {127 return nil128 }129 return i.m[pkg][name]130}131func (i *interfaceCache) GetASTIface(pkg, name string) *ast.InterfaceType {132 if _, ok := i.m[pkg]; !ok {133 return nil134 }135 it, ok := i.m[pkg][name]136 if !ok {137 return nil138 }139 return it.it140}141type fileParser struct {142 fileSet *token.FileSet143 imports map[string]importedPackage // package name => imported package144 importedInterfaces *interfaceCache145 auxFiles []*ast.File146 auxInterfaces *interfaceCache147 srcDir string148}149func (p *fileParser) errorf(pos token.Pos, format string, args ...interface{}) error {150 ps := p.fileSet.Position(pos)151 format = "%s:%d:%d: " + format152 args = append([]interface{}{ps.Filename, ps.Line, ps.Column}, args...)153 return fmt.Errorf(format, args...)154}155func (p *fileParser) parseAuxFiles(auxFiles string) error {156 auxFiles = strings.TrimSpace(auxFiles)157 if auxFiles == "" {158 return nil159 }160 for _, kv := range strings.Split(auxFiles, ",") {161 parts := strings.SplitN(kv, "=", 2)162 if len(parts) != 2 {163 return fmt.Errorf("bad aux file spec: %v", kv)164 }165 pkg, fpath := parts[0], parts[1]166 file, err := parser.ParseFile(p.fileSet, fpath, nil, 0)167 if err != nil {168 return err169 }170 p.auxFiles = append(p.auxFiles, file)171 p.addAuxInterfacesFromFile(pkg, file)172 }173 return nil174}175func (p *fileParser) addAuxInterfacesFromFile(pkg string, file *ast.File) {176 for ni := range iterInterfaces(file) {177 p.auxInterfaces.Set(pkg, ni.name.Name, ni)178 }179}180// parseFile loads all file imports and auxiliary files import into the181// fileParser, parses all file interfaces and returns package model.182func (p *fileParser) parseFile(importPath string, file *ast.File) (*model.Package, error) {183 allImports, dotImports := importsOfFile(file)184 // Don't stomp imports provided by -imports. Those should take precedence.185 for pkg, pkgI := range allImports {186 if _, ok := p.imports[pkg]; !ok {187 p.imports[pkg] = pkgI188 }189 }190 // Add imports from auxiliary files, which might be needed for embedded interfaces.191 // Don't stomp any other imports.192 for _, f := range p.auxFiles {193 auxImports, _ := importsOfFile(f)194 for pkg, pkgI := range auxImports {195 if _, ok := p.imports[pkg]; !ok {196 p.imports[pkg] = pkgI197 }198 }199 }200 var is []*model.Interface201 for ni := range iterInterfaces(file) {202 i, err := p.parseInterface(ni.name.String(), importPath, ni)203 if err != nil {204 return nil, err205 }206 is = append(is, i)207 }208 return &model.Package{209 Name: file.Name.String(),210 PkgPath: importPath,211 Interfaces: is,212 DotImports: dotImports,213 }, nil214}215// parsePackage loads package specified by path, parses it and returns216// a new fileParser with the parsed imports and interfaces.217func (p *fileParser) parsePackage(path string) (*fileParser, error) {218 newP := &fileParser{219 fileSet: token.NewFileSet(),220 imports: make(map[string]importedPackage),221 importedInterfaces: newInterfaceCache(),222 auxInterfaces: newInterfaceCache(),223 srcDir: p.srcDir,224 }225 var pkgs map[string]*ast.Package226 if imp, err := build.Import(path, newP.srcDir, build.FindOnly); err != nil {227 return nil, err228 } else if pkgs, err = parser.ParseDir(newP.fileSet, imp.Dir, nil, 0); err != nil {229 return nil, err230 }231 for _, pkg := range pkgs {232 file := ast.MergePackageFiles(pkg, ast.FilterFuncDuplicates|ast.FilterUnassociatedComments|ast.FilterImportDuplicates)233 for ni := range iterInterfaces(file) {234 newP.importedInterfaces.Set(path, ni.name.Name, ni)235 }236 imports, _ := importsOfFile(file)...
udp-server.go
Source:udp-server.go
...80 IP: ip,81 Port: config.Port,82 Zone: config.Iface,83 }84 ic, err := newInterfaceCache(env, config.Iface)85 if err != nil {86 return nil, err87 }88 eg, egCtx := errgroup.WithContext(context.Background())89 ctx, cancel := context.WithCancel(egCtx)90 pl := newPeerLookup()91 ret := &LinkServer{92 config: config,93 net: env,94 conn: nil, // this will be filled in by `Start()`95 addr: addr,96 dev: dev,97 eg: eg,98 ctx: ctx,...
parse_test.go
Source:parse_test.go
...12 }13 p := fileParser{14 fileSet: fs,15 imports: make(map[string]importedPackage),16 importedInterfaces: newInterfaceCache(),17 }18 pkg, err := p.parseFile("", file)19 if err != nil {20 t.Fatalf("Unexpected error: %v", err)21 }22 checkGreeterImports(t, p.imports)23 expectedName := "greeter"24 if pkg.Name != expectedName {25 t.Fatalf("Expected name to be %v but got %v", expectedName, pkg.Name)26 }27 expectedInterfaceName := "InputMaker"28 if pkg.Interfaces[0].Name != expectedInterfaceName {29 t.Fatalf("Expected interface name to be %v but got %v", expectedInterfaceName, pkg.Interfaces[0].Name)30 }31}32func TestFileParser_ParsePackage(t *testing.T) {33 fs := token.NewFileSet()34 _, err := parser.ParseFile(fs, "internal/tests/custom_package_name/greeter/greeter.go", nil, 0)35 if err != nil {36 t.Fatalf("Unexpected error: %v", err)37 }38 p := fileParser{39 fileSet: fs,40 imports: make(map[string]importedPackage),41 importedInterfaces: newInterfaceCache(),42 }43 newP, err := p.parsePackage("github.com/golang/mock/mockgen/internal/tests/custom_package_name/greeter")44 if err != nil {45 t.Fatalf("Unexpected error: %v", err)46 }47 checkGreeterImports(t, newP.imports)48}49func TestImportsOfFile(t *testing.T) {50 fs := token.NewFileSet()51 file, err := parser.ParseFile(fs, "internal/tests/custom_package_name/greeter/greeter.go", nil, 0)52 if err != nil {53 t.Fatalf("Unexpected error: %v", err)54 }55 imports, _ := importsOfFile(file)56 checkGreeterImports(t, imports)57}58func checkGreeterImports(t *testing.T, imports map[string]importedPackage) {59 // check that imports have stdlib package "fmt"60 if fmtPackage, ok := imports["fmt"]; !ok {61 t.Errorf("Expected imports to have key \"fmt\"")62 } else {63 expectedFmtPackage := "fmt"64 if fmtPackage.Path() != expectedFmtPackage {65 t.Errorf("Expected fmt key to have value %s but got %s", expectedFmtPackage, fmtPackage.Path())66 }67 }68 // check that imports have package named "validator"69 if validatorPackage, ok := imports["validator"]; !ok {70 t.Errorf("Expected imports to have key \"fmt\"")71 } else {72 expectedValidatorPackage := "github.com/golang/mock/mockgen/internal/tests/custom_package_name/validator"73 if validatorPackage.Path() != expectedValidatorPackage {74 t.Errorf("Expected validator key to have value %s but got %s", expectedValidatorPackage, validatorPackage.Path())75 }76 }77 // check that imports have package named "client"78 if clientPackage, ok := imports["client"]; !ok {79 t.Errorf("Expected imports to have key \"client\"")80 } else {81 expectedClientPackage := "github.com/golang/mock/mockgen/internal/tests/custom_package_name/client/v1"82 if clientPackage.Path() != expectedClientPackage {83 t.Errorf("Expected client key to have value %s but got %s", expectedClientPackage, clientPackage.Path())84 }85 }86 // check that imports don't have package named "v1"87 if _, ok := imports["v1"]; ok {88 t.Errorf("Expected import not to have key \"v1\"")89 }90}91func Benchmark_parseFile(b *testing.B) {92 source := "internal/tests/performance/big_interface/big_interface.go"93 for n := 0; n < b.N; n++ {94 sourceMode(source)95 }96}97func TestParseArrayWithConstLength(t *testing.T) {98 fs := token.NewFileSet()99 srcDir := "internal/tests/const_array_length/input.go"100 file, err := parser.ParseFile(fs, srcDir, nil, 0)101 if err != nil {102 t.Fatalf("Unexpected error: %v", err)103 }104 p := fileParser{105 fileSet: fs,106 imports: make(map[string]importedPackage),107 importedInterfaces: newInterfaceCache(),108 auxInterfaces: newInterfaceCache(),109 srcDir: srcDir,110 }111 pkg, err := p.parseFile("", file)112 if err != nil {113 t.Fatalf("Unexpected error: %v", err)114 }115 expects := []string{"[2]int", "[2]int", "[127]int", "[3]int", "[3]int", "[7]int"}116 for i, e := range expects {117 got := pkg.Interfaces[0].Methods[i].Out[0].Type.String(nil, "")118 if got != e {119 t.Fatalf("got %v; expected %v", got, e)120 }121 }122}...
newInterfaceCache
Using AI Code Generation
1import (2func main() {3 cache = newInterfaceCache()4 cache.set("foo", "bar")5 fmt.Println(cache.get("foo"))6}7import "fmt"8type myInterface interface {9 foo()10}11type myStruct struct {12}13func (m myStruct) foo() {14 fmt.Println("Hello", m.name)15}16func main() {17 m = myStruct{"John Doe"}18 m.foo()19}20import "fmt"21type myInterface interface {22 foo()23}24type myStruct struct {25}26func (m myStruct) foo() {27 fmt.Println("Hello", m.name)28}29func main() {30 m = myStruct{"John Doe"}31 m.foo()32}33import "fmt"34type myInterface interface {35 foo()36}37type myStruct struct {38}39func (m myStruct) foo() {40 fmt.Println("Hello", m.name)41}42func main() {43 m = myStruct{"John Doe"}44 m.foo()45}
newInterfaceCache
Using AI Code Generation
1func main() {2 var cache *InterfaceCache = NewInterfaceCache()3}4func main() {5 var cache *InterfaceCache = NewInterfaceCache()6}7func main() {8 var cache *InterfaceCache = NewInterfaceCache()9}10func main() {11 var cache *InterfaceCache = NewInterfaceCache()12}13func main() {14 var cache *InterfaceCache = NewInterfaceCache()15}16func main() {17 var cache *InterfaceCache = NewInterfaceCache()18}19func main() {20 var cache *InterfaceCache = NewInterfaceCache()21}22func main() {23 var cache *InterfaceCache = NewInterfaceCache()24}25func main() {26 var cache *InterfaceCache = NewInterfaceCache()27}28func main() {29 var cache *InterfaceCache = NewInterfaceCache()30}31func main() {32 var cache *InterfaceCache = NewInterfaceCache()33}34func main() {35 var cache *InterfaceCache = NewInterfaceCache()36}
newInterfaceCache
Using AI Code Generation
1func main() {2 var cache = newInterfaceCache()3 cache.set("name", "John")4 var name = cache.get("name")5 fmt.Println(name)6}7func main() {8 var cache = newInterfaceCache()9 cache.set("name", "John")10 var name = cache.get("name")11 fmt.Println(name)12}13func main() {14 var cache = newInterfaceCache()15 cache.set("name", "John")16 var name = cache.get("name")17 fmt.Println(name)18}
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!