Exemplo n.º 1
0
func TestTake(t *testing.T) {
	s := FromInts(1, 2, 3, 4, 5)
	a := s.Take(2).ToArray()
	b := s.Take(3).ToArray()
	assert.Equal(t, []int{1, 2}, a)
	assert.Equal(t, []int{1, 2, 3}, b)
}
Exemplo n.º 2
0
func TestRepeat(t *testing.T) {
	s := RepeatInt(5, 3)
	a := s.ToArray()
	b := s.ToArray()
	assert.Equal(t, []int{5, 5, 5}, a)
	assert.Equal(t, []int{5, 5, 5}, b)
}
Exemplo n.º 3
0
func TestHiddenCmdCompletion(t *testing.T) {
	app := newTestApp()

	// top level visible & hidden cmds, with no sub-cmds
	app.Command("visible1", "")
	app.Command("hidden1", "").Hidden()

	// visible cmd with visible & hidden sub-cmds
	visible2 := app.Command("visible2", "")
	visible2.Command("visible2-visible", "")
	visible2.Command("visible2-hidden", "").Hidden()

	// hidden cmd with visible & hidden sub-cmds
	hidden2 := app.Command("hidden2", "").Hidden()
	hidden2.Command("hidden2-visible", "")
	hidden2.Command("hidden2-hidden", "").Hidden()

	// Only top level visible cmds should show
	assert.Equal(t, []string{"help", "visible1", "visible2"}, complete(t, app))

	// Only visible sub-cmds should show
	assert.Equal(t, []string{"visible2-visible"}, complete(t, app, "visible2"))

	// Hidden commands should still complete visible sub-cmds
	assert.Equal(t, []string{"hidden2-visible"}, complete(t, app, "hidden2"))
}
Exemplo n.º 4
0
func TestTakeLast(t *testing.T) {
	s := FromInts(1, 2, 3, 4, 5)
	a := s.TakeLast(2).ToArray()
	b := s.TakeLast(3).ToArray()
	assert.Equal(t, []int{4, 5}, a)
	assert.Equal(t, []int{3, 4, 5}, b)
}
Exemplo n.º 5
0
func TestRange(t *testing.T) {
	s := Range(1, 5)
	a := s.ToArray()
	b := s.ToArray()
	assert.Equal(t, []int{1, 2, 3, 4, 5}, a)
	assert.Equal(t, []int{1, 2, 3, 4, 5}, b)
}
Exemplo n.º 6
0
func TestStart(t *testing.T) {
	s := StartInt(func() (int, error) { return 42, nil })
	a := s.ToArray()
	b := s.ToArray()
	assert.Equal(t, []int{42}, a)
	assert.Equal(t, []int{42}, b)
}
Exemplo n.º 7
0
func TestAccumulatorStrings(t *testing.T) {
	target := []string{}
	acc := newAccumulator(&target, func(v interface{}) Value { return newStringValue(v.(*string)) })
	acc.Set("a")
	assert.Equal(t, []string{"a"}, target)
	acc.Set("b")
	assert.Equal(t, []string{"a", "b"}, target)
}
Exemplo n.º 8
0
func TestConcat(t *testing.T) {
	a := []int{1, 2, 3}
	b := []int{4, 5}
	c := []int{6, 7}
	s := FromIntArray(a).Concat(FromIntArray(b)).Concat(FromIntArray(c)).ToArray()
	assert.Equal(t, []int{1, 2, 3, 4, 5, 6, 7}, s)
	s = FromIntArray(a).Concat(FromIntArray(b), FromIntArray(c)).ToArray()
	assert.Equal(t, []int{1, 2, 3, 4, 5, 6, 7}, s)
}
Exemplo n.º 9
0
func TestDefaultCmdCompletion(t *testing.T) {
	app := newTestApp()

	cmd1 := app.Command("cmd1", "")

	cmd1Sub1 := cmd1.Command("cmd1-sub1", "")
	cmd1Sub1.Arg("cmd1-sub1-arg1", "").HintOptions("cmd1-arg1").String()

	cmd2 := app.Command("cmd2", "").Default()

	cmd2.Command("cmd2-sub1", "")

	cmd2Sub2 := cmd2.Command("cmd2-sub2", "").Default()

	cmd2Sub2Sub1 := cmd2Sub2.Command("cmd2-sub2-sub1", "").Default()
	cmd2Sub2Sub1.Arg("cmd2-sub2-sub1-arg1", "").HintOptions("cmd2-sub2-sub1-arg1").String()
	cmd2Sub2Sub1.Arg("cmd2-sub2-sub1-arg2", "").HintOptions("cmd2-sub2-sub1-arg2").String()

	// Without args, should get:
	//   - root cmds (incuding implicit "help")
	//   - thread of default cmds
	//   - first arg hints for the final default cmd
	assert.Equal(t, []string{"cmd1", "cmd2", "cmd2-sub1", "cmd2-sub2", "cmd2-sub2-sub1", "cmd2-sub2-sub1-arg1", "help"}, complete(t, app))

	// With a non-default cmd already listed, should get:
	//   - sub cmds of that arg
	assert.Equal(t, []string{"cmd1-sub1"}, complete(t, app, "cmd1"))

	// With an explicit default cmd listed, should get:
	//   - default child-cmds
	//   - first arg hints for the final default cmd
	assert.Equal(t, []string{"cmd2-sub1", "cmd2-sub2", "cmd2-sub2-sub1", "cmd2-sub2-sub1-arg1"}, complete(t, app, "cmd2"))

	// Args should be completed when all preceding cmds are explicit, and when
	// any of them are implicit (not listed). Check this by trying all possible
	// combinations of choosing/excluding the three levels of cmds. This tests
	// root-level default, middle default, and end default.
	for i := 0; i < 8; i++ {
		var cmdline []string

		if i&1 != 0 {
			cmdline = append(cmdline, "cmd2")
		}
		if i&2 != 0 {
			cmdline = append(cmdline, "cmd2-sub2")
		}
		if i&4 != 0 {
			cmdline = append(cmdline, "cmd2-sub2-sub1")
		}

		assert.Contains(t, complete(t, app, cmdline...), "cmd2-sub2-sub1-arg1", "with cmdline: %v", cmdline)
	}

	// With both args of a default sub cmd, should get no completions
	assert.Empty(t, complete(t, app, "arg1", "arg2"))
}
Exemplo n.º 10
0
func TestCmdCompletion(t *testing.T) {
	app := newTestApp()
	app.Command("one", "")
	two := app.Command("two", "")
	two.Command("sub1", "")
	two.Command("sub2", "")

	assert.Equal(t, []string{"help", "one", "two"}, complete(t, app))
	assert.Equal(t, []string{"sub1", "sub2"}, complete(t, app, "two"))
}
Exemplo n.º 11
0
func TestAliasedCommand(t *testing.T) {
	app := newTestApp()
	app.Command("one", "").Alias("two")
	selected, _ := app.Parse([]string{"one"})
	assert.Equal(t, "one", selected)
	selected, _ = app.Parse([]string{"two"})
	assert.Equal(t, "one", selected)
	// 2 due to "help" and "one"
	assert.Equal(t, 2, len(app.Model().FlattenedCommands()))
}
Exemplo n.º 12
0
func Test_Writer(t *testing.T) {
	dropSize := 16

	numShort := 0
	shortSize := 0
	validate := func(buf []byte) error {
		switch {
		case len(buf) == dropSize:
			if numShort > 0 {
				return fmt.Errorf("got full after short")
			}
			return nil
		case len(buf) < dropSize:
			if numShort > 0 {
				return fmt.Errorf("got second short (%d)", len(buf))
			}
			numShort++
			shortSize = len(buf)
		default:
			return fmt.Errorf("drop too large (%d > %d)", len(buf), dropSize)
		}

		return nil
	}

	buf := make([]byte, dropSize)
	countingWriter := counter.NewWriter(nil)

	dw := &Writer{
		Buffer:   buf,
		Validate: validate,
		Writer:   countingWriter,
	}

	rbuf := make([]byte, 128)

	write := func(l int) {
		written, wErr := dw.Write(rbuf[0:l])
		assert.Equal(t, l, written)
		assert.NoError(t, wErr)
	}

	write(12)
	write(4)
	write(10)
	write(6)
	write(16)
	write(64)
	write(5)

	assert.NoError(t, dw.Close())
	assert.Equal(t, 5, shortSize)
	assert.Equal(t, int64(12+4+10+6+16+64+5), countingWriter.Count())
}
Exemplo n.º 13
0
func TestDefaultEnvars(t *testing.T) {
	a := New("some-app", "").Terminate(nil).DefaultEnvars()
	f0 := a.Flag("some-flag", "")
	f0.Bool()
	f1 := a.Flag("some-other-flag", "").NoEnvar()
	f1.Bool()
	_, err := a.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, "SOME_APP_SOME_FLAG", f0.envar)
	assert.Equal(t, "", f1.envar)
}
Exemplo n.º 14
0
func TestGetFlagAndOverrideDefault(t *testing.T) {
	app := newTestApp()
	a := app.Flag("a", "").Default("default").String()
	_, err := app.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, "default", *a)
	app.GetFlag("a").Default("new")
	_, err = app.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, "new", *a)
}
Exemplo n.º 15
0
func TestInterspersedFalse(t *testing.T) {
	app := newTestApp().Interspersed(false)
	a1 := app.Arg("a1", "").String()
	a2 := app.Arg("a2", "").String()
	f1 := app.Flag("flag", "").String()

	_, err := app.Parse([]string{"a1", "--flag=flag"})
	assert.NoError(t, err)
	assert.Equal(t, "a1", *a1)
	assert.Equal(t, "--flag=flag", *a2)
	assert.Equal(t, "", *f1)
}
Exemplo n.º 16
0
func TestCreate(t *testing.T) {
	s := CreateInt(func(observer IntObserver, subscription Subscription) {
		observer.Next(0)
		observer.Next(1)
		observer.Next(2)
		observer.Complete()
	})
	a := s.ToArray()
	b := s.ToArray()
	assert.Equal(t, []int{0, 1, 2}, a)
	assert.Equal(t, []int{0, 1, 2}, b)
}
Exemplo n.º 17
0
func TestDefaultSubcommandWithFlags(t *testing.T) {
	app := newTestApp()
	c0 := app.Command("c0", "").Default()
	_ = c0.Flag("f0", "").Int()
	c0c1 := c0.Command("c1", "").Default()
	c0c1f1 := c0c1.Flag("f1", "").Int()
	selected, err := app.Parse([]string{"--f1=2"})
	assert.NoError(t, err)
	assert.Equal(t, "c0 c1", selected)
	assert.Equal(t, 2, *c0c1f1)
	_, err = app.Parse([]string{"--f2"})
	assert.Error(t, err)
}
Exemplo n.º 18
0
func TestNestedCommandsWithArgs(t *testing.T) {
	app := New("app", "")
	cmd := app.Command("a", "").Command("b", "")
	a := cmd.Arg("a", "").String()
	b := cmd.Arg("b", "").String()
	context := tokenize([]string{"a", "b", "c", "d"}, false)
	selected, err := parseAndExecute(app, context)
	assert.NoError(t, err)
	assert.True(t, context.EOL())
	assert.Equal(t, "a b", selected)
	assert.Equal(t, "c", *a)
	assert.Equal(t, "d", *b)
}
Exemplo n.º 19
0
func TestReplay(t *testing.T) {
	ch := make(chan int, 5)
	for i := 0; i < 5; i++ {
		ch <- i
	}
	close(ch)
	s := FromIntChannel(ch).Replay(0, 0)
	a := s.ToArray()
	b := s.ToArray()
	expected := []int{0, 1, 2, 3, 4}
	assert.Equal(t, expected, a)
	assert.Equal(t, expected, b)
}
Exemplo n.º 20
0
func TestDefaultSubcommandWithArg(t *testing.T) {
	app := newTestApp()
	c0 := app.Command("c0", "").Default()
	c01 := c0.Command("c01", "").Default()
	c012 := c01.Command("c012", "").Default()
	a0 := c012.Arg("a0", "").String()
	c0.Command("c02", "")

	cmd, err := app.Parse([]string{"c0", "hello"})
	assert.NoError(t, err)
	assert.Equal(t, "c0 c01 c012", cmd)
	assert.Equal(t, "hello", *a0)
}
Exemplo n.º 21
0
func TestReplayWithSize(t *testing.T) {
	ch := make(chan int, 5)
	for i := 0; i < 5; i++ {
		ch <- i
	}
	close(ch)
	s := FromIntChannel(ch).Replay(2, 0)
	a := s.ToArray()
	b := s.ToArray()
	assert.Equal(t, []int{0, 1, 2, 3, 4}, a)
	assert.Equal(t, []int{3, 4}, b)
	assert.Equal(t, []int{3, 4}, s.ToArray())
}
Exemplo n.º 22
0
func TestReplayWithExpiry(t *testing.T) {
	ch := make(chan int)
	go func() {
		for i := 0; i < 5; i++ {
			ch <- i
			time.Sleep(time.Millisecond * 100)
		}
		close(ch)
	}()
	s := FromIntChannel(ch).Replay(0, time.Millisecond*600)
	time.Sleep(time.Millisecond * 500)
	assert.Equal(t, []int{0, 1, 2, 3, 4}, s.ToArray())
	time.Sleep(time.Millisecond * 100)
	assert.Equal(t, []int{1, 2, 3, 4}, s.ToArray())
}
Exemplo n.º 23
0
func TestParseContextPush(t *testing.T) {
	app := New("test", "")
	app.Command("foo", "").Command("bar", "")
	c := tokenize([]string{"foo", "bar"}, false)
	a := c.Next()
	assert.Equal(t, TokenArg, a.Type)
	b := c.Next()
	assert.Equal(t, TokenArg, b.Type)
	c.Push(b)
	c.Push(a)
	a = c.Next()
	assert.Equal(t, "foo", a.Value)
	b = c.Next()
	assert.Equal(t, "bar", b.Value)
}
Exemplo n.º 24
0
func TestNestedCommandsWithFlags(t *testing.T) {
	app := New("app", "")
	cmd := app.Command("a", "").Command("b", "")
	a := cmd.Flag("aaa", "").Short('a').String()
	b := cmd.Flag("bbb", "").Short('b').String()
	err := app.init()
	assert.NoError(t, err)
	context := tokenize(strings.Split("a b --aaa x -b x", " "), false)
	selected, err := parseAndExecute(app, context)
	assert.NoError(t, err)
	assert.True(t, context.EOL())
	assert.Equal(t, "a b", selected)
	assert.Equal(t, "x", *a)
	assert.Equal(t, "x", *b)
}
Exemplo n.º 25
0
func TestCommander(t *testing.T) {
	c := newTestApp()
	ping := c.Command("ping", "Ping an IP address.")
	pingTTL := ping.Flag("ttl", "TTL for ICMP packets").Short('t').Default("5s").Duration()

	selected, err := c.Parse([]string{"ping"})
	assert.NoError(t, err)
	assert.Equal(t, "ping", selected)
	assert.Equal(t, 5*time.Second, *pingTTL)

	selected, err = c.Parse([]string{"ping", "--ttl=10s"})
	assert.NoError(t, err)
	assert.Equal(t, "ping", selected)
	assert.Equal(t, 10*time.Second, *pingTTL)
}
Exemplo n.º 26
0
func TestFlagEnumVar(t *testing.T) {
	c := newTestApp()
	var bar string

	a := c.Flag("foo", "foo")
	a.Enum("opt1", "opt2")
	b := c.Flag("bar", "bar")
	b.EnumVar(&bar, "opt3", "opt4")

	args := a.resolveCompletions()
	assert.Equal(t, []string{"opt1", "opt2"}, args)

	args = b.resolveCompletions()
	assert.Equal(t, []string{"opt3", "opt4"}, args)
}
Exemplo n.º 27
0
func TestCombinedShortFlagArg(t *testing.T) {
	a := newTestApp()
	n := a.Flag("short", "").Short('s').Int()
	_, err := a.Parse([]string{"-s10"})
	assert.NoError(t, err)
	assert.Equal(t, 10, *n)
}
Exemplo n.º 28
0
func TestMultiHintOptions(t *testing.T) {
	c := newTestApp()

	a := c.Flag("foo", "foo").HintOptions("opt1").HintOptions("opt2")
	args := a.resolveCompletions()
	assert.Equal(t, []string{"opt1", "opt2"}, args)
}
Exemplo n.º 29
0
func TestFlagMultipleValuesDefault(t *testing.T) {
	app := newTestApp()
	a := app.Flag("a", "").Default("default1", "default2").Strings()
	_, err := app.Parse([]string{})
	assert.NoError(t, err)
	assert.Equal(t, []string{"default1", "default2"}, *a)
}
Exemplo n.º 30
0
func TestTopLevelArgWorks(t *testing.T) {
	c := newTestApp()
	s := c.Arg("arg", "help").String()
	_, err := c.Parse([]string{"foo"})
	assert.NoError(t, err)
	assert.Equal(t, "foo", *s)
}