aboutsummaryrefslogtreecommitdiffhomepage
path: root/command/builder_test.go
blob: 7b9a1701425025c5b64fd06775a141aa0a7e50f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package command_test

import (
	"testing"

	"hakurei.app/command"
)

func TestBuild(t *testing.T) {
	t.Parallel()
	c := command.New(nil, nil, "test", nil)
	stubHandler := func([]string) error { panic("unreachable") }

	t.Run("nil direct handler", func(t *testing.T) {
		defer checkRecover(t, "Command", "invalid handler")
		c.Command("name", "usage", nil)
	})

	t.Run("direct zero length", func(t *testing.T) {
		wantPanic := "invalid subcommand"
		t.Run("zero length name", func(t *testing.T) { defer checkRecover(t, "Command", wantPanic); c.Command("", "usage", stubHandler) })
		t.Run("zero length usage", func(t *testing.T) { defer checkRecover(t, "Command", wantPanic); c.Command("name", "", stubHandler) })
	})

	t.Run("direct adopt unique names", func(t *testing.T) {
		c.Command("d0", "usage", stubHandler)
		c.Command("d1", "usage", stubHandler)
	})

	t.Run("direct adopt non-unique name", func(t *testing.T) {
		defer checkRecover(t, "Command", "attempted to initialise subcommand with non-unique name")
		c.Command("d0", "usage", stubHandler)
	})

	t.Run("zero length", func(t *testing.T) {
		wantPanic := "invalid subcommand tree"
		t.Run("zero length name", func(t *testing.T) { defer checkRecover(t, "New", wantPanic); c.New("", "usage") })
		t.Run("zero length usage", func(t *testing.T) { defer checkRecover(t, "New", wantPanic); c.New("name", "") })
	})

	t.Run("direct adopt unique names", func(t *testing.T) {
		c.New("t0", "usage")
		c.New("t1", "usage")
	})

	t.Run("direct adopt non-unique name", func(t *testing.T) {
		defer checkRecover(t, "Command", "attempted to initialise subcommand tree with non-unique name")
		c.New("t0", "usage")
	})
}

func checkRecover(t *testing.T, name, wantPanic string) {
	if r := recover(); r != wantPanic {
		t.Errorf("%s: panic = %v; wantPanic %v",
			name, r, wantPanic)
	}
}