github.com/wangyougui/gf/v2@v2.6.5/os/gcmd/gcmd_z_unit_feature_object1_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/wangyougui/gf. 6 7 // go test *.go -bench=".*" -benchmem 8 9 package gcmd_test 10 11 import ( 12 "context" 13 "os" 14 "testing" 15 16 "github.com/wangyougui/gf/v2/frame/g" 17 "github.com/wangyougui/gf/v2/os/gcmd" 18 "github.com/wangyougui/gf/v2/os/gctx" 19 "github.com/wangyougui/gf/v2/test/gtest" 20 ) 21 22 type TestCmdObject struct { 23 g.Meta `name:"root" usage:"root env/test" brief:"root env command" dc:"description" ad:"ad"` 24 } 25 26 type TestCmdObjectEnvInput struct { 27 g.Meta `name:"env" usage:"root env" brief:"root env command" dc:"root env command description" ad:"root env command ad"` 28 } 29 30 type TestCmdObjectEnvOutput struct{} 31 32 type TestCmdObjectTestInput struct { 33 g.Meta `name:"test" usage:"root test" brief:"root test command" dc:"root test command description" ad:"root test command ad"` 34 Name string `name:"yourname" v:"required" short:"n" orphan:"false" brief:"name for test command" d:"tom"` 35 Version bool `name:"version" short:"v" orphan:"true" brief:"show version"` 36 } 37 38 type TestCmdObjectTestOutput struct { 39 Name string 40 Version bool 41 } 42 43 func (TestCmdObject) Env(ctx context.Context, in TestCmdObjectEnvInput) (out *TestCmdObjectEnvOutput, err error) { 44 return 45 } 46 47 func (TestCmdObject) Test(ctx context.Context, in TestCmdObjectTestInput) (out *TestCmdObjectTestOutput, err error) { 48 out = &TestCmdObjectTestOutput{ 49 Name: in.Name, 50 Version: in.Version, 51 } 52 return 53 } 54 55 func Test_Command_NewFromObject_Help(t *testing.T) { 56 gtest.C(t, func(t *gtest.T) { 57 var ( 58 ctx = gctx.New() 59 cmd, err = gcmd.NewFromObject(&TestCmdObject{}) 60 ) 61 t.AssertNil(err) 62 t.Assert(cmd.Name, "root") 63 64 os.Args = []string{"root"} 65 value, err := cmd.RunWithValueError(ctx) 66 t.AssertNil(err) 67 t.Assert(value, nil) 68 }) 69 } 70 71 func Test_Command_NewFromObject_Run(t *testing.T) { 72 gtest.C(t, func(t *gtest.T) { 73 var ( 74 ctx = gctx.New() 75 cmd, err = gcmd.NewFromObject(&TestCmdObject{}) 76 ) 77 t.AssertNil(err) 78 t.Assert(cmd.Name, "root") 79 80 os.Args = []string{"root", "test", "-n=john"} 81 82 cmd.Run(ctx) 83 }) 84 } 85 86 func Test_Command_NewFromObject_RunWithValue(t *testing.T) { 87 gtest.C(t, func(t *gtest.T) { 88 var ( 89 ctx = gctx.New() 90 cmd, err = gcmd.NewFromObject(&TestCmdObject{}) 91 ) 92 t.AssertNil(err) 93 t.Assert(cmd.Name, "root") 94 95 // test short name 96 os.Args = []string{"root", "test", "-n=john"} 97 value, err := cmd.RunWithValueError(ctx) 98 t.AssertNil(err) 99 t.Assert(value, `{"Name":"john","Version":false}`) 100 101 // test name tag name 102 os.Args = []string{"root", "test", "-yourname=hailaz"} 103 value1, err1 := cmd.RunWithValueError(ctx) 104 t.AssertNil(err1) 105 t.Assert(value1, `{"Name":"hailaz","Version":false}`) 106 107 // test default tag value 108 os.Args = []string{"root", "test"} 109 value2, err2 := cmd.RunWithValueError(ctx) 110 t.AssertNil(err2) 111 t.Assert(value2, `{"Name":"tom","Version":false}`) 112 113 // test name tag and orphan tag true 114 os.Args = []string{"root", "test", "-v"} 115 value3, err3 := cmd.RunWithValueError(ctx) 116 t.AssertNil(err3) 117 t.Assert(value3, `{"Name":"tom","Version":true}`) 118 }) 119 } 120 121 func Test_Command_NewFromObject_RunWithSpecificArgs(t *testing.T) { 122 gtest.C(t, func(t *gtest.T) { 123 var ( 124 ctx = gctx.New() 125 cmd, err = gcmd.NewFromObject(&TestCmdObject{}) 126 ) 127 t.AssertNil(err) 128 t.Assert(cmd.Name, "root") 129 130 // test short name 131 args := []string{"root", "test", "-n=john"} 132 value, err := cmd.RunWithSpecificArgs(ctx, args) 133 t.AssertNil(err) 134 t.Assert(value, `{"Name":"john","Version":false}`) 135 136 // test name tag name 137 args = []string{"root", "test", "-yourname=hailaz"} 138 value1, err1 := cmd.RunWithSpecificArgs(ctx, args) 139 t.AssertNil(err1) 140 t.Assert(value1, `{"Name":"hailaz","Version":false}`) 141 142 // test default tag value 143 args = []string{"root", "test"} 144 value2, err2 := cmd.RunWithSpecificArgs(ctx, args) 145 t.AssertNil(err2) 146 t.Assert(value2, `{"Name":"tom","Version":false}`) 147 148 // test name tag and orphan tag true 149 args = []string{"root", "test", "-v"} 150 value3, err3 := cmd.RunWithSpecificArgs(ctx, args) 151 t.AssertNil(err3) 152 t.Assert(value3, `{"Name":"tom","Version":true}`) 153 154 // test empty args 155 value4, err4 := cmd.RunWithSpecificArgs(ctx, nil) 156 t.Assert(err4, "args can not be empty!") 157 t.Assert(value4, nil) 158 }) 159 } 160 161 func Test_Command_AddObject(t *testing.T) { 162 gtest.C(t, func(t *gtest.T) { 163 var ( 164 ctx = gctx.New() 165 command = gcmd.Command{ 166 Name: "start", 167 } 168 ) 169 err := command.AddObject(&TestCmdObject{}) 170 t.AssertNil(err) 171 172 os.Args = []string{"start", "root", "test", "-n=john"} 173 value, err := command.RunWithValueError(ctx) 174 t.AssertNil(err) 175 t.Assert(value, `{"Name":"john","Version":false}`) 176 }) 177 } 178 179 type TestObjectForRootTag struct { 180 g.Meta `name:"root" root:"root"` 181 } 182 183 type TestObjectForRootTagEnvInput struct { 184 g.Meta `name:"env" usage:"root env" brief:"root env command" dc:"root env command description" ad:"root env command ad"` 185 } 186 187 type TestObjectForRootTagEnvOutput struct{} 188 189 type TestObjectForRootTagTestInput struct { 190 g.Meta `name:"root"` 191 Name string `v:"required" short:"n" orphan:"false" brief:"name for test command"` 192 } 193 194 type TestObjectForRootTagTestOutput struct { 195 Content string 196 } 197 198 func (TestObjectForRootTag) Env(ctx context.Context, in TestObjectForRootTagEnvInput) (out *TestObjectForRootTagEnvOutput, err error) { 199 return 200 } 201 202 func (TestObjectForRootTag) Root(ctx context.Context, in TestObjectForRootTagTestInput) (out *TestObjectForRootTagTestOutput, err error) { 203 out = &TestObjectForRootTagTestOutput{ 204 Content: in.Name, 205 } 206 return 207 } 208 209 func Test_Command_RootTag(t *testing.T) { 210 gtest.C(t, func(t *gtest.T) { 211 var ( 212 ctx = gctx.New() 213 ) 214 cmd, err := gcmd.NewFromObject(TestObjectForRootTag{}) 215 t.AssertNil(err) 216 217 os.Args = []string{"root", "-n=john"} 218 value, err := cmd.RunWithValueError(ctx) 219 t.AssertNil(err) 220 t.Assert(value, `{"Content":"john"}`) 221 }) 222 // Pointer. 223 gtest.C(t, func(t *gtest.T) { 224 var ( 225 ctx = gctx.New() 226 ) 227 cmd, err := gcmd.NewFromObject(&TestObjectForRootTag{}) 228 t.AssertNil(err) 229 230 os.Args = []string{"root", "-n=john"} 231 value, err := cmd.RunWithValueError(ctx) 232 t.AssertNil(err) 233 t.Assert(value, `{"Content":"john"}`) 234 }) 235 } 236 237 type TestObjectForNeedArgs struct { 238 g.Meta `name:"root" root:"root"` 239 } 240 241 type TestObjectForNeedArgsEnvInput struct { 242 g.Meta `name:"env" usage:"root env" brief:"root env command" dc:"root env command description" ad:"root env command ad"` 243 } 244 245 type TestObjectForNeedArgsEnvOutput struct{} 246 247 type TestObjectForNeedArgsTestInput struct { 248 g.Meta `name:"test"` 249 Arg1 string `arg:"true" brief:"arg1 for test command"` 250 Arg2 string `arg:"true" brief:"arg2 for test command"` 251 Name string `v:"required" short:"n" orphan:"false" brief:"name for test command"` 252 } 253 254 type TestObjectForNeedArgsTestOutput struct { 255 Args []string 256 } 257 258 func (TestObjectForNeedArgs) Env(ctx context.Context, in TestObjectForNeedArgsEnvInput) (out *TestObjectForNeedArgsEnvOutput, err error) { 259 return 260 } 261 262 func (TestObjectForNeedArgs) Test(ctx context.Context, in TestObjectForNeedArgsTestInput) (out *TestObjectForNeedArgsTestOutput, err error) { 263 out = &TestObjectForNeedArgsTestOutput{ 264 Args: []string{in.Arg1, in.Arg2, in.Name}, 265 } 266 return 267 } 268 269 func Test_Command_NeedArgs(t *testing.T) { 270 gtest.C(t, func(t *gtest.T) { 271 var ( 272 ctx = gctx.New() 273 ) 274 cmd, err := gcmd.NewFromObject(TestObjectForNeedArgs{}) 275 t.AssertNil(err) 276 277 //os.Args = []string{"root", "test", "a", "b", "c", "-h"} 278 //value, err := cmd.RunWithValueError(ctx) 279 //t.AssertNil(err) 280 281 os.Args = []string{"root", "test", "a", "b", "c", "-n=john"} 282 value, err := cmd.RunWithValueError(ctx) 283 t.AssertNil(err) 284 t.Assert(value, `{"Args":["a","b","john"]}`) 285 }) 286 } 287 288 type TestObjectPointerTag struct { 289 g.Meta `name:"root" root:"root"` 290 } 291 292 type TestObjectPointerTagEnvInput struct { 293 g.Meta `name:"env" usage:"root env" brief:"root env command" dc:"root env command description" ad:"root env command ad"` 294 } 295 296 type TestObjectPointerTagEnvOutput struct{} 297 298 type TestObjectPointerTagTestInput struct { 299 g.Meta `name:"root"` 300 Name string `v:"required" short:"n" orphan:"false" brief:"name for test command"` 301 } 302 303 type TestObjectPointerTagTestOutput struct { 304 Content string 305 } 306 307 func (c *TestObjectPointerTag) Env(ctx context.Context, in TestObjectPointerTagEnvInput) (out *TestObjectPointerTagEnvOutput, err error) { 308 return 309 } 310 311 func (c *TestObjectPointerTag) Root(ctx context.Context, in TestObjectPointerTagTestInput) (out *TestObjectPointerTagTestOutput, err error) { 312 out = &TestObjectPointerTagTestOutput{ 313 Content: in.Name, 314 } 315 return 316 } 317 318 func Test_Command_Pointer(t *testing.T) { 319 gtest.C(t, func(t *gtest.T) { 320 var ( 321 ctx = gctx.New() 322 ) 323 cmd, err := gcmd.NewFromObject(TestObjectPointerTag{}) 324 t.AssertNil(err) 325 326 os.Args = []string{"root", "-n=john"} 327 value, err := cmd.RunWithValueError(ctx) 328 t.AssertNil(err) 329 t.Assert(value, `{"Content":"john"}`) 330 }) 331 332 gtest.C(t, func(t *gtest.T) { 333 var ( 334 ctx = gctx.New() 335 ) 336 cmd, err := gcmd.NewFromObject(&TestObjectPointerTag{}) 337 t.AssertNil(err) 338 339 os.Args = []string{"root", "-n=john"} 340 value, err := cmd.RunWithValueError(ctx) 341 t.AssertNil(err) 342 t.Assert(value, `{"Content":"john"}`) 343 }) 344 } 345 346 type TestCommandOrphan struct { 347 g.Meta `name:"root" root:"root"` 348 } 349 350 type TestCommandOrphanIndexInput struct { 351 g.Meta `name:"index"` 352 Orphan1 bool `short:"n1" orphan:"true"` 353 Orphan2 bool `short:"n2" orphan:"true"` 354 Orphan3 bool `short:"n3" orphan:"true"` 355 } 356 357 type TestCommandOrphanIndexOutput struct { 358 Orphan1 bool 359 Orphan2 bool 360 Orphan3 bool 361 } 362 363 func (c *TestCommandOrphan) Index(ctx context.Context, in TestCommandOrphanIndexInput) (out *TestCommandOrphanIndexOutput, err error) { 364 out = &TestCommandOrphanIndexOutput{ 365 Orphan1: in.Orphan1, 366 Orphan2: in.Orphan2, 367 Orphan3: in.Orphan3, 368 } 369 return 370 } 371 372 func Test_Command_Orphan_Parameter(t *testing.T) { 373 gtest.C(t, func(t *gtest.T) { 374 var ctx = gctx.New() 375 cmd, err := gcmd.NewFromObject(TestCommandOrphan{}) 376 t.AssertNil(err) 377 378 os.Args = []string{"root", "index", "-n1", "-n2=0", "-n3=1"} 379 value, err := cmd.RunWithValueError(ctx) 380 t.AssertNil(err) 381 t.Assert(value.(*TestCommandOrphanIndexOutput).Orphan1, true) 382 t.Assert(value.(*TestCommandOrphanIndexOutput).Orphan2, false) 383 t.Assert(value.(*TestCommandOrphanIndexOutput).Orphan3, true) 384 }) 385 }