challah 0.3.4 → 0.3.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (48) hide show
  1. data/CHANGELOG.md +4 -0
  2. data/lib/challah/version.rb +1 -1
  3. data/lib/tasks/crud.rake +23 -53
  4. data/vendor/bundle/cache/highline-1.6.11.gem +0 -0
  5. data/vendor/bundle/gems/highline-1.6.11/AUTHORS +3 -0
  6. data/vendor/bundle/gems/highline-1.6.11/CHANGELOG +304 -0
  7. data/vendor/bundle/gems/highline-1.6.11/COPYING +340 -0
  8. data/vendor/bundle/gems/highline-1.6.11/INSTALL +55 -0
  9. data/vendor/bundle/gems/highline-1.6.11/LICENSE +7 -0
  10. data/vendor/bundle/gems/highline-1.6.11/README +63 -0
  11. data/vendor/bundle/gems/highline-1.6.11/Rakefile +53 -0
  12. data/vendor/bundle/gems/highline-1.6.11/TODO +6 -0
  13. data/vendor/bundle/gems/highline-1.6.11/examples/ansi_colors.rb +38 -0
  14. data/vendor/bundle/gems/highline-1.6.11/examples/asking_for_arrays.rb +18 -0
  15. data/vendor/bundle/gems/highline-1.6.11/examples/basic_usage.rb +75 -0
  16. data/vendor/bundle/gems/highline-1.6.11/examples/color_scheme.rb +32 -0
  17. data/vendor/bundle/gems/highline-1.6.11/examples/limit.rb +12 -0
  18. data/vendor/bundle/gems/highline-1.6.11/examples/menus.rb +65 -0
  19. data/vendor/bundle/gems/highline-1.6.11/examples/overwrite.rb +19 -0
  20. data/vendor/bundle/gems/highline-1.6.11/examples/page_and_wrap.rb +322 -0
  21. data/vendor/bundle/gems/highline-1.6.11/examples/password.rb +7 -0
  22. data/vendor/bundle/gems/highline-1.6.11/examples/trapping_eof.rb +22 -0
  23. data/vendor/bundle/gems/highline-1.6.11/examples/using_readline.rb +17 -0
  24. data/vendor/bundle/gems/highline-1.6.11/highline.gemspec +36 -0
  25. data/vendor/bundle/gems/highline-1.6.11/lib/highline/color_scheme.rb +136 -0
  26. data/vendor/bundle/gems/highline-1.6.11/lib/highline/compatibility.rb +16 -0
  27. data/vendor/bundle/gems/highline-1.6.11/lib/highline/import.rb +43 -0
  28. data/vendor/bundle/gems/highline-1.6.11/lib/highline/menu.rb +398 -0
  29. data/vendor/bundle/gems/highline-1.6.11/lib/highline/question.rb +465 -0
  30. data/vendor/bundle/gems/highline-1.6.11/lib/highline/string_extensions.rb +98 -0
  31. data/vendor/bundle/gems/highline-1.6.11/lib/highline/style.rb +184 -0
  32. data/vendor/bundle/gems/highline-1.6.11/lib/highline/system_extensions.rb +180 -0
  33. data/vendor/bundle/gems/highline-1.6.11/lib/highline.rb +978 -0
  34. data/vendor/bundle/gems/highline-1.6.11/setup.rb +1360 -0
  35. data/vendor/bundle/gems/highline-1.6.11/site/highline.css +65 -0
  36. data/vendor/bundle/gems/highline-1.6.11/site/images/logo.png +0 -0
  37. data/vendor/bundle/gems/highline-1.6.11/site/index.html +58 -0
  38. data/vendor/bundle/gems/highline-1.6.11/test/string_methods.rb +34 -0
  39. data/vendor/bundle/gems/highline-1.6.11/test/tc_color_scheme.rb +98 -0
  40. data/vendor/bundle/gems/highline-1.6.11/test/tc_highline.rb +962 -0
  41. data/vendor/bundle/gems/highline-1.6.11/test/tc_import.rb +54 -0
  42. data/vendor/bundle/gems/highline-1.6.11/test/tc_menu.rb +429 -0
  43. data/vendor/bundle/gems/highline-1.6.11/test/tc_string_extension.rb +22 -0
  44. data/vendor/bundle/gems/highline-1.6.11/test/tc_string_highline.rb +40 -0
  45. data/vendor/bundle/gems/highline-1.6.11/test/tc_style.rb +569 -0
  46. data/vendor/bundle/gems/highline-1.6.11/test/ts_all.rb +18 -0
  47. data/vendor/bundle/specifications/highline-1.6.11.gemspec +29 -0
  48. metadata +63 -8
@@ -0,0 +1,962 @@
1
+ #!/usr/local/bin/ruby -w
2
+
3
+ # tc_highline.rb
4
+ #
5
+ # Created by James Edward Gray II on 2005-04-26.
6
+ # Copyright 2005 Gray Productions. 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
+ if HighLine::CHARACTER_MODE == "Win32API"
16
+ class HighLine
17
+ # Override Windows' character reading so it's not tied to STDIN.
18
+ def get_character( input = STDIN )
19
+ input.getc
20
+ end
21
+ end
22
+ end
23
+
24
+ class TestHighLine < Test::Unit::TestCase
25
+ def setup
26
+ @input = StringIO.new
27
+ @output = StringIO.new
28
+ @terminal = HighLine.new(@input, @output)
29
+ end
30
+
31
+ def test_agree
32
+ @input << "y\nyes\nYES\nHell no!\nNo\n"
33
+ @input.rewind
34
+
35
+ assert_equal(true, @terminal.agree("Yes or no? "))
36
+ assert_equal(true, @terminal.agree("Yes or no? "))
37
+ assert_equal(true, @terminal.agree("Yes or no? "))
38
+ assert_equal(false, @terminal.agree("Yes or no? "))
39
+
40
+ @input.truncate(@input.rewind)
41
+ @input << "yellow"
42
+ @input.rewind
43
+
44
+ assert_equal(true, @terminal.agree("Yes or no? ", :getc))
45
+ end
46
+
47
+ def test_agree_with_block
48
+ @input << "\n\n"
49
+ @input.rewind
50
+
51
+ assert_equal(true, @terminal.agree("Yes or no? ") { |q| q.default = "y" })
52
+ assert_equal(false, @terminal.agree("Yes or no? ") { |q| q.default = "n" })
53
+ end
54
+
55
+ def test_ask
56
+ name = "James Edward Gray II"
57
+ @input << name << "\n"
58
+ @input.rewind
59
+
60
+ assert_equal(name, @terminal.ask("What is your name? "))
61
+
62
+ assert_raise(EOFError) { @terminal.ask("Any input left? ") }
63
+ end
64
+
65
+ def test_ask_string
66
+ name = "James Edward Gray II"
67
+ @input << name << "\n"
68
+ @input.rewind
69
+
70
+ assert_equal(name, @terminal.ask("What is your name? ", String))
71
+
72
+ assert_raise(EOFError) { @terminal.ask("Any input left? ", String) }
73
+ end
74
+
75
+ def test_bug_fixes
76
+ # auto-complete bug
77
+ @input << "ruby\nRuby\n"
78
+ @input.rewind
79
+
80
+ languages = [:Perl, :Python, :Ruby]
81
+ answer = @terminal.ask( "What is your favorite programming language? ",
82
+ languages )
83
+ assert_equal(languages.last, answer)
84
+
85
+ @input.truncate(@input.rewind)
86
+ @input << "ruby\n"
87
+ @input.rewind
88
+
89
+ answer = @terminal.ask( "What is your favorite programming language? ",
90
+ languages ) do |q|
91
+ q.case = :capitalize
92
+ end
93
+ assert_equal(languages.last, answer)
94
+
95
+ # poor auto-complete error message
96
+ @input.truncate(@input.rewind)
97
+ @input << "lisp\nruby\n"
98
+ @input.rewind
99
+ @output.truncate(@output.rewind)
100
+
101
+ answer = @terminal.ask( "What is your favorite programming language? ",
102
+ languages ) do |q|
103
+ q.case = :capitalize
104
+ end
105
+ assert_equal(languages.last, answer)
106
+ assert_equal( "What is your favorite programming language? " +
107
+ "You must choose one of [:Perl, :Python, :Ruby].\n" +
108
+ "? ", @output.string )
109
+ end
110
+
111
+ def test_case_changes
112
+ @input << "jeg2\n"
113
+ @input.rewind
114
+
115
+ answer = @terminal.ask("Enter your initials ") do |q|
116
+ q.case = :up
117
+ end
118
+ assert_equal("JEG2", answer)
119
+
120
+ @input.truncate(@input.rewind)
121
+ @input << "cRaZY\n"
122
+ @input.rewind
123
+
124
+ answer = @terminal.ask("Enter a search string: ") do |q|
125
+ q.case = :down
126
+ end
127
+ assert_equal("crazy", answer)
128
+ end
129
+
130
+ def test_character_echo
131
+ @input << "password\r"
132
+ @input.rewind
133
+
134
+ answer = @terminal.ask("Please enter your password: ") do |q|
135
+ q.echo = "*"
136
+ end
137
+ assert_equal("password", answer)
138
+ assert_equal("Please enter your password: ********\n", @output.string)
139
+
140
+ @input.truncate(@input.rewind)
141
+ @input << "2"
142
+ @input.rewind
143
+ @output.truncate(@output.rewind)
144
+
145
+ answer = @terminal.ask( "Select an option (1, 2 or 3): ",
146
+ Integer ) do |q|
147
+ q.echo = "*"
148
+ q.character = true
149
+ end
150
+ assert_equal(2, answer)
151
+ assert_equal("Select an option (1, 2 or 3): *\n", @output.string)
152
+ end
153
+
154
+ def test_backspace_does_not_enter_prompt
155
+ @input << "\b\b"
156
+ @input.rewind
157
+ answer = @terminal.ask("Please enter your password: ") do |q|
158
+ q.echo = "*"
159
+ end
160
+ assert_equal("", answer)
161
+ assert_equal("Please enter your password: \n",@output.string)
162
+ end
163
+
164
+ def test_readline_on_non_echo_question_has_prompt
165
+ @input << "you can't see me"
166
+ @input.rewind
167
+ answer = @terminal.ask("Please enter some hidden text: ") do |q|
168
+ q.readline = true
169
+ q.echo = "*"
170
+ end
171
+ assert_equal("you can't see me", answer)
172
+ assert_equal("Please enter some hidden text: ****************\n",@output.string)
173
+ end
174
+
175
+ def test_character_reading
176
+ # WARNING: This method does NOT cover Unix and Windows savvy testing!
177
+ @input << "12345"
178
+ @input.rewind
179
+
180
+ answer = @terminal.ask("Enter a single digit: ", Integer) do |q|
181
+ q.character = :getc
182
+ end
183
+ assert_equal(1, answer)
184
+ end
185
+
186
+ def test_color
187
+ @terminal.say("This should be <%= BLUE %>blue<%= CLEAR %>!")
188
+ assert_equal("This should be \e[34mblue\e[0m!\n", @output.string)
189
+
190
+ @output.truncate(@output.rewind)
191
+
192
+ @terminal.say( "This should be " +
193
+ "<%= BOLD + ON_WHITE %>bold on white<%= CLEAR %>!" )
194
+ assert_equal( "This should be \e[1m\e[47mbold on white\e[0m!\n",
195
+ @output.string )
196
+
197
+ @output.truncate(@output.rewind)
198
+
199
+ @terminal.say("This should be <%= color('cyan', CYAN) %>!")
200
+ assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string)
201
+
202
+ @output.truncate(@output.rewind)
203
+
204
+ @terminal.say( "This should be " +
205
+ "<%= color('blinking on red', :blink, :on_red) %>!" )
206
+ assert_equal( "This should be \e[5m\e[41mblinking on red\e[0m!\n",
207
+ @output.string )
208
+
209
+ @output.truncate(@output.rewind)
210
+
211
+ @terminal.say("This should be <%= NONE %>none<%= CLEAR %>!")
212
+ assert_equal("This should be \e[38mnone\e[0m!\n", @output.string)
213
+
214
+ @output.truncate(@output.rewind)
215
+
216
+ @terminal.say("This should be <%= RGB_906030 %>rgb_906030<%= CLEAR %>!")
217
+ assert_equal("This should be \e[38;5;137mrgb_906030\e[0m!\n", @output.string)
218
+
219
+ @output.truncate(@output.rewind)
220
+
221
+ @terminal.say("This should be <%= ON_RGB_C06030 %>on_rgb_c06030<%= CLEAR %>!")
222
+ assert_equal("This should be \e[48;5;173mon_rgb_c06030\e[0m!\n", @output.string)
223
+
224
+ @output.truncate(@output.rewind)
225
+
226
+ # Does class method work, too?
227
+ @terminal.say("This should be <%= HighLine.color('reverse underlined magenta', :reverse, :underline, :magenta) %>!")
228
+ assert_equal( "This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n",
229
+ @output.string )
230
+
231
+ @output.truncate(@output.rewind)
232
+
233
+ # turn off color
234
+ old_setting = HighLine.use_color?
235
+ assert_nothing_raised(Exception) { HighLine.use_color = false }
236
+ @terminal.say("This should be <%= color('cyan', CYAN) %>!")
237
+ assert_equal("This should be cyan!\n", @output.string)
238
+ HighLine.use_color = old_setting
239
+ end
240
+
241
+ def test_uncolor
242
+ # instance method
243
+ assert_equal( "This should be reverse underlined magenta!\n",
244
+ @terminal.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n")
245
+ )
246
+
247
+ @output.truncate(@output.rewind)
248
+
249
+ # class method
250
+ assert_equal( "This should be reverse underlined magenta!\n",
251
+ HighLine.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n")
252
+ )
253
+
254
+ @output.truncate(@output.rewind)
255
+
256
+ # RGB color
257
+ assert_equal( "This should be rgb_906030!\n",
258
+ @terminal.uncolor("This should be \e[38;5;137mrgb_906030\e[0m!\n")
259
+ )
260
+ end
261
+
262
+ def test_confirm
263
+ @input << "junk.txt\nno\nsave.txt\ny\n"
264
+ @input.rewind
265
+
266
+ answer = @terminal.ask("Enter a filename: ") do |q|
267
+ q.confirm = "Are you sure you want to overwrite <%= @answer %>? "
268
+ q.responses[:ask_on_error] = :question
269
+ end
270
+ assert_equal("save.txt", answer)
271
+ assert_equal( "Enter a filename: " +
272
+ "Are you sure you want to overwrite junk.txt? " +
273
+ "Enter a filename: " +
274
+ "Are you sure you want to overwrite save.txt? ",
275
+ @output.string )
276
+
277
+ @input.truncate(@input.rewind)
278
+ @input << "junk.txt\nyes\nsave.txt\nn\n"
279
+ @input.rewind
280
+ @output.truncate(@output.rewind)
281
+
282
+ answer = @terminal.ask("Enter a filename: ") do |q|
283
+ q.confirm = "Are you sure you want to overwrite <%= @answer %>? "
284
+ end
285
+ assert_equal("junk.txt", answer)
286
+ assert_equal( "Enter a filename: " +
287
+ "Are you sure you want to overwrite junk.txt? ",
288
+ @output.string )
289
+ end
290
+
291
+ def test_defaults
292
+ @input << "\nNo Comment\n"
293
+ @input.rewind
294
+
295
+ answer = @terminal.ask("Are you sexually active? ") do |q|
296
+ q.validate = /\Ay(?:es)?|no?|no comment\Z/i
297
+ end
298
+ assert_equal("No Comment", answer)
299
+
300
+ @input.truncate(@input.rewind)
301
+ @input << "\nYes\n"
302
+ @input.rewind
303
+ @output.truncate(@output.rewind)
304
+
305
+ answer = @terminal.ask("Are you sexually active? ") do |q|
306
+ q.default = "No Comment"
307
+ q.validate = /\Ay(?:es)?|no?|no comment\Z/i
308
+ end
309
+ assert_equal("No Comment", answer)
310
+ assert_equal( "Are you sexually active? |No Comment| ",
311
+ @output.string )
312
+ end
313
+
314
+ def test_empty
315
+ @input << "\n"
316
+ @input.rewind
317
+
318
+ answer = @terminal.ask("") do |q|
319
+ q.default = "yes"
320
+ q.validate = /\Ay(?:es)?|no?\Z/i
321
+ end
322
+ assert_equal("yes", answer)
323
+ end
324
+
325
+ def test_erb
326
+ @terminal.say( "The integers from 1 to 10 are:\n" +
327
+ "% (1...10).each do |n|\n" +
328
+ "\t<%= n %>,\n" +
329
+ "% end\n" +
330
+ "\tand 10" )
331
+ assert_equal( "The integers from 1 to 10 are:\n" +
332
+ "\t1,\n\t2,\n\t3,\n\t4,\n\t5,\n" +
333
+ "\t6,\n\t7,\n\t8,\n\t9,\n\tand 10\n",
334
+ @output.string )
335
+ end
336
+
337
+ def test_files
338
+ @input << "#{File.basename(__FILE__)[0, 5]}\n"
339
+ @input.rewind
340
+
341
+ assert_equal "tc_hi\n",@input.read
342
+ @input.rewind
343
+
344
+ file = @terminal.ask("Select a file: ", File) do |q|
345
+ q.directory = File.expand_path(File.dirname(__FILE__))
346
+ q.glob = "*.rb"
347
+ end
348
+ assert_instance_of(File, file)
349
+ assert_equal("#!/usr/local/bin/ruby -w\n", file.gets)
350
+ assert_equal("\n", file.gets)
351
+ assert_equal("# tc_highline.rb\n", file.gets)
352
+ file.close
353
+
354
+ @input.rewind
355
+
356
+ pathname = @terminal.ask("Select a file: ", Pathname) do |q|
357
+ q.directory = File.expand_path(File.dirname(__FILE__))
358
+ q.glob = "*.rb"
359
+ end
360
+ assert_instance_of(Pathname, pathname)
361
+ assert_equal(File.size(__FILE__), pathname.size)
362
+ end
363
+
364
+ def test_gather
365
+ @input << "James\nDana\nStorm\nGypsy\n\n"
366
+ @input.rewind
367
+
368
+ answers = @terminal.ask("Enter four names:") do |q|
369
+ q.gather = 4
370
+ end
371
+ assert_equal(%w{James Dana Storm Gypsy}, answers)
372
+ assert_equal("\n", @input.gets)
373
+ assert_equal("Enter four names:\n", @output.string)
374
+
375
+ @input.rewind
376
+
377
+ answers = @terminal.ask("Enter four names:") do |q|
378
+ q.gather = ""
379
+ end
380
+ assert_equal(%w{James Dana Storm Gypsy}, answers)
381
+
382
+ @input.rewind
383
+
384
+ answers = @terminal.ask("Enter four names:") do |q|
385
+ q.gather = /^\s*$/
386
+ end
387
+ assert_equal(%w{James Dana Storm Gypsy}, answers)
388
+
389
+ @input.truncate(@input.rewind)
390
+ @input << "29\n49\n30\n"
391
+ @input.rewind
392
+ @output.truncate(@output.rewind)
393
+
394
+ answers = @terminal.ask("<%= @key %>: ", Integer) do |q|
395
+ q.gather = { "Age" => 0, "Wife's Age" => 0, "Father's Age" => 0}
396
+ end
397
+ assert_equal( { "Age" => 29, "Wife's Age" => 30, "Father's Age" => 49},
398
+ answers )
399
+ assert_equal("Age: Father's Age: Wife's Age: ", @output.string)
400
+ end
401
+
402
+ def test_lists
403
+ digits = %w{Zero One Two Three Four Five Six Seven Eight Nine}
404
+ erb_digits = digits.dup
405
+ erb_digits[erb_digits.index("Five")] = "<%= color('Five', :blue) %%>"
406
+
407
+ @terminal.say("<%= list(#{digits.inspect}) %>")
408
+ assert_equal(digits.map { |d| "#{d}\n" }.join, @output.string)
409
+
410
+ @output.truncate(@output.rewind)
411
+
412
+ @terminal.say("<%= list(#{digits.inspect}, :inline) %>")
413
+ assert_equal( digits[0..-2].join(", ") + " or #{digits.last}\n",
414
+ @output.string )
415
+
416
+ @output.truncate(@output.rewind)
417
+
418
+ @terminal.say("<%= list(#{digits.inspect}, :inline, ' and ') %>")
419
+ assert_equal( digits[0..-2].join(", ") + " and #{digits.last}\n",
420
+ @output.string )
421
+
422
+ @output.truncate(@output.rewind)
423
+
424
+ @terminal.say("<%= list(#{digits.inspect}, :columns_down, 3) %>")
425
+ assert_equal( "Zero Four Eight\n" +
426
+ "One Five Nine \n" +
427
+ "Two Six \n" +
428
+ "Three Seven\n",
429
+ @output.string )
430
+
431
+ @output.truncate(@output.rewind)
432
+
433
+ @terminal.say("<%= list(#{erb_digits.inspect}, :columns_down, 3) %>")
434
+ assert_equal( "Zero Four Eight\n" +
435
+ "One \e[34mFive\e[0m Nine \n" +
436
+ "Two Six \n" +
437
+ "Three Seven\n",
438
+ @output.string )
439
+
440
+ colums_of_twenty = ["12345678901234567890"] * 5
441
+
442
+ @output.truncate(@output.rewind)
443
+
444
+ @terminal.say("<%= list(#{colums_of_twenty.inspect}, :columns_down) %>")
445
+ assert_equal( "12345678901234567890 12345678901234567890 " +
446
+ "12345678901234567890\n" +
447
+ "12345678901234567890 12345678901234567890\n",
448
+ @output.string )
449
+
450
+ @output.truncate(@output.rewind)
451
+
452
+ @terminal.say("<%= list(#{digits.inspect}, :columns_across, 3) %>")
453
+ assert_equal( "Zero One Two \n" +
454
+ "Three Four Five \n" +
455
+ "Six Seven Eight\n" +
456
+ "Nine \n",
457
+ @output.string )
458
+
459
+ colums_of_twenty.pop
460
+
461
+ @output.truncate(@output.rewind)
462
+
463
+ @terminal.say("<%= list( #{colums_of_twenty.inspect}, :columns_across ) %>")
464
+ assert_equal( "12345678901234567890 12345678901234567890 " +
465
+ "12345678901234567890\n" +
466
+ "12345678901234567890\n",
467
+ @output.string )
468
+
469
+ @output.truncate(@output.rewind)
470
+
471
+ wide = %w[0123456789 a b c d e f g h i j k l m n o p q r s t u v w x y z]
472
+
473
+ @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_across ) %>")
474
+ assert_equal( "0123456789 a b c d e f g h i j k l m n o " +
475
+ "p q r s t u v w\n" +
476
+ "x y z\n",
477
+ @output.string )
478
+
479
+ @output.truncate(@output.rewind)
480
+
481
+ @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_across, 10 ) %>")
482
+ assert_equal( "0123456789 a b c d e f g h i\n" +
483
+ "j k l m n o p q r s\n" +
484
+ "t u v w x y z\n",
485
+ @output.string )
486
+
487
+ @output.truncate(@output.rewind)
488
+
489
+ @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_down ) %>")
490
+ assert_equal( "0123456789 b d f h j l n p r t v x z\n" +
491
+ "a c e g i k m o q s u w y\n",
492
+ @output.string )
493
+
494
+ @output.truncate(@output.rewind)
495
+
496
+ @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_down, 10 ) %>")
497
+ assert_equal( "0123456789 c f i l o r u x\n" +
498
+ "a d g j m p s v y\n" +
499
+ "b e h k n q t w z\n",
500
+ @output.string )
501
+ end
502
+
503
+ def test_lists_with_zero_items
504
+ modes = [nil, :rows, :inline, :columns_across, :columns_down]
505
+ modes.each do |mode|
506
+ result = @terminal.list([], mode)
507
+ assert_equal("", result)
508
+ end
509
+ end
510
+
511
+ def test_lists_with_one_item
512
+ items = ['Zero']
513
+ modes = { nil => "Zero\n",
514
+ :rows => "Zero\n",
515
+ :inline => "Zero",
516
+ :columns_across => "Zero\n",
517
+ :columns_down => "Zero\n" }
518
+
519
+ modes.each do |mode, expected|
520
+ result = @terminal.list(items, mode)
521
+ assert_equal(expected, result)
522
+ end
523
+ end
524
+
525
+ def test_lists_with_two_items
526
+ items = ['Zero', 'One']
527
+ modes = { nil => "Zero\nOne\n",
528
+ :rows => "Zero\nOne\n",
529
+ :inline => "Zero or One",
530
+ :columns_across => "Zero One \n",
531
+ :columns_down => "Zero One \n" }
532
+
533
+ modes.each do |mode, expected|
534
+ result = @terminal.list(items, mode)
535
+ assert_equal(expected, result)
536
+ end
537
+ end
538
+
539
+ def test_lists_with_three_items
540
+ items = ['Zero', 'One', 'Two']
541
+ modes = { nil => "Zero\nOne\nTwo\n",
542
+ :rows => "Zero\nOne\nTwo\n",
543
+ :inline => "Zero, One or Two",
544
+ :columns_across => "Zero One Two \n",
545
+ :columns_down => "Zero One Two \n" }
546
+
547
+ modes.each do |mode, expected|
548
+ result = @terminal.list(items, mode)
549
+ assert_equal(expected, result)
550
+ end
551
+ end
552
+
553
+ def test_mode
554
+ assert(%w[Win32API termios ncurses stty].include?(HighLine::CHARACTER_MODE))
555
+ end
556
+
557
+ class NameClass
558
+ def self.parse( string )
559
+ if string =~ /^\s*(\w+),\s*(\w+)\s+(\w+)\s*$/
560
+ self.new($2, $3, $1)
561
+ else
562
+ raise ArgumentError, "Invalid name format."
563
+ end
564
+ end
565
+
566
+ def initialize(first, middle, last)
567
+ @first, @middle, @last = first, middle, last
568
+ end
569
+
570
+ attr_reader :first, :middle, :last
571
+ end
572
+
573
+ def test_my_class_conversion
574
+ @input << "Gray, James Edward\n"
575
+ @input.rewind
576
+
577
+ answer = @terminal.ask("Your name? ", NameClass) do |q|
578
+ q.validate = lambda do |name|
579
+ names = name.split(/,\s*/)
580
+ return false unless names.size == 2
581
+ return false if names.first =~ /\s/
582
+ names.last.split.size == 2
583
+ end
584
+ end
585
+ assert_instance_of(NameClass, answer)
586
+ assert_equal("Gray", answer.last)
587
+ assert_equal("James", answer.first)
588
+ assert_equal("Edward", answer.middle)
589
+ end
590
+
591
+ def test_no_echo
592
+ @input << "password\r"
593
+ @input.rewind
594
+
595
+ answer = @terminal.ask("Please enter your password: ") do |q|
596
+ q.echo = false
597
+ end
598
+ assert_equal("password", answer)
599
+ assert_equal("Please enter your password: \n", @output.string)
600
+
601
+ @input.rewind
602
+ @output.truncate(@output.rewind)
603
+
604
+ answer = @terminal.ask("Pick a letter or number: ") do |q|
605
+ q.character = true
606
+ q.echo = false
607
+ end
608
+ assert_equal("p", answer)
609
+ assert_equal("a", @input.getc.chr)
610
+ assert_equal("Pick a letter or number: \n", @output.string)
611
+ end
612
+
613
+ def test_paging
614
+ @terminal.page_at = 22
615
+
616
+ @input << "\n\n"
617
+ @input.rewind
618
+
619
+ @terminal.say((1..50).map { |n| "This is line #{n}.\n"}.join)
620
+ assert_equal( (1..22).map { |n| "This is line #{n}.\n"}.join +
621
+ "\n-- press enter/return to continue or q to stop -- \n\n" +
622
+ (23..44).map { |n| "This is line #{n}.\n"}.join +
623
+ "\n-- press enter/return to continue or q to stop -- \n\n" +
624
+ (45..50).map { |n| "This is line #{n}.\n"}.join,
625
+ @output.string )
626
+ end
627
+
628
+ def test_range_requirements
629
+ @input << "112\n-541\n28\n"
630
+ @input.rewind
631
+
632
+ answer = @terminal.ask("Tell me your age.", Integer) do |q|
633
+ q.in = 0..105
634
+ end
635
+ assert_equal(28, answer)
636
+ assert_equal( "Tell me your age.\n" +
637
+ "Your answer isn't within the expected range " +
638
+ "(included in 0..105).\n" +
639
+ "? " +
640
+ "Your answer isn't within the expected range " +
641
+ "(included in 0..105).\n" +
642
+ "? ", @output.string )
643
+
644
+ @input.truncate(@input.rewind)
645
+ @input << "1\n-541\n28\n"
646
+ @input.rewind
647
+ @output.truncate(@output.rewind)
648
+
649
+ answer = @terminal.ask("Tell me your age.", Integer) do |q|
650
+ q.above = 3
651
+ end
652
+ assert_equal(28, answer)
653
+ assert_equal( "Tell me your age.\n" +
654
+ "Your answer isn't within the expected range " +
655
+ "(above 3).\n" +
656
+ "? " +
657
+ "Your answer isn't within the expected range " +
658
+ "(above 3).\n" +
659
+ "? ", @output.string )
660
+
661
+ @input.truncate(@input.rewind)
662
+ @input << "1\n28\n-541\n"
663
+ @input.rewind
664
+ @output.truncate(@output.rewind)
665
+
666
+ answer = @terminal.ask("Lowest numer you can think of?", Integer) do |q|
667
+ q.below = 0
668
+ end
669
+ assert_equal(-541, answer)
670
+ assert_equal( "Lowest numer you can think of?\n" +
671
+ "Your answer isn't within the expected range " +
672
+ "(below 0).\n" +
673
+ "? " +
674
+ "Your answer isn't within the expected range " +
675
+ "(below 0).\n" +
676
+ "? ", @output.string )
677
+
678
+ @input.truncate(@input.rewind)
679
+ @input << "1\n-541\n6\n"
680
+ @input.rewind
681
+ @output.truncate(@output.rewind)
682
+
683
+ answer = @terminal.ask("Enter a low even number: ", Integer) do |q|
684
+ q.above = 0
685
+ q.below = 10
686
+ q.in = [2, 4, 6, 8]
687
+ end
688
+ assert_equal(6, answer)
689
+ assert_equal( "Enter a low even number: " +
690
+ "Your answer isn't within the expected range " +
691
+ "(above 0, below 10, and included in [2, 4, 6, 8]).\n" +
692
+ "? " +
693
+ "Your answer isn't within the expected range " +
694
+ "(above 0, below 10, and included in [2, 4, 6, 8]).\n" +
695
+ "? ", @output.string )
696
+ end
697
+
698
+ def test_reask
699
+ number = 61676
700
+ @input << "Junk!\n" << number << "\n"
701
+ @input.rewind
702
+
703
+ answer = @terminal.ask("Favorite number? ", Integer)
704
+ assert_kind_of(Integer, number)
705
+ assert_instance_of(Fixnum, number)
706
+ assert_equal(number, answer)
707
+ assert_equal( "Favorite number? " +
708
+ "You must enter a valid Integer.\n" +
709
+ "? ", @output.string )
710
+
711
+ @input.rewind
712
+ @output.truncate(@output.rewind)
713
+
714
+ answer = @terminal.ask("Favorite number? ", Integer) do |q|
715
+ q.responses[:ask_on_error] = :question
716
+ q.responses[:invalid_type] = "Not a valid number!"
717
+ end
718
+ assert_kind_of(Integer, number)
719
+ assert_instance_of(Fixnum, number)
720
+ assert_equal(number, answer)
721
+ assert_equal( "Favorite number? " +
722
+ "Not a valid number!\n" +
723
+ "Favorite number? ", @output.string )
724
+
725
+ @input.truncate(@input.rewind)
726
+ @input << "gen\ngene\n"
727
+ @input.rewind
728
+ @output.truncate(@output.rewind)
729
+
730
+ answer = @terminal.ask("Select a mode: ", [:generate, :gentle])
731
+ assert_instance_of(Symbol, answer)
732
+ assert_equal(:generate, answer)
733
+ assert_equal( "Select a mode: " +
734
+ "Ambiguous choice. " +
735
+ "Please choose one of [:generate, :gentle].\n" +
736
+ "? ", @output.string )
737
+ end
738
+
739
+ def test_response_embedding
740
+ @input << "112\n-541\n28\n"
741
+ @input.rewind
742
+
743
+ answer = @terminal.ask("Tell me your age.", Integer) do |q|
744
+ q.in = 0..105
745
+ q.responses[:not_in_range] = "Need a <%= @question.answer_type %>" +
746
+ " <%= @question.expected_range %>."
747
+ end
748
+ assert_equal(28, answer)
749
+ assert_equal( "Tell me your age.\n" +
750
+ "Need a Integer included in 0..105.\n" +
751
+ "? " +
752
+ "Need a Integer included in 0..105.\n" +
753
+ "? ", @output.string )
754
+ end
755
+
756
+ def test_say
757
+ @terminal.say("This will have a newline.")
758
+ assert_equal("This will have a newline.\n", @output.string)
759
+
760
+ @output.truncate(@output.rewind)
761
+
762
+ @terminal.say("This will also have one newline.\n")
763
+ assert_equal("This will also have one newline.\n", @output.string)
764
+
765
+ @output.truncate(@output.rewind)
766
+
767
+ @terminal.say("This will not have a newline. ")
768
+ assert_equal("This will not have a newline. ", @output.string)
769
+ end
770
+
771
+ def test_type_conversion
772
+ number = 61676
773
+ @input << number << "\n"
774
+ @input.rewind
775
+
776
+ answer = @terminal.ask("Favorite number? ", Integer)
777
+ assert_kind_of(Integer, answer)
778
+ assert_instance_of(Fixnum, answer)
779
+ assert_equal(number, answer)
780
+
781
+ @input.truncate(@input.rewind)
782
+ number = 1_000_000_000_000_000_000_000_000_000_000
783
+ @input << number << "\n"
784
+ @input.rewind
785
+
786
+ answer = @terminal.ask("Favorite number? ", Integer)
787
+ assert_kind_of(Integer, answer)
788
+ assert_instance_of(Bignum, answer)
789
+ assert_equal(number, answer)
790
+
791
+ @input.truncate(@input.rewind)
792
+ number = 10.5002
793
+ @input << number << "\n"
794
+ @input.rewind
795
+
796
+ answer = @terminal.ask( "Favorite number? ",
797
+ lambda { |n| n.to_f.abs.round } )
798
+ assert_kind_of(Integer, answer)
799
+ assert_instance_of(Fixnum, answer)
800
+ assert_equal(11, answer)
801
+
802
+ @input.truncate(@input.rewind)
803
+ animal = :dog
804
+ @input << animal << "\n"
805
+ @input.rewind
806
+
807
+ answer = @terminal.ask("Favorite animal? ", Symbol)
808
+ assert_instance_of(Symbol, answer)
809
+ assert_equal(animal, answer)
810
+
811
+ @input.truncate(@input.rewind)
812
+ @input << "16th June 1976\n"
813
+ @input.rewind
814
+
815
+ answer = @terminal.ask("Enter your birthday.", Date)
816
+ assert_instance_of(Date, answer)
817
+ assert_equal(16, answer.day)
818
+ assert_equal(6, answer.month)
819
+ assert_equal(1976, answer.year)
820
+
821
+ @input.truncate(@input.rewind)
822
+ pattern = "^yes|no$"
823
+ @input << pattern << "\n"
824
+ @input.rewind
825
+
826
+ answer = @terminal.ask("Give me a pattern to match with: ", Regexp)
827
+ assert_instance_of(Regexp, answer)
828
+ assert_equal(/#{pattern}/, answer)
829
+
830
+ @input.truncate(@input.rewind)
831
+ @input << "gen\n"
832
+ @input.rewind
833
+
834
+ answer = @terminal.ask("Select a mode: ", [:generate, :run])
835
+ assert_instance_of(Symbol, answer)
836
+ assert_equal(:generate, answer)
837
+ end
838
+
839
+ def test_validation
840
+ @input << "system 'rm -rf /'\n105\n0b101_001\n"
841
+ @input.rewind
842
+
843
+ answer = @terminal.ask("Enter a binary number: ") do |q|
844
+ q.validate = /\A(?:0b)?[01_]+\Z/
845
+ end
846
+ assert_equal("0b101_001", answer)
847
+ assert_equal( "Enter a binary number: " +
848
+ "Your answer isn't valid " +
849
+ "(must match /\\A(?:0b)?[01_]+\\Z/).\n" +
850
+ "? " +
851
+ "Your answer isn't valid " +
852
+ "(must match /\\A(?:0b)?[01_]+\\Z/).\n" +
853
+ "? ", @output.string )
854
+
855
+ @input.truncate(@input.rewind)
856
+ @input << "Gray II, James Edward\n" +
857
+ "Gray, Dana Ann Leslie\n" +
858
+ "Gray, James Edward\n"
859
+ @input.rewind
860
+
861
+ answer = @terminal.ask("Your name? ") do |q|
862
+ q.validate = lambda do |name|
863
+ names = name.split(/,\s*/)
864
+ return false unless names.size == 2
865
+ return false if names.first =~ /\s/
866
+ names.last.split.size == 2
867
+ end
868
+ end
869
+ assert_equal("Gray, James Edward", answer)
870
+ end
871
+
872
+ def test_whitespace
873
+ @input << " A lot\tof \t space\t \there! \n"
874
+ @input.rewind
875
+
876
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
877
+ q.whitespace = :chomp
878
+ end
879
+ assert_equal(" A lot\tof \t space\t \there! ", answer)
880
+
881
+ @input.rewind
882
+
883
+ answer = @terminal.ask("Enter a whitespace filled string: ")
884
+ assert_equal("A lot\tof \t space\t \there!", answer)
885
+
886
+ @input.rewind
887
+
888
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
889
+ q.whitespace = :strip_and_collapse
890
+ end
891
+ assert_equal("A lot of space here!", answer)
892
+
893
+ @input.rewind
894
+
895
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
896
+ q.whitespace = :remove
897
+ end
898
+ assert_equal("Alotofspacehere!", answer)
899
+
900
+ @input.rewind
901
+
902
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
903
+ q.whitespace = :none
904
+ end
905
+ assert_equal(" A lot\tof \t space\t \there! \n", answer)
906
+ end
907
+
908
+ def test_wrap
909
+ @terminal.wrap_at = 80
910
+
911
+ @terminal.say("This is a very short line.")
912
+ assert_equal("This is a very short line.\n", @output.string)
913
+
914
+ @output.truncate(@output.rewind)
915
+
916
+ @terminal.say( "This is a long flowing paragraph meant to span " +
917
+ "several lines. This text should definitely be " +
918
+ "wrapped at the set limit, in the result. Your code " +
919
+ "does well with things like this.\n\n" +
920
+ " * This is a simple embedded list.\n" +
921
+ " * You're code should not mess with this...\n" +
922
+ " * Because it's already formatted correctly and " +
923
+ "does not\n" +
924
+ " exceed the limit!" )
925
+ assert_equal( "This is a long flowing paragraph meant to span " +
926
+ "several lines. This text should\n" +
927
+ "definitely be wrapped at the set limit, in the " +
928
+ "result. Your code does well with\n" +
929
+ "things like this.\n\n" +
930
+ " * This is a simple embedded list.\n" +
931
+ " * You're code should not mess with this...\n" +
932
+ " * Because it's already formatted correctly and does " +
933
+ "not\n" +
934
+ " exceed the limit!\n", @output.string )
935
+
936
+ @output.truncate(@output.rewind)
937
+
938
+ @terminal.say("-=" * 50)
939
+ assert_equal(("-=" * 40 + "\n") + ("-=" * 10 + "\n"), @output.string)
940
+ end
941
+
942
+ def test_track_eof
943
+ assert_raise(EOFError) { @terminal.ask("Any input left? ") }
944
+
945
+ # turn EOF tracking
946
+ old_setting = HighLine.track_eof?
947
+ assert_nothing_raised(Exception) { HighLine.track_eof = false }
948
+ begin
949
+ @terminal.ask("And now? ") # this will still blow up, nothing available
950
+ rescue
951
+ assert_not_equal(EOFError, $!.class) # but HighLine's safe guards are off
952
+ end
953
+ HighLine.track_eof = old_setting
954
+ end
955
+
956
+ def test_version
957
+ assert_not_nil(HighLine::VERSION)
958
+ assert_instance_of(String, HighLine::VERSION)
959
+ assert(HighLine::VERSION.frozen?)
960
+ assert_match(/\A\d+\.\d+\.\d+\Z/, HighLine::VERSION)
961
+ end
962
+ end