From 3f76896c4f02d0f9fe83200b855304a47ee63128 Mon Sep 17 00:00:00 2001 From: Jeremy Saenz Date: Thu, 18 Jul 2013 17:29:06 -0700 Subject: [PATCH] JMS #4: Getting some cli tests going. Testing out flags --- cli.go | 15 +-------------- cli_test.go | 30 ++++++++++++++++++++++++++++++ command.go | 10 ++++++++++ command_test.go | 12 ++++++++++++ context.go | 6 +++--- context_test.go | 27 +++++++-------------------- flag.go | 2 +- 7 files changed, 64 insertions(+), 38 deletions(-) create mode 100644 cli_test.go create mode 100644 command.go create mode 100644 command_test.go diff --git a/cli.go b/cli.go index e925872..049b471 100644 --- a/cli.go +++ b/cli.go @@ -22,11 +22,7 @@ var Action = ShowHelp func Run(args []string) { set := flagSet(Flags) - err := set.Parse(args[1:]) - if err != nil { - println(err) - return - } + set.Parse(args[1:]) context := NewContext(set) if len(args) > 1 { @@ -43,13 +39,4 @@ func Run(args []string) { Action(context) } -type Command struct { - Name string - ShortName string - Usage string - Description string - Action Handler - Flags []Flag -} - type Handler func(context *Context) diff --git a/cli_test.go b/cli_test.go new file mode 100644 index 0000000..2309a5f --- /dev/null +++ b/cli_test.go @@ -0,0 +1,30 @@ +package cli + +import ( + "reflect" + "testing" +) + +func Test_SimpleCLIFlags(t *testing.T) { + Flags = []Flag{ + StringFlag{"foo", "default", "a foo flag"}, + } + Action = func(c *Context) { + expect(t, c.String("foo"), "hello world") + } + Run([]string{ "command", "--foo", "hello world" }) +} + +/* Helpers */ + +func expect(t *testing.T, a interface{}, b interface{}) { + if a != b { + t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) + } +} + +func refute(t *testing.T, a interface{}, b interface{}) { + if a == b { + t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) + } +} diff --git a/command.go b/command.go new file mode 100644 index 0000000..f790515 --- /dev/null +++ b/command.go @@ -0,0 +1,10 @@ +package cli + +type Command struct { + Name string + ShortName string + Usage string + Description string + Action Handler + Flags []Flag +} diff --git a/command_test.go b/command_test.go new file mode 100644 index 0000000..81c75c0 --- /dev/null +++ b/command_test.go @@ -0,0 +1,12 @@ +package cli + +import ( + "testing" +) + +func Test_True(t *testing.T) { + expect(t, true, true) +} + +func Test_Run(t *testing.T) { +} diff --git a/context.go b/context.go index cbfe2f1..2f94568 100644 --- a/context.go +++ b/context.go @@ -17,7 +17,7 @@ func NewContext(flagSet *flag.FlagSet) *Context { return &Context{flagSet} } -func (c *Context) IntFlag(name string) int { +func (c *Context) Int(name string) int { flag := c.flagSet.Lookup(name) if flag != nil { val, err := strconv.Atoi(flag.Value.String()) @@ -30,7 +30,7 @@ func (c *Context) IntFlag(name string) int { } } -func (c *Context) BoolFlag(name string) bool { +func (c *Context) Bool(name string) bool { flag := c.flagSet.Lookup(name) if flag != nil { val, err := strconv.ParseBool(flag.Value.String()) @@ -43,7 +43,7 @@ func (c *Context) BoolFlag(name string) bool { } } -func (c *Context) StringFlag(name string) string { +func (c *Context) String(name string) string { flag := c.flagSet.Lookup(name) if flag != nil { return flag.Value.String() diff --git a/context_test.go b/context_test.go index 1ce6a35..7df9793 100644 --- a/context_test.go +++ b/context_test.go @@ -2,41 +2,28 @@ package cli import ( "flag" - "reflect" "testing" ) -func expect(t *testing.T, a interface{}, b interface{}) { - if a != b { - t.Errorf("Expected %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) - } -} - -func refute(t *testing.T, a interface{}, b interface{}) { - if a == b { - t.Errorf("Did not expect %v (type %v) - Got %v (type %v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a)) - } -} - -func Test_IntFlag(t *testing.T) { +func Test_Int(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Int("myflag", 12, "doc") c := NewContext(set) - expect(t, c.IntFlag("myflag"), 12) + expect(t, c.Int("myflag"), 12) } -func Test_StringFlag(t *testing.T) { +func Test_String(t *testing.T) { set := flag.NewFlagSet("test", 0) set.String("myflag", "hello world", "doc") c := NewContext(set) - expect(t, c.StringFlag("myflag"), "hello world") + expect(t, c.String("myflag"), "hello world") } -func Test_BoolFlag(t *testing.T) { +func Test_Bool(t *testing.T) { set := flag.NewFlagSet("test", 0) set.Bool("myflag", false, "doc") c := NewContext(set) - expect(t, c.BoolFlag("myflag"), false) + expect(t, c.Bool("myflag"), false) } func Test_Args(t *testing.T) { @@ -45,5 +32,5 @@ func Test_Args(t *testing.T) { c := NewContext(set) set.Parse([]string{"--myflag", "bat", "baz"}) expect(t, len(c.Args()), 2) - expect(t, c.BoolFlag("myflag"), true) + expect(t, c.Bool("myflag"), true) } diff --git a/flag.go b/flag.go index 28430a6..528f9d1 100644 --- a/flag.go +++ b/flag.go @@ -36,7 +36,7 @@ func (f BoolFlag) Apply(set *flag.FlagSet) { } func flagSet(flags []Flag) *flag.FlagSet { - set := flag.NewFlagSet(Name, 0) + set := flag.NewFlagSet(Name, flag.ExitOnError) for _, f := range flags { f.Apply(set) }