|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"sort"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewContext(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("myflag", 12, "doc")
|
|
|
|
set.Float64("myflag64", float64(17), "doc")
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Int("myflag", 42, "doc")
|
|
|
|
globalSet.Float64("myflag64", float64(47), "doc")
|
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
command := Command{Name: "mycommand"}
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
|
|
|
c.Command = command
|
|
|
|
expect(t, c.Int("myflag"), 12)
|
|
|
|
expect(t, c.Float64("myflag64"), float64(17))
|
|
|
|
expect(t, c.Command.Name, "mycommand")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Int(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("myflag", 12, "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
expect(t, c.Int("myflag"), 12)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Float64(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Float64("myflag", float64(17), "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
expect(t, c.Float64("myflag"), float64(17))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Duration(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Duration("myflag", time.Duration(12*time.Second), "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
expect(t, c.Duration("myflag"), time.Duration(12*time.Second))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_String(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String("myflag", "hello world", "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
expect(t, c.String("myflag"), "hello world")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Bool(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
expect(t, c.Bool("myflag"), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_BoolT(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", true, "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
expect(t, c.BoolT("myflag"), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Args(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
|
|
expect(t, len(c.Args()), 2)
|
|
|
|
expect(t, c.Bool("myflag"), true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_NArg(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
|
|
expect(t, c.NArg(), 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_IsSet(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
set.String("otherflag", "hello world", "doc")
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Bool("myflagGlobal", true, "doc")
|
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
|
|
|
set.Parse([]string{"--myflag", "bat", "baz"})
|
|
|
|
globalSet.Parse([]string{"--myflagGlobal", "bat", "baz"})
|
|
|
|
expect(t, c.IsSet("myflag"), true)
|
|
|
|
expect(t, c.IsSet("otherflag"), false)
|
|
|
|
expect(t, c.IsSet("bogusflag"), false)
|
|
|
|
expect(t, c.IsSet("myflagGlobal"), false)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_NumFlags(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("myflag", false, "doc")
|
|
|
|
set.String("otherflag", "hello world", "doc")
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Bool("myflagGlobal", true, "doc")
|
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
|
|
|
set.Parse([]string{"--myflag", "--otherflag=foo"})
|
|
|
|
globalSet.Parse([]string{"--myflagGlobal"})
|
|
|
|
expect(t, c.NumFlags(), 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_Set(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Int("int", 5, "an int")
|
|
|
|
c := NewContext(nil, set, nil)
|
|
|
|
|
|
|
|
c.Set("int", "1")
|
|
|
|
expect(t, c.Int("int"), 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_LocalFlagNames(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("one-flag", false, "doc")
|
|
|
|
set.String("two-flag", "hello world", "doc")
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Bool("top-flag", true, "doc")
|
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
|
|
|
set.Parse([]string{"--one-flag", "--two-flag=foo"})
|
|
|
|
globalSet.Parse([]string{"--top-flag"})
|
|
|
|
|
|
|
|
actualFlags := c.LocalFlagNames()
|
|
|
|
sort.Strings(actualFlags)
|
|
|
|
|
|
|
|
expect(t, actualFlags, []string{"one-flag", "two-flag"})
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestContext_FlagNames(t *testing.T) {
|
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.Bool("one-flag", false, "doc")
|
|
|
|
set.String("two-flag", "hello world", "doc")
|
|
|
|
globalSet := flag.NewFlagSet("test", 0)
|
|
|
|
globalSet.Bool("top-flag", true, "doc")
|
|
|
|
globalCtx := NewContext(nil, globalSet, nil)
|
|
|
|
c := NewContext(nil, set, globalCtx)
|
|
|
|
set.Parse([]string{"--one-flag", "--two-flag=foo"})
|
|
|
|
globalSet.Parse([]string{"--top-flag"})
|
|
|
|
|
|
|
|
actualFlags := c.FlagNames()
|
|
|
|
sort.Strings(actualFlags)
|
|
|
|
|
|
|
|
expect(t, actualFlags, []string{"one-flag", "top-flag", "two-flag"})
|
|
|
|
}
|