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