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

import "testing"

func TestOptionValidation(t *testing.T) {
Matt Bell's avatar
Matt Bell committed
6 7 8 9 10
	cmd := Command{
		Options: []Option{
			Option{[]string{"b", "beep"}, Int},
			Option{[]string{"B", "boop"}, String},
		},
11
		Run: func(res Response, req Request) {},
Matt Bell's avatar
Matt Bell committed
12 13
	}

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

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

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

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

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

Matt Bell's avatar
Matt Bell committed
52 53 54 55 56 57 58
	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
59
	req = NewEmptyRequest()
60
	req.SetOption(EncShort, "json")
Matt Bell's avatar
Matt Bell committed
61
	res = cmd.Call(req)
62
	if res.Error() != nil {
Matt Bell's avatar
Matt Bell committed
63 64
		t.Error("Should have passed")
	}
65

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

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

func TestRegistration(t *testing.T) {
82
	noop := func(res Response, req Request) {}
Matt Bell's avatar
Matt Bell committed
83

Matt Bell's avatar
Matt Bell committed
84 85 86
	cmdA := &Command{
		Options: []Option{
			Option{[]string{"beep"}, Int},
Matt Bell's avatar
Matt Bell committed
87
		},
Matt Bell's avatar
Matt Bell committed
88 89
		Run: noop,
	}
Matt Bell's avatar
Matt Bell committed
90

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

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

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

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

func TestResolving(t *testing.T) {
	cmdC := &Command{}
Matt Bell's avatar
Matt Bell committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
	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,
		},
	}
138

Matt Bell's avatar
Matt Bell committed
139
	cmds, err := cmd.Resolve([]string{"a", "b", "c"})
140 141 142 143 144 145
	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
146
}