-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclicr_spec.cr
284 lines (247 loc) · 6.65 KB
/
clicr_spec.cr
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
require "spec"
require "../src/clicr.cr"
class TestCLI
getter help : String = ""
getter error : String = ""
@clicr : Clicr
def initialize(args)
other_options = {
"sub-var": {
label: "sub variable",
type: String,
},
}
main_options = {
name: {
label: "Your name",
default: "foo",
short: 'n',
},
yes: {
short: 'y',
label: "Print the name",
},
}
@clicr = Clicr.new(
args: args,
name: "myapp",
commands: {
talk: {
label: "Talk",
action: {"TestCLI.talk": "t"},
options: main_options,
},
"test-multiple-no-limit": {
action: {"TestCLI.args": ""},
arguments: %w(app numbers),
options: {
var: {
type: String,
},
}.merge(main_options),
},
"test-simple": {
action: {"TestCLI.args": ""},
arguments: %w(application folder),
options: {name: main_options[:name]},
},
"tuple-args": {
action: {"TestCLI.tuple_args": ""},
label: "Test args",
arguments: {"one", "two"},
},
"test-parens": {
action: {"TestCLI.args().to_s": ""},
},
"cast-option": {
action: {"TestCLI.cast_option": ""},
options: {
port: {
type: Int32,
short: 'p',
},
other: {
default: 123,
},
},
},
options_variables: {
label: "Test sub options/variables",
description: <<-E.to_s,
Multi-line
description
E
action: {"TestCLI.args": ""},
options: {
sub_opt: {
short: 's',
label: "sub options",
},
}.merge(**other_options, **main_options),
},
},
options: main_options,
)
@clicr.help_callback = ->(msg : String) {
@help = msg
# puts msg
}
@clicr.error_callback = ->(msg : String) {
@error = msg
# puts msg
}
end
def run
@clicr.run
end
def self.cast_option(port : Int32?, other : Int32) : {Int32?, Int32}
{port, other}
end
def self.tuple_args(arguments : Tuple(String, String))
arguments
end
def self.talk(name : String, yes : Bool)
{name, yes}
end
def self.args(**args)
args
end
end
describe Clicr do
describe "commands" do
it "runs a full command" do
TestCLI.new(["talk"]).run.should eq({"foo", false})
end
it "runs a single character command" do
TestCLI.new(["t"]).run.should eq({"foo", false})
end
it "tests calling a method with parenthesis" do
TestCLI.new(["test-parens"]).run.should eq "{}"
end
end
describe "arguments" do
it "uses simple" do
TestCLI.new(["test-simple", "myapp", "/tmp"]).run.should eq({
arguments: ["myapp", "/tmp"],
name: "foo",
})
end
it "uses multiple with no limit" do
TestCLI.new(["test-multiple-no-limit", "myapp", "-y", "2", "3"]).run.should eq({
arguments: ["myapp", "2", "3"],
var: nil,
name: "foo",
yes: true,
})
end
it "sets a variable with arguments" do
TestCLI.new(["test-multiple-no-limit", "myapp", "2", "--var", "Value", "3"]).run.should eq({
arguments: ["myapp", "2", "3"],
var: "Value",
name: "foo",
yes: false,
})
end
describe "tuple args" do
it "provides the expected number" do
TestCLI.new(["tuple-args", "1", "2"]).run.should eq({"1", "2"})
end
it "fails because of too many" do
cli = TestCLI.new(["tuple-args", "1"])
cli.run.should be_nil
cli.error.should_not be_empty
end
it "fails because of too few" do
cli = TestCLI.new(["tuple-args", "1", "2", "3"])
cli.run.should be_nil
cli.error.should_not be_empty
end
end
end
describe "unknown command" do
it "not known sub-command" do
cli = TestCLI.new(["talk", "Not exists"])
cli.run.should be_nil
cli.error.should_not be_empty
end
end
describe "options" do
describe "boolean" do
it "use one at the end" do
TestCLI.new(["talk", "--yes"]).run.should eq({"foo", true})
end
it "uses a single char one at the end" do
TestCLI.new(["talk", "-y"]).run.should eq({"foo", true})
end
it "uses concatenated single chars" do
TestCLI.new(["options_variables", "-ys"]).run.should eq({
sub_opt: true,
sub_var: nil,
name: "foo",
yes: true,
})
end
end
describe "string" do
it "sets one with equal '='" do
TestCLI.new(["talk", "--name=bar"]).run.should eq({"bar", false})
end
it "sets one with space ' '" do
TestCLI.new(["talk", "--name", "bar"]).run.should eq({"bar", false})
end
it "sets a short one" do
TestCLI.new(["talk", "-n", "bar"]).run.should eq({"bar", false})
end
it "casts string option with a type given" do
TestCLI.new(["cast-option", "-p", "8080"]).run.should eq({8080, 123})
end
it "casts string option with a default given" do
TestCLI.new(["cast-option", "--other=8080"]).run.should eq({nil, 8080})
end
end
end
describe "help" do
describe "print main help" do
help = <<-HELP
Usage: myapp COMMANDS [OPTIONS]
COMMANDS
cast-option
options_variables Test sub options/variables
talk, t Talk
test-multiple-no-limit
test-parens
test-simple
tuple-args Test args
OPTIONS
--name, -n foo Your name
--yes, -y Print the name
'myapp --help' to show the help.
HELP
it "with -h" do
cli = TestCLI.new(["-h"])
cli.run
cli.help.should eq help
end
it "with no arguments" do
cli = TestCLI.new Array(String).new
cli.run
cli.help.should eq help
end
end
it "prints for sub command" do
cli = TestCLI.new(["options_variables", "--help"])
cli.run
cli.help.should eq <<-HELP
Usage: myapp options_variables [OPTIONS]
Multi-line
description
OPTIONS
--name, -n foo Your name
--sub-var String sub variable
--sub_opt, -s sub options
--yes, -y Print the name
'myapp options_variables --help' to show the help.
HELP
end
end
end