highline 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/test/tc_import.rb CHANGED
@@ -11,16 +11,16 @@ require "highline/import"
11
11
  require "stringio"
12
12
 
13
13
  class TestImport < Test::Unit::TestCase
14
- def test_import
15
- assert_respond_to(self, :agree)
16
- assert_respond_to(self, :ask)
17
- assert_respond_to(self, :choose)
18
- assert_respond_to(self, :say)
19
- end
20
-
21
- def test_redirection
22
- $terminal = HighLine.new(nil, (output = StringIO.new))
23
- say("Testing...")
24
- assert_equal("Testing...\n", output.string)
25
- end
14
+ def test_import
15
+ assert_respond_to(self, :agree)
16
+ assert_respond_to(self, :ask)
17
+ assert_respond_to(self, :choose)
18
+ assert_respond_to(self, :say)
19
+ end
20
+
21
+ def test_redirection
22
+ $terminal = HighLine.new(nil, (output = StringIO.new))
23
+ say("Testing...")
24
+ assert_equal("Testing...\n", output.string)
25
+ end
26
26
  end
data/test/tc_menu.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # tc_menu.rb
4
4
  #
5
5
  # Created by Gregory Thomas Brown on 2005-05-10.
6
- # Copyright 2005 smtose.org. All rights reserved.
6
+ # Copyright 2005. All rights reserved.
7
7
 
8
8
  require "test/unit"
9
9
 
@@ -11,346 +11,346 @@ require "highline"
11
11
  require "stringio"
12
12
 
13
13
  class TestMenu < Test::Unit::TestCase
14
- def setup
15
- @input = StringIO.new
16
- @output = StringIO.new
17
- @terminal = HighLine.new(@input, @output)
18
- end
19
-
20
- def test_choices
21
- @input << "2\n"
22
- @input.rewind
23
-
24
- output = @terminal.choose do |menu|
25
- menu.choices("Sample1", "Sample2", "Sample3")
26
- end
27
- assert_equal("Sample2", output)
28
- end
29
-
30
- def test_flow
31
- @input << "Sample1\n"
32
- @input.rewind
33
-
34
- @terminal.choose do |menu|
35
- # Default: menu.flow = :rows
36
-
37
- menu.choice "Sample1"
38
- menu.choice "Sample2"
39
- menu.choice "Sample3"
40
- end
41
- assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
42
-
43
- @output.truncate(@output.rewind)
44
- @input.rewind
45
-
46
- @terminal.choose do |menu|
47
- menu.flow = :columns_across
48
-
49
- menu.choice "Sample1"
50
- menu.choice "Sample2"
51
- menu.choice "Sample3"
52
- end
53
- assert_equal("1. Sample1 2. Sample2 3. Sample3\n? ", @output.string)
54
-
55
- @output.truncate(@output.rewind)
56
- @input.rewind
57
-
58
- @terminal.choose do |menu|
59
- menu.flow = :inline
60
- menu.index = :none
61
-
62
- menu.choice "Sample1"
63
- menu.choice "Sample2"
64
- menu.choice "Sample3"
65
- end
66
- assert_equal("Sample1, Sample2 or Sample3? ", @output.string)
67
- end
68
-
69
- def test_help
70
- @input << "help\nhelp load\nhelp rules\nhelp missing\n"
71
- @input.rewind
72
-
73
- 4.times do
74
- @terminal.choose do |menu|
75
- menu.shell = true
76
-
77
- menu.choice(:load, "Load a file.")
78
- menu.choice(:save, "Save data in file.")
79
- menu.choice(:quit, "Exit program.")
80
-
81
- menu.help("rules", "The rules of this system are as follows...")
82
- end
83
- end
84
- assert_equal( "1. load\n2. save\n3. quit\n4. help\n? " +
85
- "This command will display helpful messages about " +
86
- "functionality, like this one. To see the help for a " +
87
- "specific topic enter:\n" +
88
- "\thelp [TOPIC]\n" +
89
- "Try asking for help on any of the following:\n" +
90
- "\nload quit rules save \n" +
91
- "1. load\n2. save\n3. quit\n4. help\n? " +
92
- "= load\n\n" +
93
- "Load a file.\n" +
94
- "1. load\n2. save\n3. quit\n4. help\n? " +
95
- "= rules\n\n" +
96
- "The rules of this system are as follows...\n" +
97
- "1. load\n2. save\n3. quit\n4. help\n? " +
98
- "= missing\n\n" +
99
- "There's no help for that topic.\n", @output.string )
100
- end
101
-
102
- def test_index
103
- @input << "Sample1\n"
104
- @input.rewind
105
-
106
- @terminal.choose do |menu|
107
- # Default: menu.index = :number
108
-
109
- menu.choice "Sample1"
110
- menu.choice "Sample2"
111
- menu.choice "Sample3"
112
- end
113
- assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
114
-
115
- @output.truncate(@output.rewind)
116
- @input.rewind
117
-
118
- @terminal.choose do |menu|
119
- menu.index = :letter
120
- menu.index_suffix = ") "
121
-
122
- menu.choice "Sample1"
123
- menu.choice "Sample2"
124
- menu.choice "Sample3"
125
- end
126
- assert_equal("a) Sample1\nb) Sample2\nc) Sample3\n? ", @output.string)
127
-
128
- @output.truncate(@output.rewind)
129
- @input.rewind
130
-
131
- @terminal.choose do |menu|
132
- menu.index = :none
133
-
134
- menu.choice "Sample1"
135
- menu.choice "Sample2"
136
- menu.choice "Sample3"
137
- end
138
- assert_equal("Sample1\nSample2\nSample3\n? ", @output.string)
139
-
140
- @output.truncate(@output.rewind)
141
- @input.rewind
142
-
143
- @terminal.choose do |menu|
144
- menu.index = "*"
145
-
146
- menu.choice "Sample1"
147
- menu.choice "Sample2"
148
- menu.choice "Sample3"
149
- end
150
- assert_equal("* Sample1\n* Sample2\n* Sample3\n? ", @output.string)
151
- end
152
-
153
- def test_layouts
154
- @input << "save\n"
155
- @input.rewind
156
-
157
- @terminal.choose(:load, :save, :quit) # Default: layout = :list
158
- assert_equal("1. load\n2. save\n3. quit\n? ", @output.string)
159
-
160
- @input.rewind
161
- @output.truncate(@output.rewind)
162
-
163
- @terminal.choose(:load, :save, :quit) do |menu|
164
- menu.header = "File Menu"
165
- end
166
- assert_equal( "File Menu:\n" +
167
- "1. load\n2. save\n3. quit\n? ", @output.string )
168
-
169
- @input.rewind
170
- @output.truncate(@output.rewind)
171
-
172
- @terminal.choose(:load, :save, :quit) do |menu|
173
- menu.layout = :one_line
174
- menu.header = "File Menu"
175
- menu.prompt = "Operation? "
176
- end
177
- assert_equal( "File Menu: Operation? " +
178
- "(load, save or quit) ", @output.string )
179
-
180
- @input.rewind
181
- @output.truncate(@output.rewind)
182
-
183
- @terminal.choose(:load, :save, :quit) do |menu|
184
- menu.layout = :menu_only
185
- end
186
- assert_equal("load, save or quit? ", @output.string)
187
-
188
- @input.rewind
189
- @output.truncate(@output.rewind)
190
-
191
- @terminal.choose(:load, :save, :quit) do |menu|
192
- menu.layout = '<%= list(@menu) %>File Menu: '
193
- end
194
- assert_equal("1. load\n2. save\n3. quit\nFile Menu: ", @output.string)
195
- end
196
-
197
- def test_list_option
198
- @input << "l\n"
199
- @input.rewind
200
-
201
- @terminal.choose(:load, :save, :quit) do |menu|
202
- menu.layout = :menu_only
203
- menu.list_option = ", or "
204
- end
205
- assert_equal("load, save, or quit? ", @output.string)
206
- end
207
-
208
- def test_nil_on_handled
209
- @input << "3\n3\n2\n"
210
- @input.rewind
211
-
212
- # Shows that by default proc results are returned.
213
- output = @terminal.choose do |menu|
214
- menu.choice "Sample1" do "output1" end
215
- menu.choice "Sample2" do "output2" end
216
- menu.choice "Sample3" do "output3" end
217
- end
218
- assert_equal("output3", output)
219
-
220
- #
221
- # Shows that they can be replaced with +nil+ by setting
222
- # _nil_on_handled to +true+.
223
- #
224
- output = @terminal.choose do |menu|
225
- menu.nil_on_handled = true
226
- menu.choice "Sample1" do "output1" end
227
- menu.choice "Sample2" do "output2" end
228
- menu.choice "Sample3" do "output3" end
229
- end
230
- assert_equal(nil, output)
231
-
232
- # Shows that a menu item without a proc will be returned no matter what.
233
- output = @terminal.choose do |menu|
234
- menu.choice "Sample1"
235
- menu.choice "Sample2"
236
- menu.choice "Sample3"
237
- end
238
- assert_equal("Sample2", output)
239
- end
240
-
241
- def test_passed_command
242
- @input << "q\n"
243
- @input.rewind
244
-
245
- selected = nil
246
- @terminal.choose do |menu|
247
- menu.choices(:load, :save, :quit) { |command| selected = command }
248
- end
249
- assert_equal(:quit, selected)
250
- end
251
-
252
- def test_question_options
253
- @input << "save\n"
254
- @input.rewind
255
-
256
- answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
257
- menu.case = :capitalize
258
- end
259
- assert_equal(:Save, answer)
260
-
261
- @input.rewind
262
-
263
- answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
264
- menu.case = :capitalize
265
- menu.character = :getc
266
- end
267
- assert_equal(:Save, answer)
268
- assert_equal(?a, @input.getc)
269
- end
270
-
271
- def test_select_by
272
- @input << "Sample1\n2\n"
273
- @input.rewind
274
-
275
- selected = @terminal.choose do |menu|
276
- menu.choice "Sample1"
277
- menu.choice "Sample2"
278
- menu.choice "Sample3"
279
- end
280
- assert_equal("Sample1", selected)
281
-
282
- @input.rewind
283
-
284
- selected = @terminal.choose do |menu|
285
- menu.select_by = :index
286
-
287
- menu.choice "Sample1"
288
- menu.choice "Sample2"
289
- menu.choice "Sample3"
290
- end
291
- assert_equal("Sample2", selected)
292
-
293
- @input.rewind
294
-
295
- selected = @terminal.choose do |menu|
296
- menu.select_by = :name
297
-
298
- menu.choice "Sample1"
299
- menu.choice "Sample2"
300
- menu.choice "Sample3"
301
- end
302
- assert_equal("Sample1", selected)
303
- end
304
-
305
- def test_select_by_letter
306
- @input << "b\n"
307
- @input.rewind
308
-
309
- selected = @terminal.choose do |menu|
310
- menu.index = :letter
311
- menu.choice :save
312
- menu.choice :load
313
- menu.choice :quit
314
- end
315
- assert_equal(:load, selected)
316
- end
317
-
318
- def test_shell
319
- @input << "save --some-option my_file.txt\n"
320
- @input.rewind
321
-
322
- selected = nil
323
- options = nil
324
- answer = @terminal.choose do |menu|
325
- menu.choices(:load, :quit)
326
- menu.choice(:save) do |command, details|
327
- selected = command
328
- options = details
329
-
330
- "Saved!"
331
- end
332
- menu.shell = true
333
- end
334
- assert_equal("Saved!", answer)
335
- assert_equal(:save, selected)
336
- assert_equal("--some-option my_file.txt", options)
337
- end
338
-
339
- def test_simple_menu_shortcut
340
- @input << "3\n"
341
- @input.rewind
342
-
343
- selected = @terminal.choose(:save, :load, :quit)
344
- assert_equal(:quit, selected)
345
- end
346
-
347
- def test_symbols
348
- @input << "3\n"
349
- @input.rewind
350
-
351
- selected = @terminal.choose do |menu|
352
- menu.choices(:save, :load, :quit)
353
- end
354
- assert_equal(:quit, selected)
355
- end
14
+ def setup
15
+ @input = StringIO.new
16
+ @output = StringIO.new
17
+ @terminal = HighLine.new(@input, @output)
18
+ end
19
+
20
+ def test_choices
21
+ @input << "2\n"
22
+ @input.rewind
23
+
24
+ output = @terminal.choose do |menu|
25
+ menu.choices("Sample1", "Sample2", "Sample3")
26
+ end
27
+ assert_equal("Sample2", output)
28
+ end
29
+
30
+ def test_flow
31
+ @input << "Sample1\n"
32
+ @input.rewind
33
+
34
+ @terminal.choose do |menu|
35
+ # Default: menu.flow = :rows
36
+
37
+ menu.choice "Sample1"
38
+ menu.choice "Sample2"
39
+ menu.choice "Sample3"
40
+ end
41
+ assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
42
+
43
+ @output.truncate(@output.rewind)
44
+ @input.rewind
45
+
46
+ @terminal.choose do |menu|
47
+ menu.flow = :columns_across
48
+
49
+ menu.choice "Sample1"
50
+ menu.choice "Sample2"
51
+ menu.choice "Sample3"
52
+ end
53
+ assert_equal("1. Sample1 2. Sample2 3. Sample3\n? ", @output.string)
54
+
55
+ @output.truncate(@output.rewind)
56
+ @input.rewind
57
+
58
+ @terminal.choose do |menu|
59
+ menu.flow = :inline
60
+ menu.index = :none
61
+
62
+ menu.choice "Sample1"
63
+ menu.choice "Sample2"
64
+ menu.choice "Sample3"
65
+ end
66
+ assert_equal("Sample1, Sample2 or Sample3? ", @output.string)
67
+ end
68
+
69
+ def test_help
70
+ @input << "help\nhelp load\nhelp rules\nhelp missing\n"
71
+ @input.rewind
72
+
73
+ 4.times do
74
+ @terminal.choose do |menu|
75
+ menu.shell = true
76
+
77
+ menu.choice(:load, "Load a file.")
78
+ menu.choice(:save, "Save data in file.")
79
+ menu.choice(:quit, "Exit program.")
80
+
81
+ menu.help("rules", "The rules of this system are as follows...")
82
+ end
83
+ end
84
+ assert_equal( "1. load\n2. save\n3. quit\n4. help\n? " +
85
+ "This command will display helpful messages about " +
86
+ "functionality, like this one. To see the help for a " +
87
+ "specific topic enter:\n" +
88
+ "\thelp [TOPIC]\n" +
89
+ "Try asking for help on any of the following:\n" +
90
+ "\nload quit rules save \n" +
91
+ "1. load\n2. save\n3. quit\n4. help\n? " +
92
+ "= load\n\n" +
93
+ "Load a file.\n" +
94
+ "1. load\n2. save\n3. quit\n4. help\n? " +
95
+ "= rules\n\n" +
96
+ "The rules of this system are as follows...\n" +
97
+ "1. load\n2. save\n3. quit\n4. help\n? " +
98
+ "= missing\n\n" +
99
+ "There's no help for that topic.\n", @output.string )
100
+ end
101
+
102
+ def test_index
103
+ @input << "Sample1\n"
104
+ @input.rewind
105
+
106
+ @terminal.choose do |menu|
107
+ # Default: menu.index = :number
108
+
109
+ menu.choice "Sample1"
110
+ menu.choice "Sample2"
111
+ menu.choice "Sample3"
112
+ end
113
+ assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
114
+
115
+ @output.truncate(@output.rewind)
116
+ @input.rewind
117
+
118
+ @terminal.choose do |menu|
119
+ menu.index = :letter
120
+ menu.index_suffix = ") "
121
+
122
+ menu.choice "Sample1"
123
+ menu.choice "Sample2"
124
+ menu.choice "Sample3"
125
+ end
126
+ assert_equal("a) Sample1\nb) Sample2\nc) Sample3\n? ", @output.string)
127
+
128
+ @output.truncate(@output.rewind)
129
+ @input.rewind
130
+
131
+ @terminal.choose do |menu|
132
+ menu.index = :none
133
+
134
+ menu.choice "Sample1"
135
+ menu.choice "Sample2"
136
+ menu.choice "Sample3"
137
+ end
138
+ assert_equal("Sample1\nSample2\nSample3\n? ", @output.string)
139
+
140
+ @output.truncate(@output.rewind)
141
+ @input.rewind
142
+
143
+ @terminal.choose do |menu|
144
+ menu.index = "*"
145
+
146
+ menu.choice "Sample1"
147
+ menu.choice "Sample2"
148
+ menu.choice "Sample3"
149
+ end
150
+ assert_equal("* Sample1\n* Sample2\n* Sample3\n? ", @output.string)
151
+ end
152
+
153
+ def test_layouts
154
+ @input << "save\n"
155
+ @input.rewind
156
+
157
+ @terminal.choose(:load, :save, :quit) # Default: layout = :list
158
+ assert_equal("1. load\n2. save\n3. quit\n? ", @output.string)
159
+
160
+ @input.rewind
161
+ @output.truncate(@output.rewind)
162
+
163
+ @terminal.choose(:load, :save, :quit) do |menu|
164
+ menu.header = "File Menu"
165
+ end
166
+ assert_equal( "File Menu:\n" +
167
+ "1. load\n2. save\n3. quit\n? ", @output.string )
168
+
169
+ @input.rewind
170
+ @output.truncate(@output.rewind)
171
+
172
+ @terminal.choose(:load, :save, :quit) do |menu|
173
+ menu.layout = :one_line
174
+ menu.header = "File Menu"
175
+ menu.prompt = "Operation? "
176
+ end
177
+ assert_equal( "File Menu: Operation? " +
178
+ "(load, save or quit) ", @output.string )
179
+
180
+ @input.rewind
181
+ @output.truncate(@output.rewind)
182
+
183
+ @terminal.choose(:load, :save, :quit) do |menu|
184
+ menu.layout = :menu_only
185
+ end
186
+ assert_equal("load, save or quit? ", @output.string)
187
+
188
+ @input.rewind
189
+ @output.truncate(@output.rewind)
190
+
191
+ @terminal.choose(:load, :save, :quit) do |menu|
192
+ menu.layout = '<%= list(@menu) %>File Menu: '
193
+ end
194
+ assert_equal("1. load\n2. save\n3. quit\nFile Menu: ", @output.string)
195
+ end
196
+
197
+ def test_list_option
198
+ @input << "l\n"
199
+ @input.rewind
200
+
201
+ @terminal.choose(:load, :save, :quit) do |menu|
202
+ menu.layout = :menu_only
203
+ menu.list_option = ", or "
204
+ end
205
+ assert_equal("load, save, or quit? ", @output.string)
206
+ end
207
+
208
+ def test_nil_on_handled
209
+ @input << "3\n3\n2\n"
210
+ @input.rewind
211
+
212
+ # Shows that by default proc results are returned.
213
+ output = @terminal.choose do |menu|
214
+ menu.choice "Sample1" do "output1" end
215
+ menu.choice "Sample2" do "output2" end
216
+ menu.choice "Sample3" do "output3" end
217
+ end
218
+ assert_equal("output3", output)
219
+
220
+ #
221
+ # Shows that they can be replaced with +nil+ by setting
222
+ # _nil_on_handled to +true+.
223
+ #
224
+ output = @terminal.choose do |menu|
225
+ menu.nil_on_handled = true
226
+ menu.choice "Sample1" do "output1" end
227
+ menu.choice "Sample2" do "output2" end
228
+ menu.choice "Sample3" do "output3" end
229
+ end
230
+ assert_equal(nil, output)
231
+
232
+ # Shows that a menu item without a proc will be returned no matter what.
233
+ output = @terminal.choose do |menu|
234
+ menu.choice "Sample1"
235
+ menu.choice "Sample2"
236
+ menu.choice "Sample3"
237
+ end
238
+ assert_equal("Sample2", output)
239
+ end
240
+
241
+ def test_passed_command
242
+ @input << "q\n"
243
+ @input.rewind
244
+
245
+ selected = nil
246
+ @terminal.choose do |menu|
247
+ menu.choices(:load, :save, :quit) { |command| selected = command }
248
+ end
249
+ assert_equal(:quit, selected)
250
+ end
251
+
252
+ def test_question_options
253
+ @input << "save\n"
254
+ @input.rewind
255
+
256
+ answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
257
+ menu.case = :capitalize
258
+ end
259
+ assert_equal(:Save, answer)
260
+
261
+ @input.rewind
262
+
263
+ answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
264
+ menu.case = :capitalize
265
+ menu.character = :getc
266
+ end
267
+ assert_equal(:Save, answer)
268
+ assert_equal(?a, @input.getc)
269
+ end
270
+
271
+ def test_select_by
272
+ @input << "Sample1\n2\n"
273
+ @input.rewind
274
+
275
+ selected = @terminal.choose do |menu|
276
+ menu.choice "Sample1"
277
+ menu.choice "Sample2"
278
+ menu.choice "Sample3"
279
+ end
280
+ assert_equal("Sample1", selected)
281
+
282
+ @input.rewind
283
+
284
+ selected = @terminal.choose do |menu|
285
+ menu.select_by = :index
286
+
287
+ menu.choice "Sample1"
288
+ menu.choice "Sample2"
289
+ menu.choice "Sample3"
290
+ end
291
+ assert_equal("Sample2", selected)
292
+
293
+ @input.rewind
294
+
295
+ selected = @terminal.choose do |menu|
296
+ menu.select_by = :name
297
+
298
+ menu.choice "Sample1"
299
+ menu.choice "Sample2"
300
+ menu.choice "Sample3"
301
+ end
302
+ assert_equal("Sample1", selected)
303
+ end
304
+
305
+ def test_select_by_letter
306
+ @input << "b\n"
307
+ @input.rewind
308
+
309
+ selected = @terminal.choose do |menu|
310
+ menu.index = :letter
311
+ menu.choice :save
312
+ menu.choice :load
313
+ menu.choice :quit
314
+ end
315
+ assert_equal(:load, selected)
316
+ end
317
+
318
+ def test_shell
319
+ @input << "save --some-option my_file.txt\n"
320
+ @input.rewind
321
+
322
+ selected = nil
323
+ options = nil
324
+ answer = @terminal.choose do |menu|
325
+ menu.choices(:load, :quit)
326
+ menu.choice(:save) do |command, details|
327
+ selected = command
328
+ options = details
329
+
330
+ "Saved!"
331
+ end
332
+ menu.shell = true
333
+ end
334
+ assert_equal("Saved!", answer)
335
+ assert_equal(:save, selected)
336
+ assert_equal("--some-option my_file.txt", options)
337
+ end
338
+
339
+ def test_simple_menu_shortcut
340
+ @input << "3\n"
341
+ @input.rewind
342
+
343
+ selected = @terminal.choose(:save, :load, :quit)
344
+ assert_equal(:quit, selected)
345
+ end
346
+
347
+ def test_symbols
348
+ @input << "3\n"
349
+ @input.rewind
350
+
351
+ selected = @terminal.choose do |menu|
352
+ menu.choices(:save, :load, :quit)
353
+ end
354
+ assert_equal(:quit, selected)
355
+ end
356
356
  end