highline 1.7.10 → 2.0.0.pre.develop.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.simplecov +5 -0
- data/.travis.yml +11 -6
- data/Changelog.md +112 -20
- data/Gemfile +8 -7
- data/README.rdoc +3 -0
- data/Rakefile +7 -2
- data/appveyor.yml +19 -0
- data/examples/asking_for_arrays.rb +3 -0
- data/examples/basic_usage.rb +3 -0
- data/examples/get_character.rb +3 -0
- data/examples/limit.rb +3 -0
- data/examples/menus.rb +3 -0
- data/examples/overwrite.rb +3 -0
- data/examples/password.rb +3 -0
- data/examples/repeat_entry.rb +4 -1
- data/lib/highline.rb +182 -704
- data/lib/highline/builtin_styles.rb +109 -0
- data/lib/highline/color_scheme.rb +4 -1
- data/lib/highline/compatibility.rb +2 -0
- data/lib/highline/custom_errors.rb +19 -0
- data/lib/highline/import.rb +4 -2
- data/lib/highline/list.rb +93 -0
- data/lib/highline/list_renderer.rb +232 -0
- data/lib/highline/menu.rb +20 -20
- data/lib/highline/paginator.rb +43 -0
- data/lib/highline/question.rb +157 -97
- data/lib/highline/question/answer_converter.rb +84 -0
- data/lib/highline/question_asker.rb +147 -0
- data/lib/highline/simulate.rb +5 -1
- data/lib/highline/statement.rb +58 -0
- data/lib/highline/string.rb +34 -0
- data/lib/highline/string_extensions.rb +3 -28
- data/lib/highline/style.rb +18 -8
- data/lib/highline/template_renderer.rb +38 -0
- data/lib/highline/terminal.rb +78 -0
- data/lib/highline/terminal/io_console.rb +98 -0
- data/lib/highline/terminal/ncurses.rb +38 -0
- data/lib/highline/terminal/unix_stty.rb +94 -0
- data/lib/highline/version.rb +3 -1
- data/lib/highline/wrapper.rb +43 -0
- data/test/acceptance/acceptance.rb +62 -0
- data/test/acceptance/acceptance_test.rb +69 -0
- data/test/acceptance/at_color_output_using_erb_templates.rb +17 -0
- data/test/acceptance/at_echo_false.rb +23 -0
- data/test/acceptance/at_readline.rb +37 -0
- data/test/io_console_compatible.rb +37 -0
- data/test/string_methods.rb +3 -0
- data/test/test_answer_converter.rb +26 -0
- data/test/{tc_color_scheme.rb → test_color_scheme.rb} +7 -9
- data/test/test_helper.rb +26 -0
- data/test/{tc_highline.rb → test_highline.rb} +193 -136
- data/test/{tc_import.rb → test_import.rb} +5 -2
- data/test/test_list.rb +60 -0
- data/test/{tc_menu.rb → test_menu.rb} +6 -3
- data/test/test_paginator.rb +73 -0
- data/test/test_question_asker.rb +20 -0
- data/test/test_simulator.rb +24 -0
- data/test/test_string_extension.rb +72 -0
- data/test/{tc_string_highline.rb → test_string_highline.rb} +7 -3
- data/test/{tc_style.rb → test_style.rb} +70 -35
- data/test/test_wrapper.rb +188 -0
- metadata +57 -22
- data/lib/highline/system_extensions.rb +0 -254
- data/test/tc_simulator.rb +0 -33
- data/test/tc_string_extension.rb +0 -33
@@ -0,0 +1,69 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'highline/import'
|
4
|
+
|
5
|
+
class HighLine::AcceptanceTest
|
6
|
+
@@answers ||= {}
|
7
|
+
|
8
|
+
def self.check(&block)
|
9
|
+
caller_file = File.basename(caller[0].split(":")[-3])
|
10
|
+
|
11
|
+
test = new
|
12
|
+
yield test
|
13
|
+
test.caller_file = caller_file
|
14
|
+
test.check
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.answers
|
18
|
+
@@answers
|
19
|
+
end
|
20
|
+
|
21
|
+
def self.answers_for_report
|
22
|
+
answers.map do |file, answer|
|
23
|
+
"#{file}: #{answer}"
|
24
|
+
end.join("\n")
|
25
|
+
end
|
26
|
+
|
27
|
+
# A test description to be shown to user.
|
28
|
+
# It should express what the user is
|
29
|
+
# expected to check.
|
30
|
+
attr_accessor :desc
|
31
|
+
|
32
|
+
# A test action to be checked by the user
|
33
|
+
attr_accessor :action
|
34
|
+
|
35
|
+
# A text asking the confirmation if
|
36
|
+
# the action worked (y) or not (n).
|
37
|
+
attr_accessor :question
|
38
|
+
|
39
|
+
# Automatically filled attribute pointing
|
40
|
+
# to the file where the current test
|
41
|
+
# source is located. So we could check
|
42
|
+
# at the report what tests passed or failed.
|
43
|
+
attr_accessor :caller_file
|
44
|
+
|
45
|
+
def check
|
46
|
+
# Print a header with the test description
|
47
|
+
puts "====="
|
48
|
+
puts " #{caller_file}"
|
49
|
+
puts "====="
|
50
|
+
puts
|
51
|
+
puts desc
|
52
|
+
|
53
|
+
# Execute the proc/lambda assigned to action
|
54
|
+
puts "---"
|
55
|
+
puts
|
56
|
+
action.call
|
57
|
+
puts
|
58
|
+
puts "---"
|
59
|
+
puts
|
60
|
+
|
61
|
+
# Gather the user feedback about the test
|
62
|
+
print question
|
63
|
+
answer = STDIN.gets.chomp
|
64
|
+
answer = "y" if answer.empty?
|
65
|
+
@@answers[caller_file] = answer
|
66
|
+
|
67
|
+
puts
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require_relative 'acceptance_test'
|
4
|
+
|
5
|
+
HighLine::AcceptanceTest.check do |t|
|
6
|
+
t.desc =
|
7
|
+
"This step checks if coloring " \
|
8
|
+
"with erb templates is working ok.\n" \
|
9
|
+
"You should see the word _grass_ " \
|
10
|
+
"colored in green color"
|
11
|
+
|
12
|
+
t.action = Proc.new do
|
13
|
+
say "The <%= color('grass', :green) %> should be green!"
|
14
|
+
end
|
15
|
+
|
16
|
+
t.question = "Do you see the word 'grass' on green color (y/n)? "
|
17
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require_relative 'acceptance_test'
|
4
|
+
|
5
|
+
HighLine::AcceptanceTest.check do |t|
|
6
|
+
t.desc =
|
7
|
+
"This step checks if the 'echo = false' " \
|
8
|
+
"setting is effective in hiding the user " \
|
9
|
+
"typed characters.\n" \
|
10
|
+
"This functionality is useful when asking " \
|
11
|
+
"for passwords.\n" \
|
12
|
+
"When typing the characters you should not " \
|
13
|
+
"see any of them on the screen."
|
14
|
+
|
15
|
+
t.action = Proc.new do
|
16
|
+
answer = ask "Enter some characters and press <enter>: " do |q|
|
17
|
+
q.echo = false
|
18
|
+
end
|
19
|
+
puts "You've entered -> #{answer} <-"
|
20
|
+
end
|
21
|
+
|
22
|
+
t.question = "Were the characters adequately hidden when you typed them (y/n)? "
|
23
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require_relative 'acceptance_test'
|
4
|
+
|
5
|
+
HighLine::AcceptanceTest.check do |t|
|
6
|
+
t.desc =
|
7
|
+
"This step checks if the readline autocomplete " \
|
8
|
+
"feature is working. \n" \
|
9
|
+
"The test has 5 options you can choose from: " \
|
10
|
+
"save, sample, exec, exit and load.\n" \
|
11
|
+
"If you type the first character of one of them and then press \n" \
|
12
|
+
"the <TAB> key you should see the options available for autocomplete.\n\n" \
|
13
|
+
"For example, if I type 's' and then I press <TAB> I should see a list\n" \
|
14
|
+
"with 'save' and 'sample' as possible options for autocomplete.\n\n" \
|
15
|
+
"Although, if I type 'l' and then press the <TAB> key it should be \n" \
|
16
|
+
"readly autcompleted as 'load', because 'load' is the only option\n" \
|
17
|
+
"that begins with the 'l' letter in this particular case.\n\n" \
|
18
|
+
"If I don't type any character but press <TAB> two times, I should\n" \
|
19
|
+
"be able to see ALL available options.\n\n" \
|
20
|
+
"Please, play with Readline autocomplete for a while, pressing <ENTER>\n" \
|
21
|
+
"to see that it really gets the selected answer.\n" \
|
22
|
+
"When ready, just type 'exit' and the loop will finish.\n\n" \
|
23
|
+
"Don't forget to answer 'y' (yes) or 'n' (no) to the question at the end."
|
24
|
+
|
25
|
+
t.action = Proc.new do
|
26
|
+
loop do
|
27
|
+
cmd =
|
28
|
+
ask "Enter command: ", %w{ save sample exec exit load } do |q|
|
29
|
+
q.readline = true
|
30
|
+
end
|
31
|
+
say("Executing \"#{cmd}\"...")
|
32
|
+
break if cmd == "exit"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
t.question = "Did the Readline autocomplete work fine (y/n)? "
|
37
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
require 'stringio'
|
4
|
+
require 'tempfile'
|
5
|
+
|
6
|
+
#
|
7
|
+
# On tests, we try to simulate input output with
|
8
|
+
# StringIO, Tempfile and File objects.
|
9
|
+
#
|
10
|
+
# For this to be accomplished, we have to do some
|
11
|
+
# tweaking so that they respond adequately to the
|
12
|
+
# called methods during tests.
|
13
|
+
#
|
14
|
+
|
15
|
+
module IOConsoleCompatible
|
16
|
+
def getch
|
17
|
+
getc
|
18
|
+
end
|
19
|
+
|
20
|
+
attr_accessor :echo
|
21
|
+
|
22
|
+
def winsize
|
23
|
+
[24, 80]
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class Tempfile
|
28
|
+
include IOConsoleCompatible
|
29
|
+
end
|
30
|
+
|
31
|
+
class File
|
32
|
+
include IOConsoleCompatible
|
33
|
+
end
|
34
|
+
|
35
|
+
class StringIO
|
36
|
+
include IOConsoleCompatible
|
37
|
+
end
|
data/test/string_methods.rb
CHANGED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
require "test_helper"
|
5
|
+
|
6
|
+
require "highline/question"
|
7
|
+
|
8
|
+
class TestAnswerConverter < Minitest::Test
|
9
|
+
def test_integer_convertion
|
10
|
+
question = HighLine::Question.new("What's your age?", Integer)
|
11
|
+
question.answer = "18"
|
12
|
+
answer_converter = HighLine::Question::AnswerConverter.new(question)
|
13
|
+
|
14
|
+
refute_equal "18", answer_converter.convert
|
15
|
+
assert_equal 18, answer_converter.convert
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_float_convertion
|
19
|
+
question = HighLine::Question.new("Write PI", Float)
|
20
|
+
question.answer = "3.14159"
|
21
|
+
answer_converter = HighLine::Question::AnswerConverter.new(question)
|
22
|
+
|
23
|
+
refute_equal "3.14159", answer_converter.convert
|
24
|
+
assert_equal 3.14159, answer_converter.convert
|
25
|
+
end
|
26
|
+
end
|
@@ -1,3 +1,6 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
1
4
|
# tc_color_scheme.rb
|
2
5
|
#
|
3
6
|
# Created by Jeremy Hinegardner on 2007-01-24.
|
@@ -5,22 +8,17 @@
|
|
5
8
|
#
|
6
9
|
# This is Free Software. See LICENSE and COPYING for details.
|
7
10
|
|
8
|
-
require "
|
11
|
+
require "test_helper"
|
9
12
|
|
10
13
|
require "highline"
|
11
14
|
require "stringio"
|
12
15
|
|
13
|
-
class TestColorScheme < Test
|
16
|
+
class TestColorScheme < Minitest::Test
|
14
17
|
def setup
|
18
|
+
HighLine.reset
|
15
19
|
@input = StringIO.new
|
16
20
|
@output = StringIO.new
|
17
21
|
@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
22
|
end
|
25
23
|
|
26
24
|
def test_using_color_scheme
|
@@ -88,7 +86,7 @@ class TestColorScheme < Test::Unit::TestCase
|
|
88
86
|
assert_equal [:bold, :yellow], HighLine.color_scheme.definition(:warning)
|
89
87
|
|
90
88
|
# turn it back off, should raise an exception
|
91
|
-
HighLine.color_scheme =
|
89
|
+
HighLine.color_scheme = nil
|
92
90
|
assert_raises(NameError) {
|
93
91
|
@terminal.say("This should be <%= color('nothing at all', :error) %>.")
|
94
92
|
}
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
4
|
+
require 'simplecov'
|
5
|
+
|
6
|
+
if ENV['CODECLIMATE_REPO_TOKEN']
|
7
|
+
require "codeclimate-test-reporter"
|
8
|
+
CodeClimate::TestReporter.start
|
9
|
+
end
|
10
|
+
|
11
|
+
# Compatibility module for StringIO, File
|
12
|
+
# and Tempfile. Necessary for some tests.
|
13
|
+
require "io_console_compatible"
|
14
|
+
|
15
|
+
require 'highline'
|
16
|
+
debug_message = "Tests will be run under:\n"
|
17
|
+
debug_message << " - #{HighLine.new.terminal.class}\n"
|
18
|
+
debug_message << " - HighLine::VERSION #{HighLine::VERSION}\n"
|
19
|
+
|
20
|
+
if defined? RUBY_DESCRIPTION
|
21
|
+
debug_message << " - #{RUBY_DESCRIPTION}\n"
|
22
|
+
end
|
23
|
+
|
24
|
+
puts debug_message
|
25
|
+
|
26
|
+
require "minitest/autorun"
|
@@ -1,4 +1,6 @@
|
|
1
|
-
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# coding: utf-8
|
3
|
+
|
2
4
|
# tc_highline.rb
|
3
5
|
#
|
4
6
|
# Created by James Edward Gray II on 2005-04-26.
|
@@ -6,13 +8,14 @@
|
|
6
8
|
#
|
7
9
|
# This is Free Software. See LICENSE and COPYING for details.
|
8
10
|
|
9
|
-
require "
|
11
|
+
require "test_helper"
|
10
12
|
|
11
13
|
require "highline"
|
12
14
|
require "stringio"
|
13
15
|
require "readline"
|
14
16
|
require "tempfile"
|
15
17
|
|
18
|
+
=begin
|
16
19
|
if HighLine::CHARACTER_MODE == "Win32API"
|
17
20
|
class HighLine
|
18
21
|
# Override Windows' character reading so it's not tied to STDIN.
|
@@ -21,9 +24,11 @@ if HighLine::CHARACTER_MODE == "Win32API"
|
|
21
24
|
end
|
22
25
|
end
|
23
26
|
end
|
27
|
+
=end
|
24
28
|
|
25
|
-
class TestHighLine < Test
|
29
|
+
class TestHighLine < Minitest::Test
|
26
30
|
def setup
|
31
|
+
HighLine.reset
|
27
32
|
@input = StringIO.new
|
28
33
|
@output = StringIO.new
|
29
34
|
@terminal = HighLine.new(@input, @output)
|
@@ -60,7 +65,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
60
65
|
|
61
66
|
assert_equal(name, @terminal.ask("What is your name? "))
|
62
67
|
|
63
|
-
|
68
|
+
assert_raises(EOFError) { @terminal.ask("Any input left? ") }
|
64
69
|
end
|
65
70
|
|
66
71
|
def test_ask_string
|
@@ -70,7 +75,25 @@ class TestHighLine < Test::Unit::TestCase
|
|
70
75
|
|
71
76
|
assert_equal(name, @terminal.ask("What is your name? ", String))
|
72
77
|
|
73
|
-
|
78
|
+
assert_raises(EOFError) { @terminal.ask("Any input left? ", String) }
|
79
|
+
end
|
80
|
+
|
81
|
+
def test_ask_string_converting
|
82
|
+
name = "James Edward Gray II"
|
83
|
+
@input << name << "\n"
|
84
|
+
@input.rewind
|
85
|
+
|
86
|
+
answer = @terminal.ask("What is your name? ", String)
|
87
|
+
|
88
|
+
assert_instance_of HighLine::String, answer
|
89
|
+
|
90
|
+
@input.rewind
|
91
|
+
|
92
|
+
answer = @terminal.ask("What is your name? ", HighLine::String)
|
93
|
+
|
94
|
+
assert_instance_of HighLine::String, answer
|
95
|
+
|
96
|
+
assert_raises(EOFError) { @terminal.ask("Any input left? ", HighLine::String) }
|
74
97
|
end
|
75
98
|
|
76
99
|
def test_indent
|
@@ -91,28 +114,29 @@ class TestHighLine < Test::Unit::TestCase
|
|
91
114
|
assert_equal(' '*10+text, @output.string)
|
92
115
|
|
93
116
|
@output.truncate(@output.rewind)
|
117
|
+
@terminal.indent_level=0
|
94
118
|
@terminal.indent_size=4
|
95
119
|
@terminal.indent {
|
96
|
-
|
120
|
+
@terminal.say(text)
|
97
121
|
}
|
98
122
|
assert_equal(' '*4+text, @output.string)
|
99
123
|
|
100
124
|
@output.truncate(@output.rewind)
|
101
125
|
@terminal.indent_size=2
|
102
126
|
@terminal.indent(3) { |t|
|
103
|
-
|
127
|
+
t.say(text)
|
104
128
|
}
|
105
129
|
assert_equal(' '*6+text, @output.string)
|
106
130
|
|
107
131
|
@output.truncate(@output.rewind)
|
108
132
|
@terminal.indent { |t|
|
133
|
+
t.indent {
|
109
134
|
t.indent {
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
}
|
114
|
-
}
|
135
|
+
t.indent { |tt|
|
136
|
+
tt.say(text)
|
137
|
+
}
|
115
138
|
}
|
139
|
+
}
|
116
140
|
}
|
117
141
|
assert_equal(' '*8+text, @output.string)
|
118
142
|
|
@@ -194,6 +218,35 @@ class TestHighLine < Test::Unit::TestCase
|
|
194
218
|
assert_equal("crazy", answer)
|
195
219
|
end
|
196
220
|
|
221
|
+
def test_ask_with_overwrite
|
222
|
+
@input << "Yes, sure!\n"
|
223
|
+
@input.rewind
|
224
|
+
|
225
|
+
answer = @terminal.ask("Do you like Ruby? ") do |q|
|
226
|
+
q.overwrite = true
|
227
|
+
q.echo = false
|
228
|
+
end
|
229
|
+
|
230
|
+
assert_equal("Yes, sure!", answer)
|
231
|
+
erase_sequence = "\r#{HighLine.Style(:erase_line).code}"
|
232
|
+
assert_equal("Do you like Ruby? #{erase_sequence}", @output.string)
|
233
|
+
end
|
234
|
+
|
235
|
+
def test_ask_with_overwrite_and_character_mode
|
236
|
+
@input << "Y"
|
237
|
+
@input.rewind
|
238
|
+
|
239
|
+
answer = @terminal.ask("Do you like Ruby (Y/N)? ") do |q|
|
240
|
+
q.overwrite = true
|
241
|
+
q.echo = false
|
242
|
+
q.character = true
|
243
|
+
end
|
244
|
+
|
245
|
+
assert_equal("Y", answer)
|
246
|
+
erase_sequence = "\r#{HighLine.Style(:erase_line).code}"
|
247
|
+
assert_equal("Do you like Ruby (Y/N)? #{erase_sequence}", @output.string)
|
248
|
+
end
|
249
|
+
|
197
250
|
def test_character_echo
|
198
251
|
@input << "password\r"
|
199
252
|
@input.rewind
|
@@ -249,12 +302,20 @@ class TestHighLine < Test::Unit::TestCase
|
|
249
302
|
end
|
250
303
|
|
251
304
|
def test_readline_mode
|
252
|
-
#
|
253
|
-
# and
|
254
|
-
#
|
255
|
-
#
|
256
|
-
|
257
|
-
|
305
|
+
#
|
306
|
+
# Rubinius (and JRuby) seems to be ignoring
|
307
|
+
# Readline input and output assignments. This
|
308
|
+
# ruins testing.
|
309
|
+
#
|
310
|
+
# But it doesn't mean readline is not working
|
311
|
+
# properly on rubinius or jruby.
|
312
|
+
#
|
313
|
+
|
314
|
+
terminal = @terminal.terminal
|
315
|
+
|
316
|
+
if terminal.jruby? or terminal.rubinius? or terminal.windows?
|
317
|
+
skip "We can't test Readline on JRuby, Rubinius and Windows yet"
|
318
|
+
end
|
258
319
|
|
259
320
|
# Creating Tempfiles here because Readline.input
|
260
321
|
# and Readline.output only accepts a File object
|
@@ -381,6 +442,14 @@ class TestHighLine < Test::Unit::TestCase
|
|
381
442
|
@terminal.say("This should be <%= ON_RGB_C06030 %>on_rgb_c06030<%= CLEAR %>!")
|
382
443
|
assert_equal("This should be \e[48;5;173mon_rgb_c06030\e[0m!\n", @output.string)
|
383
444
|
|
445
|
+
# Relying on const_missing
|
446
|
+
assert_instance_of HighLine::Style, HighLine::ON_RGB_C06031_STYLE
|
447
|
+
assert_instance_of String , HighLine::ON_RGB_C06032
|
448
|
+
assert_raises(NameError) { HighLine::ON_RGB_ZZZZZZ }
|
449
|
+
|
450
|
+
# Retrieving color_code from a style
|
451
|
+
assert_equal "\e[41m", @terminal.color_code([HighLine::ON_RED_STYLE])
|
452
|
+
|
384
453
|
@output.truncate(@output.rewind)
|
385
454
|
|
386
455
|
# Does class method work, too?
|
@@ -392,7 +461,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
392
461
|
|
393
462
|
# turn off color
|
394
463
|
old_setting = HighLine.use_color?
|
395
|
-
|
464
|
+
HighLine.use_color = false
|
396
465
|
@terminal.say("This should be <%= color('cyan', CYAN) %>!")
|
397
466
|
assert_equal("This should be cyan!\n", @output.string)
|
398
467
|
HighLine.use_color = old_setting
|
@@ -448,7 +517,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
448
517
|
@input.rewind
|
449
518
|
|
450
519
|
answer = @terminal.ask("Enter a filename: ") do |q|
|
451
|
-
q.confirm = "Are you sure you want to overwrite <%=
|
520
|
+
q.confirm = "Are you sure you want to overwrite <%= answer %>? "
|
452
521
|
q.responses[:ask_on_error] = :question
|
453
522
|
end
|
454
523
|
assert_equal("save.txt", answer)
|
@@ -464,7 +533,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
464
533
|
@output.truncate(@output.rewind)
|
465
534
|
|
466
535
|
answer = @terminal.ask("Enter a filename: ") do |q|
|
467
|
-
q.confirm = "Are you sure you want to overwrite <%=
|
536
|
+
q.confirm = "Are you sure you want to overwrite <%= answer %>? "
|
468
537
|
end
|
469
538
|
assert_equal("junk.txt", answer)
|
470
539
|
assert_equal( "Enter a filename: " +
|
@@ -472,6 +541,35 @@ class TestHighLine < Test::Unit::TestCase
|
|
472
541
|
@output.string )
|
473
542
|
end
|
474
543
|
|
544
|
+
def test_generic_confirm_with_true
|
545
|
+
@input << "junk.txt\nno\nsave.txt\ny\n"
|
546
|
+
@input.rewind
|
547
|
+
|
548
|
+
answer = @terminal.ask("Enter a filename: ") do |q|
|
549
|
+
q.confirm = true
|
550
|
+
q.responses[:ask_on_error] = :question
|
551
|
+
end
|
552
|
+
assert_equal("save.txt", answer)
|
553
|
+
assert_equal( "Enter a filename: " +
|
554
|
+
"Are you sure? " +
|
555
|
+
"Enter a filename: " +
|
556
|
+
"Are you sure? ",
|
557
|
+
@output.string )
|
558
|
+
|
559
|
+
@input.truncate(@input.rewind)
|
560
|
+
@input << "junk.txt\nyes\nsave.txt\nn\n"
|
561
|
+
@input.rewind
|
562
|
+
@output.truncate(@output.rewind)
|
563
|
+
|
564
|
+
answer = @terminal.ask("Enter a filename: ") do |q|
|
565
|
+
q.confirm = true
|
566
|
+
end
|
567
|
+
assert_equal("junk.txt", answer)
|
568
|
+
assert_equal( "Enter a filename: " +
|
569
|
+
"Are you sure? ",
|
570
|
+
@output.string )
|
571
|
+
end
|
572
|
+
|
475
573
|
def test_defaults
|
476
574
|
@input << "\nNo Comment\n"
|
477
575
|
@input.rewind
|
@@ -576,10 +674,10 @@ class TestHighLine < Test::Unit::TestCase
|
|
576
674
|
end
|
577
675
|
|
578
676
|
def test_files
|
579
|
-
@input << "#{File.basename(__FILE__)[0,
|
677
|
+
@input << "#{File.basename(__FILE__)[0, 7]}\n"
|
580
678
|
@input.rewind
|
581
679
|
|
582
|
-
assert_equal "
|
680
|
+
assert_equal "test_hi\n",@input.read
|
583
681
|
@input.rewind
|
584
682
|
|
585
683
|
file = @terminal.ask("Select a file: ", File) do |q|
|
@@ -587,7 +685,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
587
685
|
q.glob = "*.rb"
|
588
686
|
end
|
589
687
|
assert_instance_of(File, file)
|
590
|
-
assert_equal("
|
688
|
+
assert_equal("#!/usr/bin/env ruby\n", file.gets)
|
591
689
|
file.close
|
592
690
|
|
593
691
|
@input.rewind
|
@@ -600,7 +698,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
600
698
|
assert_equal(File.size(__FILE__), pathname.size)
|
601
699
|
end
|
602
700
|
|
603
|
-
def
|
701
|
+
def test_gather_with_integer
|
604
702
|
@input << "James\nDana\nStorm\nGypsy\n\n"
|
605
703
|
@input.rewind
|
606
704
|
|
@@ -610,27 +708,33 @@ class TestHighLine < Test::Unit::TestCase
|
|
610
708
|
assert_equal(%w{James Dana Storm Gypsy}, answers)
|
611
709
|
assert_equal("\n", @input.gets)
|
612
710
|
assert_equal("Enter four names:\n", @output.string)
|
711
|
+
end
|
613
712
|
|
713
|
+
def test_gather_with_an_empty_string
|
714
|
+
@input << "James\nDana\nStorm\nGypsy\n\n"
|
614
715
|
@input.rewind
|
615
716
|
|
616
717
|
answers = @terminal.ask("Enter four names:") do |q|
|
617
718
|
q.gather = ""
|
618
719
|
end
|
619
720
|
assert_equal(%w{James Dana Storm Gypsy}, answers)
|
721
|
+
end
|
620
722
|
|
723
|
+
def test_gather_with_regexp
|
724
|
+
@input << "James\nDana\nStorm\nGypsy\n\n"
|
621
725
|
@input.rewind
|
622
726
|
|
623
727
|
answers = @terminal.ask("Enter four names:") do |q|
|
624
728
|
q.gather = /^\s*$/
|
625
729
|
end
|
626
730
|
assert_equal(%w{James Dana Storm Gypsy}, answers)
|
731
|
+
end
|
627
732
|
|
628
|
-
|
733
|
+
def test_gather_with_hash
|
629
734
|
@input << "29\n49\n30\n"
|
630
735
|
@input.rewind
|
631
|
-
@output.truncate(@output.rewind)
|
632
736
|
|
633
|
-
answers = @terminal.ask("<%=
|
737
|
+
answers = @terminal.ask("<%= key %>: ", Integer) do |q|
|
634
738
|
q.gather = { "Age" => 0, "Wife's Age" => 0, "Father's Age" => 0}
|
635
739
|
end
|
636
740
|
assert_equal( { "Age" => 29, "Wife's Age" => 30, "Father's Age" => 49},
|
@@ -669,7 +773,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
669
773
|
@input.rewind
|
670
774
|
@output.truncate(@output.rewind)
|
671
775
|
|
672
|
-
answer = @terminal.ask("<%=
|
776
|
+
answer = @terminal.ask("<%= key %>: ") do |q|
|
673
777
|
q.verify_match = true
|
674
778
|
q.gather = {"Enter a password" => '', "Please type it again" => ''}
|
675
779
|
end
|
@@ -680,7 +784,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
680
784
|
@input.rewind
|
681
785
|
@output.truncate(@output.rewind)
|
682
786
|
|
683
|
-
answer = @terminal.ask("<%=
|
787
|
+
answer = @terminal.ask("<%= key %>: ") do |q|
|
684
788
|
q.verify_match = true
|
685
789
|
q.responses[:mismatch] = 'Typing mismatch!'
|
686
790
|
q.responses[:ask_on_error] = ''
|
@@ -855,8 +959,8 @@ class TestHighLine < Test::Unit::TestCase
|
|
855
959
|
end
|
856
960
|
|
857
961
|
def test_mode
|
858
|
-
assert(%w[Win32API termios ncurses stty jline].include?(
|
859
|
-
"#{
|
962
|
+
assert(%w[io_console Win32API termios ncurses stty unix_stty jline].include?(@terminal.terminal.character_mode),
|
963
|
+
"#{@terminal.terminal.character_mode} not in list")
|
860
964
|
end
|
861
965
|
|
862
966
|
class NameClass
|
@@ -924,7 +1028,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
924
1028
|
end
|
925
1029
|
|
926
1030
|
assert_equal "ação", answer
|
927
|
-
assert_equal Encoding::
|
1031
|
+
assert_equal Encoding::UTF_8, answer.encoding
|
928
1032
|
end
|
929
1033
|
|
930
1034
|
def test_backspace_with_ascii_when_echo_false
|
@@ -935,7 +1039,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
935
1039
|
q.echo = false
|
936
1040
|
end
|
937
1041
|
|
938
|
-
|
1042
|
+
refute_equal("password", answer)
|
939
1043
|
assert_equal("passwor", answer)
|
940
1044
|
end
|
941
1045
|
|
@@ -947,7 +1051,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
947
1051
|
q.echo = false
|
948
1052
|
end
|
949
1053
|
|
950
|
-
|
1054
|
+
refute_equal("maçã", answer)
|
951
1055
|
assert_equal("maç", answer)
|
952
1056
|
end
|
953
1057
|
|
@@ -962,63 +1066,6 @@ class TestHighLine < Test::Unit::TestCase
|
|
962
1066
|
assert_equal("Type: ****\n", @output.string)
|
963
1067
|
assert_equal("maçã", answer)
|
964
1068
|
end
|
965
|
-
|
966
|
-
def test_paging
|
967
|
-
@terminal.page_at = 22
|
968
|
-
|
969
|
-
@input << "\n\n"
|
970
|
-
@input.rewind
|
971
|
-
|
972
|
-
@terminal.say((1..50).map { |n| "This is line #{n}.\n"}.join)
|
973
|
-
assert_equal( (1..22).map { |n| "This is line #{n}.\n"}.join +
|
974
|
-
"\n-- press enter/return to continue or q to stop -- \n\n" +
|
975
|
-
(23..44).map { |n| "This is line #{n}.\n"}.join +
|
976
|
-
"\n-- press enter/return to continue or q to stop -- \n\n" +
|
977
|
-
(45..50).map { |n| "This is line #{n}.\n"}.join,
|
978
|
-
@output.string )
|
979
|
-
end
|
980
|
-
|
981
|
-
def test_statement_lines_count_equal_to_page_at_shouldnt_paginate
|
982
|
-
@terminal.page_at = 6
|
983
|
-
|
984
|
-
@input << "\n"
|
985
|
-
@input.rewind
|
986
|
-
|
987
|
-
list = "a\nb\nc\nd\ne\nf\n"
|
988
|
-
|
989
|
-
@terminal.say(list)
|
990
|
-
assert_equal(list, @output.string)
|
991
|
-
end
|
992
|
-
|
993
|
-
def test_statement_with_one_line_bigger_than_page_at_should_paginate
|
994
|
-
@terminal.page_at = 6
|
995
|
-
|
996
|
-
@input << "\n"
|
997
|
-
@input.rewind
|
998
|
-
|
999
|
-
list = "a\nb\nc\nd\ne\nf\ng\n"
|
1000
|
-
|
1001
|
-
paginated =
|
1002
|
-
"a\nb\nc\nd\ne\nf\n" \
|
1003
|
-
"\n-- press enter/return to continue or q to stop -- \n\n" \
|
1004
|
-
"g\n"
|
1005
|
-
|
1006
|
-
@terminal.say(list)
|
1007
|
-
assert_equal(paginated, @output.string)
|
1008
|
-
end
|
1009
|
-
|
1010
|
-
def test_quiting_paging_shouldnt_raise
|
1011
|
-
# See https://github.com/JEG2/highline/issues/168
|
1012
|
-
|
1013
|
-
@terminal.page_at = 6
|
1014
|
-
|
1015
|
-
@input << "q"
|
1016
|
-
@input.rewind
|
1017
|
-
|
1018
|
-
list = "a\nb\nc\nd\ne\nf\n"
|
1019
|
-
|
1020
|
-
assert_nothing_raised { @terminal.say(list) }
|
1021
|
-
end
|
1022
1069
|
|
1023
1070
|
def test_range_requirements
|
1024
1071
|
@input << "112\n-541\n28\n"
|
@@ -1070,6 +1117,24 @@ class TestHighLine < Test::Unit::TestCase
|
|
1070
1117
|
"(below 0).\n" +
|
1071
1118
|
"? ", @output.string )
|
1072
1119
|
|
1120
|
+
@input.truncate(@input.rewind)
|
1121
|
+
@input << "-541\n11\n6\n"
|
1122
|
+
@input.rewind
|
1123
|
+
@output.truncate(@output.rewind)
|
1124
|
+
|
1125
|
+
answer = @terminal.ask("Enter a low even number: ", Integer) do |q|
|
1126
|
+
q.above = 0
|
1127
|
+
q.below = 10
|
1128
|
+
end
|
1129
|
+
assert_equal(6, answer)
|
1130
|
+
assert_equal( "Enter a low even number: " +
|
1131
|
+
"Your answer isn't within the expected range " +
|
1132
|
+
"(above 0 and below 10).\n" +
|
1133
|
+
"? " +
|
1134
|
+
"Your answer isn't within the expected range " +
|
1135
|
+
"(above 0 and below 10).\n" +
|
1136
|
+
"? ", @output.string )
|
1137
|
+
|
1073
1138
|
@input.truncate(@input.rewind)
|
1074
1139
|
@input << "1\n-541\n6\n"
|
1075
1140
|
@input.rewind
|
@@ -1137,8 +1202,8 @@ class TestHighLine < Test::Unit::TestCase
|
|
1137
1202
|
|
1138
1203
|
answer = @terminal.ask("Tell me your age.", Integer) do |q|
|
1139
1204
|
q.in = 0..105
|
1140
|
-
q.responses[:not_in_range] = "Need a
|
1141
|
-
"
|
1205
|
+
q.responses[:not_in_range] = "Need a #{q.answer_type}" +
|
1206
|
+
" #{q.expected_range}."
|
1142
1207
|
end
|
1143
1208
|
assert_equal(28, answer)
|
1144
1209
|
assert_equal( "Tell me your age.\n" +
|
@@ -1162,6 +1227,11 @@ class TestHighLine < Test::Unit::TestCase
|
|
1162
1227
|
@terminal.say("This will not have a newline. ")
|
1163
1228
|
assert_equal("This will not have a newline. ", @output.string)
|
1164
1229
|
|
1230
|
+
@output.truncate(@output.rewind)
|
1231
|
+
|
1232
|
+
@terminal.say("This will not have a newline.\t")
|
1233
|
+
assert_equal("This will not have a newline.\t", @output.string)
|
1234
|
+
|
1165
1235
|
@output.truncate(@output.rewind)
|
1166
1236
|
|
1167
1237
|
@terminal.say("This will not\n end with a newline. ")
|
@@ -1186,7 +1256,7 @@ class TestHighLine < Test::Unit::TestCase
|
|
1186
1256
|
|
1187
1257
|
@output.truncate(@output.rewind)
|
1188
1258
|
|
1189
|
-
|
1259
|
+
@terminal.say(nil)
|
1190
1260
|
assert_equal("", @output.string)
|
1191
1261
|
end
|
1192
1262
|
|
@@ -1194,18 +1264,18 @@ class TestHighLine < Test::Unit::TestCase
|
|
1194
1264
|
integer = 10
|
1195
1265
|
hash = { :a => 20 }
|
1196
1266
|
|
1197
|
-
|
1267
|
+
@terminal.say(integer)
|
1198
1268
|
assert_equal String(integer), @output.string.chomp
|
1199
1269
|
|
1200
1270
|
@output.truncate(@output.rewind)
|
1201
1271
|
|
1202
|
-
|
1272
|
+
@terminal.say(hash)
|
1203
1273
|
assert_equal String(hash), @output.string.chomp
|
1204
1274
|
end
|
1205
1275
|
|
1206
1276
|
def test_terminal_size
|
1207
|
-
assert_instance_of(Fixnum, @terminal.terminal_size[0])
|
1208
|
-
assert_instance_of(Fixnum, @terminal.terminal_size[1])
|
1277
|
+
assert_instance_of(Fixnum, @terminal.terminal.terminal_size[0])
|
1278
|
+
assert_instance_of(Fixnum, @terminal.terminal.terminal_size[1])
|
1209
1279
|
end
|
1210
1280
|
|
1211
1281
|
def test_type_conversion
|
@@ -1320,6 +1390,20 @@ class TestHighLine < Test::Unit::TestCase
|
|
1320
1390
|
|
1321
1391
|
@input.rewind
|
1322
1392
|
|
1393
|
+
answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
|
1394
|
+
q.whitespace = :strip
|
1395
|
+
end
|
1396
|
+
assert_equal("A lot\tof \t space\t \there!", answer)
|
1397
|
+
|
1398
|
+
@input.rewind
|
1399
|
+
|
1400
|
+
answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
|
1401
|
+
q.whitespace = :collapse
|
1402
|
+
end
|
1403
|
+
assert_equal(" A lot of space here! ", answer)
|
1404
|
+
|
1405
|
+
@input.rewind
|
1406
|
+
|
1323
1407
|
answer = @terminal.ask("Enter a whitespace filled string: ")
|
1324
1408
|
assert_equal("A lot\tof \t space\t \there!", answer)
|
1325
1409
|
|
@@ -1343,60 +1427,33 @@ class TestHighLine < Test::Unit::TestCase
|
|
1343
1427
|
q.whitespace = :none
|
1344
1428
|
end
|
1345
1429
|
assert_equal(" A lot\tof \t space\t \there! \n", answer)
|
1346
|
-
end
|
1347
|
-
|
1348
|
-
def test_wrap
|
1349
|
-
@terminal.wrap_at = 80
|
1350
|
-
|
1351
|
-
@terminal.say("This is a very short line.")
|
1352
|
-
assert_equal("This is a very short line.\n", @output.string)
|
1353
|
-
|
1354
|
-
@output.truncate(@output.rewind)
|
1355
1430
|
|
1356
|
-
@
|
1357
|
-
"several lines. This text should definitely be " +
|
1358
|
-
"wrapped at the set limit, in the result. Your code " +
|
1359
|
-
"does well with things like this.\n\n" +
|
1360
|
-
" * This is a simple embedded list.\n" +
|
1361
|
-
" * You're code should not mess with this...\n" +
|
1362
|
-
" * Because it's already formatted correctly and " +
|
1363
|
-
"does not\n" +
|
1364
|
-
" exceed the limit!" )
|
1365
|
-
assert_equal( "This is a long flowing paragraph meant to span " +
|
1366
|
-
"several lines. This text should\n" +
|
1367
|
-
"definitely be wrapped at the set limit, in the " +
|
1368
|
-
"result. Your code does well with\n" +
|
1369
|
-
"things like this.\n\n" +
|
1370
|
-
" * This is a simple embedded list.\n" +
|
1371
|
-
" * You're code should not mess with this...\n" +
|
1372
|
-
" * Because it's already formatted correctly and does " +
|
1373
|
-
"not\n" +
|
1374
|
-
" exceed the limit!\n", @output.string )
|
1375
|
-
|
1376
|
-
@output.truncate(@output.rewind)
|
1431
|
+
@input.rewind
|
1377
1432
|
|
1378
|
-
@terminal.
|
1379
|
-
|
1433
|
+
answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
|
1434
|
+
q.whitespace = nil
|
1435
|
+
end
|
1436
|
+
assert_equal(" A lot\tof \t space\t \there! \n", answer)
|
1380
1437
|
end
|
1381
1438
|
|
1382
1439
|
def test_track_eof
|
1383
|
-
|
1440
|
+
assert_raises(EOFError) { @terminal.ask("Any input left? ") }
|
1384
1441
|
|
1385
1442
|
# turn EOF tracking
|
1386
1443
|
old_setting = HighLine.track_eof?
|
1387
|
-
|
1444
|
+
HighLine.track_eof = false
|
1388
1445
|
begin
|
1389
1446
|
@terminal.ask("And now? ") # this will still blow up, nothing available
|
1390
1447
|
rescue
|
1391
|
-
|
1448
|
+
refute_equal(EOFError, $!.class) # but HighLine's safe guards are off
|
1392
1449
|
end
|
1393
1450
|
HighLine.track_eof = old_setting
|
1394
1451
|
end
|
1395
1452
|
|
1396
1453
|
def test_version
|
1397
|
-
|
1454
|
+
refute_nil(HighLine::VERSION)
|
1398
1455
|
assert_instance_of(String, HighLine::VERSION)
|
1399
1456
|
assert(HighLine::VERSION.frozen?)
|
1400
|
-
assert_match(/\A\d+\.\d+\.\d
|
1457
|
+
assert_match(/\A\d+\.\d+\.\d+(-.*)?/, HighLine::VERSION)
|
1401
1458
|
end
|
1402
1459
|
end
|