mirror of
https://gitee.com/johng/gf
synced 2026-06-06 16:21:40 +08:00
add unit test cases for cmdenv/gcmd
This commit is contained in:
@ -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])
|
||||
|
||||
29
g/internal/cmdenv/cmdenv_test.go
Normal file
29
g/internal/cmdenv/cmdenv_test.go
Normal 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")
|
||||
})
|
||||
}
|
||||
|
||||
@ -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 {
|
||||
|
||||
28
g/os/gcmd/gcmd_z_unit_test.go
Normal file
28
g/os/gcmd/gcmd_z_unit_test.go
Normal 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), "")
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user