JMS #4: Getting some cli tests going. Testing out flags

This commit is contained in:
Jeremy Saenz
2013-07-18 17:29:06 -07:00
parent 04496e4364
commit 3f76896c4f
7 changed files with 64 additions and 38 deletions

View File

@@ -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)
}