add unit test cases for cmdenv/gcmd

This commit is contained in:
John
2019-06-13 22:58:58 +08:00
parent d068c1418e
commit 46c42ec249
4 changed files with 72 additions and 4 deletions

View File

@ -19,7 +19,12 @@ var (
cmdOptions = make(map[string]string)
)
func init() {
func init() {
doInit()
}
// doInit does the initialization for this package.
func doInit() {
reg := regexp.MustCompile(`\-\-{0,1}(.+?)=(.+)`)
for i := 0; i < len(os.Args); i++ {
result := reg.FindStringSubmatch(os.Args[i])

View File

@ -0,0 +1,29 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). 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 cmdenv
import (
"github.com/gogf/gf/g/test/gtest"
"os"
"testing"
)
func Test_Get(t *testing.T) {
os.Args = []string{"--gf.test.value1=111"}
os.Setenv("GF_TEST_VALUE1", "222")
os.Setenv("GF_TEST_VALUE2", "333")
doInit()
gtest.Case(t, func() {
gtest.Assert(Get("gf.test.value1").String(), "111")
gtest.Assert(Get("gf.test.value2").String(), "333")
gtest.Assert(Get("gf.test.value3").String(), "")
gtest.Assert(Get("gf.test.value3", 1).String(), "1")
})
}

View File

@ -24,13 +24,19 @@ type gCmdOption struct {
options map[string]string
}
var Value = &gCmdValue{} // Console values.
var Option = &gCmdOption{} // Console options.
var Value = &gCmdValue{} // Console values.
var Option = &gCmdOption{} // Console options.
var cmdFuncMap = make(map[string]func()) // Registered callback functions.
func init() {
reg := regexp.MustCompile(`\-\-{0,1}(.+?)=(.+)`)
doInit()
}
// doInit does the initialization for this package.
func doInit() {
Value.values = Value.values[:0]
Option.options = make(map[string]string)
reg := regexp.MustCompile(`\-\-{0,1}(.+?)=(.+)`)
for i := 0; i < len(os.Args); i++ {
result := reg.FindStringSubmatch(os.Args[i])
if len(result) > 1 {

View File

@ -0,0 +1,28 @@
// Copyright 2017 gf Author(https://github.com/gogf/gf). 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
import (
"github.com/gogf/gf/g/test/gtest"
"os"
"testing"
)
func Test_ValueAndOption(t *testing.T) {
os.Args = []string{"v1", "v2", "--o1=111", "-o2=222"}
doInit()
gtest.Case(t, func() {
gtest.Assert(Value.GetAll(), []string{"v1", "v2"})
gtest.Assert(Value.Get(0), "v1")
gtest.Assert(Value.Get(1), "v2")
gtest.Assert(Value.Get(2), "")
})
}