Files
gf/os/gcmd/gcmd_z_unit_parser_test.go

92 lines
2.6 KiB
Go
Raw Permalink Normal View History

2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-09-05 11:38:36 +08:00
//
// 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_test
import (
"os"
"testing"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/os/gcmd"
"github.com/gogf/gf/v2/test/gtest"
2019-09-05 11:38:36 +08:00
)
func Test_Parse(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2019-09-05 11:38:36 +08:00
os.Args = []string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"}
p, err := gcmd.Parse(map[string]bool{
"n, name": true,
"p, prefix": true,
"f,force": false,
"q,quiet": false,
})
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(len(p.GetArgAll()), 3)
t.Assert(p.GetArg(0), "gf")
t.Assert(p.GetArg(1), "remove")
t.Assert(p.GetArg(2), "path")
t.Assert(p.GetArg(2).String(), "path")
2020-03-19 22:56:12 +08:00
t.Assert(len(p.GetOptAll()), 8)
t.Assert(p.GetOpt("n"), "root")
t.Assert(p.GetOpt("name"), "root")
t.Assert(p.GetOpt("p"), "www")
t.Assert(p.GetOpt("prefix"), "www")
t.Assert(p.GetOpt("prefix").String(), "www")
2020-03-19 22:56:12 +08:00
2022-01-19 21:07:48 +08:00
t.Assert(p.GetOpt("n") != nil, true)
t.Assert(p.GetOpt("name") != nil, true)
t.Assert(p.GetOpt("p") != nil, true)
t.Assert(p.GetOpt("prefix") != nil, true)
t.Assert(p.GetOpt("f") != nil, true)
t.Assert(p.GetOpt("force") != nil, true)
t.Assert(p.GetOpt("q") != nil, true)
t.Assert(p.GetOpt("quiet") != nil, true)
t.Assert(p.GetOpt("none") != nil, false)
2022-02-27 21:00:23 +08:00
_, err = p.MarshalJSON()
t.AssertNil(err)
2019-09-05 11:38:36 +08:00
})
}
2022-01-19 21:07:48 +08:00
func Test_ParseArgs(t *testing.T) {
2020-03-19 22:56:12 +08:00
gtest.C(t, func(t *gtest.T) {
2022-01-19 21:07:48 +08:00
p, err := gcmd.ParseArgs(
2019-09-05 11:38:36 +08:00
[]string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"},
map[string]bool{
"n, name": true,
"p, prefix": true,
"f,force": false,
"q,quiet": false,
})
2022-03-10 11:36:40 +08:00
t.AssertNil(err)
2020-03-19 22:56:12 +08:00
t.Assert(len(p.GetArgAll()), 3)
t.Assert(p.GetArg(0), "gf")
t.Assert(p.GetArg(1), "remove")
t.Assert(p.GetArg(2), "path")
t.Assert(p.GetArg(2).String(), "path")
2020-03-19 22:56:12 +08:00
t.Assert(len(p.GetOptAll()), 8)
t.Assert(p.GetOpt("n"), "root")
t.Assert(p.GetOpt("name"), "root")
t.Assert(p.GetOpt("p"), "www")
t.Assert(p.GetOpt("prefix"), "www")
t.Assert(p.GetOpt("prefix").String(), "www")
2020-03-19 22:56:12 +08:00
2022-01-19 21:07:48 +08:00
t.Assert(p.GetOpt("n") != nil, true)
t.Assert(p.GetOpt("name") != nil, true)
t.Assert(p.GetOpt("p") != nil, true)
t.Assert(p.GetOpt("prefix") != nil, true)
t.Assert(p.GetOpt("f") != nil, true)
t.Assert(p.GetOpt("force") != nil, true)
t.Assert(p.GetOpt("q") != nil, true)
t.Assert(p.GetOpt("quiet") != nil, true)
t.Assert(p.GetOpt("none") != nil, false)
2019-09-05 11:38:36 +08:00
})
}