github.com/gogf/gf/v2@v2.7.4/os/gcmd/gcmd_z_unit_feature_object4_test.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gcmd_test
     8  
     9  import (
    10  	"context"
    11  	"os"
    12  	"testing"
    13  
    14  	"github.com/gogf/gf/v2/frame/g"
    15  	"github.com/gogf/gf/v2/os/gcmd"
    16  	"github.com/gogf/gf/v2/os/gctx"
    17  	"github.com/gogf/gf/v2/test/gtest"
    18  )
    19  
    20  type TestNoNameTagCase struct {
    21  	g.Meta `name:"root"`
    22  }
    23  
    24  type TestNoNameTagCaseRootInput struct {
    25  	Name string
    26  }
    27  
    28  type TestNoNameTagCaseRootOutput struct {
    29  	Content string
    30  }
    31  
    32  func (c *TestNoNameTagCase) TEST(ctx context.Context, in TestNoNameTagCaseRootInput) (out *TestNoNameTagCaseRootOutput, err error) {
    33  	out = &TestNoNameTagCaseRootOutput{
    34  		Content: in.Name,
    35  	}
    36  	return
    37  }
    38  
    39  func Test_Command_NoNameTagCase(t *testing.T) {
    40  	gtest.C(t, func(t *gtest.T) {
    41  		var ctx = gctx.New()
    42  		cmd, err := gcmd.NewFromObject(TestNoNameTagCase{})
    43  		t.AssertNil(err)
    44  
    45  		os.Args = []string{"root", "TEST", "-name=john"}
    46  		value, err := cmd.RunWithValueError(ctx)
    47  		t.AssertNil(err)
    48  		t.Assert(value, `{"Content":"john"}`)
    49  	})
    50  }