From 8acffd1186a34ad537c19f2ae190aa25e7f15611 Mon Sep 17 00:00:00 2001 From: huangqian Date: Sun, 27 Feb 2022 21:00:23 +0800 Subject: [PATCH] Improving gcmd Code Coverage --- os/gcmd/gcmd_parser.go | 2 +- os/gcmd/gcmd_z_example_test.go | 65 ++++++++++++++++++++++++++++++ os/gcmd/gcmd_z_unit_parser_test.go | 3 ++ 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/os/gcmd/gcmd_parser.go b/os/gcmd/gcmd_parser.go index 35aa4ae82..abbb6f251 100644 --- a/os/gcmd/gcmd_parser.go +++ b/os/gcmd/gcmd_parser.go @@ -200,7 +200,7 @@ func (p *Parser) GetOptAll() map[string]string { // GetArg returns the argument at `index` as gvar.Var. func (p *Parser) GetArg(index int, def ...string) *gvar.Var { - if index < len(p.parsedArgs) { + if index >= 0 && index < len(p.parsedArgs) { return gvar.New(p.parsedArgs[index]) } if len(def) > 0 { diff --git a/os/gcmd/gcmd_z_example_test.go b/os/gcmd/gcmd_z_example_test.go index 6b6447067..d5f661a41 100644 --- a/os/gcmd/gcmd_z_example_test.go +++ b/os/gcmd/gcmd_z_example_test.go @@ -55,6 +55,15 @@ func ExampleGetOpt() { // Opt["o"]: "gf.exe", Opt["y"]: "", Opt["d"]: "default value" } +func ExampleGetOpt_Def() { + gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y") + + fmt.Println(gcmd.GetOpt("s", "Def").String()) + + // Output: + // Def +} + func ExampleGetOptAll() { gcmd.Init("gf", "build", "main.go", "-o=gf.exe", "-y") fmt.Printf(`%#v`, gcmd.GetOptAll()) @@ -87,6 +96,7 @@ func ExampleParse() { fmt.Println(p.GetOpt("y") != nil) fmt.Println(p.GetOpt("yes") != nil) fmt.Println(p.GetOpt("none") != nil) + fmt.Println(p.GetOpt("none", "Def")) // Output: // gf.exe @@ -94,6 +104,7 @@ func ExampleParse() { // true // true // false + // Def } func ExampleCommandFromCtx() { @@ -204,3 +215,57 @@ func ExampleCommand_Print() { //COMMAND // start } + +func ExampleScan() { + fmt.Println(gcmd.Scan("gf scan")) + + // Output: + // gf scan +} + +func ExampleScanf() { + fmt.Println(gcmd.Scanf("gf %s", "scanf")) + + // Output: + // gf scanf +} + +func ExampleParserFromCtx() { + parser, _ := gcmd.Parse(nil) + + ctx := context.WithValue(gctx.New(), gcmd.CtxKeyParser, parser) + nilCtx := context.WithValue(gctx.New(), "NilCtxKeyParser", parser) + + fmt.Println(gcmd.ParserFromCtx(ctx).GetArgAll()) + fmt.Println(gcmd.ParserFromCtx(nilCtx) == nil) + + // Output: + // [gf build main.go] + // true +} + +func ExampleParseArgs() { + p, _ := gcmd.ParseArgs([]string{ + "gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root", + }, nil) + + fmt.Println(p.GetArgAll()) + fmt.Println(p.GetOptAll()) + + // Output: + // [gf path] + // map[force:remove fq: n:root p:www] +} + +func ExampleParser_GetArg() { + p, _ := gcmd.ParseArgs([]string{ + "gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root", + }, nil) + + fmt.Println(p.GetArg(-1, "Def").String()) + fmt.Println(p.GetArg(-1) == nil) + + // Output: + // Def + // true +} diff --git a/os/gcmd/gcmd_z_unit_parser_test.go b/os/gcmd/gcmd_z_unit_parser_test.go index 7fd45a02a..2b7699f68 100644 --- a/os/gcmd/gcmd_z_unit_parser_test.go +++ b/os/gcmd/gcmd_z_unit_parser_test.go @@ -48,6 +48,9 @@ func Test_Parse(t *testing.T) { t.Assert(p.GetOpt("q") != nil, true) t.Assert(p.GetOpt("quiet") != nil, true) t.Assert(p.GetOpt("none") != nil, false) + + _, err = p.MarshalJSON() + t.AssertNil(err) }) }