MCPcopy
hub / github.com/spf13/cobra / TestMatchAll

Function TestMatchAll

args_test.go:440–486  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

438}
439
440func TestMatchAll(t *testing.T) {
441 // Somewhat contrived example check that ensures there are exactly 3
442 // arguments, and each argument is exactly 2 bytes long.
443 pargs := MatchAll(
444 ExactArgs(3),
445 func(cmd *Command, args []string) error {
446 for _, arg := range args {
447 if len([]byte(arg)) != 2 {
448 return fmt.Errorf("expected to be exactly 2 bytes long")
449 }
450 }
451 return nil
452 },
453 )
454
455 testCases := map[string]struct {
456 args []string
457 fail bool
458 }{
459 "happy path": {
460 []string{"aa", "bb", "cc"},
461 false,
462 },
463 "incorrect number of args": {
464 []string{"aa", "bb", "cc", "dd"},
465 true,
466 },
467 "incorrect number of bytes in one arg": {
468 []string{"aa", "bb", "abc"},
469 true,
470 },
471 }
472
473 rootCmd := &Command{Use: "root", Args: pargs, Run: emptyRun}
474
475 for name, tc := range testCases {
476 t.Run(name, func(t *testing.T) {
477 _, err := executeCommand(rootCmd, tc.args...)
478 if err != nil && !tc.fail {
479 t.Errorf("unexpected: %v\n", err)
480 }
481 if err == nil && tc.fail {
482 t.Errorf("expected error")
483 }
484 })
485 }
486}
487
488// DEPRECATED
489

Callers

nothing calls this directly

Calls 3

MatchAllFunction · 0.85
ExactArgsFunction · 0.85
executeCommandFunction · 0.85

Tested by

no test coverage detected