command_test.go 3.2 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
			IntOption("b", "beep", "enables beeper"),
			StringOption("B", "boop", "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")
	}

71
	req, _ = NewRequest(nil, nil, nil, nil, &cmd, 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 80 81 82 83 84 85 86 87 88 89 90

	err := req.SetOptions(map[string]interface{}{
		"b": 100,
	})
	if err != nil {
		t.Error("Should have passed")
	}

	err = req.SetOptions(map[string]interface{}{
		"b": ":)",
	})
	if err == nil {
		t.Error("Should have failed (string value not convertible to int)")
	}
91
}
92 93

func TestRegistration(t *testing.T) {
Matt Bell's avatar
Matt Bell committed
94 95
	cmdA := &Command{
		Options: []Option{
Matt Bell's avatar
Matt Bell committed
96
			IntOption("beep", "number of beeps"),
Matt Bell's avatar
Matt Bell committed
97
		},
Matt Bell's avatar
Matt Bell committed
98 99
		Run: noop,
	}
Matt Bell's avatar
Matt Bell committed
100

Matt Bell's avatar
Matt Bell committed
101 102
	cmdB := &Command{
		Options: []Option{
Matt Bell's avatar
Matt Bell committed
103
			IntOption("beep", "number of beeps"),
Matt Bell's avatar
Matt Bell committed
104
		},
Matt Bell's avatar
Matt Bell committed
105 106 107
		Run: noop,
		Subcommands: map[string]*Command{
			"a": cmdA,
Matt Bell's avatar
Matt Bell committed
108 109 110
		},
	}

Matt Bell's avatar
Matt Bell committed
111 112
	cmdC := &Command{
		Options: []Option{
Matt Bell's avatar
Matt Bell committed
113
			StringOption("encoding", "data encoding type"),
Matt Bell's avatar
Matt Bell committed
114 115
		},
		Run: noop,
Matt Bell's avatar
Matt Bell committed
116 117
	}

118 119 120
	path := []string{"a"}
	_, err := cmdB.GetOptions(path)
	if err == nil {
Matt Bell's avatar
Matt Bell committed
121 122 123
		t.Error("Should have failed (option name collision)")
	}

124 125
	_, err = cmdC.GetOptions(nil)
	if err == nil {
Matt Bell's avatar
Matt Bell committed
126 127
		t.Error("Should have failed (option name collision with global options)")
	}
128
}
129 130 131

func TestResolving(t *testing.T) {
	cmdC := &Command{}
Matt Bell's avatar
Matt Bell committed
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
	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,
		},
	}
149

Matt Bell's avatar
Matt Bell committed
150
	cmds, err := cmd.Resolve([]string{"a", "b", "c"})
151 152 153 154 155 156
	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
157
}