github.com/furusax0621/goa-v1@v1.4.3/goagen/gen_controller/generator_test.go (about) 1 package gencontroller_test 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "strings" 8 9 "github.com/goadesign/goa/design" 10 "github.com/goadesign/goa/goagen/codegen" 11 gencontroller "github.com/goadesign/goa/goagen/gen_controller" 12 "github.com/goadesign/goa/version" 13 . "github.com/onsi/ginkgo" 14 . "github.com/onsi/gomega" 15 ) 16 17 var _ = Describe("Generate", func() { 18 var workspace *codegen.Workspace 19 var outDir string 20 var pkg string 21 var files []string 22 var genErr error 23 24 BeforeEach(func() { 25 var err error 26 workspace, err = codegen.NewWorkspace("test") 27 Ω(err).ShouldNot(HaveOccurred()) 28 outDir, err = ioutil.TempDir(workspace.Path, "") 29 Ω(err).ShouldNot(HaveOccurred()) 30 os.Args = []string{"goagen", "--out=" + outDir, "--design=foo", "--version=" + version.String()} 31 }) 32 33 JustBeforeEach(func() { 34 files, genErr = gencontroller.Generate() 35 }) 36 37 AfterEach(func() { 38 workspace.Delete() 39 }) 40 41 Context("with a simple API", func() { 42 BeforeEach(func() { 43 design.Design = &design.APIDefinition{ 44 Name: "testapi", 45 Resources: map[string]*design.ResourceDefinition{ 46 "foo": { 47 Name: "foo", 48 Actions: map[string]*design.ActionDefinition{ 49 "show": { 50 Name: "show", 51 Routes: []*design.RouteDefinition{ 52 { 53 Verb: "GET", 54 Path: "", 55 }, 56 }, 57 }, 58 }, 59 }, 60 }, 61 } 62 fooRes := design.Design.Resources["foo"] 63 showAct := fooRes.Actions["show"] 64 showAct.Parent = fooRes 65 showAct.Routes[0].Parent = showAct 66 }) 67 68 It("generates a simple controller", func() { 69 Ω(genErr).Should(BeNil()) 70 Ω(files).Should(HaveLen(1)) 71 content, err := ioutil.ReadFile(filepath.Join(outDir, "foo.go")) 72 Ω(err).ShouldNot(HaveOccurred()) 73 Ω(len(strings.Split(string(content), "\n"))).Should(BeNumerically(">=", 16)) 74 }) 75 76 Context("with --out", func() { 77 BeforeEach(func() { 78 var err error 79 outDir, err = ioutil.TempDir(workspace.Path, "foo") 80 Ω(err).ShouldNot(HaveOccurred()) 81 os.Args = []string{"goagen", "--out=" + outDir, "--design=foo", "--version=" + version.String()} 82 }) 83 84 It("generates a controller package named from --out", func() { 85 Ω(genErr).Should(BeNil()) 86 Ω(files).Should(HaveLen(1)) 87 content, err := ioutil.ReadFile(filepath.Join(outDir, "foo.go")) 88 Ω(err).ShouldNot(HaveOccurred()) 89 Ω(string(content)).Should(HavePrefix("package foo")) 90 }) 91 92 Context("with --pkg", func() { 93 BeforeEach(func() { 94 pkg = "bar" 95 os.Args = []string{"goagen", "--out=" + outDir, "--pkg", pkg, "--design=foo", "--version=" + version.String()} 96 }) 97 98 It("generates a controller package named from --pkg", func() { 99 Ω(genErr).Should(BeNil()) 100 Ω(files).Should(HaveLen(1)) 101 content, err := ioutil.ReadFile(filepath.Join(outDir, "foo.go")) 102 Ω(err).ShouldNot(HaveOccurred()) 103 Ω(string(content)).Should(HavePrefix("package bar")) 104 }) 105 }) 106 }) 107 108 Context("without --out", func() { 109 BeforeEach(func() { 110 os.Args = []string{"goagen", "--out=" + outDir, "--design=foo", "--version=" + version.String()} 111 }) 112 113 It("generates a controller package named from default", func() { 114 Ω(genErr).Should(BeNil()) 115 Ω(files).Should(HaveLen(1)) 116 content, err := ioutil.ReadFile(filepath.Join(outDir, "foo.go")) 117 Ω(err).ShouldNot(HaveOccurred()) 118 Ω(string(content)).Should(HavePrefix("package main")) 119 }) 120 121 Context("with --pkg", func() { 122 BeforeEach(func() { 123 pkg = "bar" 124 os.Args = []string{"goagen", "--out=" + outDir, "--pkg", pkg, "--design=foo", "--version=" + version.String()} 125 }) 126 127 It("generates a controller package named from --pkg", func() { 128 Ω(genErr).Should(BeNil()) 129 Ω(files).Should(HaveLen(1)) 130 content, err := ioutil.ReadFile(filepath.Join(outDir, "foo.go")) 131 Ω(err).ShouldNot(HaveOccurred()) 132 Ω(string(content)).Should(HavePrefix("package bar")) 133 }) 134 }) 135 }) 136 }) 137 }) 138 139 var _ = Describe("NewGenerator", func() { 140 var generator *gencontroller.Generator 141 142 var args = struct { 143 api *design.APIDefinition 144 outDir string 145 designPkg string 146 appPkg string 147 force bool 148 regen bool 149 pkg string 150 resource string 151 noExample bool 152 }{ 153 api: &design.APIDefinition{ 154 Name: "test api", 155 }, 156 outDir: "out_dir", 157 designPkg: "design", 158 appPkg: "app", 159 pkg: "controller", 160 resource: "controller", 161 force: false, 162 } 163 164 Context("with options all options set", func() { 165 BeforeEach(func() { 166 167 generator = gencontroller.NewGenerator( 168 gencontroller.API(args.api), 169 gencontroller.OutDir(args.outDir), 170 gencontroller.DesignPkg(args.designPkg), 171 gencontroller.AppPkg(args.appPkg), 172 gencontroller.Pkg(args.pkg), 173 gencontroller.Resource(args.resource), 174 gencontroller.Force(args.force), 175 gencontroller.Regen(args.regen), 176 ) 177 }) 178 179 It("has all public properties set with expected value", func() { 180 Ω(generator).ShouldNot(BeNil()) 181 Ω(generator.API.Name).Should(Equal(args.api.Name)) 182 Ω(generator.OutDir).Should(Equal(args.outDir)) 183 Ω(generator.DesignPkg).Should(Equal(args.designPkg)) 184 Ω(generator.AppPkg).Should(Equal(args.appPkg)) 185 Ω(generator.Pkg).Should(Equal(args.pkg)) 186 Ω(generator.Resource).Should(Equal(args.resource)) 187 Ω(generator.Force).Should(Equal(args.force)) 188 Ω(generator.Regen).Should(Equal(args.regen)) 189 }) 190 191 }) 192 })