highline 0.5.0 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -17,6 +17,7 @@ class TestImport < Test::Unit::TestCase
17
17
  def test_import
18
18
  assert_respond_to(self, :agree)
19
19
  assert_respond_to(self, :ask)
20
+ assert_respond_to(self, :choose)
20
21
  assert_respond_to(self, :say)
21
22
  end
22
23
 
@@ -0,0 +1,326 @@
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 smtose.org. All rights reserved.
7
+
8
+ $test_lib_dir ||= File.join(File.dirname(__FILE__), "..", "lib")
9
+ $:.unshift($test_lib_dir) unless $:.include?($test_lib_dir)
10
+
11
+ require "test/unit"
12
+
13
+ require "highline"
14
+ require "stringio"
15
+
16
+ class TestMenu < Test::Unit::TestCase
17
+ def setup
18
+ @input = StringIO.new
19
+ @output = StringIO.new
20
+ @terminal = HighLine.new(@input, @output)
21
+ end
22
+
23
+ def test_choices
24
+ @input << "2\n"
25
+ @input.rewind
26
+
27
+ output = @terminal.choose do |menu|
28
+ menu.choices("Sample1", "Sample2", "Sample3")
29
+ end
30
+ assert_equal("Sample2", output)
31
+ end
32
+
33
+ def test_flow
34
+ @input << "Sample1\n"
35
+ @input.rewind
36
+
37
+ @terminal.choose do |menu|
38
+ # Default: menu.flow = :rows
39
+
40
+ menu.choice "Sample1"
41
+ menu.choice "Sample2"
42
+ menu.choice "Sample3"
43
+ end
44
+ assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
45
+
46
+ @output.truncate(@output.rewind)
47
+ @input.rewind
48
+
49
+ @terminal.choose do |menu|
50
+ menu.flow = :columns_across
51
+
52
+ menu.choice "Sample1"
53
+ menu.choice "Sample2"
54
+ menu.choice "Sample3"
55
+ end
56
+ assert_equal("1. Sample1 2. Sample2 3. Sample3\n? ", @output.string)
57
+
58
+ @output.truncate(@output.rewind)
59
+ @input.rewind
60
+
61
+ @terminal.choose do |menu|
62
+ menu.flow = :inline
63
+ menu.index = :none
64
+
65
+ menu.choice "Sample1"
66
+ menu.choice "Sample2"
67
+ menu.choice "Sample3"
68
+ end
69
+ assert_equal("Sample1, Sample2 or Sample3? ", @output.string)
70
+ end
71
+
72
+ def test_index
73
+ @input << "Sample1\n"
74
+ @input.rewind
75
+
76
+ @terminal.choose do |menu|
77
+ # Default: menu.index = :number
78
+
79
+ menu.choice "Sample1"
80
+ menu.choice "Sample2"
81
+ menu.choice "Sample3"
82
+ end
83
+ assert_equal("1. Sample1\n2. Sample2\n3. Sample3\n? ", @output.string)
84
+
85
+ @output.truncate(@output.rewind)
86
+ @input.rewind
87
+
88
+ @terminal.choose do |menu|
89
+ menu.index = :letter
90
+ menu.index_suffix = ") "
91
+
92
+ menu.choice "Sample1"
93
+ menu.choice "Sample2"
94
+ menu.choice "Sample3"
95
+ end
96
+ assert_equal("a) Sample1\nb) Sample2\nc) Sample3\n? ", @output.string)
97
+
98
+ @output.truncate(@output.rewind)
99
+ @input.rewind
100
+
101
+ @terminal.choose do |menu|
102
+ menu.index = :none
103
+
104
+ menu.choice "Sample1"
105
+ menu.choice "Sample2"
106
+ menu.choice "Sample3"
107
+ end
108
+ assert_equal("Sample1\nSample2\nSample3\n? ", @output.string)
109
+
110
+ @output.truncate(@output.rewind)
111
+ @input.rewind
112
+
113
+ @terminal.choose do |menu|
114
+ menu.index = "*"
115
+
116
+ menu.choice "Sample1"
117
+ menu.choice "Sample2"
118
+ menu.choice "Sample3"
119
+ end
120
+ assert_equal("* Sample1\n* Sample2\n* Sample3\n? ", @output.string)
121
+ end
122
+
123
+ def test_layouts
124
+ @input << "save\n"
125
+ @input.rewind
126
+
127
+ @terminal.choose(:load, :save, :quit) # Default: layout = :list
128
+ assert_equal("1. load\n2. save\n3. quit\n? ", @output.string)
129
+
130
+ @input.rewind
131
+ @output.truncate(@output.rewind)
132
+
133
+ @terminal.choose(:load, :save, :quit) do |menu|
134
+ menu.header = "File Menu"
135
+ end
136
+ assert_equal( "File Menu:\n" +
137
+ "1. load\n2. save\n3. quit\n? ", @output.string )
138
+
139
+ @input.rewind
140
+ @output.truncate(@output.rewind)
141
+
142
+ @terminal.choose(:load, :save, :quit) do |menu|
143
+ menu.layout = :one_line
144
+ menu.header = "File Menu"
145
+ menu.prompt = "Operation? "
146
+ end
147
+ assert_equal( "File Menu: Operation? " +
148
+ "(load, save or quit) ", @output.string )
149
+
150
+ @input.rewind
151
+ @output.truncate(@output.rewind)
152
+
153
+ @terminal.choose(:load, :save, :quit) do |menu|
154
+ menu.layout = :menu_only
155
+ end
156
+ assert_equal("load, save or quit? ", @output.string)
157
+
158
+ @input.rewind
159
+ @output.truncate(@output.rewind)
160
+
161
+ @terminal.choose(:load, :save, :quit) do |menu|
162
+ menu.layout = '<%= list(@menu) %>File Menu: '
163
+ end
164
+ assert_equal("1. load\n2. save\n3. quit\nFile Menu: ", @output.string)
165
+ end
166
+
167
+ def test_list_option
168
+ @input << "l\n"
169
+ @input.rewind
170
+
171
+ @terminal.choose(:load, :save, :quit) do |menu|
172
+ menu.layout = :menu_only
173
+ menu.list_option = ", or "
174
+ end
175
+ assert_equal("load, save, or quit? ", @output.string)
176
+ end
177
+
178
+ def test_nil_on_handled
179
+ @input << "3\n3\n2\n"
180
+ @input.rewind
181
+
182
+ # Shows that by default proc results are returned.
183
+ output = @terminal.choose do |menu|
184
+ menu.choice "Sample1" do "output1" end
185
+ menu.choice "Sample2" do "output2" end
186
+ menu.choice "Sample3" do "output3" end
187
+ end
188
+ assert_equal("output3", output)
189
+
190
+ #
191
+ # Shows that they can be replaced with +nil+ by setting
192
+ # _nil_on_handled to +true+.
193
+ #
194
+ output = @terminal.choose do |menu|
195
+ menu.nil_on_handled = true
196
+ menu.choice "Sample1" do "output1" end
197
+ menu.choice "Sample2" do "output2" end
198
+ menu.choice "Sample3" do "output3" end
199
+ end
200
+ assert_equal(nil, output)
201
+
202
+ # Shows that a menu item without a proc will be returned no matter what.
203
+ output = @terminal.choose do |menu|
204
+ menu.choice "Sample1"
205
+ menu.choice "Sample2"
206
+ menu.choice "Sample3"
207
+ end
208
+ assert_equal("Sample2", output)
209
+ end
210
+
211
+ def test_passed_command
212
+ @input << "q\n"
213
+ @input.rewind
214
+
215
+ selected = nil
216
+ @terminal.choose do |menu|
217
+ menu.choices(:load, :save, :quit) { |command| selected = command }
218
+ end
219
+ assert_equal(:quit, selected)
220
+ end
221
+
222
+ def test_question_options
223
+ @input << "save\n"
224
+ @input.rewind
225
+
226
+ answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
227
+ menu.case = :capitalize
228
+ end
229
+ assert_equal(:Save, answer)
230
+
231
+ @input.rewind
232
+
233
+ answer = @terminal.choose(:Load, :Save, :Quit) do |menu|
234
+ menu.case = :capitalize
235
+ menu.character = :getc
236
+ end
237
+ assert_equal(:Save, answer)
238
+ assert_equal(?a, @input.getc)
239
+ end
240
+
241
+ def test_select_by
242
+ @input << "Sample1\n2\n"
243
+ @input.rewind
244
+
245
+ selected = @terminal.choose do |menu|
246
+ menu.choice "Sample1"
247
+ menu.choice "Sample2"
248
+ menu.choice "Sample3"
249
+ end
250
+ assert_equal("Sample1", selected)
251
+
252
+ @input.rewind
253
+
254
+ selected = @terminal.choose do |menu|
255
+ menu.select_by = :index
256
+
257
+ menu.choice "Sample1"
258
+ menu.choice "Sample2"
259
+ menu.choice "Sample3"
260
+ end
261
+ assert_equal("Sample2", selected)
262
+
263
+ @input.rewind
264
+
265
+ selected = @terminal.choose do |menu|
266
+ menu.select_by = :name
267
+
268
+ menu.choice "Sample1"
269
+ menu.choice "Sample2"
270
+ menu.choice "Sample3"
271
+ end
272
+ assert_equal("Sample1", selected)
273
+ end
274
+
275
+ def test_select_by_letter
276
+ @input << "b\n"
277
+ @input.rewind
278
+
279
+ selected = @terminal.choose do |menu|
280
+ menu.index = :letter
281
+ menu.choice :save
282
+ menu.choice :load
283
+ menu.choice :quit
284
+ end
285
+ assert_equal(:load, selected)
286
+ end
287
+
288
+ def test_shell
289
+ @input << "save --some-option my_file.txt\n"
290
+ @input.rewind
291
+
292
+ selected = nil
293
+ options = nil
294
+ answer = @terminal.choose(:load, :save, :quit) do |menu|
295
+ menu.choices(:load, :quit)
296
+ menu.choice(:save) do |command, details|
297
+ selected = command
298
+ options = details
299
+
300
+ "Saved!"
301
+ end
302
+ menu.shell = true
303
+ end
304
+ assert_equal("Saved!", answer)
305
+ assert_equal(:save, selected)
306
+ assert_equal("--some-option my_file.txt", options)
307
+ end
308
+
309
+ def test_simple_menu_shortcut
310
+ @input << "3\n"
311
+ @input.rewind
312
+
313
+ selected = @terminal.choose(:save, :load, :quit)
314
+ assert_equal(:quit, selected)
315
+ end
316
+
317
+ def test_symbols
318
+ @input << "3\n"
319
+ @input.rewind
320
+
321
+ selected = @terminal.choose do |menu|
322
+ menu.choices(:save, :load, :quit)
323
+ end
324
+ assert_equal(:quit, selected)
325
+ end
326
+ end
@@ -12,3 +12,4 @@ require "test/unit"
12
12
 
13
13
  require "tc_highline"
14
14
  require "tc_import"
15
+ require "tc_menu"
metadata CHANGED
@@ -3,8 +3,8 @@ rubygems_version: 0.8.10
3
3
  specification_version: 1
4
4
  name: highline
5
5
  version: !ruby/object:Gem::Version
6
- version: 0.5.0
7
- date: 2005-05-09
6
+ version: 0.6.0
7
+ date: 2005-05-20
8
8
  summary: HighLine is a high-level line oriented console interface.
9
9
  require_paths:
10
10
  - lib
@@ -36,9 +36,11 @@ files:
36
36
  - examples/password.rb
37
37
  - lib/highline.rb
38
38
  - lib/highline/import.rb
39
+ - lib/highline/menu.rb
39
40
  - lib/highline/question.rb
40
41
  - test/tc_highline.rb
41
42
  - test/tc_import.rb
43
+ - test/tc_menu.rb
42
44
  - test/ts_all.rb
43
45
  - Rakefile
44
46
  - setup.rb
@@ -63,4 +65,14 @@ extra_rdoc_files:
63
65
  executables: []
64
66
  extensions: []
65
67
  requirements: []
66
- dependencies: []
68
+ dependencies:
69
+ - !ruby/object:Gem::Dependency
70
+ name: termios
71
+ version_requirement:
72
+ version_requirements: !ruby/object:Gem::Version::Requirement
73
+ requirements:
74
+ -
75
+ - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 0.9.4
78
+ version: