Testing when given missing flag

This commit is contained in:
spf13 2013-09-04 11:27:10 -04:00
parent ccbe1b2359
commit 3f47f33e39
1 changed files with 21 additions and 10 deletions

View File

@ -2,10 +2,13 @@ package cobra_test
import ( import (
. "cobra" . "cobra"
"fmt"
"strings" "strings"
"testing" "testing"
) )
var _ = fmt.Println
var tp, te, tt, t1 []string var tp, te, tt, t1 []string
var flagb1, flagb2, flagb3 bool var flagb1, flagb2, flagb3 bool
var flags1, flags2, flags3 string var flags1, flags2, flags3 string
@ -168,7 +171,7 @@ func TestFlagShort(t *testing.T) {
t.Errorf("int flag didn't get correct value, had %d", flagi3) t.Errorf("int flag didn't get correct value, had %d", flagi3)
} }
if flagi1 != 123 { if flagi1 != 123 {
t.Errorf("default flag value changed on different comamnd with same shortname, 234 expected, %d given", flagi2) t.Errorf("default flag value changed on different command with same shortname, 234 expected, %d given", flagi2)
} }
} }
@ -183,22 +186,30 @@ func TestChildCommandFlags(t *testing.T) {
t.Errorf("flags didn't leave proper args remaining..%s given", tt) t.Errorf("flags didn't leave proper args remaining..%s given", tt)
} }
//c = initialize() // Testing with flag that shouldn't be persistent
//cmdEcho.AddCommand(cmdTimes) c = initialize()
//c.AddCommand(cmdPrint, cmdEcho) cmdEcho.AddCommand(cmdTimes)
//c.SetArgs(strings.Split("echo times -j 99 -i 77 one two", " ")) c.AddCommand(cmdPrint, cmdEcho)
//c.Execute() c.SetArgs(strings.Split("echo times -j 99 -i77 one two", " "))
e := c.Execute()
//if strings.Join(tt, " ") != "one two" { if e == nil {
//t.Errorf("flags didn't leave proper args remaining..%s given", tt) t.Errorf("invalid flag should generate error")
//} }
if flagi2 != 99 {
t.Errorf("flag value should be 99, %d given", flagi2)
}
if flagi1 != 123 {
t.Errorf("unset flag should have default value, expecting 123, given %d", flagi1)
}
} }
func TestPersistentFlags(t *testing.T) { func TestPersistentFlags(t *testing.T) {
c := initialize() c := initialize()
cmdEcho.AddCommand(cmdTimes) cmdEcho.AddCommand(cmdTimes)
c.AddCommand(cmdPrint, cmdEcho) c.AddCommand(cmdPrint, cmdEcho)
flagInit()
c.SetArgs(strings.Split("echo -s something more here", " ")) c.SetArgs(strings.Split("echo -s something more here", " "))
c.Execute() c.Execute()