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