Add tests for required flags

This commit is contained in:
Jesse Howarth and Michael Ivey 2014-12-02 21:08:24 +00:00 committed by Some DATDevs
parent 73e64a14fd
commit 4b2fcdb1ad

60
required_flags_test.go Normal file
View File

@ -0,0 +1,60 @@
package cli
import (
"flag"
"testing"
)
func TestContext_CheckRequiredFlagsSuccess(t *testing.T) {
flags := []Flag{
StringFlag{
Name: "required",
Required: true,
},
StringFlag{
Name: "optional",
},
}
set := flag.NewFlagSet("test", 0)
for _, f := range flags {
f.Apply(set)
}
e := set.Parse([]string{"--required", "foo"})
if e != nil {
t.Errorf("Expected no error parsing but there was one: %s", e)
}
err := checkRequiredFlags(flags, set)
if err != nil {
t.Error("Expected flag parsing to be successful")
}
}
func TestContext_CheckRequiredFlagsFailure(t *testing.T) {
flags := []Flag{
StringFlag{
Name: "required",
Required: true,
},
StringFlag{
Name: "optional",
},
}
set := flag.NewFlagSet("test", 0)
for _, f := range flags {
f.Apply(set)
}
e := set.Parse([]string{"--optional", "foo"})
if e != nil {
t.Errorf("Expected no error parsing but there was one: %s", e)
}
err := checkRequiredFlags(flags, set)
if err == nil {
t.Error("Expected flag parsing to be unsuccessful")
}
}