2021-11-22 11:23:46 +08:00
|
|
|
// 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.
|
|
|
|
|
|
|
|
// go test *.go -bench=".*" -benchmem
|
|
|
|
|
|
|
|
package gcmd_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-11-23 20:26:55 +08:00
|
|
|
"os"
|
2021-11-22 11:23:46 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
|
|
"github.com/gogf/gf/v2/os/gcmd"
|
|
|
|
"github.com/gogf/gf/v2/os/gctx"
|
|
|
|
"github.com/gogf/gf/v2/test/gtest"
|
|
|
|
)
|
|
|
|
|
|
|
|
type TestCmdObject struct{}
|
|
|
|
|
|
|
|
type TestCmdObjectInput struct {
|
2021-11-23 20:26:55 +08:00
|
|
|
g.Meta `root:"true" name:"root" usage:"root env/test" brief:"root env command" dc:"description" ad:"ad"`
|
2021-11-22 11:23:46 +08:00
|
|
|
}
|
|
|
|
type TestCmdObjectOutput struct{}
|
|
|
|
|
|
|
|
type TestCmdObjectEnvInput struct {
|
2021-11-23 20:26:55 +08:00
|
|
|
g.Meta `name:"env" usage:"root env" brief:"root env command" dc:"root env command description" ad:"root env command ad"`
|
2021-11-22 11:23:46 +08:00
|
|
|
}
|
|
|
|
type TestCmdObjectEnvOutput struct{}
|
|
|
|
|
|
|
|
type TestCmdObjectTestInput struct {
|
2021-11-23 20:26:55 +08:00
|
|
|
g.Meta `name:"test" usage:"root test" brief:"root test command" dc:"root test command description" ad:"root test command ad"`
|
|
|
|
Name string `v:"required" short:"n" orphan:"false" brief:"name for test command"`
|
|
|
|
}
|
|
|
|
type TestCmdObjectTestOutput struct {
|
|
|
|
Content string
|
2021-11-22 11:23:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (TestCmdObject) Root(ctx context.Context, in TestCmdObjectInput) (out *TestCmdObjectOutput, err error) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (TestCmdObject) Env(ctx context.Context, in TestCmdObjectEnvInput) (out *TestCmdObjectEnvOutput, err error) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (TestCmdObject) Test(ctx context.Context, in TestCmdObjectTestInput) (out *TestCmdObjectTestOutput, err error) {
|
2021-11-23 20:26:55 +08:00
|
|
|
out = &TestCmdObjectTestOutput{
|
|
|
|
Content: in.Name,
|
|
|
|
}
|
2021-11-22 11:23:46 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-23 20:26:55 +08:00
|
|
|
func Test_Command_NewFromObject(t *testing.T) {
|
2021-11-22 11:23:46 +08:00
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var (
|
2021-11-23 20:26:55 +08:00
|
|
|
ctx = gctx.New()
|
|
|
|
cmd, err = gcmd.NewFromObject(&TestCmdObject{})
|
2021-11-22 11:23:46 +08:00
|
|
|
)
|
2021-11-23 20:26:55 +08:00
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(cmd.Name, "root")
|
|
|
|
|
|
|
|
os.Args = []string{"root", "test", "-n=john"}
|
|
|
|
value, err := cmd.RunWithValue(ctx)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(value, `{"Content":"john"}`)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_Command_AddObject(t *testing.T) {
|
|
|
|
gtest.C(t, func(t *gtest.T) {
|
|
|
|
var (
|
|
|
|
ctx = gctx.New()
|
|
|
|
command = gcmd.Command{
|
|
|
|
Name: "start",
|
|
|
|
}
|
2021-11-22 11:23:46 +08:00
|
|
|
)
|
2021-11-23 20:26:55 +08:00
|
|
|
err := command.AddObject(&TestCmdObject{})
|
|
|
|
t.AssertNil(err)
|
2021-11-22 11:23:46 +08:00
|
|
|
|
2021-11-23 20:26:55 +08:00
|
|
|
os.Args = []string{"start", "root", "test", "-n=john"}
|
|
|
|
value, err := command.RunWithValue(ctx)
|
|
|
|
t.AssertNil(err)
|
|
|
|
t.Assert(value, `{"Content":"john"}`)
|
2021-11-22 11:23:46 +08:00
|
|
|
})
|
|
|
|
}
|