github.com/zhongdalu/gf@v1.0.0/g/os/gcmd/gcmd_z_unit_test.go (about)

     1  // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). 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/zhongdalu/gf.
     6  
     7  // go test *.go -bench=".*" -benchmem
     8  
     9  package gcmd
    10  
    11  import (
    12  	"os"
    13  	"testing"
    14  
    15  	"github.com/zhongdalu/gf/g/test/gtest"
    16  )
    17  
    18  func Test_ValueAndOption(t *testing.T) {
    19  	os.Args = []string{"v1", "v2", "--o1=111", "-o2=222"}
    20  	doInit()
    21  	gtest.Case(t, func() {
    22  		gtest.Assert(Value.GetAll(), []string{"v1", "v2"})
    23  		gtest.Assert(Value.Get(0), "v1")
    24  		gtest.Assert(Value.Get(1), "v2")
    25  		gtest.Assert(Value.Get(2), "")
    26  		gtest.Assert(Value.Get(2, "1"), "1")
    27  		gtest.Assert(Value.GetVar(1, "1").String(), "v2")
    28  		gtest.Assert(Value.GetVar(2, "1").String(), "1")
    29  
    30  		gtest.Assert(Option.GetAll(), map[string]string{"o1": "111", "o2": "222"})
    31  		gtest.Assert(Option.Get("o1"), "111")
    32  		gtest.Assert(Option.Get("o2"), "222")
    33  		gtest.Assert(Option.Get("o3", "1"), "1")
    34  		gtest.Assert(Option.GetVar("o2", "1").String(), "222")
    35  		gtest.Assert(Option.GetVar("o3", "1").String(), "1")
    36  
    37  	})
    38  }
    39  
    40  func Test_Handle(t *testing.T) {
    41  	os.Args = []string{"gf", "gf"}
    42  	doInit()
    43  	gtest.Case(t, func() {
    44  		num := 1
    45  		BindHandle("gf", func() {
    46  			num += 1
    47  		})
    48  		RunHandle("gf")
    49  		gtest.AssertEQ(num, 2)
    50  		AutoRun()
    51  		gtest.AssertEQ(num, 3)
    52  	})
    53  }