-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_test.go
145 lines (139 loc) · 2.9 KB
/
command_test.go
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package conf
import (
"errors"
"fmt"
"testing"
)
func TestCommandGetCmd(t *testing.T) {
const fname = "TestCommandGetCmd"
c = &Config{}
cmd := c.Command("one", "like that it is")
cmd2 := c.Command("two", "or like that")
var opts = []Option{
{
Type: Int,
Flag: "a",
Usage: "like this",
Default: 1,
Commands: cmd,
},
}
_, err := c.Compose(opts...)
if err != nil {
t.Errorf("%s: should not raise an error: %s",
fname, err)
}
mode := c.Cmd()
if mode != cmd {
t.Errorf("%s: expected \"*\" received %q", fname, mode)
}
if !isInSet(c, cmd2) {
t.Errorf("%s: not a valid Command token", fname)
}
}
func TestCommandDuplicateKeys(t *testing.T) {
const fname = "TestCommandDuplicateKeys"
config := Config{}
m1 := config.Command("one", "like that it is")
m2 := config.Command("modetwo", "alternatively so")
var opts = []Option{
{
Type: Int,
Flag: "a",
Usage: "like this",
Default: 1,
Commands: m1,
},
{
Type: Int,
Flag: "a",
Usage: "like that",
Default: 1,
Commands: m2,
},
}
_, err := config.Compose(opts...)
if err != nil {
t.Errorf("%s: should not raise an error: %s",
fname, err)
}
}
func TestCommandTooMany(t *testing.T) {
const fname = "TestCommandTooMany"
config := Config{}
_ = config.Command("one", "like this")
names := make([]string, 65)
for i := 0; i <= 64; i++ {
names[i] = fmt.Sprint(i + '0')
}
for i := 0; i <= 64; i++ {
_ = config.Command(names[i], "")
}
_, err := config.Compose()
if !errors.Is(err, errConfig) {
t.Errorf("%s: %s", fname, err)
}
}
func TestCommandNotThere(t *testing.T) {
const fname = "TestCommandNotThere"
config := Config{}
_ = config.Command("", "")
m := CMD(2)
var opts = []Option{
{
Type: Int,
Flag: "a",
Usage: "like this",
Default: 1,
Commands: m,
},
}
_, err := config.Compose(opts...)
if !errors.Is(err, errConfig) {
t.Errorf("%s: %s", fname, err)
}
}
func TestCommandTokens(t *testing.T) {
const fname = "TestCommandTokenIs"
c := &Config{}
cmd1 := c.Command("one", "the first way")
cmd2 := c.Command("two", "the second way")
cmd3 := c.Command("three", "the third way")
var opts = []Option{
{
Type: Int,
Flag: "a",
Usage: "like this",
Default: 1,
Commands: cmd1,
},
}
_, err := c.Compose(opts...)
if err != nil {
t.Errorf("%s: %s", fname, err)
}
v := isInSet(c, cmd1)
if !v {
t.Errorf("%s: received false expected true", fname)
}
v = isInSet(c, cmd2)
if !v {
t.Errorf("%s: received false expected true", fname)
}
v = isInSet(c, cmd3)
if !v {
t.Errorf("%s: received false expected true", fname)
}
v = isInSet(c, cmd1|cmd3)
if !v {
t.Errorf("%s: received false expected true", fname)
}
v = isInSet(c, cmd1|cmd2|cmd3)
if !v {
t.Errorf("%s: received false expected true", fname)
}
v = isInSet(c, c.position)
if !v {
t.Errorf("%s: received false expected true", fname)
}
}