JMS #4: Flag testing

main
Jeremy Saenz 11 years ago
parent 3f76896c4f
commit 3119b075d6

@ -21,8 +21,8 @@ var Action = ShowHelp
func Run(args []string) {
set := flagSet(Flags)
set.Parse(args[1:])
set := flagSet(Flags)
set.Parse(args[1:])
context := NewContext(set)
if len(args) > 1 {

@ -1,22 +1,39 @@
package cli
import (
"reflect"
"testing"
"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" })
func Test_SettingFlags(t *testing.T) {
Flags = []Flag{
StringFlag{"foo", "default", "a string flag"},
IntFlag{"bar", 42, "an int flag"},
BoolFlag{"bat", "a bool flag"},
}
Action = func(c *Context) {
expect(t, c.String("foo"), "hello world")
expect(t, c.Int("bar"), 245)
expect(t, c.Bool("bat"), true)
}
Run([]string{"command", "--foo", "hello world", "--bar", "245", "--bat"})
}
/* Helpers */
func Test_FlagDefaults(t *testing.T) {
Flags = []Flag{
StringFlag{"foo", "default", "a string flag"},
IntFlag{"bar", 42, "an int flag"},
BoolFlag{"bat", "a bool flag"},
}
Action = func(c *Context) {
expect(t, c.String("foo"), "default")
expect(t, c.Int("bar"), 42)
expect(t, c.Bool("bat"), false)
}
Run([]string{"command"})
}
/* Test 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))

@ -1,11 +1,11 @@
package cli
import (
"testing"
"testing"
)
func Test_True(t *testing.T) {
expect(t, true, true)
expect(t, true, true)
}
func Test_Run(t *testing.T) {

@ -1,36 +1,36 @@
package cli
import (
"flag"
"testing"
"flag"
"testing"
)
func Test_Int(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Int("myflag", 12, "doc")
c := NewContext(set)
expect(t, c.Int("myflag"), 12)
set := flag.NewFlagSet("test", 0)
set.Int("myflag", 12, "doc")
c := NewContext(set)
expect(t, c.Int("myflag"), 12)
}
func Test_String(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.String("myflag", "hello world", "doc")
c := NewContext(set)
expect(t, c.String("myflag"), "hello world")
set := flag.NewFlagSet("test", 0)
set.String("myflag", "hello world", "doc")
c := NewContext(set)
expect(t, c.String("myflag"), "hello world")
}
func Test_Bool(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc")
c := NewContext(set)
expect(t, c.Bool("myflag"), false)
set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc")
c := NewContext(set)
expect(t, c.Bool("myflag"), false)
}
func Test_Args(t *testing.T) {
set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc")
c := NewContext(set)
set.Parse([]string{"--myflag", "bat", "baz"})
expect(t, len(c.Args()), 2)
expect(t, c.Bool("myflag"), true)
set := flag.NewFlagSet("test", 0)
set.Bool("myflag", false, "doc")
c := NewContext(set)
set.Parse([]string{"--myflag", "bat", "baz"})
expect(t, len(c.Args()), 2)
expect(t, c.Bool("myflag"), true)
}

@ -4,41 +4,55 @@ import "fmt"
import "flag"
type Flag interface {
fmt.Stringer
Apply(*flag.FlagSet)
fmt.Stringer
Apply(*flag.FlagSet)
}
func flagSet(flags []Flag) *flag.FlagSet {
set := flag.NewFlagSet(Name, flag.ExitOnError)
for _, f := range flags {
f.Apply(set)
}
return set
}
type BoolFlag struct {
Name string
Usage string
Name string
Usage string
}
func (f BoolFlag) String() string {
return fmt.Sprintf("--%v\t%v", f.Name, f.Usage)
}
func (f BoolFlag) Apply(set *flag.FlagSet) {
set.Bool(f.Name, false, f.Usage)
}
type StringFlag struct {
Name string
Value string
Usage string
Name string
Value string
Usage string
}
func (f StringFlag) String() string {
return fmt.Sprintf("--%v 'string'\t%v", f.Name, f.Usage)
return fmt.Sprintf("--%v '%v'\t%v", f.Name, f.Value, f.Usage)
}
func (f StringFlag) Apply(set *flag.FlagSet) {
set.String(f.Name, f.Value, f.Usage)
set.String(f.Name, f.Value, f.Usage)
}
func (f BoolFlag) String() string {
return fmt.Sprintf("--%v\t%v", f.Name, f.Usage)
type IntFlag struct {
Name string
Value int
Usage string
}
func (f BoolFlag) Apply(set *flag.FlagSet) {
set.Bool(f.Name, false, f.Usage)
func (f IntFlag) String() string {
return fmt.Sprintf("--%v '%v'\t%v", f.Name, f.Value, f.Usage)
}
func flagSet(flags []Flag) *flag.FlagSet {
set := flag.NewFlagSet(Name, flag.ExitOnError)
for _, f := range flags {
f.Apply(set)
}
return set
func (f IntFlag) Apply(set *flag.FlagSet) {
set.Int(f.Name, f.Value, f.Usage)
}

Loading…
Cancel
Save