command_test.go 2.99 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
	}

18
	opts, _ := cmd.GetOptions(nil)
Matt Bell's avatar
Matt Bell committed
19

Matt Bell's avatar
Matt Bell committed
20
	req, _ := NewRequest(nil, nil, nil, nil, nil, opts)
21 22
	req.SetOption("beep", true)
	res := cmd.Call(req)
23
	if res.Error() == nil {
Matt Bell's avatar
Matt Bell committed
24 25 26
		t.Error("Should have failed (incorrect type)")
	}

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

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

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

Matt Bell's avatar
Matt Bell committed
50
	req, _ = NewRequest(nil, nil, nil, nil, nil, opts)
Matt Bell's avatar
Matt Bell committed
51 52 53 54 55 56
	req.SetOption("foo", 5)
	res = cmd.Call(req)
	if res.Error() != nil {
		t.Error("Should have passed")
	}

Matt Bell's avatar
Matt Bell committed
57
	req, _ = NewRequest(nil, nil, nil, nil, nil, opts)
58
	req.SetOption(EncShort, "json")
Matt Bell's avatar
Matt Bell committed
59
	res = cmd.Call(req)
60
	if res.Error() != nil {
Matt Bell's avatar
Matt Bell committed
61 62
		t.Error("Should have passed")
	}
63

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

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

func TestRegistration(t *testing.T) {
Matt Bell's avatar
Matt Bell committed
80 81
	cmdA := &Command{
		Options: []Option{
Matt Bell's avatar
Matt Bell committed
82
			Option{[]string{"beep"}, Int, "number of beeps"},
Matt Bell's avatar
Matt Bell committed
83
		},
Matt Bell's avatar
Matt Bell committed
84 85
		Run: noop,
	}
Matt Bell's avatar
Matt Bell committed
86

Matt Bell's avatar
Matt Bell committed
87 88
	cmdB := &Command{
		Options: []Option{
Matt Bell's avatar
Matt Bell committed
89
			Option{[]string{"beep"}, Int, "number of beeps"},
Matt Bell's avatar
Matt Bell committed
90
		},
Matt Bell's avatar
Matt Bell committed
91 92 93
		Run: noop,
		Subcommands: map[string]*Command{
			"a": cmdA,
Matt Bell's avatar
Matt Bell committed
94 95 96
		},
	}

Matt Bell's avatar
Matt Bell committed
97 98
	cmdC := &Command{
		Options: []Option{
Matt Bell's avatar
Matt Bell committed
99
			Option{[]string{"encoding"}, String, "data encoding type"},
Matt Bell's avatar
Matt Bell committed
100 101
		},
		Run: noop,
Matt Bell's avatar
Matt Bell committed
102 103
	}

104 105 106
	path := []string{"a"}
	_, err := cmdB.GetOptions(path)
	if err == nil {
Matt Bell's avatar
Matt Bell committed
107 108 109
		t.Error("Should have failed (option name collision)")
	}

110 111
	_, err = cmdC.GetOptions(nil)
	if err == nil {
Matt Bell's avatar
Matt Bell committed
112 113
		t.Error("Should have failed (option name collision with global options)")
	}
114
}
115 116 117

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

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