JMS #4: TDD Our options map

main
Jeremy Saenz 11 years ago
parent e2733adc45
commit 5f85917dc2

@ -1,6 +1,5 @@
package cli
import "fmt"
import "os"
// The name of the program. Defaults to os.Args[0]

@ -0,0 +1,30 @@
package cli
type Options map[string] interface{}
func (o Options) Int(key string) int {
val := o[key]
if val != nil {
return val.(int)
} else {
return 0
}
}
func (o Options) String(key string) string {
val := o[key]
if val != nil {
return val.(string)
} else {
return ""
}
}
func (o Options) Bool(key string) bool {
val := o[key]
if val != nil {
return val.(bool)
} else {
return false
}
}

@ -0,0 +1,52 @@
package cli
import "testing"
import "reflect"
func expect(t *testing.T, a interface{}, b interface{}) {
if a != b {
t.Errorf("Expected %v (%v) - Got %v (%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 (%v) - Got %v (%v)", b, reflect.TypeOf(b), a, reflect.TypeOf(a))
}
}
func Test_Int(t *testing.T) {
opts := Options{
"foo": 1,
"bar": 2,
"bat": 3,
}
expect(t, opts.Int("foo"), 1)
expect(t, opts.Int("bar"), 2)
expect(t, opts.Int("bat"), 3)
refute(t, opts.Int("foo"), "1")
expect(t, opts.Int("nope"), 0)
}
func Test_String(t *testing.T) {
opts := Options{
"foo": "bar",
"bat": "baz",
}
expect(t, opts.String("foo"), "bar")
expect(t, opts.String("bat"), "baz")
expect(t, opts.String("nope"), "")
}
func Test_Bool(t *testing.T) {
opts := Options{
"foo": false,
"bar": true,
}
expect(t, opts.Bool("foo"), false)
expect(t, opts.Bool("bar"), true)
expect(t, opts.Bool("nope"), false)
}
Loading…
Cancel
Save