2013-07-20 22:53:25 +00:00
|
|
|
package cli_test
|
2013-07-18 00:00:02 +00:00
|
|
|
|
|
|
|
import (
|
2013-07-19 00:39:42 +00:00
|
|
|
"flag"
|
2013-07-20 22:53:25 +00:00
|
|
|
"github.com/codegangsta/cli"
|
2013-07-24 14:35:45 +00:00
|
|
|
"testing"
|
2013-07-18 00:00:02 +00:00
|
|
|
)
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestNewContext(t *testing.T) {
|
2013-07-19 01:00:52 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("myflag", 12, "doc")
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Int("myflag", 42, "doc")
|
2014-03-29 19:02:05 +00:00
|
|
|
command := cli.Command{Name: "mycommand"}
|
2013-07-20 22:53:25 +00:00
|
|
|
c := cli.NewContext(nil, set, globalSet)
|
2014-03-29 19:02:05 +00:00
|
|
|
c.Command = command
|
2013-07-19 01:00:52 +00:00
|
|
|
expect(t, c.Int("myflag"), 12)
|
|
|
|
expect(t, c.GlobalInt("myflag"), 42)
|
2014-03-29 19:02:05 +00:00
|
|
|
expect(t, c.Command.Name, "mycommand")
|
2013-07-19 01:00:52 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_Int(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("myflag", 12, "doc")
|
2013-07-20 22:53:25 +00:00
|
|
|
c := cli.NewContext(nil, set, set)
|
2013-07-19 00:39:42 +00:00
|
|
|
expect(t, c.Int("myflag"), 12)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_String(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String("myflag", "hello world", "doc")
|
2013-07-20 22:53:25 +00:00
|
|
|
c := cli.NewContext(nil, set, set)
|
2013-07-19 00:39:42 +00:00
|
|
|
expect(t, c.String("myflag"), "hello world")
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_Bool(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
2013-07-20 22:53:25 +00:00
|
|
|
c := cli.NewContext(nil, set, set)
|
2013-07-19 00:39:42 +00:00
|
|
|
expect(t, c.Bool("myflag"), false)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
|
|
|
|
2014-03-06 01:24:22 +00:00
|
|
|
func TestContext_BoolT(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", true, "doc")
|
|
|
|
c := cli.NewContext(nil, set, set)
|
|
|
|
expect(t, c.BoolT("myflag"), true)
|
|
|
|
}
|
|
|
|
|
2013-07-20 22:53:25 +00:00
|
|
|
func TestContext_Args(t *testing.T) {
|
2013-07-19 00:39:42 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
2013-07-20 22:53:25 +00:00
|
|
|
c := cli.NewContext(nil, set, set)
|
2013-07-19 00:39:42 +00:00
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
|
|
expect(t, len(c.Args()), 2)
|
|
|
|
expect(t, c.Bool("myflag"), true)
|
2013-07-18 00:00:02 +00:00
|
|
|
}
|
2013-12-18 18:09:16 +00:00
|
|
|
|
|
|
|
func TestContext_IsSet(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
set.String("otherflag", "hello world", "doc")
|
|
|
|
c := cli.NewContext(nil, set, set)
|
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
|
|
expect(t, c.IsSet("myflag"), true)
|
|
|
|
expect(t, c.IsSet("otherflag"), false)
|
|
|
|
expect(t, c.IsSet("bogusflag"), false)
|
|
|
|
}
|