github.com/wangyougui/gf/v2@v2.6.5/os/gcmd/gcmd_z_unit_feature_object3_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/wangyougui/gf.
     6  
     7  package gcmd_test
     8  
     9  import (
    10  	"context"
    11  	"os"
    12  	"testing"
    13  
    14  	"github.com/wangyougui/gf/v2/frame/g"
    15  	"github.com/wangyougui/gf/v2/os/gcmd"
    16  	"github.com/wangyougui/gf/v2/os/gctx"
    17  	"github.com/wangyougui/gf/v2/test/gtest"
    18  )
    19  
    20  type TestParamsCase struct {
    21  	g.Meta `name:"root" root:"root"`
    22  }
    23  
    24  type TestParamsCaseRootInput struct {
    25  	g.Meta `name:"root"`
    26  	Name   string
    27  }
    28  
    29  type TestParamsCaseRootOutput struct {
    30  	Content string
    31  }
    32  
    33  func (c *TestParamsCase) Root(ctx context.Context, in TestParamsCaseRootInput) (out *TestParamsCaseRootOutput, err error) {
    34  	out = &TestParamsCaseRootOutput{
    35  		Content: in.Name,
    36  	}
    37  	return
    38  }
    39  
    40  func Test_Command_ParamsCase(t *testing.T) {
    41  	gtest.C(t, func(t *gtest.T) {
    42  		var ctx = gctx.New()
    43  		cmd, err := gcmd.NewFromObject(TestParamsCase{})
    44  		t.AssertNil(err)
    45  
    46  		os.Args = []string{"root", "-name=john"}
    47  		value, err := cmd.RunWithValueError(ctx)
    48  		t.AssertNil(err)
    49  		t.Assert(value, `{"Content":"john"}`)
    50  	})
    51  }