aboutsummaryrefslogtreecommitdiff
path: root/flag_test.go
diff options
context:
space:
mode:
authorEric Paris <[email protected]>2017-05-05 09:40:53 -0400
committerAlbert Nigmatzianov <[email protected]>2017-05-05 16:36:56 +0200
commit80fe0fb4eba54167e2ccae1c6c950e72abf61b73 (patch)
tree43848e259c223b1891f60613f48a1cb29398b128 /flag_test.go
parent75859d1ee5f135362c9534f7db667e0150d9f577 (diff)
Testing for Shorthand Lookup
Diffstat (limited to 'flag_test.go')
-rw-r--r--flag_test.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/flag_test.go b/flag_test.go
index 8248277..c3def0f 100644
--- a/flag_test.go
+++ b/flag_test.go
@@ -446,6 +446,42 @@ func TestShorthand(t *testing.T) {
}
}
+func TestShorthandLookup(t *testing.T) {
+ f := NewFlagSet("shorthand", ContinueOnError)
+ if f.Parsed() {
+ t.Error("f.Parse() = true before Parse")
+ }
+ f.BoolP("boola", "a", false, "bool value")
+ f.BoolP("boolb", "b", false, "bool2 value")
+ args := []string{
+ "-ab",
+ }
+ f.SetOutput(ioutil.Discard)
+ if err := f.Parse(args); err != nil {
+ t.Error("expected no error, got ", err)
+ }
+ if !f.Parsed() {
+ t.Error("f.Parse() = false after Parse")
+ }
+ flag := f.ShorthandLookup("a")
+ if flag == nil {
+ t.Errorf("f.ShorthandLookup(\"a\") returned nil")
+ }
+ if flag.Name != "boola" {
+ t.Errorf("f.ShorthandLookup(\"a\") found %q instead of \"boola\"", flag.Name)
+ }
+ flag = f.ShorthandLookup("")
+ if flag != nil {
+ t.Errorf("f.ShorthandLookup(\"\") did not return nil")
+ }
+ defer func() {
+ recover()
+ }()
+ flag = f.ShorthandLookup("ab")
+ // should NEVER get here. lookup should panic. defer'd func should recover it.
+ t.Errorf("f.ShorthandLookup(\"ab\") did not panic")
+}
+
func TestParse(t *testing.T) {
ResetForTesting(func() { t.Error("bad parse") })
testParse(GetCommandLine(), t)