clone 1.0.0.alpha → 1.0.0.beta

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 (189) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +2 -1
  3. data/Gemfile +1 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +22 -1
  6. data/VERSION +1 -1
  7. data/bin/clone +7 -0
  8. data/clone.gemspec +4 -0
  9. data/docs/readme +16 -0
  10. data/docs/readme.txt +5 -0
  11. data/lib/clone.rb +13 -2
  12. data/lib/clone/config/config.rb +19 -0
  13. data/lib/clone/config/config.yml +22 -0
  14. data/lib/clone/config/default_config.yml +20 -0
  15. data/lib/clone/config/require.rb +3 -0
  16. data/lib/clone/config/version.rb +10 -0
  17. data/lib/clone/config/yml.rb +22 -0
  18. data/lib/clone/config/z_defaults.rb +28 -0
  19. data/lib/clone/generator/engine.rb +581 -0
  20. data/lib/clone/generator/terminal.rb +104 -0
  21. data/lib/clone/helpers/helper_methods.rb +251 -0
  22. data/lib/clone/helpers/local_methods.rb +150 -0
  23. data/lib/clone/helpers/require.rb +37 -0
  24. data/module/Marshal.4.8 +0 -0
  25. data/module/Marshal.4.8.Z +0 -0
  26. data/module/gems/commander-4.1.3/.gitignore +6 -0
  27. data/module/gems/commander-4.1.3/.travis.yml +11 -0
  28. data/module/gems/commander-4.1.3/DEVELOPMENT +15 -0
  29. data/module/gems/commander-4.1.3/Gemfile +3 -0
  30. data/module/gems/commander-4.1.3/History.rdoc +345 -0
  31. data/module/gems/commander-4.1.3/Manifest +38 -0
  32. data/module/gems/commander-4.1.3/README.rdoc +375 -0
  33. data/module/gems/commander-4.1.3/Rakefile +10 -0
  34. data/module/gems/commander-4.1.3/bin/commander +55 -0
  35. data/module/gems/commander-4.1.3/commander.gemspec +26 -0
  36. data/module/gems/commander-4.1.3/lib/commander.rb +32 -0
  37. data/module/gems/commander-4.1.3/lib/commander/blank.rb +8 -0
  38. data/module/gems/commander-4.1.3/lib/commander/command.rb +213 -0
  39. data/module/gems/commander-4.1.3/lib/commander/core_ext.rb +3 -0
  40. data/module/gems/commander-4.1.3/lib/commander/core_ext/array.rb +26 -0
  41. data/module/gems/commander-4.1.3/lib/commander/core_ext/object.rb +11 -0
  42. data/module/gems/commander-4.1.3/lib/commander/delegates.rb +13 -0
  43. data/module/gems/commander-4.1.3/lib/commander/help_formatters.rb +8 -0
  44. data/module/gems/commander-4.1.3/lib/commander/help_formatters/base.rb +18 -0
  45. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal.rb +20 -0
  46. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal/command_help.erb +35 -0
  47. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal/help.erb +36 -0
  48. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal_compact.rb +12 -0
  49. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal_compact/command_help.erb +27 -0
  50. data/module/gems/commander-4.1.3/lib/commander/help_formatters/terminal_compact/help.erb +29 -0
  51. data/module/gems/commander-4.1.3/lib/commander/import.rb +10 -0
  52. data/module/gems/commander-4.1.3/lib/commander/platform.rb +8 -0
  53. data/module/gems/commander-4.1.3/lib/commander/runner.rb +411 -0
  54. data/module/gems/commander-4.1.3/lib/commander/user_interaction.rb +521 -0
  55. data/module/gems/commander-4.1.3/lib/commander/version.rb +3 -0
  56. data/module/gems/commander-4.1.3/spec/command_spec.rb +157 -0
  57. data/module/gems/commander-4.1.3/spec/core_ext/array_spec.rb +20 -0
  58. data/module/gems/commander-4.1.3/spec/core_ext/object_spec.rb +21 -0
  59. data/module/gems/commander-4.1.3/spec/help_formatters/terminal_spec.rb +67 -0
  60. data/module/gems/commander-4.1.3/spec/runner_spec.rb +526 -0
  61. data/module/gems/commander-4.1.3/spec/spec_helper.rb +59 -0
  62. data/module/gems/commander-4.1.3/spec/ui_spec.rb +30 -0
  63. data/module/gems/hello.rb +1 -0
  64. data/module/gems/highline-1.6.19/.gitignore +2 -0
  65. data/module/gems/highline-1.6.19/AUTHORS +3 -0
  66. data/module/gems/highline-1.6.19/CHANGELOG +346 -0
  67. data/module/gems/highline-1.6.19/COPYING +340 -0
  68. data/module/gems/highline-1.6.19/INSTALL +55 -0
  69. data/module/gems/highline-1.6.19/LICENSE +7 -0
  70. data/module/gems/highline-1.6.19/README.rdoc +63 -0
  71. data/module/gems/highline-1.6.19/Rakefile +50 -0
  72. data/module/gems/highline-1.6.19/TODO +6 -0
  73. data/module/gems/highline-1.6.19/examples/ansi_colors.rb +38 -0
  74. data/module/gems/highline-1.6.19/examples/asking_for_arrays.rb +18 -0
  75. data/module/gems/highline-1.6.19/examples/basic_usage.rb +75 -0
  76. data/module/gems/highline-1.6.19/examples/color_scheme.rb +32 -0
  77. data/module/gems/highline-1.6.19/examples/get_character.rb +12 -0
  78. data/module/gems/highline-1.6.19/examples/limit.rb +12 -0
  79. data/module/gems/highline-1.6.19/examples/menus.rb +65 -0
  80. data/module/gems/highline-1.6.19/examples/overwrite.rb +19 -0
  81. data/module/gems/highline-1.6.19/examples/page_and_wrap.rb +322 -0
  82. data/module/gems/highline-1.6.19/examples/password.rb +7 -0
  83. data/module/gems/highline-1.6.19/examples/repeat_entry.rb +21 -0
  84. data/module/gems/highline-1.6.19/examples/trapping_eof.rb +22 -0
  85. data/module/gems/highline-1.6.19/examples/using_readline.rb +17 -0
  86. data/module/gems/highline-1.6.19/highline.gemspec +37 -0
  87. data/module/gems/highline-1.6.19/lib/highline.rb +1012 -0
  88. data/module/gems/highline-1.6.19/lib/highline/color_scheme.rb +134 -0
  89. data/module/gems/highline-1.6.19/lib/highline/compatibility.rb +16 -0
  90. data/module/gems/highline-1.6.19/lib/highline/import.rb +41 -0
  91. data/module/gems/highline-1.6.19/lib/highline/menu.rb +398 -0
  92. data/module/gems/highline-1.6.19/lib/highline/question.rb +475 -0
  93. data/module/gems/highline-1.6.19/lib/highline/simulate.rb +48 -0
  94. data/module/gems/highline-1.6.19/lib/highline/string_extensions.rb +131 -0
  95. data/module/gems/highline-1.6.19/lib/highline/style.rb +181 -0
  96. data/module/gems/highline-1.6.19/lib/highline/system_extensions.rb +222 -0
  97. data/module/gems/highline-1.6.19/setup.rb +1360 -0
  98. data/module/gems/highline-1.6.19/site/.cvsignore +1 -0
  99. data/module/gems/highline-1.6.19/site/highline.css +65 -0
  100. data/module/gems/highline-1.6.19/site/images/logo.png +0 -0
  101. data/module/gems/highline-1.6.19/site/index.html +58 -0
  102. data/module/gems/highline-1.6.19/test/string_methods.rb +32 -0
  103. data/module/gems/highline-1.6.19/test/tc_color_scheme.rb +96 -0
  104. data/module/gems/highline-1.6.19/test/tc_highline.rb +1128 -0
  105. data/module/gems/highline-1.6.19/test/tc_import.rb +52 -0
  106. data/module/gems/highline-1.6.19/test/tc_menu.rb +439 -0
  107. data/module/gems/highline-1.6.19/test/tc_string_extension.rb +20 -0
  108. data/module/gems/highline-1.6.19/test/tc_string_highline.rb +38 -0
  109. data/module/gems/highline-1.6.19/test/tc_style.rb +567 -0
  110. data/module/gems/highline-1.6.19/test/ts_all.rb +16 -0
  111. data/module/latest_specs.4.8 +0 -0
  112. data/module/latest_specs.4.8.gz +0 -0
  113. data/module/prerelease_specs.4.8 +0 -0
  114. data/module/prerelease_specs.4.8.gz +0 -0
  115. data/module/specs.4.8 +0 -0
  116. data/module/specs.4.8.gz +0 -0
  117. data/samples/blather/restlike/Gemfile +4 -0
  118. data/samples/blather/restlike/cmd.yml +1 -0
  119. data/samples/blather/restlike/lib/blather.rb +9 -0
  120. data/samples/blather/restlike/lib/blather/dsl/api.rb +78 -0
  121. data/samples/blather/restlike/lib/blather/dsl/call.rb +13 -0
  122. data/samples/blather/restlike/lib/blather/dsl/client.rb +58 -0
  123. data/samples/blather/restlike/lib/blather/dsl/config.rb +11 -0
  124. data/samples/blather/restlike/lib/blather/dsl/extraDSL.rb +163 -0
  125. data/samples/blather/restlike/lib/blather/meta/require.rb +8 -0
  126. data/samples/blather/restlike/lib/blather/meta/xmpp.yml +5 -0
  127. data/samples/blather/restlike/lib/blather/vendors/xmpp_default.rb +27 -0
  128. data/samples/blather/restlike/readme +2 -0
  129. data/samples/grape/init/Gemfile +2 -0
  130. data/samples/grape/init/cmd.yml +3 -0
  131. data/samples/grape/init/config.ru +2 -0
  132. data/samples/grape/init/docs/grape/documentation.txt +939 -0
  133. data/samples/grape/init/docs/grape/generate_rest_routes.rb +37 -0
  134. data/samples/grape/init/docs/grape/ls_routes.rb +31 -0
  135. data/samples/grape/init/lib/grape.rb +4 -0
  136. data/samples/grape/init/lib/grape/meta/subclasses.rb +20 -0
  137. data/samples/grape/init/lib/grape/xpath/app.rb +30 -0
  138. data/samples/grape/init/lib/grape/xpath/ruotes.rb +6 -0
  139. data/samples/grape/init/readme +1 -0
  140. data/samples/grape/readme +29 -0
  141. data/samples/grape/vendor/lib/grape/vendors/v1/rest.rb +57 -0
  142. data/samples/mongoid/cmd.yml +1 -0
  143. data/samples/mongoid/init/Gemfile +3 -0
  144. data/samples/mongoid/init/cmd.yml +2 -0
  145. data/samples/mongoid/init/docs/mongoid/ModelsRelations.rb +11 -0
  146. data/samples/mongoid/init/docs/mongoid/documents.xls +0 -0
  147. data/samples/mongoid/init/docs/mongoid/generate_modelsToDocs.rb +25 -0
  148. data/samples/mongoid/init/docs/mongoid/modelsToDocs.rb +25 -0
  149. data/samples/mongoid/init/docs/mongoid/relations.txt +1354 -0
  150. data/samples/mongoid/init/lib/mongoid.rb +44 -0
  151. data/samples/mongoid/init/lib/mongoid/dsl/extraDSL_CRUD.rb +446 -0
  152. data/samples/mongoid/init/lib/mongoid/dsl/extraDSL_MP.rb +517 -0
  153. data/samples/mongoid/init/lib/mongoid/dsl/init.rb +37 -0
  154. data/samples/mongoid/init/lib/mongoid/dsl/params.rb +67 -0
  155. data/samples/mongoid/init/lib/mongoid/meta/banned.rb +147 -0
  156. data/samples/mongoid/init/lib/mongoid/meta/control.yml +13 -0
  157. data/samples/mongoid/init/lib/mongoid/meta/mongoid.yml +6 -0
  158. data/samples/mongoid/init/lib/mongoid/meta/mpatch.rb +14 -0
  159. data/samples/mongoid/model/lib/mongoid/models/model.rb +28 -0
  160. data/samples/mongoid/readme +33 -0
  161. data/samples/rack/init/Gemfile +10 -0
  162. data/samples/rack/init/cmd.yml +2 -0
  163. data/samples/rack/init/config.ru +1 -0
  164. data/samples/rack/init/docs/rack/rake introducing.txt +60 -0
  165. data/samples/rack/init/docs/rack/webservers/Thin +43 -0
  166. data/samples/rack/init/docs/rack/webservers/ebb +72 -0
  167. data/samples/rack/init/docs/rack/webservers/fcgi +103 -0
  168. data/samples/rack/init/docs/rack/webservers/mongrel +74 -0
  169. data/samples/rack/init/docs/rack/webservers/passenger +37 -0
  170. data/samples/rack/init/docs/rack/webservers/scgi +188 -0
  171. data/samples/rack/init/lib/rack.rb +1 -0
  172. data/samples/rack/init/lib/rack/meta/webserver/thin.rb +45 -0
  173. data/samples/rack/init/lib/rack/meta/webserver/thin.yml +6 -0
  174. data/samples/rack/init/server.rb +0 -0
  175. data/samples/rack/readme +13 -0
  176. data/samples/rest_client/init/Gemfile +5 -0
  177. data/samples/rest_client/init/boot.rb +2 -0
  178. data/samples/rest_client/init/cmd.yml +1 -0
  179. data/samples/rest_client/init/config/rest_client/defaults.rb +16 -0
  180. data/samples/rest_client/init/docs/rest_client/simple overlook +251 -0
  181. data/samples/rest_client/init/test/rest_client/rest_dsl.rb +5 -0
  182. data/samples/rest_client/readme +7 -0
  183. data/samples/scripts/lines_counter/lines_number.rb +32 -0
  184. data/samples/scripts/lines_counter/readme +5 -0
  185. data/samples/scripts/readme +1 -0
  186. metadata +197 -7
  187. data/lib/clone/cms.rb +0 -56
  188. data/lib/clone/ext.rb +0 -77
  189. data/sample/test.rb +0 -30
@@ -0,0 +1,65 @@
1
+ body {
2
+ margin: 0;
3
+ padding: 0;
4
+ text-align: center;
5
+ background: #abc;
6
+ font: 13px Trebuchet MS, Lucida Sans Unicode, Arial, sans-serif;
7
+ }
8
+
9
+ #container {
10
+ margin: 0 auto;
11
+ margin-top: 10px;
12
+ position: relative;
13
+ width: 785px;
14
+ background: #fff repeat;
15
+ padding-top: 20px;
16
+ }
17
+
18
+ #container #header {
19
+ padding-top: 20px;
20
+ height: 141px;
21
+ width: 785px;
22
+ background: url(images/logo.png) center no-repeat;
23
+ }
24
+
25
+ #container #content {
26
+ text-align: left;
27
+ margin-left: 230px;
28
+ width: 500px;
29
+ }
30
+
31
+ #container #sidebar {
32
+ text-align: left;
33
+ position: absolute;
34
+ top: 181px;
35
+ left: 0;
36
+ width: 170px;
37
+ margin-right: 25px;
38
+ }
39
+
40
+ #container #footer {
41
+ font: 9px Arial;
42
+ padding: 4px;
43
+ border-top: 1px solid;
44
+ color: #aaa;
45
+ }
46
+
47
+ ul {
48
+ list-style: none;
49
+ }
50
+
51
+ li {
52
+ border-bottom: 1px solid #e8e8e8;
53
+ padding-top: 10px;
54
+ }
55
+
56
+ a {
57
+ text-decoration: none;
58
+ color: #15528a;
59
+ }
60
+
61
+ pre {
62
+ padding: 5px;
63
+ padding-left: 20px;
64
+ font: 11px Courier;
65
+ }
@@ -0,0 +1,58 @@
1
+ <html>
2
+ <head>
3
+ <title> HighLine </title>
4
+ <style type="text/css"> @import url(highline.css); </style>
5
+ </head>
6
+ <body>
7
+ <div id="container">
8
+ <div id="header"></div>
9
+ <div id="content">
10
+ <h2>HighLine is about&#8230;</h2>
11
+
12
+
13
+ <h3>Saving time.</h3>
14
+
15
+
16
+ <p>Command line interfaces are meant to be easy. So why shouldn&#8217;t building
17
+ them be easy, too? HighLine provides a solid toolset to help you get
18
+ the job done cleanly so you can focus on the real task at hand,
19
+ <em>your task.</em></p>
20
+
21
+
22
+ <h3>Clean and intuitive design.</h3>
23
+
24
+
25
+ <p>Want to get a taste for how HighLine is used? Take a look at this simple
26
+ example, which asks a user for a zip code, automatically does validation,
27
+ and returns the result:</p>
28
+
29
+
30
+ <pre><code>zip = ask("Zip? ") { |q| q.validate = /\A\d{5}(?:-?\d{4})?\Z/ }</code></pre>
31
+
32
+
33
+ <h3>Hassle-free Installation.</h3>
34
+
35
+
36
+ <p>Installation is easy via RubyGems. Simply enter the command:</p>
37
+
38
+
39
+ <pre><code>sudo gem install highline</code></pre>
40
+
41
+
42
+ <p>and you&#8217;ll be on your way! Of course, manual installation is an option,
43
+ too.</p>
44
+ </div>
45
+ <div id="sidebar">
46
+ <ul>
47
+ <li><a href="http://rubyforge.org/projects/highline">Project Page</a></li>
48
+ <li><a href="http://highline.rubyforge.org/doc">Documentation</a></li>
49
+ <li><a href="https://github.com/JEG2/highline">Source</a></li>
50
+ <li><a href="mailto:james@grayproductions.net">Contact</a></li>
51
+ </ul>
52
+ </div>
53
+ <div id="footer">
54
+ Created by James Edward Gray II and Gregory Brown
55
+ </div>
56
+ </div>
57
+ </body>
58
+ </html>
@@ -0,0 +1,32 @@
1
+ # string_methods.rb
2
+ #
3
+ # Created by Richard LeBer 2011-06-27
4
+ #
5
+ # This is Free Software. See LICENSE and COPYING for details.
6
+ #
7
+ # String class convenience methods
8
+
9
+ module StringMethods
10
+ def test_color
11
+ assert_equal("\e[34mstring\e[0m", @string.color(:blue))
12
+ assert_equal("\e[1m\e[47mstring\e[0m", @string.color(:bold,:on_white))
13
+ assert_equal("\e[45mstring\e[0m", @string.on(:magenta))
14
+ assert_equal("\e[36mstring\e[0m", @string.cyan)
15
+ assert_equal("\e[41m\e[5mstring\e[0m\e[0m", @string.blink.on_red)
16
+ assert_equal("\e[38;5;137mstring\e[0m", @string.color(:rgb_906030))
17
+ assert_equal("\e[38;5;101mstring\e[0m", @string.rgb('606030'))
18
+ assert_equal("\e[38;5;107mstring\e[0m", @string.rgb('60','90','30'))
19
+ assert_equal("\e[38;5;107mstring\e[0m", @string.rgb(96,144,48))
20
+ assert_equal("\e[38;5;173mstring\e[0m", @string.rgb_c06030)
21
+ assert_equal("\e[48;5;137mstring\e[0m", @string.color(:on_rgb_906030))
22
+ assert_equal("\e[48;5;101mstring\e[0m", @string.on_rgb('606030'))
23
+ assert_equal("\e[48;5;107mstring\e[0m", @string.on_rgb('60','90','30'))
24
+ assert_equal("\e[48;5;107mstring\e[0m", @string.on_rgb(96,144,48))
25
+ assert_equal("\e[48;5;173mstring\e[0m", @string.on_rgb_c06030)
26
+ end
27
+
28
+ def test_uncolor
29
+ colored_string = HighLine::String("\e[38;5;137mstring\e[0m")
30
+ assert_equal "string", colored_string.uncolor
31
+ end
32
+ end
@@ -0,0 +1,96 @@
1
+ # tc_color_scheme.rb
2
+ #
3
+ # Created by Jeremy Hinegardner on 2007-01-24.
4
+ # Copyright 2007 Jeremy Hinegardner. All rights reserved.
5
+ #
6
+ # This is Free Software. See LICENSE and COPYING for details.
7
+
8
+ require "test/unit"
9
+
10
+ require "highline"
11
+ require "stringio"
12
+
13
+ class TestColorScheme < Test::Unit::TestCase
14
+ def setup
15
+ @input = StringIO.new
16
+ @output = StringIO.new
17
+ @terminal = HighLine.new(@input, @output)
18
+
19
+ @old_color_scheme = HighLine.color_scheme
20
+ end
21
+
22
+ def teardown
23
+ HighLine.color_scheme = @old_color_scheme
24
+ end
25
+
26
+ def test_using_color_scheme
27
+ assert_equal(false,HighLine.using_color_scheme?)
28
+
29
+ HighLine.color_scheme = HighLine::ColorScheme.new
30
+ assert_equal(true,HighLine.using_color_scheme?)
31
+ end
32
+
33
+ def test_scheme
34
+ HighLine.color_scheme = HighLine::SampleColorScheme.new
35
+
36
+ @terminal.say("This should be <%= color('warning yellow', :warning) %>.")
37
+ assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string)
38
+ @output.rewind
39
+
40
+ @terminal.say("This should be <%= color('warning yellow', 'warning') %>.")
41
+ assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string)
42
+ @output.rewind
43
+
44
+ @terminal.say("This should be <%= color('warning yellow', 'WarNing') %>.")
45
+ assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string)
46
+ @output.rewind
47
+
48
+ # Check that keys are available, and as expected
49
+ assert_equal ["critical", "error", "warning", "notice", "info", "debug", "row_even", "row_odd"].sort,
50
+ HighLine.color_scheme.keys.sort
51
+
52
+ # Color scheme doesn't care if we use symbols or strings, and is case-insensitive
53
+ warning1 = HighLine.color_scheme[:warning]
54
+ warning2 = HighLine.color_scheme["warning"]
55
+ warning3 = HighLine.color_scheme[:wArning]
56
+ warning4 = HighLine.color_scheme["warniNg"]
57
+ assert_instance_of HighLine::Style, warning1
58
+ assert_instance_of HighLine::Style, warning2
59
+ assert_instance_of HighLine::Style, warning3
60
+ assert_instance_of HighLine::Style, warning4
61
+ assert_equal warning1, warning2
62
+ assert_equal warning1, warning3
63
+ assert_equal warning1, warning4
64
+
65
+ # Nonexistent keys return nil
66
+ assert_nil HighLine.color_scheme[:nonexistent]
67
+
68
+ # Same as above, for definitions
69
+ defn1 = HighLine.color_scheme.definition(:warning)
70
+ defn2 = HighLine.color_scheme.definition("warning")
71
+ defn3 = HighLine.color_scheme.definition(:wArning)
72
+ defn4 = HighLine.color_scheme.definition("warniNg")
73
+ assert_instance_of Array, defn1
74
+ assert_instance_of Array, defn2
75
+ assert_instance_of Array, defn3
76
+ assert_instance_of Array, defn4
77
+ assert_equal [:bold, :yellow], defn1
78
+ assert_equal [:bold, :yellow], defn2
79
+ assert_equal [:bold, :yellow], defn3
80
+ assert_equal [:bold, :yellow], defn4
81
+ assert_nil HighLine.color_scheme.definition(:nonexistent)
82
+
83
+ color_scheme_hash = HighLine.color_scheme.to_hash
84
+ assert_instance_of Hash, color_scheme_hash
85
+ assert_equal ["critical", "error", "warning", "notice", "info", "debug", "row_even", "row_odd"].sort,
86
+ color_scheme_hash.keys.sort
87
+ assert_instance_of Array, HighLine.color_scheme.definition(:warning)
88
+ assert_equal [:bold, :yellow], HighLine.color_scheme.definition(:warning)
89
+
90
+ # turn it back off, should raise an exception
91
+ HighLine.color_scheme = @old_color_scheme
92
+ assert_raises(NameError) {
93
+ @terminal.say("This should be <%= color('nothing at all', :error) %>.")
94
+ }
95
+ end
96
+ end
@@ -0,0 +1,1128 @@
1
+ # tc_highline.rb
2
+ #
3
+ # Created by James Edward Gray II on 2005-04-26.
4
+ # Copyright 2005 Gray Productions. All rights reserved.
5
+ #
6
+ # This is Free Software. See LICENSE and COPYING for details.
7
+
8
+ require "test/unit"
9
+
10
+ require "highline"
11
+ require "stringio"
12
+
13
+ if HighLine::CHARACTER_MODE == "Win32API"
14
+ class HighLine
15
+ # Override Windows' character reading so it's not tied to STDIN.
16
+ def get_character( input = STDIN )
17
+ input.getc
18
+ end
19
+ end
20
+ end
21
+
22
+ class TestHighLine < Test::Unit::TestCase
23
+ def setup
24
+ @input = StringIO.new
25
+ @output = StringIO.new
26
+ @terminal = HighLine.new(@input, @output)
27
+ end
28
+
29
+ def test_agree
30
+ @input << "y\nyes\nYES\nHell no!\nNo\n"
31
+ @input.rewind
32
+
33
+ assert_equal(true, @terminal.agree("Yes or no? "))
34
+ assert_equal(true, @terminal.agree("Yes or no? "))
35
+ assert_equal(true, @terminal.agree("Yes or no? "))
36
+ assert_equal(false, @terminal.agree("Yes or no? "))
37
+
38
+ @input.truncate(@input.rewind)
39
+ @input << "yellow"
40
+ @input.rewind
41
+
42
+ assert_equal(true, @terminal.agree("Yes or no? ", :getc))
43
+ end
44
+
45
+ def test_agree_with_block
46
+ @input << "\n\n"
47
+ @input.rewind
48
+
49
+ assert_equal(true, @terminal.agree("Yes or no? ") { |q| q.default = "y" })
50
+ assert_equal(false, @terminal.agree("Yes or no? ") { |q| q.default = "n" })
51
+ end
52
+
53
+ def test_ask
54
+ name = "James Edward Gray II"
55
+ @input << name << "\n"
56
+ @input.rewind
57
+
58
+ assert_equal(name, @terminal.ask("What is your name? "))
59
+
60
+ assert_raise(EOFError) { @terminal.ask("Any input left? ") }
61
+ end
62
+
63
+ def test_ask_string
64
+ name = "James Edward Gray II"
65
+ @input << name << "\n"
66
+ @input.rewind
67
+
68
+ assert_equal(name, @terminal.ask("What is your name? ", String))
69
+
70
+ assert_raise(EOFError) { @terminal.ask("Any input left? ", String) }
71
+ end
72
+
73
+ def test_indent
74
+ text = "Testing...\n"
75
+ @terminal.indent_level=1
76
+ @terminal.say(text)
77
+ assert_equal(' '*3+text, @output.string)
78
+
79
+ @output.truncate(@output.rewind)
80
+ @terminal.indent_level=3
81
+ @terminal.say(text)
82
+ assert_equal(' '*9+text, @output.string)
83
+
84
+ @output.truncate(@output.rewind)
85
+ @terminal.indent_level=0
86
+ @terminal.indent_size=5
87
+ @terminal.indent(2, text)
88
+ assert_equal(' '*10+text, @output.string)
89
+
90
+ @output.truncate(@output.rewind)
91
+ @terminal.indent_size=4
92
+ @terminal.indent {
93
+ @terminal.say(text)
94
+ }
95
+ assert_equal(' '*4+text, @output.string)
96
+
97
+ @output.truncate(@output.rewind)
98
+ @terminal.indent_size=2
99
+ @terminal.indent(3) { |t|
100
+ t.say(text)
101
+ }
102
+ assert_equal(' '*6+text, @output.string)
103
+
104
+ @output.truncate(@output.rewind)
105
+ @terminal.indent { |t|
106
+ t.indent {
107
+ t.indent {
108
+ t.indent { |tt|
109
+ tt.say(text)
110
+ }
111
+ }
112
+ }
113
+ }
114
+ assert_equal(' '*8+text, @output.string)
115
+
116
+ text = "Multi\nLine\nIndentation\n"
117
+ indent = ' '*4
118
+ @terminal.indent_level=2
119
+ @output.truncate(@output.rewind)
120
+ @terminal.say(text)
121
+ assert_equal("#{indent}Multi\n#{indent}Line\n#{indent}Indentation\n", @output.string)
122
+
123
+ @output.truncate(@output.rewind)
124
+ @terminal.multi_indent = false
125
+ @terminal.say(text)
126
+ assert_equal("#{indent}Multi\nLine\nIndentation\n", @output.string)
127
+
128
+ @output.truncate(@output.rewind)
129
+ @terminal.indent(0, text, true)
130
+ assert_equal("#{indent}Multi\n#{indent}Line\n#{indent}Indentation\n", @output.string)
131
+ end
132
+
133
+ def test_newline
134
+ @terminal.newline
135
+ @terminal.newline
136
+ assert_equal("\n\n", @output.string)
137
+ end
138
+
139
+ def test_bug_fixes
140
+ # auto-complete bug
141
+ @input << "ruby\nRuby\n"
142
+ @input.rewind
143
+
144
+ languages = [:Perl, :Python, :Ruby]
145
+ answer = @terminal.ask( "What is your favorite programming language? ",
146
+ languages )
147
+ assert_equal(languages.last, answer)
148
+
149
+ @input.truncate(@input.rewind)
150
+ @input << "ruby\n"
151
+ @input.rewind
152
+
153
+ answer = @terminal.ask( "What is your favorite programming language? ",
154
+ languages ) do |q|
155
+ q.case = :capitalize
156
+ end
157
+ assert_equal(languages.last, answer)
158
+
159
+ # poor auto-complete error message
160
+ @input.truncate(@input.rewind)
161
+ @input << "lisp\nruby\n"
162
+ @input.rewind
163
+ @output.truncate(@output.rewind)
164
+
165
+ answer = @terminal.ask( "What is your favorite programming language? ",
166
+ languages ) do |q|
167
+ q.case = :capitalize
168
+ end
169
+ assert_equal(languages.last, answer)
170
+ assert_equal( "What is your favorite programming language? " +
171
+ "You must choose one of [:Perl, :Python, :Ruby].\n" +
172
+ "? ", @output.string )
173
+ end
174
+
175
+ def test_case_changes
176
+ @input << "jeg2\n"
177
+ @input.rewind
178
+
179
+ answer = @terminal.ask("Enter your initials ") do |q|
180
+ q.case = :up
181
+ end
182
+ assert_equal("JEG2", answer)
183
+
184
+ @input.truncate(@input.rewind)
185
+ @input << "cRaZY\n"
186
+ @input.rewind
187
+
188
+ answer = @terminal.ask("Enter a search string: ") do |q|
189
+ q.case = :down
190
+ end
191
+ assert_equal("crazy", answer)
192
+ end
193
+
194
+ def test_character_echo
195
+ @input << "password\r"
196
+ @input.rewind
197
+
198
+ answer = @terminal.ask("Please enter your password: ") do |q|
199
+ q.echo = "*"
200
+ end
201
+ assert_equal("password", answer)
202
+ assert_equal("Please enter your password: ********\n", @output.string)
203
+
204
+ @input.truncate(@input.rewind)
205
+ @input << "2"
206
+ @input.rewind
207
+ @output.truncate(@output.rewind)
208
+
209
+ answer = @terminal.ask( "Select an option (1, 2 or 3): ",
210
+ Integer ) do |q|
211
+ q.echo = "*"
212
+ q.character = true
213
+ end
214
+ assert_equal(2, answer)
215
+ assert_equal("Select an option (1, 2 or 3): *\n", @output.string)
216
+ end
217
+
218
+ def test_backspace_does_not_enter_prompt
219
+ @input << "\b\b"
220
+ @input.rewind
221
+ answer = @terminal.ask("Please enter your password: ") do |q|
222
+ q.echo = "*"
223
+ end
224
+ assert_equal("", answer)
225
+ assert_equal("Please enter your password: \n", @output.string)
226
+ end
227
+
228
+ def test_readline_on_non_echo_question_has_prompt
229
+ @input << "you can't see me"
230
+ @input.rewind
231
+ answer = @terminal.ask("Please enter some hidden text: ") do |q|
232
+ q.readline = true
233
+ q.echo = "*"
234
+ end
235
+ assert_equal("you can't see me", answer)
236
+ assert_equal("Please enter some hidden text: ****************\n", @output.string)
237
+ end
238
+
239
+ def test_character_reading
240
+ # WARNING: This method does NOT cover Unix and Windows savvy testing!
241
+ @input << "12345"
242
+ @input.rewind
243
+
244
+ answer = @terminal.ask("Enter a single digit: ", Integer) do |q|
245
+ q.character = :getc
246
+ end
247
+ assert_equal(1, answer)
248
+ end
249
+
250
+ def test_color
251
+ @terminal.say("This should be <%= BLUE %>blue<%= CLEAR %>!")
252
+ assert_equal("This should be \e[34mblue\e[0m!\n", @output.string)
253
+
254
+ @output.truncate(@output.rewind)
255
+
256
+ @terminal.say( "This should be " +
257
+ "<%= BOLD + ON_WHITE %>bold on white<%= CLEAR %>!" )
258
+ assert_equal( "This should be \e[1m\e[47mbold on white\e[0m!\n",
259
+ @output.string )
260
+
261
+ @output.truncate(@output.rewind)
262
+
263
+ @terminal.say("This should be <%= color('cyan', CYAN) %>!")
264
+ assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string)
265
+
266
+ @output.truncate(@output.rewind)
267
+
268
+ @terminal.say( "This should be " +
269
+ "<%= color('blinking on red', :blink, :on_red) %>!" )
270
+ assert_equal( "This should be \e[5m\e[41mblinking on red\e[0m!\n",
271
+ @output.string )
272
+
273
+ @output.truncate(@output.rewind)
274
+
275
+ @terminal.say("This should be <%= NONE %>none<%= CLEAR %>!")
276
+ assert_equal("This should be \e[38mnone\e[0m!\n", @output.string)
277
+
278
+ @output.truncate(@output.rewind)
279
+
280
+ @terminal.say("This should be <%= RGB_906030 %>rgb_906030<%= CLEAR %>!")
281
+ assert_equal("This should be \e[38;5;137mrgb_906030\e[0m!\n", @output.string)
282
+
283
+ @output.truncate(@output.rewind)
284
+
285
+ @terminal.say("This should be <%= ON_RGB_C06030 %>on_rgb_c06030<%= CLEAR %>!")
286
+ assert_equal("This should be \e[48;5;173mon_rgb_c06030\e[0m!\n", @output.string)
287
+
288
+ @output.truncate(@output.rewind)
289
+
290
+ # Does class method work, too?
291
+ @terminal.say("This should be <%= HighLine.color('reverse underlined magenta', :reverse, :underline, :magenta) %>!")
292
+ assert_equal( "This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n",
293
+ @output.string )
294
+
295
+ @output.truncate(@output.rewind)
296
+
297
+ # turn off color
298
+ old_setting = HighLine.use_color?
299
+ assert_nothing_raised(Exception) { HighLine.use_color = false }
300
+ @terminal.say("This should be <%= color('cyan', CYAN) %>!")
301
+ assert_equal("This should be cyan!\n", @output.string)
302
+ HighLine.use_color = old_setting
303
+ end
304
+
305
+ def test_uncolor
306
+ # instance method
307
+ assert_equal( "This should be reverse underlined magenta!\n",
308
+ @terminal.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n")
309
+ )
310
+
311
+ @output.truncate(@output.rewind)
312
+
313
+ # class method
314
+ assert_equal( "This should be reverse underlined magenta!\n",
315
+ HighLine.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n")
316
+ )
317
+
318
+ @output.truncate(@output.rewind)
319
+
320
+ # RGB color
321
+ assert_equal( "This should be rgb_906030!\n",
322
+ @terminal.uncolor("This should be \e[38;5;137mrgb_906030\e[0m!\n")
323
+ )
324
+ end
325
+
326
+ def test_confirm
327
+ @input << "junk.txt\nno\nsave.txt\ny\n"
328
+ @input.rewind
329
+
330
+ answer = @terminal.ask("Enter a filename: ") do |q|
331
+ q.confirm = "Are you sure you want to overwrite <%= @answer %>? "
332
+ q.responses[:ask_on_error] = :question
333
+ end
334
+ assert_equal("save.txt", answer)
335
+ assert_equal( "Enter a filename: " +
336
+ "Are you sure you want to overwrite junk.txt? " +
337
+ "Enter a filename: " +
338
+ "Are you sure you want to overwrite save.txt? ",
339
+ @output.string )
340
+
341
+ @input.truncate(@input.rewind)
342
+ @input << "junk.txt\nyes\nsave.txt\nn\n"
343
+ @input.rewind
344
+ @output.truncate(@output.rewind)
345
+
346
+ answer = @terminal.ask("Enter a filename: ") do |q|
347
+ q.confirm = "Are you sure you want to overwrite <%= @answer %>? "
348
+ end
349
+ assert_equal("junk.txt", answer)
350
+ assert_equal( "Enter a filename: " +
351
+ "Are you sure you want to overwrite junk.txt? ",
352
+ @output.string )
353
+ end
354
+
355
+ def test_defaults
356
+ @input << "\nNo Comment\n"
357
+ @input.rewind
358
+
359
+ answer = @terminal.ask("Are you sexually active? ") do |q|
360
+ q.validate = /\Ay(?:es)?|no?|no comment\Z/i
361
+ end
362
+ assert_equal("No Comment", answer)
363
+
364
+ @input.truncate(@input.rewind)
365
+ @input << "\nYes\n"
366
+ @input.rewind
367
+ @output.truncate(@output.rewind)
368
+
369
+ answer = @terminal.ask("Are you sexually active? ") do |q|
370
+ q.default = "No Comment"
371
+ q.validate = /\Ay(?:es)?|no?|no comment\Z/i
372
+ end
373
+ assert_equal("No Comment", answer)
374
+ assert_equal( "Are you sexually active? |No Comment| ",
375
+ @output.string )
376
+ end
377
+
378
+ def test_string_preservation
379
+ @input << "Maybe\nYes\n"
380
+ @input.rewind
381
+
382
+ my_string = "Is that your final answer? "
383
+
384
+ @terminal.ask(my_string) { |q| q.default = "Possibly" }
385
+ @terminal.ask(my_string) { |q| q.default = "Maybe" }
386
+
387
+ assert_equal("Is that your final answer? ", my_string)
388
+ end
389
+
390
+ def test_empty
391
+ @input << "\n"
392
+ @input.rewind
393
+
394
+ answer = @terminal.ask("") do |q|
395
+ q.default = "yes"
396
+ q.validate = /\Ay(?:es)?|no?\Z/i
397
+ end
398
+ assert_equal("yes", answer)
399
+ end
400
+
401
+ def test_erb
402
+ @terminal.say( "The integers from 1 to 10 are:\n" +
403
+ "% (1...10).each do |n|\n" +
404
+ "\t<%= n %>,\n" +
405
+ "% end\n" +
406
+ "\tand 10" )
407
+ assert_equal( "The integers from 1 to 10 are:\n" +
408
+ "\t1,\n\t2,\n\t3,\n\t4,\n\t5,\n" +
409
+ "\t6,\n\t7,\n\t8,\n\t9,\n\tand 10\n",
410
+ @output.string )
411
+ end
412
+
413
+ def test_files
414
+ @input << "#{File.basename(__FILE__)[0, 5]}\n"
415
+ @input.rewind
416
+
417
+ assert_equal "tc_hi\n",@input.read
418
+ @input.rewind
419
+
420
+ file = @terminal.ask("Select a file: ", File) do |q|
421
+ q.directory = File.expand_path(File.dirname(__FILE__))
422
+ q.glob = "*.rb"
423
+ end
424
+ assert_instance_of(File, file)
425
+ assert_equal("# tc_highline.rb\n", file.gets)
426
+ file.close
427
+
428
+ @input.rewind
429
+
430
+ pathname = @terminal.ask("Select a file: ", Pathname) do |q|
431
+ q.directory = File.expand_path(File.dirname(__FILE__))
432
+ q.glob = "*.rb"
433
+ end
434
+ assert_instance_of(Pathname, pathname)
435
+ assert_equal(File.size(__FILE__), pathname.size)
436
+ end
437
+
438
+ def test_gather
439
+ @input << "James\nDana\nStorm\nGypsy\n\n"
440
+ @input.rewind
441
+
442
+ answers = @terminal.ask("Enter four names:") do |q|
443
+ q.gather = 4
444
+ end
445
+ assert_equal(%w{James Dana Storm Gypsy}, answers)
446
+ assert_equal("\n", @input.gets)
447
+ assert_equal("Enter four names:\n", @output.string)
448
+
449
+ @input.rewind
450
+
451
+ answers = @terminal.ask("Enter four names:") do |q|
452
+ q.gather = ""
453
+ end
454
+ assert_equal(%w{James Dana Storm Gypsy}, answers)
455
+
456
+ @input.rewind
457
+
458
+ answers = @terminal.ask("Enter four names:") do |q|
459
+ q.gather = /^\s*$/
460
+ end
461
+ assert_equal(%w{James Dana Storm Gypsy}, answers)
462
+
463
+ @input.truncate(@input.rewind)
464
+ @input << "29\n49\n30\n"
465
+ @input.rewind
466
+ @output.truncate(@output.rewind)
467
+
468
+ answers = @terminal.ask("<%= @key %>: ", Integer) do |q|
469
+ q.gather = { "Age" => 0, "Wife's Age" => 0, "Father's Age" => 0}
470
+ end
471
+ assert_equal( { "Age" => 29, "Wife's Age" => 30, "Father's Age" => 49},
472
+ answers )
473
+ assert_equal("Age: Father's Age: Wife's Age: ", @output.string)
474
+ end
475
+
476
+ def test_typing_verification
477
+ @input << "all work and no play makes jack a dull boy\n" * 3
478
+ @input.rewind
479
+
480
+ answer = @terminal.ask("How's work? ") do |q|
481
+ q.gather = 3
482
+ q.verify_match = true
483
+ end
484
+ assert_equal("all work and no play makes jack a dull boy", answer)
485
+
486
+ @input.truncate(@input.rewind)
487
+ @input << "all play and no work makes jack a mere toy\n"
488
+ @input << "all work and no play makes jack a dull boy\n" * 5
489
+ @input.rewind
490
+ @output.truncate(@output.rewind)
491
+
492
+ answer = @terminal.ask("How are things going? ") do |q|
493
+ q.gather = 3
494
+ q.verify_match = true
495
+ q.responses[:mismatch] = 'Typing mismatch!'
496
+ q.responses[:ask_on_error] = ''
497
+ end
498
+ assert_equal("all work and no play makes jack a dull boy", answer)
499
+
500
+ # now try using a hash for gather
501
+
502
+ @input.truncate(@input.rewind)
503
+ @input << "Password\nPassword\n"
504
+ @input.rewind
505
+ @output.truncate(@output.rewind)
506
+
507
+ answer = @terminal.ask("<%= @key %>: ") do |q|
508
+ q.verify_match = true
509
+ q.gather = {"Enter a password" => '', "Please type it again" => ''}
510
+ end
511
+ assert_equal("Password", answer)
512
+
513
+ @input.truncate(@input.rewind)
514
+ @input << "Password\nMistake\nPassword\nPassword\n"
515
+ @input.rewind
516
+ @output.truncate(@output.rewind)
517
+
518
+ answer = @terminal.ask("<%= @key %>: ") do |q|
519
+ q.verify_match = true
520
+ q.responses[:mismatch] = 'Typing mismatch!'
521
+ q.responses[:ask_on_error] = ''
522
+ q.gather = {"Enter a password" => '', "Please type it again" => ''}
523
+ end
524
+
525
+ assert_equal("Password", answer)
526
+ assert_equal( "Enter a password: " +
527
+ "Please type it again: " +
528
+ "Typing mismatch!\n" +
529
+ "Enter a password: " +
530
+ "Please type it again: ", @output.string )
531
+ end
532
+
533
+ def test_lists
534
+ digits = %w{Zero One Two Three Four Five Six Seven Eight Nine}
535
+ erb_digits = digits.dup
536
+ erb_digits[erb_digits.index("Five")] = "<%= color('Five', :blue) %%>"
537
+
538
+ @terminal.say("<%= list(#{digits.inspect}) %>")
539
+ assert_equal(digits.map { |d| "#{d}\n" }.join, @output.string)
540
+
541
+ @output.truncate(@output.rewind)
542
+
543
+ @terminal.say("<%= list(#{digits.inspect}, :inline) %>")
544
+ assert_equal( digits[0..-2].join(", ") + " or #{digits.last}\n",
545
+ @output.string )
546
+
547
+ @output.truncate(@output.rewind)
548
+
549
+ @terminal.say("<%= list(#{digits.inspect}, :inline, ' and ') %>")
550
+ assert_equal( digits[0..-2].join(", ") + " and #{digits.last}\n",
551
+ @output.string )
552
+
553
+ @output.truncate(@output.rewind)
554
+
555
+ @terminal.say("<%= list(#{digits.inspect}, :columns_down, 3) %>")
556
+ assert_equal( "Zero Four Eight\n" +
557
+ "One Five Nine \n" +
558
+ "Two Six \n" +
559
+ "Three Seven\n",
560
+ @output.string )
561
+
562
+ @output.truncate(@output.rewind)
563
+
564
+ @terminal.say("<%= list(#{erb_digits.inspect}, :columns_down, 3) %>")
565
+ assert_equal( "Zero Four Eight\n" +
566
+ "One \e[34mFive\e[0m Nine \n" +
567
+ "Two Six \n" +
568
+ "Three Seven\n",
569
+ @output.string )
570
+
571
+ colums_of_twenty = ["12345678901234567890"] * 5
572
+
573
+ @output.truncate(@output.rewind)
574
+
575
+ @terminal.say("<%= list(#{colums_of_twenty.inspect}, :columns_down) %>")
576
+ assert_equal( "12345678901234567890 12345678901234567890 " +
577
+ "12345678901234567890\n" +
578
+ "12345678901234567890 12345678901234567890\n",
579
+ @output.string )
580
+
581
+ @output.truncate(@output.rewind)
582
+
583
+ @terminal.say("<%= list(#{digits.inspect}, :columns_across, 3) %>")
584
+ assert_equal( "Zero One Two \n" +
585
+ "Three Four Five \n" +
586
+ "Six Seven Eight\n" +
587
+ "Nine \n",
588
+ @output.string )
589
+
590
+ colums_of_twenty.pop
591
+
592
+ @output.truncate(@output.rewind)
593
+
594
+ @terminal.say("<%= list( #{colums_of_twenty.inspect}, :columns_across ) %>")
595
+ assert_equal( "12345678901234567890 12345678901234567890 " +
596
+ "12345678901234567890\n" +
597
+ "12345678901234567890\n",
598
+ @output.string )
599
+
600
+ @output.truncate(@output.rewind)
601
+
602
+ 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]
603
+
604
+ @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_across ) %>")
605
+ assert_equal( "0123456789 a b c d e f g h i j k l m n o " +
606
+ "p q r s t u v w\n" +
607
+ "x y z\n",
608
+ @output.string )
609
+
610
+ @output.truncate(@output.rewind)
611
+
612
+ @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_across, 10 ) %>")
613
+ assert_equal( "0123456789 a b c d e f g h i\n" +
614
+ "j k l m n o p q r s\n" +
615
+ "t u v w x y z\n",
616
+ @output.string )
617
+
618
+ @output.truncate(@output.rewind)
619
+
620
+ @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_down ) %>")
621
+ assert_equal( "0123456789 b d f h j l n p r t v x z\n" +
622
+ "a c e g i k m o q s u w y\n",
623
+ @output.string )
624
+
625
+ @output.truncate(@output.rewind)
626
+
627
+ @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_down, 10 ) %>")
628
+ assert_equal( "0123456789 c f i l o r u x\n" +
629
+ "a d g j m p s v y\n" +
630
+ "b e h k n q t w z\n",
631
+ @output.string )
632
+ end
633
+
634
+ def test_lists_with_zero_items
635
+ modes = [nil, :rows, :inline, :columns_across, :columns_down]
636
+ modes.each do |mode|
637
+ result = @terminal.list([], mode)
638
+ assert_equal("", result)
639
+ end
640
+ end
641
+
642
+ def test_lists_with_nil_items
643
+ modes = [nil]
644
+ modes.each do |mode|
645
+ result = @terminal.list([nil], mode)
646
+ assert_equal("\n", result)
647
+ end
648
+ end
649
+
650
+ def test_lists_with_one_item
651
+ items = ['Zero']
652
+ modes = { nil => "Zero\n",
653
+ :rows => "Zero\n",
654
+ :inline => "Zero",
655
+ :columns_across => "Zero\n",
656
+ :columns_down => "Zero\n" }
657
+
658
+ modes.each do |mode, expected|
659
+ result = @terminal.list(items, mode)
660
+ assert_equal(expected, result)
661
+ end
662
+ end
663
+
664
+ def test_lists_with_two_items
665
+ items = ['Zero', 'One']
666
+ modes = { nil => "Zero\nOne\n",
667
+ :rows => "Zero\nOne\n",
668
+ :inline => "Zero or One",
669
+ :columns_across => "Zero One \n",
670
+ :columns_down => "Zero One \n" }
671
+
672
+ modes.each do |mode, expected|
673
+ result = @terminal.list(items, mode)
674
+ assert_equal(expected, result)
675
+ end
676
+ end
677
+
678
+ def test_lists_with_three_items
679
+ items = ['Zero', 'One', 'Two']
680
+ modes = { nil => "Zero\nOne\nTwo\n",
681
+ :rows => "Zero\nOne\nTwo\n",
682
+ :inline => "Zero, One or Two",
683
+ :columns_across => "Zero One Two \n",
684
+ :columns_down => "Zero One Two \n" }
685
+
686
+ modes.each do |mode, expected|
687
+ result = @terminal.list(items, mode)
688
+ assert_equal(expected, result)
689
+ end
690
+ end
691
+
692
+ def test_mode
693
+ assert(%w[Win32API termios ncurses stty].include?(HighLine::CHARACTER_MODE))
694
+ end
695
+
696
+ class NameClass
697
+ def self.parse( string )
698
+ if string =~ /^\s*(\w+),\s*(\w+)\s+(\w+)\s*$/
699
+ self.new($2, $3, $1)
700
+ else
701
+ raise ArgumentError, "Invalid name format."
702
+ end
703
+ end
704
+
705
+ def initialize(first, middle, last)
706
+ @first, @middle, @last = first, middle, last
707
+ end
708
+
709
+ attr_reader :first, :middle, :last
710
+ end
711
+
712
+ def test_my_class_conversion
713
+ @input << "Gray, James Edward\n"
714
+ @input.rewind
715
+
716
+ answer = @terminal.ask("Your name? ", NameClass) do |q|
717
+ q.validate = lambda do |name|
718
+ names = name.split(/,\s*/)
719
+ return false unless names.size == 2
720
+ return false if names.first =~ /\s/
721
+ names.last.split.size == 2
722
+ end
723
+ end
724
+ assert_instance_of(NameClass, answer)
725
+ assert_equal("Gray", answer.last)
726
+ assert_equal("James", answer.first)
727
+ assert_equal("Edward", answer.middle)
728
+ end
729
+
730
+ def test_no_echo
731
+ @input << "password\r"
732
+ @input.rewind
733
+
734
+ answer = @terminal.ask("Please enter your password: ") do |q|
735
+ q.echo = false
736
+ end
737
+ assert_equal("password", answer)
738
+ assert_equal("Please enter your password: \n", @output.string)
739
+
740
+ @input.rewind
741
+ @output.truncate(@output.rewind)
742
+
743
+ answer = @terminal.ask("Pick a letter or number: ") do |q|
744
+ q.character = true
745
+ q.echo = false
746
+ end
747
+ assert_equal("p", answer)
748
+ assert_equal("a", @input.getc.chr)
749
+ assert_equal("Pick a letter or number: \n", @output.string)
750
+ end
751
+
752
+ def test_paging
753
+ @terminal.page_at = 22
754
+
755
+ @input << "\n\n"
756
+ @input.rewind
757
+
758
+ @terminal.say((1..50).map { |n| "This is line #{n}.\n"}.join)
759
+ assert_equal( (1..22).map { |n| "This is line #{n}.\n"}.join +
760
+ "\n-- press enter/return to continue or q to stop -- \n\n" +
761
+ (23..44).map { |n| "This is line #{n}.\n"}.join +
762
+ "\n-- press enter/return to continue or q to stop -- \n\n" +
763
+ (45..50).map { |n| "This is line #{n}.\n"}.join,
764
+ @output.string )
765
+ end
766
+
767
+ def test_range_requirements
768
+ @input << "112\n-541\n28\n"
769
+ @input.rewind
770
+
771
+ answer = @terminal.ask("Tell me your age.", Integer) do |q|
772
+ q.in = 0..105
773
+ end
774
+ assert_equal(28, answer)
775
+ assert_equal( "Tell me your age.\n" +
776
+ "Your answer isn't within the expected range " +
777
+ "(included in 0..105).\n" +
778
+ "? " +
779
+ "Your answer isn't within the expected range " +
780
+ "(included in 0..105).\n" +
781
+ "? ", @output.string )
782
+
783
+ @input.truncate(@input.rewind)
784
+ @input << "1\n-541\n28\n"
785
+ @input.rewind
786
+ @output.truncate(@output.rewind)
787
+
788
+ answer = @terminal.ask("Tell me your age.", Integer) do |q|
789
+ q.above = 3
790
+ end
791
+ assert_equal(28, answer)
792
+ assert_equal( "Tell me your age.\n" +
793
+ "Your answer isn't within the expected range " +
794
+ "(above 3).\n" +
795
+ "? " +
796
+ "Your answer isn't within the expected range " +
797
+ "(above 3).\n" +
798
+ "? ", @output.string )
799
+
800
+ @input.truncate(@input.rewind)
801
+ @input << "1\n28\n-541\n"
802
+ @input.rewind
803
+ @output.truncate(@output.rewind)
804
+
805
+ answer = @terminal.ask("Lowest numer you can think of?", Integer) do |q|
806
+ q.below = 0
807
+ end
808
+ assert_equal(-541, answer)
809
+ assert_equal( "Lowest numer you can think of?\n" +
810
+ "Your answer isn't within the expected range " +
811
+ "(below 0).\n" +
812
+ "? " +
813
+ "Your answer isn't within the expected range " +
814
+ "(below 0).\n" +
815
+ "? ", @output.string )
816
+
817
+ @input.truncate(@input.rewind)
818
+ @input << "1\n-541\n6\n"
819
+ @input.rewind
820
+ @output.truncate(@output.rewind)
821
+
822
+ answer = @terminal.ask("Enter a low even number: ", Integer) do |q|
823
+ q.above = 0
824
+ q.below = 10
825
+ q.in = [2, 4, 6, 8]
826
+ end
827
+ assert_equal(6, answer)
828
+ assert_equal( "Enter a low even number: " +
829
+ "Your answer isn't within the expected range " +
830
+ "(above 0, below 10, and included in [2, 4, 6, 8]).\n" +
831
+ "? " +
832
+ "Your answer isn't within the expected range " +
833
+ "(above 0, below 10, and included in [2, 4, 6, 8]).\n" +
834
+ "? ", @output.string )
835
+ end
836
+
837
+ def test_reask
838
+ number = 61676
839
+ @input << "Junk!\n" << number << "\n"
840
+ @input.rewind
841
+
842
+ answer = @terminal.ask("Favorite number? ", Integer)
843
+ assert_kind_of(Integer, number)
844
+ assert_instance_of(Fixnum, number)
845
+ assert_equal(number, answer)
846
+ assert_equal( "Favorite number? " +
847
+ "You must enter a valid Integer.\n" +
848
+ "? ", @output.string )
849
+
850
+ @input.rewind
851
+ @output.truncate(@output.rewind)
852
+
853
+ answer = @terminal.ask("Favorite number? ", Integer) do |q|
854
+ q.responses[:ask_on_error] = :question
855
+ q.responses[:invalid_type] = "Not a valid number!"
856
+ end
857
+ assert_kind_of(Integer, number)
858
+ assert_instance_of(Fixnum, number)
859
+ assert_equal(number, answer)
860
+ assert_equal( "Favorite number? " +
861
+ "Not a valid number!\n" +
862
+ "Favorite number? ", @output.string )
863
+
864
+ @input.truncate(@input.rewind)
865
+ @input << "gen\ngene\n"
866
+ @input.rewind
867
+ @output.truncate(@output.rewind)
868
+
869
+ answer = @terminal.ask("Select a mode: ", [:generate, :gentle])
870
+ assert_instance_of(Symbol, answer)
871
+ assert_equal(:generate, answer)
872
+ assert_equal( "Select a mode: " +
873
+ "Ambiguous choice. " +
874
+ "Please choose one of [:generate, :gentle].\n" +
875
+ "? ", @output.string )
876
+ end
877
+
878
+ def test_response_embedding
879
+ @input << "112\n-541\n28\n"
880
+ @input.rewind
881
+
882
+ answer = @terminal.ask("Tell me your age.", Integer) do |q|
883
+ q.in = 0..105
884
+ q.responses[:not_in_range] = "Need a <%= @question.answer_type %>" +
885
+ " <%= @question.expected_range %>."
886
+ end
887
+ assert_equal(28, answer)
888
+ assert_equal( "Tell me your age.\n" +
889
+ "Need a Integer included in 0..105.\n" +
890
+ "? " +
891
+ "Need a Integer included in 0..105.\n" +
892
+ "? ", @output.string )
893
+ end
894
+
895
+ def test_say
896
+ @terminal.say("This will have a newline.")
897
+ assert_equal("This will have a newline.\n", @output.string)
898
+
899
+ @output.truncate(@output.rewind)
900
+
901
+ @terminal.say("This will also have one newline.\n")
902
+ assert_equal("This will also have one newline.\n", @output.string)
903
+
904
+ @output.truncate(@output.rewind)
905
+
906
+ @terminal.say("This will not have a newline. ")
907
+ assert_equal("This will not have a newline. ", @output.string)
908
+
909
+ @output.truncate(@output.rewind)
910
+
911
+ @terminal.say("This will not\n end with a newline. ")
912
+ assert_equal("This will not\n end with a newline. ", @output.string)
913
+
914
+ @output.truncate(@output.rewind)
915
+
916
+ @terminal.say("This will \nend with a newline.")
917
+ assert_equal("This will \nend with a newline.\n", @output.string)
918
+
919
+ @output.truncate(@output.rewind)
920
+
921
+ colorized = @terminal.color("This will not have a newline. ", :green)
922
+ @terminal.say(colorized)
923
+ assert_equal("\e[32mThis will not have a newline. \e[0m", @output.string)
924
+
925
+ @output.truncate(@output.rewind)
926
+
927
+ colorized = @terminal.color("This will have a newline.", :green)
928
+ @terminal.say(colorized)
929
+ assert_equal("\e[32mThis will have a newline.\e[0m\n", @output.string)
930
+ end
931
+
932
+ def test_terminal_size
933
+ assert_instance_of(Fixnum, @terminal.terminal_size[0])
934
+ assert_instance_of(Fixnum, @terminal.terminal_size[1])
935
+ end
936
+
937
+ def test_type_conversion
938
+ number = 61676
939
+ @input << number << "\n"
940
+ @input.rewind
941
+
942
+ answer = @terminal.ask("Favorite number? ", Integer)
943
+ assert_kind_of(Integer, answer)
944
+ assert_instance_of(Fixnum, answer)
945
+ assert_equal(number, answer)
946
+
947
+ @input.truncate(@input.rewind)
948
+ number = 1_000_000_000_000_000_000_000_000_000_000
949
+ @input << number << "\n"
950
+ @input.rewind
951
+
952
+ answer = @terminal.ask("Favorite number? ", Integer)
953
+ assert_kind_of(Integer, answer)
954
+ assert_instance_of(Bignum, answer)
955
+ assert_equal(number, answer)
956
+
957
+ @input.truncate(@input.rewind)
958
+ number = 10.5002
959
+ @input << number << "\n"
960
+ @input.rewind
961
+
962
+ answer = @terminal.ask( "Favorite number? ",
963
+ lambda { |n| n.to_f.abs.round } )
964
+ assert_kind_of(Integer, answer)
965
+ assert_instance_of(Fixnum, answer)
966
+ assert_equal(11, answer)
967
+
968
+ @input.truncate(@input.rewind)
969
+ animal = :dog
970
+ @input << animal << "\n"
971
+ @input.rewind
972
+
973
+ answer = @terminal.ask("Favorite animal? ", Symbol)
974
+ assert_instance_of(Symbol, answer)
975
+ assert_equal(animal, answer)
976
+
977
+ @input.truncate(@input.rewind)
978
+ @input << "16th June 1976\n"
979
+ @input.rewind
980
+
981
+ answer = @terminal.ask("Enter your birthday.", Date)
982
+ assert_instance_of(Date, answer)
983
+ assert_equal(16, answer.day)
984
+ assert_equal(6, answer.month)
985
+ assert_equal(1976, answer.year)
986
+
987
+ @input.truncate(@input.rewind)
988
+ pattern = "^yes|no$"
989
+ @input << pattern << "\n"
990
+ @input.rewind
991
+
992
+ answer = @terminal.ask("Give me a pattern to match with: ", Regexp)
993
+ assert_instance_of(Regexp, answer)
994
+ assert_equal(/#{pattern}/, answer)
995
+
996
+ @input.truncate(@input.rewind)
997
+ @input << "gen\n"
998
+ @input.rewind
999
+
1000
+ answer = @terminal.ask("Select a mode: ", [:generate, :run])
1001
+ assert_instance_of(Symbol, answer)
1002
+ assert_equal(:generate, answer)
1003
+ end
1004
+
1005
+ def test_validation
1006
+ @input << "system 'rm -rf /'\n105\n0b101_001\n"
1007
+ @input.rewind
1008
+
1009
+ answer = @terminal.ask("Enter a binary number: ") do |q|
1010
+ q.validate = /\A(?:0b)?[01_]+\Z/
1011
+ end
1012
+ assert_equal("0b101_001", answer)
1013
+ assert_equal( "Enter a binary number: " +
1014
+ "Your answer isn't valid " +
1015
+ "(must match /\\A(?:0b)?[01_]+\\Z/).\n" +
1016
+ "? " +
1017
+ "Your answer isn't valid " +
1018
+ "(must match /\\A(?:0b)?[01_]+\\Z/).\n" +
1019
+ "? ", @output.string )
1020
+
1021
+ @input.truncate(@input.rewind)
1022
+ @input << "Gray II, James Edward\n" +
1023
+ "Gray, Dana Ann Leslie\n" +
1024
+ "Gray, James Edward\n"
1025
+ @input.rewind
1026
+
1027
+ answer = @terminal.ask("Your name? ") do |q|
1028
+ q.validate = lambda do |name|
1029
+ names = name.split(/,\s*/)
1030
+ return false unless names.size == 2
1031
+ return false if names.first =~ /\s/
1032
+ names.last.split.size == 2
1033
+ end
1034
+ end
1035
+ assert_equal("Gray, James Edward", answer)
1036
+ end
1037
+
1038
+ def test_whitespace
1039
+ @input << " A lot\tof \t space\t \there! \n"
1040
+ @input.rewind
1041
+
1042
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1043
+ q.whitespace = :chomp
1044
+ end
1045
+ assert_equal(" A lot\tof \t space\t \there! ", answer)
1046
+
1047
+ @input.rewind
1048
+
1049
+ answer = @terminal.ask("Enter a whitespace filled string: ")
1050
+ assert_equal("A lot\tof \t space\t \there!", answer)
1051
+
1052
+ @input.rewind
1053
+
1054
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1055
+ q.whitespace = :strip_and_collapse
1056
+ end
1057
+ assert_equal("A lot of space here!", answer)
1058
+
1059
+ @input.rewind
1060
+
1061
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1062
+ q.whitespace = :remove
1063
+ end
1064
+ assert_equal("Alotofspacehere!", answer)
1065
+
1066
+ @input.rewind
1067
+
1068
+ answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1069
+ q.whitespace = :none
1070
+ end
1071
+ assert_equal(" A lot\tof \t space\t \there! \n", answer)
1072
+ end
1073
+
1074
+ def test_wrap
1075
+ @terminal.wrap_at = 80
1076
+
1077
+ @terminal.say("This is a very short line.")
1078
+ assert_equal("This is a very short line.\n", @output.string)
1079
+
1080
+ @output.truncate(@output.rewind)
1081
+
1082
+ @terminal.say( "This is a long flowing paragraph meant to span " +
1083
+ "several lines. This text should definitely be " +
1084
+ "wrapped at the set limit, in the result. Your code " +
1085
+ "does well with things like this.\n\n" +
1086
+ " * This is a simple embedded list.\n" +
1087
+ " * You're code should not mess with this...\n" +
1088
+ " * Because it's already formatted correctly and " +
1089
+ "does not\n" +
1090
+ " exceed the limit!" )
1091
+ assert_equal( "This is a long flowing paragraph meant to span " +
1092
+ "several lines. This text should\n" +
1093
+ "definitely be wrapped at the set limit, in the " +
1094
+ "result. Your code does well with\n" +
1095
+ "things like this.\n\n" +
1096
+ " * This is a simple embedded list.\n" +
1097
+ " * You're code should not mess with this...\n" +
1098
+ " * Because it's already formatted correctly and does " +
1099
+ "not\n" +
1100
+ " exceed the limit!\n", @output.string )
1101
+
1102
+ @output.truncate(@output.rewind)
1103
+
1104
+ @terminal.say("-=" * 50)
1105
+ assert_equal(("-=" * 40 + "\n") + ("-=" * 10 + "\n"), @output.string)
1106
+ end
1107
+
1108
+ def test_track_eof
1109
+ assert_raise(EOFError) { @terminal.ask("Any input left? ") }
1110
+
1111
+ # turn EOF tracking
1112
+ old_setting = HighLine.track_eof?
1113
+ assert_nothing_raised(Exception) { HighLine.track_eof = false }
1114
+ begin
1115
+ @terminal.ask("And now? ") # this will still blow up, nothing available
1116
+ rescue
1117
+ assert_not_equal(EOFError, $!.class) # but HighLine's safe guards are off
1118
+ end
1119
+ HighLine.track_eof = old_setting
1120
+ end
1121
+
1122
+ def test_version
1123
+ assert_not_nil(HighLine::VERSION)
1124
+ assert_instance_of(String, HighLine::VERSION)
1125
+ assert(HighLine::VERSION.frozen?)
1126
+ assert_match(/\A\d+\.\d+\.\d+\Z/, HighLine::VERSION)
1127
+ end
1128
+ end