command_test.go
This file
Path
command_test.go
90 lines · 1.8 KiB · Go
Last changed by
Add test for persistent bool flag on subcommandJeff Lowdermilkbefore adoption79bd93d
History of this fileTake it
1package cobra23import (4 "reflect"5 "testing"6)78func TestStripFlags(t *testing.T) {9 tests := []struct {10 input []string11 output []string12 }{13 {14 []string{"foo", "bar"},15 []string{"foo", "bar"},16 },17 {18 []string{"foo", "--bar", "-b"},19 []string{"foo"},20 },21 {22 []string{"-b", "foo", "--bar", "bar"},23 []string{},24 },25 {26 []string{"-i10", "echo"},27 []string{"echo"},28 },29 {30 []string{"-i=10", "echo"},31 []string{"echo"},32 },33 {34 []string{"--int=100", "echo"},35 []string{"echo"},36 },37 {38 []string{"-ib", "echo", "-bfoo", "baz"},39 []string{"echo", "baz"},40 },41 {42 []string{"-i=baz", "bar", "-i", "foo", "blah"},43 []string{"bar", "blah"},44 },45 {46 []string{"--int=baz", "-bbar", "-i", "foo", "blah"},47 []string{"blah"},48 },49 {50 []string{"--cat", "bar", "-i", "foo", "blah"},51 []string{"bar", "blah"},52 },53 {54 []string{"-c", "bar", "-i", "foo", "blah"},55 []string{"bar", "blah"},56 },57 {58 []string{"--persist", "bar"},59 []string{"bar"},60 },61 {62 []string{"-p", "bar"},63 []string{"bar"},64 },65 }6667 cmdPrint := &Command{68 Use: "print [string to print]",69 Short: "Print anything to the screen",70 Long: `an utterly useless command for testing.`,71 Run: func(cmd *Command, args []string) {72 tp = args73 },74 }7576 var flagi int77 var flagstr string78 var flagbool bool79 cmdPrint.PersistentFlags().BoolVarP(&flagbool, "persist", "p", false, "help for persistent one")80 cmdPrint.Flags().IntVarP(&flagi, "int", "i", 345, "help message for flag int")81 cmdPrint.Flags().StringVarP(&flagstr, "bar", "b", "bar", "help message for flag string")82 cmdPrint.Flags().BoolVarP(&flagbool, "cat", "c", false, "help message for flag bool")8384 for _, test := range tests {85 output := stripFlags(test.input, cmdPrint)86 if !reflect.DeepEqual(test.output, output) {87 t.Errorf("expected: %v, got: %v", test.output, output)88 }89 }90}