github.com/gogf/gf@v1.16.9/os/gcmd/gcmd_z_unit_parser_test.go (about) 1 // Copyright GoFrame Author(https://goframe.org). 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/gogf/gf. 6 7 // go test *.go -bench=".*" -benchmem 8 9 package gcmd_test 10 11 import ( 12 "os" 13 "testing" 14 15 "github.com/gogf/gf/container/garray" 16 17 "github.com/gogf/gf/os/gcmd" 18 19 "github.com/gogf/gf/test/gtest" 20 ) 21 22 func Test_Parse(t *testing.T) { 23 gtest.C(t, func(t *gtest.T) { 24 os.Args = []string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"} 25 p, err := gcmd.Parse(map[string]bool{ 26 "n, name": true, 27 "p, prefix": true, 28 "f,force": false, 29 "q,quiet": false, 30 }) 31 t.Assert(err, nil) 32 t.Assert(len(p.GetArgAll()), 3) 33 t.Assert(p.GetArg(0), "gf") 34 t.Assert(p.GetArg(1), "remove") 35 t.Assert(p.GetArg(2), "path") 36 t.Assert(p.GetArgVar(2).String(), "path") 37 38 t.Assert(len(p.GetOptAll()), 8) 39 t.Assert(p.GetOpt("n"), "root") 40 t.Assert(p.GetOpt("name"), "root") 41 t.Assert(p.GetOpt("p"), "www") 42 t.Assert(p.GetOpt("prefix"), "www") 43 t.Assert(p.GetOptVar("prefix").String(), "www") 44 45 t.Assert(p.ContainsOpt("n"), true) 46 t.Assert(p.ContainsOpt("name"), true) 47 t.Assert(p.ContainsOpt("p"), true) 48 t.Assert(p.ContainsOpt("prefix"), true) 49 t.Assert(p.ContainsOpt("f"), true) 50 t.Assert(p.ContainsOpt("force"), true) 51 t.Assert(p.ContainsOpt("q"), true) 52 t.Assert(p.ContainsOpt("quiet"), true) 53 t.Assert(p.ContainsOpt("none"), false) 54 }) 55 } 56 57 func Test_ParseWithArgs(t *testing.T) { 58 gtest.C(t, func(t *gtest.T) { 59 p, err := gcmd.ParseWithArgs( 60 []string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"}, 61 map[string]bool{ 62 "n, name": true, 63 "p, prefix": true, 64 "f,force": false, 65 "q,quiet": false, 66 }) 67 t.Assert(err, nil) 68 t.Assert(len(p.GetArgAll()), 3) 69 t.Assert(p.GetArg(0), "gf") 70 t.Assert(p.GetArg(1), "remove") 71 t.Assert(p.GetArg(2), "path") 72 t.Assert(p.GetArgVar(2).String(), "path") 73 74 t.Assert(len(p.GetOptAll()), 8) 75 t.Assert(p.GetOpt("n"), "root") 76 t.Assert(p.GetOpt("name"), "root") 77 t.Assert(p.GetOpt("p"), "www") 78 t.Assert(p.GetOpt("prefix"), "www") 79 t.Assert(p.GetOptVar("prefix").String(), "www") 80 81 t.Assert(p.ContainsOpt("n"), true) 82 t.Assert(p.ContainsOpt("name"), true) 83 t.Assert(p.ContainsOpt("p"), true) 84 t.Assert(p.ContainsOpt("prefix"), true) 85 t.Assert(p.ContainsOpt("f"), true) 86 t.Assert(p.ContainsOpt("force"), true) 87 t.Assert(p.ContainsOpt("q"), true) 88 t.Assert(p.ContainsOpt("quiet"), true) 89 t.Assert(p.ContainsOpt("none"), false) 90 }) 91 } 92 93 func Test_Handler(t *testing.T) { 94 gtest.C(t, func(t *gtest.T) { 95 p, err := gcmd.ParseWithArgs( 96 []string{"gf", "--force", "remove", "-fq", "-p=www", "path", "-n", "root"}, 97 map[string]bool{ 98 "n, name": true, 99 "p, prefix": true, 100 "f,force": false, 101 "q,quiet": false, 102 }) 103 t.Assert(err, nil) 104 array := garray.New() 105 err = p.BindHandle("remove", func() { 106 array.Append(1) 107 }) 108 t.Assert(err, nil) 109 110 err = p.BindHandle("remove", func() { 111 array.Append(1) 112 }) 113 t.AssertNE(err, nil) 114 115 err = p.BindHandle("test", func() { 116 array.Append(1) 117 }) 118 t.Assert(err, nil) 119 120 err = p.RunHandle("remove") 121 t.Assert(err, nil) 122 t.Assert(array.Len(), 1) 123 124 err = p.RunHandle("none") 125 t.AssertNE(err, nil) 126 t.Assert(array.Len(), 1) 127 128 err = p.RunHandle("test") 129 t.Assert(err, nil) 130 t.Assert(array.Len(), 2) 131 132 err = p.AutoRun() 133 t.Assert(err, nil) 134 t.Assert(array.Len(), 3) 135 }) 136 }