command_test.go 2.97 KB
Newer Older
1 2 3 4
package commands

import "testing"

Matt Bell's avatar
Matt Bell committed
5 6 7 8
func noop(req Request) (interface{}, error) {
	return nil, nil
}

9
func TestOptionValidation(t *testing.T) {
Matt Bell's avatar
Matt Bell committed
10 11
	cmd := Command{
		Options: []Option{
Matt Bell's avatar
Matt Bell committed
12 13
			Option{[]string{"b", "beep"}, Int, "enables beeper"},
			Option{[]string{"B", "boop"}, String, "password for booper"},
Matt Bell's avatar
Matt Bell committed
14
		},
Matt Bell's avatar
Matt Bell committed
15
		Run: noop,
Matt Bell's avatar
Matt Bell committed
16 17
	}

Matt Bell's avatar
Matt Bell committed
18
	req := NewEmptyRequest()
19 20
	req.SetOption("beep", 5)
	req.SetOption("b", 10)
Matt Bell's avatar
Matt Bell committed
21
	res := cmd.Call(req)
22
	if res.Error() == nil {
Matt Bell's avatar
Matt Bell committed
23 24 25
		t.Error("Should have failed (duplicate options)")
	}

Matt Bell's avatar
Matt Bell committed
26
	req = NewEmptyRequest()
27
	req.SetOption("beep", "foo")
Matt Bell's avatar
Matt Bell committed
28
	res = cmd.Call(req)
29
	if res.Error() == nil {
Matt Bell's avatar
Matt Bell committed
30 31 32
		t.Error("Should have failed (incorrect type)")
	}

Matt Bell's avatar
Matt Bell committed
33
	req = NewEmptyRequest()
34
	req.SetOption("beep", 5)
Matt Bell's avatar
Matt Bell committed
35
	res = cmd.Call(req)
36 37
	if res.Error() != nil {
		t.Error(res.Error(), "Should have passed")
Matt Bell's avatar
Matt Bell committed
38 39
	}

Matt Bell's avatar
Matt Bell committed
40
	req = NewEmptyRequest()
41 42
	req.SetOption("beep", 5)
	req.SetOption("boop", "test")
Matt Bell's avatar
Matt Bell committed
43
	res = cmd.Call(req)
44
	if res.Error() != nil {
Matt Bell's avatar
Matt Bell committed
45 46 47
		t.Error("Should have passed")
	}

Matt Bell's avatar
Matt Bell committed
48
	req = NewEmptyRequest()
49 50
	req.SetOption("b", 5)
	req.SetOption("B", "test")
Matt Bell's avatar
Matt Bell committed
51
	res = cmd.Call(req)
52
	if res.Error() != nil {
Matt Bell's avatar
Matt Bell committed
53 54 55
		t.Error("Should have passed")
	}

Matt Bell's avatar
Matt Bell committed
56 57 58 59 60 61 62
	req = NewEmptyRequest()
	req.SetOption("foo", 5)
	res = cmd.Call(req)
	if res.Error() != nil {
		t.Error("Should have passed")
	}

Matt Bell's avatar
Matt Bell committed
63
	req = NewEmptyRequest()
64
	req.SetOption(EncShort, "json")
Matt Bell's avatar
Matt Bell committed
65
	res = cmd.Call(req)
66
	if res.Error() != nil {
Matt Bell's avatar
Matt Bell committed
67 68
		t.Error("Should have passed")
	}
69

Matt Bell's avatar
Matt Bell committed
70
	req = NewEmptyRequest()
71
	req.SetOption("b", "100")
Matt Bell's avatar
Matt Bell committed
72
	res = cmd.Call(req)
73
	if res.Error() != nil {
74 75 76
		t.Error("Should have passed")
	}

Matt Bell's avatar
Matt Bell committed
77
	req = NewEmptyRequest()
78
	req.SetOption("b", ":)")
Matt Bell's avatar
Matt Bell committed
79
	res = cmd.Call(req)
Juan Batiz-Benet's avatar
Juan Batiz-Benet committed
80 81
	if res.Error() == nil {
		t.Error(res.Error(), "Should have failed (string value not convertible to int)")
82
	}
83
}
84 85

func TestRegistration(t *testing.T) {
Matt Bell's avatar
Matt Bell committed
86 87
	cmdA := &Command{
		Options: []Option{
Matt Bell's avatar
Matt Bell committed
88
			Option{[]string{"beep"}, Int, "number of beeps"},
Matt Bell's avatar
Matt Bell committed
89
		},
Matt Bell's avatar
Matt Bell committed
90 91
		Run: noop,
	}
Matt Bell's avatar
Matt Bell committed
92

Matt Bell's avatar
Matt Bell committed
93 94
	cmdB := &Command{
		Options: []Option{
Matt Bell's avatar
Matt Bell committed
95
			Option{[]string{"beep"}, Int, "number of beeps"},
Matt Bell's avatar
Matt Bell committed
96
		},
Matt Bell's avatar
Matt Bell committed
97 98 99
		Run: noop,
		Subcommands: map[string]*Command{
			"a": cmdA,
Matt Bell's avatar
Matt Bell committed
100 101 102
		},
	}

Matt Bell's avatar
Matt Bell committed
103 104
	cmdC := &Command{
		Options: []Option{
Matt Bell's avatar
Matt Bell committed
105
			Option{[]string{"encoding"}, String, "data encoding type"},
Matt Bell's avatar
Matt Bell committed
106 107
		},
		Run: noop,
Matt Bell's avatar
Matt Bell committed
108 109
	}

Matt Bell's avatar
Matt Bell committed
110
	res := cmdB.Call(NewRequest([]string{"a"}, nil, nil, nil, nil))
Matt Bell's avatar
Matt Bell committed
111
	if res.Error() == nil {
Matt Bell's avatar
Matt Bell committed
112 113 114
		t.Error("Should have failed (option name collision)")
	}

Matt Bell's avatar
Matt Bell committed
115 116
	res = cmdC.Call(NewEmptyRequest())
	if res.Error() == nil {
Matt Bell's avatar
Matt Bell committed
117 118
		t.Error("Should have failed (option name collision with global options)")
	}
119
}
120 121 122

func TestResolving(t *testing.T) {
	cmdC := &Command{}
Matt Bell's avatar
Matt Bell committed
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
	cmdB := &Command{
		Subcommands: map[string]*Command{
			"c": cmdC,
		},
	}
	cmdB2 := &Command{}
	cmdA := &Command{
		Subcommands: map[string]*Command{
			"b": cmdB,
			"B": cmdB2,
		},
	}
	cmd := &Command{
		Subcommands: map[string]*Command{
			"a": cmdA,
		},
	}
140

Matt Bell's avatar
Matt Bell committed
141
	cmds, err := cmd.Resolve([]string{"a", "b", "c"})
142 143 144 145 146 147
	if err != nil {
		t.Error(err)
	}
	if len(cmds) != 4 || cmds[0] != cmd || cmds[1] != cmdA || cmds[2] != cmdB || cmds[3] != cmdC {
		t.Error("Returned command path is different than expected", cmds)
	}
Matt Bell's avatar
Matt Bell committed
148
}