Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 10856 Bytes
1 | #!/usr/local/bin/ruby -w |
2 | |
3 | # tc_menu.rb |
4 | # |
5 | # Created by Gregory Thomas Brown on 2005-05-10. |
6 | # Copyright 2005. All rights reserved. |
7 | # |
8 | # This is Free Software. See LICENSE and COPYING for details. |
9 | |
10 | require "test/unit" |
11 | |
12 | require "highline" |
13 | require "stringio" |
14 | |
15 | class TestMenu < Test::Unit::TestCase |
16 | def setup |
17 | @input = StringIO.new |
18 | @output = StringIO.new |
19 | @terminal = HighLine.new(@input, @output) |
20 | end |
21 | |
22 | def test_choices |
23 | @input << "2\n" |
24 | @input.rewind |
25 | |
26 | output = @terminal.choose do |menu| |
27 | menu.choices("Sample1", "Sample2", "Sample3") |
28 | end |
29 | assert_equal("Sample2", output) |
30 | end |
31 | |
32 | def test_flow |
33 | @input << "Sample1\n" |
34 | @input.rewind |
35 | |
36 | @terminal.choose do |menu| |
37 | # Default: menu.flow = :rows |
38 | |
39 | menu.choice "Sample1" |
40 | menu.choice "Sample2" |
41 | menu.choice "Sample3" |
42 | end |
43 | assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string) |
44 | |
45 | @output.truncate(@output.rewind) |
46 | @input.rewind |
47 | |
48 | @terminal.choose do |menu| |
49 | menu.flow = :columns_across |
50 | |
51 | menu.choice "Sample1" |
52 | menu.choice "Sample2" |
53 | menu.choice "Sample3" |
54 | end |
55 | assert_equal("1. Sample1 2. Sample2 3. Sample3\n? ", @output.string) |
56 | |
57 | @output.truncate(@output.rewind) |
58 | @input.rewind |
59 | |
60 | @terminal.choose do |menu| |
61 | menu.flow = :inline |
62 | menu.index = :none |
63 | |
64 | menu.choice "Sample1" |
65 | menu.choice "Sample2" |
66 | menu.choice "Sample3" |
67 | end |
68 | assert_equal("Sample1, Sample2 or Sample3? ", @output.string) |
69 | end |
70 | |
71 | def test_help |
72 | @input << "help\nhelp load\nhelp rules\nhelp missing\n" |
73 | @input.rewind |
74 | |
75 | 4.times do |
76 | @terminal.choose do |menu| |
77 | menu.shell = true |
78 | |
79 | menu.choice(:load, "Load a file.") |
80 | menu.choice(:save, "Save data in file.") |
81 | menu.choice(:quit, "Exit program.") |
82 | |
83 | menu.help("rules", "The rules of this system are as follows...") |
84 | end |
85 | end |
86 | assert_equal( "1. load\n2. save\n3. quit\n4. help\n? " + |
87 | "This command will display helpful messages about " + |
88 | "functionality, like this one. To see the help for a " + |
89 | "specific topic enter:\n" + |
90 | "\thelp [TOPIC]\n" + |
91 | "Try asking for help on any of the following:\n" + |
92 | "\nload quit rules save \n" + |
93 | "1. load\n2. save\n3. quit\n4. help\n? " + |
94 | "= load\n\n" + |
95 | "Load a file.\n" + |
96 | "1. load\n2. save\n3. quit\n4. help\n? " + |
97 | "= rules\n\n" + |
98 | "The rules of this system are as follows...\n" + |
99 | "1. load\n2. save\n3. quit\n4. help\n? " + |
100 | "= missing\n\n" + |
101 | "There's no help for that topic.\n", @output.string ) |
102 | end |
103 | |
104 | def test_index |
105 | @input << "Sample1\n" |
106 | @input.rewind |
107 | |
108 | @terminal.choose do |menu| |
109 | # Default: menu.index = :number |
110 | |
111 | menu.choice "Sample1" |
112 | menu.choice "Sample2" |
113 | menu.choice "Sample3" |
114 | end |
115 | assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string) |
116 | |
117 | @output.truncate(@output.rewind) |
118 | @input.rewind |
119 | |
120 | @terminal.choose do |menu| |
121 | menu.index = :letter |
122 | menu.index_suffix = ") " |
123 | |
124 | menu.choice "Sample1" |
125 | menu.choice "Sample2" |
126 | menu.choice "Sample3" |
127 | end |
128 | assert_equal("a) Sample1\nb) Sample2\nc) Sample3\n? ", @output.string) |
129 | |
130 | @output.truncate(@output.rewind) |
131 | @input.rewind |
132 | |
133 | @terminal.choose do |menu| |
134 | menu.index = :none |
135 | |
136 | menu.choice "Sample1" |
137 | menu.choice "Sample2" |
138 | menu.choice "Sample3" |
139 | end |
140 | assert_equal("Sample1\nSample2\nSample3\n? ", @output.string) |
141 | |
142 | @output.truncate(@output.rewind) |
143 | @input.rewind |
144 | |
145 | @terminal.choose do |menu| |
146 | menu.index = "*" |
147 | |
148 | menu.choice "Sample1" |
149 | menu.choice "Sample2" |
150 | menu.choice "Sample3" |
151 | end |
152 | assert_equal("* Sample1\n* Sample2\n* Sample3\n? ", @output.string) |
153 | end |
154 | |
155 | def test_layouts |
156 | @input << "save\n" |
157 | @input.rewind |
158 | |
159 | @terminal.choose(:load, :save, :quit) # Default: layout = :list |
160 | assert_equal("1. load\n2. save\n3. quit\n? ", @output.string) |
161 | |
162 | @input.rewind |
163 | @output.truncate(@output.rewind) |
164 | |
165 | @terminal.choose(:load, :save, :quit) do |menu| |
166 | menu.header = "File Menu" |
167 | end |
168 | assert_equal( "File Menu:\n" + |
169 | "1. load\n2. save\n3. quit\n? ", @output.string ) |
170 | |
171 | @input.rewind |
172 | @output.truncate(@output.rewind) |
173 | |
174 | @terminal.choose(:load, :save, :quit) do |menu| |
175 | menu.layout = :one_line |
176 | menu.header = "File Menu" |
177 | menu.prompt = "Operation? " |
178 | end |
179 | assert_equal( "File Menu: Operation? " + |
180 | "(load, save or quit) ", @output.string ) |
181 | |
182 | @input.rewind |
183 | @output.truncate(@output.rewind) |
184 | |
185 | @terminal.choose(:load, :save, :quit) do |menu| |
186 | menu.layout = :menu_only |
187 | end |
188 | assert_equal("load, save or quit? ", @output.string) |
189 | |
190 | @input.rewind |
191 | @output.truncate(@output.rewind) |
192 | |
193 | @terminal.choose(:load, :save, :quit) do |menu| |
194 | menu.layout = '<%= list(@menu) %>File Menu: ' |
195 | end |
196 | assert_equal("1. load\n2. save\n3. quit\nFile Menu: ", @output.string) |
197 | end |
198 | |
199 | def test_list_option |
200 | @input << "l\n" |
201 | @input.rewind |
202 | |
203 | @terminal.choose(:load, :save, :quit) do |menu| |
204 | menu.layout = :menu_only |
205 | menu.list_option = ", or " |
206 | end |
207 | assert_equal("load, save, or quit? ", @output.string) |
208 | end |
209 | |
210 | def test_nil_on_handled |
211 | @input << "3\n3\n2\n" |
212 | @input.rewind |
213 | |
214 | # Shows that by default proc results are returned. |
215 | output = @terminal.choose do |menu| |
216 | menu.choice "Sample1" do "output1" end |
217 | menu.choice "Sample2" do "output2" end |
218 | menu.choice "Sample3" do "output3" end |
219 | end |
220 | assert_equal("output3", output) |
221 | |
222 | # |
223 | # Shows that they can be replaced with +nil+ by setting |
224 | # _nil_on_handled to +true+. |
225 | # |
226 | output = @terminal.choose do |menu| |
227 | menu.nil_on_handled = true |
228 | menu.choice "Sample1" do "output1" end |
229 | menu.choice "Sample2" do "output2" end |
230 | menu.choice "Sample3" do "output3" end |
231 | end |
232 | assert_equal(nil, output) |
233 | |
234 | # Shows that a menu item without a proc will be returned no matter what. |
235 | output = @terminal.choose do |menu| |
236 | menu.choice "Sample1" |
237 | menu.choice "Sample2" |
238 | menu.choice "Sample3" |
239 | end |
240 | assert_equal("Sample2", output) |
241 | end |
242 | |
243 | def test_passed_command |
244 | @input << "q\n" |
245 | @input.rewind |
246 | |
247 | selected = nil |
248 | @terminal.choose do |menu| |
249 | menu.choices(:load, :save, :quit) { |command| selected = command } |
250 | end |
251 | assert_equal(:quit, selected) |
252 | end |
253 | |
254 | def test_question_options |
255 | @input << "save\n" |
256 | @input.rewind |
257 | |
258 | answer = @terminal.choose(:Load, :Save, :Quit) do |menu| |
259 | menu.case = :capitalize |
260 | end |
261 | assert_equal(:Save, answer) |
262 | |
263 | @input.rewind |
264 | |
265 | answer = @terminal.choose(:Load, :Save, :Quit) do |menu| |
266 | menu.case = :capitalize |
267 | menu.character = :getc |
268 | end |
269 | assert_equal(:Save, answer) |
270 | assert_equal(?a, @input.getc) |
271 | end |
272 | |
273 | def test_select_by |
274 | @input << "Sample1\n2\n" |
275 | @input.rewind |
276 | |
277 | selected = @terminal.choose do |menu| |
278 | menu.choice "Sample1" |
279 | menu.choice "Sample2" |
280 | menu.choice "Sample3" |
281 | end |
282 | assert_equal("Sample1", selected) |
283 | |
284 | @input.rewind |
285 | |
286 | selected = @terminal.choose do |menu| |
287 | menu.select_by = :index |
288 | |
289 | menu.choice "Sample1" |
290 | menu.choice "Sample2" |
291 | menu.choice "Sample3" |
292 | end |
293 | assert_equal("Sample2", selected) |
294 | |
295 | @input.rewind |
296 | |
297 | selected = @terminal.choose do |menu| |
298 | menu.select_by = :name |
299 | |
300 | menu.choice "Sample1" |
301 | menu.choice "Sample2" |
302 | menu.choice "Sample3" |
303 | end |
304 | assert_equal("Sample1", selected) |
305 | end |
306 | |
307 | def test_hidden |
308 | @input << "Hidden\n4\n" |
309 | @input.rewind |
310 | |
311 | selected = @terminal.choose do |menu| |
312 | menu.choice "Sample1" |
313 | menu.choice "Sample2" |
314 | menu.choice "Sample3" |
315 | menu.hidden "Hidden!" |
316 | end |
317 | assert_equal("Hidden!", selected) |
318 | assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string) |
319 | |
320 | @input.rewind |
321 | |
322 | selected = @terminal.choose do |menu| |
323 | menu.select_by = :index |
324 | |
325 | menu.choice "Sample1" |
326 | menu.choice "Sample2" |
327 | menu.choice "Sample3" |
328 | menu.hidden "Hidden!" |
329 | end |
330 | assert_equal("Hidden!", selected) |
331 | |
332 | @input.rewind |
333 | |
334 | selected = @terminal.choose do |menu| |
335 | menu.select_by = :name |
336 | |
337 | menu.choice "Sample1" |
338 | menu.choice "Sample2" |
339 | menu.choice "Sample3" |
340 | menu.hidden "Hidden!" |
341 | end |
342 | assert_equal("Hidden!", selected) |
343 | |
344 | @input.rewind |
345 | end |
346 | |
347 | def test_select_by_letter |
348 | @input << "b\n" |
349 | @input.rewind |
350 | |
351 | selected = @terminal.choose do |menu| |
352 | menu.index = :letter |
353 | menu.choice :save |
354 | menu.choice :load |
355 | menu.choice :quit |
356 | end |
357 | assert_equal(:load, selected) |
358 | end |
359 | |
360 | def test_shell |
361 | @input << "save --some-option my_file.txt\n" |
362 | @input.rewind |
363 | |
364 | selected = nil |
365 | options = nil |
366 | answer = @terminal.choose do |menu| |
367 | menu.choices(:load, :quit) |
368 | menu.choice(:save) do |command, details| |
369 | selected = command |
370 | options = details |
371 | |
372 | "Saved!" |
373 | end |
374 | menu.shell = true |
375 | end |
376 | assert_equal("Saved!", answer) |
377 | assert_equal(:save, selected) |
378 | assert_equal("--some-option my_file.txt", options) |
379 | end |
380 | |
381 | def test_simple_menu_shortcut |
382 | @input << "3\n" |
383 | @input.rewind |
384 | |
385 | selected = @terminal.choose(:save, :load, :quit) |
386 | assert_equal(:quit, selected) |
387 | end |
388 | |
389 | def test_symbols |
390 | @input << "3\n" |
391 | @input.rewind |
392 | |
393 | selected = @terminal.choose do |menu| |
394 | menu.choices(:save, :load, :quit) |
395 | end |
396 | assert_equal(:quit, selected) |
397 | end |
398 | |
399 | def test_paged_print_infinite_loop_bug |
400 | @terminal.page_at = 5 |
401 | # Will page twice, so start with two new lines |
402 | @input << "\n\n3\n" |
403 | @input.rewind |
404 | |
405 | # Sadly this goes into an infinite loop without the fix to page_print |
406 | selected = @terminal.choose(* 1..10) |
407 | assert_equal(selected, 3) |
408 | end |
409 | |
410 | |
411 | def test_cancel_paging |
412 | # Tests that paging can be cancelled halfway through |
413 | @terminal.page_at = 5 |
414 | # Will page twice, so stop after first page and make choice 3 |
415 | @input << "q\n3\n" |
416 | @input.rewind |
417 | |
418 | selected = @terminal.choose(* 1..10) |
419 | assert_equal(selected, 3) |
420 | |
421 | # Make sure paging message appeared |
422 | assert( @output.string.index('press enter/return to continue or q to stop'), |
423 | "Paging message did not appear." ) |
424 | |
425 | # Make sure it only appeared once |
426 | assert( @output.string !~ /q to stop.*q to stop/m, |
427 | "Paging message appeared more than once." ) |
428 | end |
429 | end |