github.com/benma/gogen@v0.0.0-20160826115606-cf49914b915a/cmd/gosimplemock/main.go (about) 1 package main 2 3 import ( 4 "flag" 5 "fmt" 6 "log" 7 "os" 8 "path" 9 "regexp" 10 "strings" 11 12 "github.com/ernesto-jimenez/gogen/automock" 13 "github.com/ernesto-jimenez/gogen/importer" 14 "github.com/ernesto-jimenez/gogen/strconv" 15 ) 16 17 var ( 18 out = flag.String("o", "", "override the name of the generated code. Default value is by generated based on the name of the interface, e.g.: Reader -> reader_mock_test.go (use \"-\" to print to stdout)") 19 mockName = flag.String("mock-name", "", "override the name for the mock struct") 20 mockPkg = flag.String("mock-pkg", "", "override the package name for the mock") 21 pkg = flag.String("pkg", ".", "override package to get the interface from. It can be specified in the interface name, e.g.: goautomock io.Reader") 22 ) 23 24 func main() { 25 flag.Parse() 26 log.SetFlags(0) 27 28 iface := flag.Arg(0) 29 30 if iface == "" { 31 log.Fatal("need to specify an interface name") 32 } 33 34 parts := strings.Split(iface, ".") 35 switch len(parts) { 36 case 1: 37 case 2: 38 if *pkg != "." { 39 log.Fatalf("unexpected -pkg value (%q), package is already defined in the interface name as %s", *pkg, parts[0]) 40 } 41 *pkg = parts[0] 42 iface = parts[1] 43 default: 44 log.Fatalf("invalid interface %q", iface) 45 } 46 47 gen, err := automock.NewGenerator(*pkg, iface) 48 if err != nil { 49 log.Fatal(err) 50 } 51 52 gen.SetTemplate(template) 53 54 if *mockName != "" { 55 gen.SetName(*mockName) 56 } 57 inPkg := *pkg == "." && path.Dir(*out) == "." 58 gen.SetInternal(inPkg) 59 if *mockPkg == "" && !inPkg { 60 p, err := importer.Default().Import(".") 61 if err != nil { 62 log.Fatal(err) 63 } 64 *mockPkg = p.Name() 65 } 66 if *mockPkg != "" { 67 gen.SetPackage(*mockPkg) 68 } 69 70 w := os.Stdout 71 if *out == "" { 72 *out = fmt.Sprintf("%s_test.go", gen.Name()) 73 if p := regexp.MustCompile(".*/").ReplaceAllString(*pkg, ""); !inPkg && p != "" && p != "." { 74 *out = p + "_" + *out 75 } 76 } 77 if *out != "-" { 78 *out = strconv.SnakeCase(*out) 79 log.Printf("Generating mock for %s in %s", iface, *out) 80 w, err = os.OpenFile(*out, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666) 81 if err != nil { 82 log.Fatal(err) 83 } 84 } 85 86 err = gen.Write(w) 87 switch err := err.(type) { 88 case automock.GenerationError: 89 log.Println(err.CodeWithLineNumbers()) 90 log.Fatal(err) 91 case error: 92 log.Fatal(err) 93 } 94 } 95 96 var template = `/* 97 * CODE GENERATED AUTOMATICALLY WITH github.com/ernesto-jimenez/gogen/cmd/gosimplemock 98 * THIS FILE SHOULD NOT BE EDITED BY HAND 99 */ 100 101 package {{.Package}} 102 103 import ( 104 "fmt" 105 {{range $path, $name := .Imports}} 106 {{$name}} "{{$path}}"{{end}} 107 ) 108 109 // {{.Name}} mock 110 type {{.Name}} struct { 111 {{range .Methods}} 112 {{.Name}}Func func({{range $index, $type := .ParamTypes}}{{if $index}}, {{end}}{{$type}}{{end}}) ({{range $index, $type := .ReturnTypes}}{{if $index}}, {{end}}{{$type}}{{end}}) 113 {{end}} 114 } 115 116 {{$gen := .}} 117 {{range .Methods}} 118 // {{.Name}} mocked method 119 func (m *{{$gen.Name}}) {{.Name}}({{range $index, $type := .ParamTypes}}{{if $index}}, {{end}}p{{$index}} {{$type}}{{end}}) ({{range $index, $type := .ReturnTypes}}{{if $index}}, {{end}}{{$type}}{{end}}) { 120 if m.{{.Name}}Func == nil { 121 panic("unexpected call to mocked method {{.Name}}") 122 } 123 {{if .ReturnTypes}}return {{end}} m.{{.Name}}Func({{range $index, $type := .ParamTypes}}{{if $index}}, {{end}}p{{$index}}{{end}}) 124 } 125 {{end}} 126 `