diff --git a/cmd/gf/internal/cmd/cmd__test.go b/cmd/gf/internal/cmd/cmd_z_init_test.go similarity index 100% rename from cmd/gf/internal/cmd/cmd__test.go rename to cmd/gf/internal/cmd/cmd_z_init_test.go diff --git a/cmd/gf/internal/cmd/cmd_fix_test.go b/cmd/gf/internal/cmd/cmd_z_unit_fix_test.go similarity index 100% rename from cmd/gf/internal/cmd/cmd_fix_test.go rename to cmd/gf/internal/cmd/cmd_z_unit_fix_test.go diff --git a/cmd/gf/internal/cmd/cmd_gen_ctrl_test.go b/cmd/gf/internal/cmd/cmd_z_unit_gen_ctrl_test.go similarity index 98% rename from cmd/gf/internal/cmd/cmd_gen_ctrl_test.go rename to cmd/gf/internal/cmd/cmd_z_unit_gen_ctrl_test.go index 03cdc284c..ca4bc06c5 100644 --- a/cmd/gf/internal/cmd/cmd_gen_ctrl_test.go +++ b/cmd/gf/internal/cmd/cmd_z_unit_gen_ctrl_test.go @@ -33,7 +33,6 @@ func Test_Gen_Ctrl_Default(t *testing.T) { } ) err := gutil.FillStructWithDefault(&in) - t.AssertNil(err) err = gfile.Mkdir(path) @@ -45,7 +44,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) { panic(err) } - // apiInterface files + // apiInterface file var ( genApi = apiFolder + filepath.FromSlash("/article/article.go") genApiExpect = apiFolder + filepath.FromSlash("/article/article_expect.go") @@ -79,7 +78,7 @@ func Test_Gen_Ctrl_Default(t *testing.T) { testPath + filepath.FromSlash("/article/article_v2_create.go"), testPath + filepath.FromSlash("/article/article_v2_update.go"), } - for i, _ := range files { + for i := range files { t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i])) } }) diff --git a/cmd/gf/internal/cmd/cmd_gen_dao_test.go b/cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go similarity index 100% rename from cmd/gf/internal/cmd/cmd_gen_dao_test.go rename to cmd/gf/internal/cmd/cmd_z_unit_gen_dao_test.go diff --git a/cmd/gf/internal/cmd/cmd_z_unit_gen_service_test.go b/cmd/gf/internal/cmd/cmd_z_unit_gen_service_test.go new file mode 100644 index 000000000..7f1f9c2fc --- /dev/null +++ b/cmd/gf/internal/cmd/cmd_z_unit_gen_service_test.go @@ -0,0 +1,72 @@ +// Copyright GoFrame gf Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package cmd + +import ( + "github.com/gogf/gf/cmd/gf/v2/internal/cmd/genservice" + "github.com/gogf/gf/v2/os/gfile" + "github.com/gogf/gf/v2/test/gtest" + "github.com/gogf/gf/v2/util/guid" + "github.com/gogf/gf/v2/util/gutil" + "path/filepath" + "testing" +) + +func Test_Gen_Service_Default(t *testing.T) { + gtest.C(t, func(t *gtest.T) { + var ( + path = gfile.Temp(guid.S()) + dstFolder = path + filepath.FromSlash("/service") + apiFolder = gtest.DataPath("genservice", "logic") + in = genservice.CGenServiceInput{ + SrcFolder: apiFolder, + DstFolder: dstFolder, + DstFileNameCase: "Snake", + WatchFile: "", + StPattern: "", + Packages: nil, + ImportPrefix: "", + Clear: false, + } + ) + err := gutil.FillStructWithDefault(&in) + t.AssertNil(err) + + err = gfile.Mkdir(path) + t.AssertNil(err) + defer gfile.Remove(path) + + _, err = genservice.CGenService{}.Service(ctx, in) + if err != nil { + panic(err) + } + + // logic file + var ( + genApi = apiFolder + filepath.FromSlash("/logic.go") + genApiExpect = apiFolder + filepath.FromSlash("/logic_expect.go") + ) + defer gfile.Remove(genApi) + t.Assert(gfile.GetContents(genApi), gfile.GetContents(genApiExpect)) + + // files + files, err := gfile.ScanDir(dstFolder, "*.go", true) + t.AssertNil(err) + t.Assert(files, []string{ + dstFolder + filepath.FromSlash("/article.go"), + }) + + // contents + testPath := gtest.DataPath("genservice", "service") + expectFiles := []string{ + testPath + filepath.FromSlash("/article.go"), + } + for i := range files { + t.Assert(gfile.GetContents(files[i]), gfile.GetContents(expectFiles[i])) + } + }) +} diff --git a/cmd/gf/internal/cmd/testdata/genservice/logic/article/article.go b/cmd/gf/internal/cmd/testdata/genservice/logic/article/article.go new file mode 100644 index 000000000..01d889a25 --- /dev/null +++ b/cmd/gf/internal/cmd/testdata/genservice/logic/article/article.go @@ -0,0 +1,33 @@ +// Copyright GoFrame Author(https://goframe.org). All Rights Reserved. +// +// This Source Code Form is subject to the terms of the MIT License. +// If a copy of the MIT was not distributed with this file, +// You can obtain one at https://github.com/gogf/gf. + +package article + +import ( + "context" + "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/service" +) + +type sArticle struct { +} + +func init() { + service.RegisterArticle(&sArticle{}) +} + +// Get article details +func (s *sArticle) Get(ctx context.Context, id uint) (info struct{}, err error) { + return struct{}{}, err +} + +// Create +/** + * create an article. + * @author oldme + */ +func (s *sArticle) Create(ctx context.Context, info struct{}) (id uint, err error) { + return id, err +} diff --git a/cmd/gf/internal/cmd/testdata/genservice/logic/logic_expect.go b/cmd/gf/internal/cmd/testdata/genservice/logic/logic_expect.go new file mode 100644 index 000000000..c7c3f52e9 --- /dev/null +++ b/cmd/gf/internal/cmd/testdata/genservice/logic/logic_expect.go @@ -0,0 +1,9 @@ +// ========================================================================== +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// ========================================================================== + +package logic + +import ( + _ "github.com/gogf/gf/cmd/gf/v2/internal/cmd/testdata/genservice/logic/article" +) diff --git a/cmd/gf/internal/cmd/testdata/genservice/service/article.go b/cmd/gf/internal/cmd/testdata/genservice/service/article.go new file mode 100644 index 000000000..765a1c8a5 --- /dev/null +++ b/cmd/gf/internal/cmd/testdata/genservice/service/article.go @@ -0,0 +1,38 @@ +// ================================================================================ +// Code generated and maintained by GoFrame CLI tool. DO NOT EDIT. +// You can delete these comments if you wish manually maintain this interface file. +// ================================================================================ + +package service + +import ( + "context" +) + +type ( + IArticle interface { + // Get article details + Get(ctx context.Context, id uint) (info struct{}, err error) + // Create + /** + * create an article. + * @author oldme + */ + Create(ctx context.Context, info struct{}) (id uint, err error) + } +) + +var ( + localArticle IArticle +) + +func Article() IArticle { + if localArticle == nil { + panic("implement not found for interface IArticle, forgot register?") + } + return localArticle +} + +func RegisterArticle(i IArticle) { + localArticle = i +}