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_highline.rb CHANGED
@@ -11,733 +11,745 @@ require "highline"
11
11
  require "stringio"
12
12
 
13
13
  class TestHighLine < 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_agree
21
- @input << "y\nyes\nYES\nHell no!\nNo\n"
22
- @input.rewind
23
-
24
- assert_equal(true, @terminal.agree("Yes or no? "))
25
- assert_equal(true, @terminal.agree("Yes or no? "))
26
- assert_equal(true, @terminal.agree("Yes or no? "))
27
- assert_equal(false, @terminal.agree("Yes or no? "))
28
-
29
- @input.truncate(@input.rewind)
30
- @input << "yellow"
31
- @input.rewind
32
-
33
- assert_equal(true, @terminal.agree("Yes or no? ", :getc))
34
- end
35
-
36
- def test_ask
37
- name = "James Edward Gray II"
38
- @input << name << "\n"
39
- @input.rewind
40
-
41
- assert_equal(name, @terminal.ask("What is your name? "))
42
- end
43
-
44
- def test_bug_fixes
45
- # auto-complete bug
46
- @input << "ruby\nRuby\n"
47
- @input.rewind
48
-
49
- languages = [:Perl, :Python, :Ruby]
50
- answer = @terminal.ask( "What is your favorite programming language? ",
51
- languages )
52
- assert_equal(languages.last, answer)
53
-
54
- @input.truncate(@input.rewind)
55
- @input << "ruby\n"
56
- @input.rewind
57
-
58
- answer = @terminal.ask( "What is your favorite programming language? ",
59
- languages ) do |q|
60
- q.case = :capitalize
61
- end
62
- assert_equal(languages.last, answer)
63
-
64
- # poor auto-complete error message
65
- @input.truncate(@input.rewind)
66
- @input << "lisp\nruby\n"
67
- @input.rewind
68
- @output.truncate(@output.rewind)
69
-
70
- answer = @terminal.ask( "What is your favorite programming language? ",
71
- languages ) do |q|
72
- q.case = :capitalize
73
- end
74
- assert_equal(languages.last, answer)
75
- assert_equal( "What is your favorite programming language? " +
76
- "You must choose one of [:Perl, :Python, :Ruby].\n" +
77
- "? ", @output.string )
78
- end
79
-
80
- def test_case_changes
81
- @input << "jeg2\n"
82
- @input.rewind
83
-
84
- answer = @terminal.ask("Enter your initials ") do |q|
85
- q.case = :up
86
- end
87
- assert_equal("JEG2", answer)
88
-
89
- @input.truncate(@input.rewind)
90
- @input << "cRaZY\n"
91
- @input.rewind
92
-
93
- answer = @terminal.ask("Enter a search string: ") do |q|
94
- q.case = :down
95
- end
96
- assert_equal("crazy", answer)
97
- end
98
-
99
- def test_character_echo
100
- @input << "password\r"
101
- @input.rewind
102
-
103
- answer = @terminal.ask("Please enter your password: ") do |q|
104
- q.echo = "*"
105
- end
106
- assert_equal("password", answer)
107
- assert_equal("Please enter your password: ********\n", @output.string)
108
-
109
- @input.truncate(@input.rewind)
110
- @input << "2"
111
- @input.rewind
112
- @output.truncate(@output.rewind)
113
-
114
- answer = @terminal.ask( "Select an option (1, 2 or 2): ",
115
- Integer ) do |q|
116
- q.echo = "*"
117
- q.character = true
118
- end
119
- assert_equal(2, answer)
120
- assert_equal("Select an option (1, 2 or 2): *\n", @output.string)
121
- end
122
-
123
- def test_character_reading
124
- # WARNING: This method does NOT cover Unix and Windows savvy testing!
125
- @input << "12345"
126
- @input.rewind
127
-
128
- answer = @terminal.ask("Enter a single digit: ", Integer) do |q|
129
- q.character = :getc
130
- end
131
- assert_equal(1, answer)
132
- end
133
-
134
- def test_color
135
- @terminal.say("This should be <%= BLUE %>blue<%= CLEAR %>!")
136
- assert_equal("This should be \e[34mblue\e[0m!\n", @output.string)
137
-
138
- @output.truncate(@output.rewind)
139
-
140
- @terminal.say( "This should be " +
141
- "<%= BOLD + ON_WHITE %>bold on white<%= CLEAR %>!" )
142
- assert_equal( "This should be \e[1m\e[47mbold on white\e[0m!\n",
143
- @output.string )
144
-
145
- @output.truncate(@output.rewind)
146
-
147
- @terminal.say("This should be <%= color('cyan', CYAN) %>!")
148
- assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string)
149
-
150
- @output.truncate(@output.rewind)
151
-
152
- @terminal.say( "This should be " +
153
- "<%= color('blinking on red', :blink, :on_red) %>!" )
154
- assert_equal( "This should be \e[5m\e[41mblinking on red\e[0m!\n",
155
- @output.string )
156
- end
157
-
158
- def test_confirm
159
- @input << "junk.txt\nno\nsave.txt\ny\n"
160
- @input.rewind
161
-
162
- answer = @terminal.ask("Enter a filename: ") do |q|
163
- q.confirm = "Are you sure you want to overwrite <%= @answer %>? "
164
- q.responses[:ask_on_error] = :question
165
- end
166
- assert_equal("save.txt", answer)
167
- assert_equal( "Enter a filename: " +
168
- "Are you sure you want to overwrite junk.txt? " +
169
- "Enter a filename: " +
170
- "Are you sure you want to overwrite save.txt? ",
171
- @output.string )
172
-
173
- @input.truncate(@input.rewind)
174
- @input << "junk.txt\nyes\nsave.txt\nn\n"
175
- @input.rewind
176
- @output.truncate(@output.rewind)
177
-
178
- answer = @terminal.ask("Enter a filename: ") do |q|
179
- q.confirm = "Are you sure you want to overwrite <%= @answer %>? "
180
- end
181
- assert_equal("junk.txt", answer)
182
- assert_equal( "Enter a filename: " +
183
- "Are you sure you want to overwrite junk.txt? ",
184
- @output.string )
185
- end
186
-
187
- def test_defaults
188
- @input << "\nNo Comment\n"
189
- @input.rewind
190
-
191
- answer = @terminal.ask("Are you sexually active? ") do |q|
192
- q.validate = /\Ay(?:es)?|no?|no comment\Z/i
193
- end
194
- assert_equal("No Comment", answer)
195
-
196
- @input.truncate(@input.rewind)
197
- @input << "\nYes\n"
198
- @input.rewind
199
- @output.truncate(@output.rewind)
200
-
201
- answer = @terminal.ask("Are you sexually active? ") do |q|
202
- q.default = "No Comment"
203
- q.validate = /\Ay(?:es)?|no?|no comment\Z/i
204
- end
205
- assert_equal("No Comment", answer)
206
- assert_equal( "Are you sexually active? |No Comment| ",
207
- @output.string )
208
- end
209
-
210
- def test_empty
211
- @input << "\n"
212
- @input.rewind
213
-
214
- answer = @terminal.ask("") do |q|
215
- q.default = "yes"
216
- q.validate = /\Ay(?:es)?|no?\Z/i
217
- end
218
- assert_equal("yes", answer)
219
- end
220
-
221
- def test_erb
222
- @terminal.say( "The integers from 1 to 10 are:\n" +
223
- "% (1...10).each do |n|\n" +
224
- "\t<%= n %>,\n" +
225
- "% end\n" +
226
- "\tand 10" )
227
- assert_equal( "The integers from 1 to 10 are:\n" +
228
- "\t1,\n\t2,\n\t3,\n\t4,\n\t5,\n" +
229
- "\t6,\n\t7,\n\t8,\n\t9,\n\tand 10\n",
230
- @output.string )
231
- end
232
-
233
- def test_files
234
- @input << "#{File.basename(__FILE__)[0, 5]}\n"
235
- @input.rewind
236
-
237
- file = @terminal.ask("Select a file: ", File) do |q|
238
- q.directory = File.expand_path(File.dirname(__FILE__))
239
- q.glob = "*.rb"
240
- end
241
- assert_instance_of(File, file)
242
- assert_equal("#!/usr/local/bin/ruby -w\n", file.gets)
243
- assert_equal("\n", file.gets)
244
- assert_equal("# tc_highline.rb\n", file.gets)
245
- file.close
246
-
247
- @input.rewind
248
-
249
- pathname = @terminal.ask("Select a file: ", Pathname) do |q|
250
- q.directory = File.expand_path(File.dirname(__FILE__))
251
- q.glob = "*.rb"
252
- end
253
- assert_instance_of(Pathname, pathname)
254
- assert_equal(File.size(__FILE__), pathname.size)
255
- end
256
-
257
- def test_gather
258
- @input << "James\nDana\nStorm\nGypsy\n\n"
259
- @input.rewind
260
-
261
- answers = @terminal.ask("Enter four names:") do |q|
262
- q.gather = 4
263
- end
264
- assert_equal(%w{James Dana Storm Gypsy}, answers)
265
- assert_equal("\n", @input.gets)
266
- assert_equal("Enter four names:\n", @output.string)
267
-
268
- @input.rewind
269
-
270
- answers = @terminal.ask("Enter four names:") do |q|
271
- q.gather = ""
272
- end
273
- assert_equal(%w{James Dana Storm Gypsy}, answers)
274
-
275
- @input.rewind
276
-
277
- answers = @terminal.ask("Enter four names:") do |q|
278
- q.gather = /^\s*$/
279
- end
280
- assert_equal(%w{James Dana Storm Gypsy}, answers)
281
-
282
- @input.truncate(@input.rewind)
283
- @input << "29\n49\n30\n"
284
- @input.rewind
285
- @output.truncate(@output.rewind)
286
-
287
- answers = @terminal.ask("<%= @key %>: ", Integer) do |q|
288
- q.gather = { "Age" => 0, "Wife's Age" => 0, "Father's Age" => 0}
289
- end
290
- assert_equal( { "Age" => 29, "Wife's Age" => 30, "Father's Age" => 49},
291
- answers )
292
- assert_equal("Age: Father's Age: Wife's Age: ", @output.string)
293
- end
294
-
295
- def test_lists
296
- digits = %w{Zero One Two Three Four Five Six Seven Eight Nine}
297
-
298
- @terminal.say("<%= list(#{digits.inspect}) %>")
299
- assert_equal(digits.map { |d| "#{d}\n" }.join, @output.string)
300
-
301
- @output.truncate(@output.rewind)
302
-
303
- @terminal.say("<%= list(#{digits.inspect}, :inline) %>")
304
- assert_equal( digits[0..-2].join(", ") + " or #{digits.last}\n",
305
- @output.string )
306
-
307
- @output.truncate(@output.rewind)
308
-
309
- @terminal.say("<%= list(#{digits.inspect}, :inline, ' and ') %>")
310
- assert_equal( digits[0..-2].join(", ") + " and #{digits.last}\n",
311
- @output.string )
312
-
313
- @output.truncate(@output.rewind)
314
-
315
- @terminal.say("<%= list(#{digits.inspect}, :columns_down, 3) %>")
316
- assert_equal( "Zero Four Eight\n" +
317
- "One Five Nine \n" +
318
- "Two Six \n" +
319
- "Three Seven\n",
320
- @output.string )
321
-
322
- colums_of_twenty = ["12345678901234567890"] * 5
323
-
324
- @output.truncate(@output.rewind)
325
-
326
- @terminal.say("<%= list(#{colums_of_twenty.inspect}, :columns_down) %>")
327
- assert_equal( "12345678901234567890 12345678901234567890 " +
328
- "12345678901234567890\n" +
329
- "12345678901234567890 12345678901234567890\n",
330
- @output.string )
331
-
332
- @output.truncate(@output.rewind)
333
-
334
- @terminal.say("<%= list(#{digits.inspect}, :columns_across, 3) %>")
335
- assert_equal( "Zero One Two \n" +
336
- "Three Four Five \n" +
337
- "Six Seven Eight\n" +
338
- "Nine \n",
339
- @output.string )
14
+ def setup
15
+ @input = StringIO.new
16
+ @output = StringIO.new
17
+ @terminal = HighLine.new(@input, @output)
18
+ end
19
+
20
+ def test_agree
21
+ @input << "y\nyes\nYES\nHell no!\nNo\n"
22
+ @input.rewind
23
+
24
+ assert_equal(true, @terminal.agree("Yes or no? "))
25
+ assert_equal(true, @terminal.agree("Yes or no? "))
26
+ assert_equal(true, @terminal.agree("Yes or no? "))
27
+ assert_equal(false, @terminal.agree("Yes or no? "))
28
+
29
+ @input.truncate(@input.rewind)
30
+ @input << "yellow"
31
+ @input.rewind
32
+
33
+ assert_equal(true, @terminal.agree("Yes or no? ", :getc))
34
+ end
35
+
36
+ def test_ask
37
+ name = "James Edward Gray II"
38
+ @input << name << "\n"
39
+ @input.rewind
40
+
41
+ assert_equal(name, @terminal.ask("What is your name? "))
42
+
43
+ assert_raise(EOFError) { @terminal.ask("Any input left? ") }
44
+ end
45
+
46
+ def test_bug_fixes
47
+ # auto-complete bug
48
+ @input << "ruby\nRuby\n"
49
+ @input.rewind
50
+
51
+ languages = [:Perl, :Python, :Ruby]
52
+ answer = @terminal.ask( "What is your favorite programming language? ",
53
+ languages )
54
+ assert_equal(languages.last, answer)
55
+
56
+ @input.truncate(@input.rewind)
57
+ @input << "ruby\n"
58
+ @input.rewind
59
+
60
+ answer = @terminal.ask( "What is your favorite programming language? ",
61
+ languages ) do |q|
62
+ q.case = :capitalize
63
+ end
64
+ assert_equal(languages.last, answer)
65
+
66
+ # poor auto-complete error message
67
+ @input.truncate(@input.rewind)
68
+ @input << "lisp\nruby\n"
69
+ @input.rewind
70
+ @output.truncate(@output.rewind)
71
+
72
+ answer = @terminal.ask( "What is your favorite programming language? ",
73
+ languages ) do |q|
74
+ q.case = :capitalize
75
+ end
76
+ assert_equal(languages.last, answer)
77
+ assert_equal( "What is your favorite programming language? " +
78
+ "You must choose one of [:Perl, :Python, :Ruby].\n" +
79
+ "? ", @output.string )
80
+ end
81
+
82
+ def test_case_changes
83
+ @input << "jeg2\n"
84
+ @input.rewind
85
+
86
+ answer = @terminal.ask("Enter your initials ") do |q|
87
+ q.case = :up
88
+ end
89
+ assert_equal("JEG2", answer)
90
+
91
+ @input.truncate(@input.rewind)
92
+ @input << "cRaZY\n"
93
+ @input.rewind
94
+
95
+ answer = @terminal.ask("Enter a search string: ") do |q|
96
+ q.case = :down
97
+ end
98
+ assert_equal("crazy", answer)
99
+ end
100
+
101
+ def test_character_echo
102
+ @input << "password\r"
103
+ @input.rewind
104
+
105
+ answer = @terminal.ask("Please enter your password: ") do |q|
106
+ q.echo = "*"
107
+ end
108
+ assert_equal("password", answer)
109
+ assert_equal("Please enter your password: ********\n", @output.string)
110
+
111
+ @input.truncate(@input.rewind)
112
+ @input << "2"
113
+ @input.rewind
114
+ @output.truncate(@output.rewind)
115
+
116
+ answer = @terminal.ask( "Select an option (1, 2 or 3): ",
117
+ Integer ) do |q|
118
+ q.echo = "*"
119
+ q.character = true
120
+ end
121
+ assert_equal(2, answer)
122
+ assert_equal("Select an option (1, 2 or 3): *\n", @output.string)
123
+ end
124
+
125
+ def test_character_reading
126
+ # WARNING: This method does NOT cover Unix and Windows savvy testing!
127
+ @input << "12345"
128
+ @input.rewind
129
+
130
+ answer = @terminal.ask("Enter a single digit: ", Integer) do |q|
131
+ q.character = :getc
132
+ end
133
+ assert_equal(1, answer)
134
+ end
135
+
136
+ def test_color
137
+ @terminal.say("This should be <%= BLUE %>blue<%= CLEAR %>!")
138
+ assert_equal("This should be \e[34mblue\e[0m!\n", @output.string)
139
+
140
+ @output.truncate(@output.rewind)
141
+
142
+ @terminal.say( "This should be " +
143
+ "<%= BOLD + ON_WHITE %>bold on white<%= CLEAR %>!" )
144
+ assert_equal( "This should be \e[1m\e[47mbold on white\e[0m!\n",
145
+ @output.string )
146
+
147
+ @output.truncate(@output.rewind)
148
+
149
+ @terminal.say("This should be <%= color('cyan', CYAN) %>!")
150
+ assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string)
151
+
152
+ @output.truncate(@output.rewind)
153
+
154
+ @terminal.say( "This should be " +
155
+ "<%= color('blinking on red', :blink, :on_red) %>!" )
156
+ assert_equal( "This should be \e[5m\e[41mblinking on red\e[0m!\n",
157
+ @output.string )
158
+ end
159
+
160
+ def test_confirm
161
+ @input << "junk.txt\nno\nsave.txt\ny\n"
162
+ @input.rewind
163
+
164
+ answer = @terminal.ask("Enter a filename: ") do |q|
165
+ q.confirm = "Are you sure you want to overwrite <%= @answer %>? "
166
+ q.responses[:ask_on_error] = :question
167
+ end
168
+ assert_equal("save.txt", answer)
169
+ assert_equal( "Enter a filename: " +
170
+ "Are you sure you want to overwrite junk.txt? " +
171
+ "Enter a filename: " +
172
+ "Are you sure you want to overwrite save.txt? ",
173
+ @output.string )
174
+
175
+ @input.truncate(@input.rewind)
176
+ @input << "junk.txt\nyes\nsave.txt\nn\n"
177
+ @input.rewind
178
+ @output.truncate(@output.rewind)
179
+
180
+ answer = @terminal.ask("Enter a filename: ") do |q|
181
+ q.confirm = "Are you sure you want to overwrite <%= @answer %>? "
182
+ end
183
+ assert_equal("junk.txt", answer)
184
+ assert_equal( "Enter a filename: " +
185
+ "Are you sure you want to overwrite junk.txt? ",
186
+ @output.string )
187
+ end
188
+
189
+ def test_defaults
190
+ @input << "\nNo Comment\n"
191
+ @input.rewind
192
+
193
+ answer = @terminal.ask("Are you sexually active? ") do |q|
194
+ q.validate = /\Ay(?:es)?|no?|no comment\Z/i
195
+ end
196
+ assert_equal("No Comment", answer)
197
+
198
+ @input.truncate(@input.rewind)
199
+ @input << "\nYes\n"
200
+ @input.rewind
201
+ @output.truncate(@output.rewind)
202
+
203
+ answer = @terminal.ask("Are you sexually active? ") do |q|
204
+ q.default = "No Comment"
205
+ q.validate = /\Ay(?:es)?|no?|no comment\Z/i
206
+ end
207
+ assert_equal("No Comment", answer)
208
+ assert_equal( "Are you sexually active? |No Comment| ",
209
+ @output.string )
210
+ end
211
+
212
+ def test_empty
213
+ @input << "\n"
214
+ @input.rewind
215
+
216
+ answer = @terminal.ask("") do |q|
217
+ q.default = "yes"
218
+ q.validate = /\Ay(?:es)?|no?\Z/i
219
+ end
220
+ assert_equal("yes", answer)
221
+ end
222
+
223
+ def test_erb
224
+ @terminal.say( "The integers from 1 to 10 are:\n" +
225
+ "% (1...10).each do |n|\n" +
226
+ "\t<%= n %>,\n" +
227
+ "% end\n" +
228
+ "\tand 10" )
229
+ assert_equal( "The integers from 1 to 10 are:\n" +
230
+ "\t1,\n\t2,\n\t3,\n\t4,\n\t5,\n" +
231
+ "\t6,\n\t7,\n\t8,\n\t9,\n\tand 10\n",
232
+ @output.string )
233
+ end
234
+
235
+ def test_files
236
+ @input << "#{File.basename(__FILE__)[0, 5]}\n"
237
+ @input.rewind
238
+
239
+ file = @terminal.ask("Select a file: ", File) do |q|
240
+ q.directory = File.expand_path(File.dirname(__FILE__))
241
+ q.glob = "*.rb"
242
+ end
243
+ assert_instance_of(File, file)
244
+ assert_equal("#!/usr/local/bin/ruby -w\n", file.gets)
245
+ assert_equal("\n", file.gets)
246
+ assert_equal("# tc_highline.rb\n", file.gets)
247
+ file.close
248
+
249
+ @input.rewind
250
+
251
+ pathname = @terminal.ask("Select a file: ", Pathname) do |q|
252
+ q.directory = File.expand_path(File.dirname(__FILE__))
253
+ q.glob = "*.rb"
254
+ end
255
+ assert_instance_of(Pathname, pathname)
256
+ assert_equal(File.size(__FILE__), pathname.size)
257
+ end
258
+
259
+ def test_gather
260
+ @input << "James\nDana\nStorm\nGypsy\n\n"
261
+ @input.rewind
262
+
263
+ answers = @terminal.ask("Enter four names:") do |q|
264
+ q.gather = 4
265
+ end
266
+ assert_equal(%w{James Dana Storm Gypsy}, answers)
267
+ assert_equal("\n", @input.gets)
268
+ assert_equal("Enter four names:\n", @output.string)
269
+
270
+ @input.rewind
271
+
272
+ answers = @terminal.ask("Enter four names:") do |q|
273
+ q.gather = ""
274
+ end
275
+ assert_equal(%w{James Dana Storm Gypsy}, answers)
276
+
277
+ @input.rewind
278
+
279
+ answers = @terminal.ask("Enter four names:") do |q|
280
+ q.gather = /^\s*$/
281
+ end
282
+ assert_equal(%w{James Dana Storm Gypsy}, answers)
283
+
284
+ @input.truncate(@input.rewind)
285
+ @input << "29\n49\n30\n"
286
+ @input.rewind
287
+ @output.truncate(@output.rewind)
288
+
289
+ answers = @terminal.ask("<%= @key %>: ", Integer) do |q|
290
+ q.gather = { "Age" => 0, "Wife's Age" => 0, "Father's Age" => 0}
291
+ end
292
+ assert_equal( { "Age" => 29, "Wife's Age" => 30, "Father's Age" => 49},
293
+ answers )
294
+ assert_equal("Age: Father's Age: Wife's Age: ", @output.string)
295
+ end
296
+
297
+ def test_lists
298
+ digits = %w{Zero One Two Three Four Five Six Seven Eight Nine}
299
+ erb_digits = digits.dup
300
+ erb_digits[erb_digits.index("Five")] = "<%= color('Five', :blue) %%>"
301
+
302
+ @terminal.say("<%= list(#{digits.inspect}) %>")
303
+ assert_equal(digits.map { |d| "#{d}\n" }.join, @output.string)
304
+
305
+ @output.truncate(@output.rewind)
306
+
307
+ @terminal.say("<%= list(#{digits.inspect}, :inline) %>")
308
+ assert_equal( digits[0..-2].join(", ") + " or #{digits.last}\n",
309
+ @output.string )
310
+
311
+ @output.truncate(@output.rewind)
312
+
313
+ @terminal.say("<%= list(#{digits.inspect}, :inline, ' and ') %>")
314
+ assert_equal( digits[0..-2].join(", ") + " and #{digits.last}\n",
315
+ @output.string )
316
+
317
+ @output.truncate(@output.rewind)
318
+
319
+ @terminal.say("<%= list(#{digits.inspect}, :columns_down, 3) %>")
320
+ assert_equal( "Zero Four Eight\n" +
321
+ "One Five Nine \n" +
322
+ "Two Six \n" +
323
+ "Three Seven\n",
324
+ @output.string )
325
+
326
+ @output.truncate(@output.rewind)
327
+
328
+ @terminal.say("<%= list(#{erb_digits.inspect}, :columns_down, 3) %>")
329
+ assert_equal( "Zero Four Eight\n" +
330
+ "One \e[34mFive\e[0m Nine \n" +
331
+ "Two Six \n" +
332
+ "Three Seven\n",
333
+ @output.string )
334
+
335
+ colums_of_twenty = ["12345678901234567890"] * 5
336
+
337
+ @output.truncate(@output.rewind)
338
+
339
+ @terminal.say("<%= list(#{colums_of_twenty.inspect}, :columns_down) %>")
340
+ assert_equal( "12345678901234567890 12345678901234567890 " +
341
+ "12345678901234567890\n" +
342
+ "12345678901234567890 12345678901234567890\n",
343
+ @output.string )
344
+
345
+ @output.truncate(@output.rewind)
346
+
347
+ @terminal.say("<%= list(#{digits.inspect}, :columns_across, 3) %>")
348
+ assert_equal( "Zero One Two \n" +
349
+ "Three Four Five \n" +
350
+ "Six Seven Eight\n" +
351
+ "Nine \n",
352
+ @output.string )
340
353
 
341
- colums_of_twenty.pop
342
-
343
- @output.truncate(@output.rewind)
344
-
345
- @terminal.say( "<%= list( #{colums_of_twenty.inspect},
346
- :columns_across ) %>" )
347
- assert_equal( "12345678901234567890 12345678901234567890 " +
348
- "12345678901234567890\n" +
349
- "12345678901234567890\n",
350
- @output.string )
351
- end
352
-
353
- def test_mode
354
- # *WARNING*: These tests wil only complete is "stty" mode!
355
- assert_equal( "stty", HighLine::CHARACTER_MODE,
356
- "Tests require \"stty\" mode." )
357
- end
358
-
359
- class NameClass
360
- def self.parse( string )
361
- if string =~ /^\s*(\w+),\s*(\w+)\s+(\w+)\s*$/
362
- self.new($2, $3, $1)
363
- else
364
- raise ArgumentError, "Invalid name format."
365
- end
366
- end
367
-
368
- def initialize(first, middle, last)
369
- @first, @middle, @last = first, middle, last
370
- end
371
-
372
- attr_reader :first, :middle, :last
373
- end
374
-
375
- def test_my_class_conversion
376
- @input << "Gray, James Edward\n"
377
- @input.rewind
378
-
379
- answer = @terminal.ask("Your name? ", NameClass) do |q|
380
- q.validate = lambda do |name|
381
- names = name.split(/,\s*/)
382
- return false unless names.size == 2
383
- return false if names.first =~ /\s/
384
- names.last.split.size == 2
385
- end
386
- end
387
- assert_instance_of(NameClass, answer)
388
- assert_equal("Gray", answer.last)
389
- assert_equal("James", answer.first)
390
- assert_equal("Edward", answer.middle)
391
- end
392
-
393
- def test_no_echo
394
- @input << "password\r"
395
- @input.rewind
396
-
397
- answer = @terminal.ask("Please enter your password: ") do |q|
398
- q.echo = false
399
- end
400
- assert_equal("password", answer)
401
- assert_equal("Please enter your password: \n", @output.string)
402
-
403
- @input.rewind
404
- @output.truncate(@output.rewind)
405
-
406
- answer = @terminal.ask("Pick a letter or number: ") do |q|
407
- q.character = true
408
- q.echo = false
409
- end
410
- assert_equal("p", answer)
411
- assert_equal("a", @input.getc.chr)
412
- assert_equal("Pick a letter or number: \n", @output.string)
413
- end
414
-
415
- def test_paging
416
- @terminal.page_at = 22
417
-
418
- @input << "\n\n"
419
- @input.rewind
420
-
421
- @terminal.say((1..50).map { |n| "This is line #{n}.\n"}.join)
422
- assert_equal( (1..22).map { |n| "This is line #{n}.\n"}.join +
423
- "\n-- press enter/return to continue -- \n" +
424
- (23..44).map { |n| "This is line #{n}.\n"}.join +
425
- "\n-- press enter/return to continue -- \n" +
426
- (45..50).map { |n| "This is line #{n}.\n"}.join,
427
- @output.string )
428
- end
429
-
430
- def test_range_requirements
431
- @input << "112\n-541\n28\n"
432
- @input.rewind
433
-
434
- answer = @terminal.ask("Tell me your age.", Integer) do |q|
435
- q.in = 0..105
436
- end
437
- assert_equal(28, answer)
438
- assert_equal( "Tell me your age.\n" +
439
- "Your answer isn't within the expected range " +
440
- "(included in 0..105).\n" +
441
- "? " +
442
- "Your answer isn't within the expected range " +
443
- "(included in 0..105).\n" +
444
- "? ", @output.string )
445
-
446
- @input.truncate(@input.rewind)
447
- @input << "1\n-541\n28\n"
448
- @input.rewind
449
- @output.truncate(@output.rewind)
450
-
451
- answer = @terminal.ask("Tell me your age.", Integer) do |q|
452
- q.above = 3
453
- end
454
- assert_equal(28, answer)
455
- assert_equal( "Tell me your age.\n" +
456
- "Your answer isn't within the expected range " +
457
- "(above 3).\n" +
458
- "? " +
459
- "Your answer isn't within the expected range " +
460
- "(above 3).\n" +
461
- "? ", @output.string )
462
-
463
- @input.truncate(@input.rewind)
464
- @input << "1\n28\n-541\n"
465
- @input.rewind
466
- @output.truncate(@output.rewind)
467
-
468
- answer = @terminal.ask("Lowest numer you can think of?", Integer) do |q|
469
- q.below = 0
470
- end
471
- assert_equal(-541, answer)
472
- assert_equal( "Lowest numer you can think of?\n" +
473
- "Your answer isn't within the expected range " +
474
- "(below 0).\n" +
475
- "? " +
476
- "Your answer isn't within the expected range " +
477
- "(below 0).\n" +
478
- "? ", @output.string )
479
-
480
- @input.truncate(@input.rewind)
481
- @input << "1\n-541\n6\n"
482
- @input.rewind
483
- @output.truncate(@output.rewind)
484
-
485
- answer = @terminal.ask("Enter a low even number: ", Integer) do |q|
486
- q.above = 0
487
- q.below = 10
488
- q.in = [2, 4, 6, 8]
489
- end
490
- assert_equal(6, answer)
491
- assert_equal( "Enter a low even number: " +
492
- "Your answer isn't within the expected range " +
493
- "(above 0, below 10, and included in [2, 4, 6, 8]).\n" +
494
- "? " +
495
- "Your answer isn't within the expected range " +
496
- "(above 0, below 10, and included in [2, 4, 6, 8]).\n" +
497
- "? ", @output.string )
498
- end
499
-
500
- def test_reask
501
- number = 61676
502
- @input << "Junk!\n" << number << "\n"
503
- @input.rewind
504
-
505
- answer = @terminal.ask("Favorite number? ", Integer)
506
- assert_kind_of(Integer, number)
507
- assert_instance_of(Fixnum, number)
508
- assert_equal(number, answer)
509
- assert_equal( "Favorite number? " +
510
- "You must enter a valid Integer.\n" +
511
- "? ", @output.string )
512
-
513
- @input.rewind
514
- @output.truncate(@output.rewind)
515
-
516
- answer = @terminal.ask("Favorite number? ", Integer) do |q|
517
- q.responses[:ask_on_error] = :question
518
- q.responses[:invalid_type] = "Not a valid number!"
519
- end
520
- assert_kind_of(Integer, number)
521
- assert_instance_of(Fixnum, number)
522
- assert_equal(number, answer)
523
- assert_equal( "Favorite number? " +
524
- "Not a valid number!\n" +
525
- "Favorite number? ", @output.string )
526
-
527
- @input.truncate(@input.rewind)
528
- @input << "gen\ngene\n"
529
- @input.rewind
530
- @output.truncate(@output.rewind)
531
-
532
- answer = @terminal.ask("Select a mode: ", [:generate, :gentle])
533
- assert_instance_of(Symbol, answer)
534
- assert_equal(:generate, answer)
535
- assert_equal("Select a mode: " +
536
- "Ambiguous choice. " +
537
- "Please choose one of [:generate, :gentle].\n" +
538
- "? ", @output.string)
539
- end
540
-
541
- def test_response_embedding
542
- @input << "112\n-541\n28\n"
543
- @input.rewind
544
-
545
- answer = @terminal.ask("Tell me your age.", Integer) do |q|
546
- q.in = 0..105
547
- q.responses[:not_in_range] = "Need a <%= @question.answer_type %>" +
548
- " <%= @question.expected_range %>."
549
- end
550
- assert_equal(28, answer)
551
- assert_equal( "Tell me your age.\n" +
552
- "Need a Integer included in 0..105.\n" +
553
- "? " +
554
- "Need a Integer included in 0..105.\n" +
555
- "? ", @output.string )
556
- end
557
-
558
- def test_say
559
- @terminal.say("This will have a newline.")
560
- assert_equal("This will have a newline.\n", @output.string)
561
-
562
- @output.truncate(@output.rewind)
563
-
564
- @terminal.say("This will also have one newline.\n")
565
- assert_equal("This will also have one newline.\n", @output.string)
566
-
567
- @output.truncate(@output.rewind)
568
-
569
- @terminal.say("This will not have a newline. ")
570
- assert_equal("This will not have a newline. ", @output.string)
571
- end
572
-
573
- def test_type_conversion
574
- number = 61676
575
- @input << number << "\n"
576
- @input.rewind
577
-
578
- answer = @terminal.ask("Favorite number? ", Integer)
579
- assert_kind_of(Integer, answer)
580
- assert_instance_of(Fixnum, answer)
581
- assert_equal(number, answer)
582
-
583
- @input.truncate(@input.rewind)
584
- number = 1_000_000_000_000_000_000_000_000_000_000
585
- @input << number << "\n"
586
- @input.rewind
587
-
588
- answer = @terminal.ask("Favorite number? ", Integer)
589
- assert_kind_of(Integer, answer)
590
- assert_instance_of(Bignum, answer)
591
- assert_equal(number, answer)
592
-
593
- @input.truncate(@input.rewind)
594
- number = 10.5002
595
- @input << number << "\n"
596
- @input.rewind
597
-
598
- answer = @terminal.ask( "Favorite number? ",
599
- lambda { |n| n.to_f.abs.round } )
600
- assert_kind_of(Integer, answer)
601
- assert_instance_of(Fixnum, answer)
602
- assert_equal(11, answer)
603
-
604
- @input.truncate(@input.rewind)
605
- animal = :dog
606
- @input << animal << "\n"
607
- @input.rewind
608
-
609
- answer = @terminal.ask("Favorite animal? ", Symbol)
610
- assert_instance_of(Symbol, answer)
611
- assert_equal(animal, answer)
612
-
613
- @input.truncate(@input.rewind)
614
- @input << "6/16/76\n"
615
- @input.rewind
616
-
617
- answer = @terminal.ask("Enter your birthday.", Date)
618
- assert_instance_of(Date, answer)
619
- assert_equal(16, answer.day)
620
- assert_equal(6, answer.month)
621
- assert_equal(76, answer.year)
622
-
623
- @input.truncate(@input.rewind)
624
- pattern = "^yes|no$"
625
- @input << pattern << "\n"
626
- @input.rewind
627
-
628
- answer = @terminal.ask("Give me a pattern to match with: ", Regexp)
629
- assert_instance_of(Regexp, answer)
630
- assert_equal(/#{pattern}/, answer)
631
-
632
- @input.truncate(@input.rewind)
633
- @input << "gen\n"
634
- @input.rewind
635
-
636
- answer = @terminal.ask("Select a mode: ", [:generate, :run])
637
- assert_instance_of(Symbol, answer)
638
- assert_equal(:generate, answer)
639
- end
640
-
641
- def test_validation
642
- @input << "system 'rm -rf /'\n105\n0b101_001\n"
643
- @input.rewind
644
-
645
- answer = @terminal.ask("Enter a binary number: ") do |q|
646
- q.validate = /\A(?:0b)?[01_]+\Z/
647
- end
648
- assert_equal("0b101_001", answer)
649
- assert_equal( "Enter a binary number: " +
650
- "Your answer isn't valid " +
651
- "(must match /\\A(?:0b)?[01_]+\\Z/).\n" +
652
- "? " +
653
- "Your answer isn't valid " +
654
- "(must match /\\A(?:0b)?[01_]+\\Z/).\n" +
655
- "? ", @output.string )
656
-
657
- @input.truncate(@input.rewind)
658
- @input << "Gray II, James Edward\n" +
659
- "Gray, Dana Ann Leslie\n" +
660
- "Gray, James Edward\n"
661
- @input.rewind
662
-
663
- answer = @terminal.ask("Your name? ") do |q|
664
- q.validate = lambda do |name|
665
- names = name.split(/,\s*/)
666
- return false unless names.size == 2
667
- return false if names.first =~ /\s/
668
- names.last.split.size == 2
669
- end
670
- end
671
- assert_equal("Gray, James Edward", answer)
672
- end
673
-
674
- def test_whitespace
675
- @input << " A lot\tof \t space\t \there! \n"
676
- @input.rewind
677
-
678
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
679
- q.whitespace = :chomp
680
- end
681
- assert_equal(" A lot\tof \t space\t \there! ", answer)
682
-
683
- @input.rewind
684
-
685
- answer = @terminal.ask("Enter a whitespace filled string: ")
686
- assert_equal("A lot\tof \t space\t \there!", answer)
687
-
688
- @input.rewind
689
-
690
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
691
- q.whitespace = :strip_and_collapse
692
- end
693
- assert_equal("A lot of space here!", answer)
694
-
695
- @input.rewind
696
-
697
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
698
- q.whitespace = :remove
699
- end
700
- assert_equal("Alotofspacehere!", answer)
701
-
702
- @input.rewind
703
-
704
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
705
- q.whitespace = :none
706
- end
707
- assert_equal(" A lot\tof \t space\t \there! \n", answer)
708
- end
709
-
710
- def test_wrap
711
- @terminal.wrap_at = 80
712
-
713
- @terminal.say("This is a very short line.")
714
- assert_equal("This is a very short line.\n", @output.string)
715
-
716
- @output.truncate(@output.rewind)
717
-
718
- @terminal.say( "This is a long flowing paragraph meant to span " +
719
- "several lines. This text should definitely be " +
720
- "wrapped at the set limit, in the result. Your code " +
721
- "does well with things like this.\n\n" +
722
- " * This is a simple embedded list.\n" +
723
- " * You're code should not mess with this...\n" +
354
+ colums_of_twenty.pop
355
+
356
+ @output.truncate(@output.rewind)
357
+
358
+ @terminal.say("<%= list( #{colums_of_twenty.inspect}, :columns_across ) %>")
359
+ assert_equal( "12345678901234567890 12345678901234567890 " +
360
+ "12345678901234567890\n" +
361
+ "12345678901234567890\n",
362
+ @output.string )
363
+ end
364
+
365
+ def test_mode
366
+ # *WARNING*: These tests wil only complete is "stty" mode!
367
+ assert_equal( "stty", HighLine::CHARACTER_MODE,
368
+ "Tests require \"stty\" mode." )
369
+ end
370
+
371
+ class NameClass
372
+ def self.parse( string )
373
+ if string =~ /^\s*(\w+),\s*(\w+)\s+(\w+)\s*$/
374
+ self.new($2, $3, $1)
375
+ else
376
+ raise ArgumentError, "Invalid name format."
377
+ end
378
+ end
379
+
380
+ def initialize(first, middle, last)
381
+ @first, @middle, @last = first, middle, last
382
+ end
383
+
384
+ attr_reader :first, :middle, :last
385
+ end
386
+
387
+ def test_my_class_conversion
388
+ @input << "Gray, James Edward\n"
389
+ @input.rewind
390
+
391
+ answer = @terminal.ask("Your name? ", NameClass) do |q|
392
+ q.validate = lambda do |name|
393
+ names = name.split(/,\s*/)
394
+ return false unless names.size == 2
395
+ return false if names.first =~ /\s/
396
+ names.last.split.size == 2
397
+ end
398
+ end
399
+ assert_instance_of(NameClass, answer)
400
+ assert_equal("Gray", answer.last)
401
+ assert_equal("James", answer.first)
402
+ assert_equal("Edward", answer.middle)
403
+ end
404
+
405
+ def test_no_echo
406
+ @input << "password\r"
407
+ @input.rewind
408
+
409
+ answer = @terminal.ask("Please enter your password: ") do |q|
410
+ q.echo = false
411
+ end
412
+ assert_equal("password", answer)
413
+ assert_equal("Please enter your password: \n", @output.string)
414
+
415
+ @input.rewind
416
+ @output.truncate(@output.rewind)
417
+
418
+ answer = @terminal.ask("Pick a letter or number: ") do |q|
419
+ q.character = true
420
+ q.echo = false
421
+ end
422
+ assert_equal("p", answer)
423
+ assert_equal("a", @input.getc.chr)
424
+ assert_equal("Pick a letter or number: \n", @output.string)
425
+ end
426
+
427
+ def test_paging
428
+ @terminal.page_at = 22
429
+
430
+ @input << "\n\n"
431
+ @input.rewind
432
+
433
+ @terminal.say((1..50).map { |n| "This is line #{n}.\n"}.join)
434
+ assert_equal( (1..22).map { |n| "This is line #{n}.\n"}.join +
435
+ "\n-- press enter/return to continue -- \n" +
436
+ (23..44).map { |n| "This is line #{n}.\n"}.join +
437
+ "\n-- press enter/return to continue -- \n" +
438
+ (45..50).map { |n| "This is line #{n}.\n"}.join,
439
+ @output.string )
440
+ end
441
+
442
+ def test_range_requirements
443
+ @input << "112\n-541\n28\n"
444
+ @input.rewind
445
+
446
+ answer = @terminal.ask("Tell me your age.", Integer) do |q|
447
+ q.in = 0..105
448
+ end
449
+ assert_equal(28, answer)
450
+ assert_equal( "Tell me your age.\n" +
451
+ "Your answer isn't within the expected range " +
452
+ "(included in 0..105).\n" +
453
+ "? " +
454
+ "Your answer isn't within the expected range " +
455
+ "(included in 0..105).\n" +
456
+ "? ", @output.string )
457
+
458
+ @input.truncate(@input.rewind)
459
+ @input << "1\n-541\n28\n"
460
+ @input.rewind
461
+ @output.truncate(@output.rewind)
462
+
463
+ answer = @terminal.ask("Tell me your age.", Integer) do |q|
464
+ q.above = 3
465
+ end
466
+ assert_equal(28, answer)
467
+ assert_equal( "Tell me your age.\n" +
468
+ "Your answer isn't within the expected range " +
469
+ "(above 3).\n" +
470
+ "? " +
471
+ "Your answer isn't within the expected range " +
472
+ "(above 3).\n" +
473
+ "? ", @output.string )
474
+
475
+ @input.truncate(@input.rewind)
476
+ @input << "1\n28\n-541\n"
477
+ @input.rewind
478
+ @output.truncate(@output.rewind)
479
+
480
+ answer = @terminal.ask("Lowest numer you can think of?", Integer) do |q|
481
+ q.below = 0
482
+ end
483
+ assert_equal(-541, answer)
484
+ assert_equal( "Lowest numer you can think of?\n" +
485
+ "Your answer isn't within the expected range " +
486
+ "(below 0).\n" +
487
+ "? " +
488
+ "Your answer isn't within the expected range " +
489
+ "(below 0).\n" +
490
+ "? ", @output.string )
491
+
492
+ @input.truncate(@input.rewind)
493
+ @input << "1\n-541\n6\n"
494
+ @input.rewind
495
+ @output.truncate(@output.rewind)
496
+
497
+ answer = @terminal.ask("Enter a low even number: ", Integer) do |q|
498
+ q.above = 0
499
+ q.below = 10
500
+ q.in = [2, 4, 6, 8]
501
+ end
502
+ assert_equal(6, answer)
503
+ assert_equal( "Enter a low even number: " +
504
+ "Your answer isn't within the expected range " +
505
+ "(above 0, below 10, and included in [2, 4, 6, 8]).\n" +
506
+ "? " +
507
+ "Your answer isn't within the expected range " +
508
+ "(above 0, below 10, and included in [2, 4, 6, 8]).\n" +
509
+ "? ", @output.string )
510
+ end
511
+
512
+ def test_reask
513
+ number = 61676
514
+ @input << "Junk!\n" << number << "\n"
515
+ @input.rewind
516
+
517
+ answer = @terminal.ask("Favorite number? ", Integer)
518
+ assert_kind_of(Integer, number)
519
+ assert_instance_of(Fixnum, number)
520
+ assert_equal(number, answer)
521
+ assert_equal( "Favorite number? " +
522
+ "You must enter a valid Integer.\n" +
523
+ "? ", @output.string )
524
+
525
+ @input.rewind
526
+ @output.truncate(@output.rewind)
527
+
528
+ answer = @terminal.ask("Favorite number? ", Integer) do |q|
529
+ q.responses[:ask_on_error] = :question
530
+ q.responses[:invalid_type] = "Not a valid number!"
531
+ end
532
+ assert_kind_of(Integer, number)
533
+ assert_instance_of(Fixnum, number)
534
+ assert_equal(number, answer)
535
+ assert_equal( "Favorite number? " +
536
+ "Not a valid number!\n" +
537
+ "Favorite number? ", @output.string )
538
+
539
+ @input.truncate(@input.rewind)
540
+ @input << "gen\ngene\n"
541
+ @input.rewind
542
+ @output.truncate(@output.rewind)
543
+
544
+ answer = @terminal.ask("Select a mode: ", [:generate, :gentle])
545
+ assert_instance_of(Symbol, answer)
546
+ assert_equal(:generate, answer)
547
+ assert_equal( "Select a mode: " +
548
+ "Ambiguous choice. " +
549
+ "Please choose one of [:generate, :gentle].\n" +
550
+ "? ", @output.string )
551
+ end
552
+
553
+ def test_response_embedding
554
+ @input << "112\n-541\n28\n"
555
+ @input.rewind
556
+
557
+ answer = @terminal.ask("Tell me your age.", Integer) do |q|
558
+ q.in = 0..105
559
+ q.responses[:not_in_range] = "Need a <%= @question.answer_type %>" +
560
+ " <%= @question.expected_range %>."
561
+ end
562
+ assert_equal(28, answer)
563
+ assert_equal( "Tell me your age.\n" +
564
+ "Need a Integer included in 0..105.\n" +
565
+ "? " +
566
+ "Need a Integer included in 0..105.\n" +
567
+ "? ", @output.string )
568
+ end
569
+
570
+ def test_say
571
+ @terminal.say("This will have a newline.")
572
+ assert_equal("This will have a newline.\n", @output.string)
573
+
574
+ @output.truncate(@output.rewind)
575
+
576
+ @terminal.say("This will also have one newline.\n")
577
+ assert_equal("This will also have one newline.\n", @output.string)
578
+
579
+ @output.truncate(@output.rewind)
580
+
581
+ @terminal.say("This will not have a newline. ")
582
+ assert_equal("This will not have a newline. ", @output.string)
583
+ end
584
+
585
+ def test_type_conversion
586
+ number = 61676
587
+ @input << number << "\n"
588
+ @input.rewind
589
+
590
+ answer = @terminal.ask("Favorite number? ", Integer)
591
+ assert_kind_of(Integer, answer)
592
+ assert_instance_of(Fixnum, answer)
593
+ assert_equal(number, answer)
594
+
595
+ @input.truncate(@input.rewind)
596
+ number = 1_000_000_000_000_000_000_000_000_000_000
597
+ @input << number << "\n"
598
+ @input.rewind
599
+
600
+ answer = @terminal.ask("Favorite number? ", Integer)
601
+ assert_kind_of(Integer, answer)
602
+ assert_instance_of(Bignum, answer)
603
+ assert_equal(number, answer)
604
+
605
+ @input.truncate(@input.rewind)
606
+ number = 10.5002
607
+ @input << number << "\n"
608
+ @input.rewind
609
+
610
+ answer = @terminal.ask( "Favorite number? ",
611
+ lambda { |n| n.to_f.abs.round } )
612
+ assert_kind_of(Integer, answer)
613
+ assert_instance_of(Fixnum, answer)
614
+ assert_equal(11, answer)
615
+
616
+ @input.truncate(@input.rewind)
617
+ animal = :dog
618
+ @input << animal << "\n"
619
+ @input.rewind
620
+
621
+ answer = @terminal.ask("Favorite animal? ", Symbol)
622
+ assert_instance_of(Symbol, answer)
623
+ assert_equal(animal, answer)
624
+
625
+ @input.truncate(@input.rewind)
626
+ @input << "6/16/76\n"
627
+ @input.rewind
628
+
629
+ answer = @terminal.ask("Enter your birthday.", Date)
630
+ assert_instance_of(Date, answer)
631
+ assert_equal(16, answer.day)
632
+ assert_equal(6, answer.month)
633
+ assert_equal(76, answer.year)
634
+
635
+ @input.truncate(@input.rewind)
636
+ pattern = "^yes|no$"
637
+ @input << pattern << "\n"
638
+ @input.rewind
639
+
640
+ answer = @terminal.ask("Give me a pattern to match with: ", Regexp)
641
+ assert_instance_of(Regexp, answer)
642
+ assert_equal(/#{pattern}/, answer)
643
+
644
+ @input.truncate(@input.rewind)
645
+ @input << "gen\n"
646
+ @input.rewind
647
+
648
+ answer = @terminal.ask("Select a mode: ", [:generate, :run])
649
+ assert_instance_of(Symbol, answer)
650
+ assert_equal(:generate, answer)
651
+ end
652
+
653
+ def test_validation
654
+ @input << "system 'rm -rf /'\n105\n0b101_001\n"
655
+ @input.rewind
656
+
657
+ answer = @terminal.ask("Enter a binary number: ") do |q|
658
+ q.validate = /\A(?:0b)?[01_]+\Z/
659
+ end
660
+ assert_equal("0b101_001", answer)
661
+ assert_equal( "Enter a binary number: " +
662
+ "Your answer isn't valid " +
663
+ "(must match /\\A(?:0b)?[01_]+\\Z/).\n" +
664
+ "? " +
665
+ "Your answer isn't valid " +
666
+ "(must match /\\A(?:0b)?[01_]+\\Z/).\n" +
667
+ "? ", @output.string )
668
+
669
+ @input.truncate(@input.rewind)
670
+ @input << "Gray II, James Edward\n" +
671
+ "Gray, Dana Ann Leslie\n" +
672
+ "Gray, James Edward\n"
673
+ @input.rewind
674
+
675
+ answer = @terminal.ask("Your name? ") do |q|
676
+ q.validate = lambda do |name|
677
+ names = name.split(/,\s*/)
678
+ return false unless names.size == 2
679
+ return false if names.first =~ /\s/
680
+ names.last.split.size == 2
681
+ end
682
+ end
683
+ assert_equal("Gray, James Edward", answer)
684
+ end
685
+
686
+ def test_whitespace
687
+ @input << " A lot\tof \t space\t \there! \n"
688
+ @input.rewind
689
+
690
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
691
+ q.whitespace = :chomp
692
+ end
693
+ assert_equal(" A lot\tof \t space\t \there! ", answer)
694
+
695
+ @input.rewind
696
+
697
+ answer = @terminal.ask("Enter a whitespace filled string: ")
698
+ assert_equal("A lot\tof \t space\t \there!", answer)
699
+
700
+ @input.rewind
701
+
702
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
703
+ q.whitespace = :strip_and_collapse
704
+ end
705
+ assert_equal("A lot of space here!", answer)
706
+
707
+ @input.rewind
708
+
709
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
710
+ q.whitespace = :remove
711
+ end
712
+ assert_equal("Alotofspacehere!", answer)
713
+
714
+ @input.rewind
715
+
716
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
717
+ q.whitespace = :none
718
+ end
719
+ assert_equal(" A lot\tof \t space\t \there! \n", answer)
720
+ end
721
+
722
+ def test_wrap
723
+ @terminal.wrap_at = 80
724
+
725
+ @terminal.say("This is a very short line.")
726
+ assert_equal("This is a very short line.\n", @output.string)
727
+
728
+ @output.truncate(@output.rewind)
729
+
730
+ @terminal.say( "This is a long flowing paragraph meant to span " +
731
+ "several lines. This text should definitely be " +
732
+ "wrapped at the set limit, in the result. Your code " +
733
+ "does well with things like this.\n\n" +
734
+ " * This is a simple embedded list.\n" +
735
+ " * You're code should not mess with this...\n" +
724
736
  " * Because it's already formatted correctly and " +
725
- "does not\n" +
726
- " exceed the limit!" )
727
- assert_equal( "This is a long flowing paragraph meant to span " +
728
- "several lines. This text should\n" +
729
- "definitely be wrapped at the set limit, in the " +
730
- "result. Your code does well with\n" +
731
- "things like this.\n\n" +
732
- " * This is a simple embedded list.\n" +
733
- " * You're code should not mess with this...\n" +
734
- " * Because it's already formatted correctly and does " +
735
- "not\n" +
736
- " exceed the limit!\n", @output.string )
737
-
738
- @output.truncate(@output.rewind)
739
-
740
- @terminal.say("-=" * 50)
741
- assert_equal(("-=" * 40 + "\n") + ("-=" * 10 + "\n"), @output.string)
742
- end
737
+ "does not\n" +
738
+ " exceed the limit!" )
739
+ assert_equal( "This is a long flowing paragraph meant to span " +
740
+ "several lines. This text should\n" +
741
+ "definitely be wrapped at the set limit, in the " +
742
+ "result. Your code does well with\n" +
743
+ "things like this.\n\n" +
744
+ " * This is a simple embedded list.\n" +
745
+ " * You're code should not mess with this...\n" +
746
+ " * Because it's already formatted correctly and does " +
747
+ "not\n" +
748
+ " exceed the limit!\n", @output.string )
749
+
750
+ @output.truncate(@output.rewind)
751
+
752
+ @terminal.say("-=" * 50)
753
+ assert_equal(("-=" * 40 + "\n") + ("-=" * 10 + "\n"), @output.string)
754
+ end
743
755
  end