highline 2.0.0.pre.develop.9 → 2.0.0.pre.develop.11

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +59 -5
  3. data/.travis.yml +9 -4
  4. data/Changelog.md +11 -0
  5. data/Gemfile +12 -19
  6. data/Rakefile +5 -11
  7. data/examples/ansi_colors.rb +6 -11
  8. data/examples/asking_for_arrays.rb +4 -3
  9. data/examples/basic_usage.rb +29 -22
  10. data/examples/color_scheme.rb +11 -10
  11. data/examples/get_character.rb +6 -5
  12. data/examples/limit.rb +2 -1
  13. data/examples/menus.rb +11 -11
  14. data/examples/overwrite.rb +7 -6
  15. data/examples/page_and_wrap.rb +5 -4
  16. data/examples/password.rb +2 -1
  17. data/examples/repeat_entry.rb +7 -5
  18. data/examples/trapping_eof.rb +2 -1
  19. data/examples/using_readline.rb +2 -1
  20. data/highline.gemspec +25 -25
  21. data/lib/highline.rb +103 -111
  22. data/lib/highline/builtin_styles.rb +45 -41
  23. data/lib/highline/color_scheme.rb +32 -28
  24. data/lib/highline/compatibility.rb +3 -3
  25. data/lib/highline/custom_errors.rb +2 -1
  26. data/lib/highline/import.rb +8 -11
  27. data/lib/highline/list.rb +4 -8
  28. data/lib/highline/list_renderer.rb +207 -201
  29. data/lib/highline/menu.rb +75 -63
  30. data/lib/highline/menu/item.rb +2 -0
  31. data/lib/highline/paginator.rb +5 -6
  32. data/lib/highline/question.rb +38 -36
  33. data/lib/highline/question/answer_converter.rb +2 -2
  34. data/lib/highline/question_asker.rb +15 -17
  35. data/lib/highline/simulate.rb +11 -13
  36. data/lib/highline/statement.rb +12 -10
  37. data/lib/highline/string.rb +9 -8
  38. data/lib/highline/string_extensions.rb +30 -14
  39. data/lib/highline/style.rb +68 -45
  40. data/lib/highline/template_renderer.rb +5 -5
  41. data/lib/highline/terminal.rb +24 -31
  42. data/lib/highline/terminal/io_console.rb +2 -2
  43. data/lib/highline/terminal/ncurses.rb +4 -3
  44. data/lib/highline/terminal/unix_stty.rb +12 -9
  45. data/lib/highline/version.rb +1 -1
  46. data/lib/highline/wrapper.rb +12 -11
  47. metadata +34 -43
  48. data/test/acceptance/acceptance.rb +0 -62
  49. data/test/acceptance/acceptance_test.rb +0 -69
  50. data/test/acceptance/at_color_output_using_erb_templates.rb +0 -17
  51. data/test/acceptance/at_echo_false.rb +0 -23
  52. data/test/acceptance/at_readline.rb +0 -37
  53. data/test/io_console_compatible.rb +0 -37
  54. data/test/string_methods.rb +0 -35
  55. data/test/test_answer_converter.rb +0 -26
  56. data/test/test_color_scheme.rb +0 -94
  57. data/test/test_helper.rb +0 -22
  58. data/test/test_highline.rb +0 -1627
  59. data/test/test_import.rb +0 -55
  60. data/test/test_list.rb +0 -60
  61. data/test/test_menu.rb +0 -749
  62. data/test/test_paginator.rb +0 -73
  63. data/test/test_question_asker.rb +0 -20
  64. data/test/test_simulator.rb +0 -24
  65. data/test/test_string_extension.rb +0 -72
  66. data/test/test_string_highline.rb +0 -42
  67. data/test/test_style.rb +0 -613
  68. data/test/test_wrapper.rb +0 -188
@@ -1,69 +0,0 @@
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
@@ -1,17 +0,0 @@
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
@@ -1,23 +0,0 @@
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
@@ -1,37 +0,0 @@
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
@@ -1,37 +0,0 @@
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
@@ -1,35 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
-
4
- # string_methods.rb
5
- #
6
- # Created by Richard LeBer 2011-06-27
7
- #
8
- # This is Free Software. See LICENSE and COPYING for details.
9
- #
10
- # String class convenience methods
11
-
12
- module StringMethods
13
- def test_color
14
- assert_equal("\e[34mstring\e[0m", @string.color(:blue))
15
- assert_equal("\e[1m\e[47mstring\e[0m", @string.color(:bold,:on_white))
16
- assert_equal("\e[45mstring\e[0m", @string.on(:magenta))
17
- assert_equal("\e[36mstring\e[0m", @string.cyan)
18
- assert_equal("\e[41m\e[5mstring\e[0m\e[0m", @string.blink.on_red)
19
- assert_equal("\e[38;5;137mstring\e[0m", @string.color(:rgb_906030))
20
- assert_equal("\e[38;5;101mstring\e[0m", @string.rgb('606030'))
21
- assert_equal("\e[38;5;107mstring\e[0m", @string.rgb('60','90','30'))
22
- assert_equal("\e[38;5;107mstring\e[0m", @string.rgb(96,144,48))
23
- assert_equal("\e[38;5;173mstring\e[0m", @string.rgb_c06030)
24
- assert_equal("\e[48;5;137mstring\e[0m", @string.color(:on_rgb_906030))
25
- assert_equal("\e[48;5;101mstring\e[0m", @string.on_rgb('606030'))
26
- assert_equal("\e[48;5;107mstring\e[0m", @string.on_rgb('60','90','30'))
27
- assert_equal("\e[48;5;107mstring\e[0m", @string.on_rgb(96,144,48))
28
- assert_equal("\e[48;5;173mstring\e[0m", @string.on_rgb_c06030)
29
- end
30
-
31
- def test_uncolor
32
- colored_string = HighLine::String("\e[38;5;137mstring\e[0m")
33
- assert_equal "string", colored_string.uncolor
34
- end
35
- end
@@ -1,26 +0,0 @@
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,94 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
-
4
- # tc_color_scheme.rb
5
- #
6
- # Created by Jeremy Hinegardner on 2007-01-24.
7
- # Copyright 2007 Jeremy Hinegardner. All rights reserved.
8
- #
9
- # This is Free Software. See LICENSE and COPYING for details.
10
-
11
- require "test_helper"
12
-
13
- require "highline"
14
- require "stringio"
15
-
16
- class TestColorScheme < Minitest::Test
17
- def setup
18
- HighLine.reset
19
- @input = StringIO.new
20
- @output = StringIO.new
21
- @terminal = HighLine.new(@input, @output)
22
- end
23
-
24
- def test_using_color_scheme
25
- assert_equal(false,HighLine.using_color_scheme?)
26
-
27
- HighLine.color_scheme = HighLine::ColorScheme.new
28
- assert_equal(true,HighLine.using_color_scheme?)
29
- end
30
-
31
- def test_scheme
32
- HighLine.color_scheme = HighLine::SampleColorScheme.new
33
-
34
- @terminal.say("This should be <%= color('warning yellow', :warning) %>.")
35
- assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string)
36
- @output.rewind
37
-
38
- @terminal.say("This should be <%= color('warning yellow', 'warning') %>.")
39
- assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string)
40
- @output.rewind
41
-
42
- @terminal.say("This should be <%= color('warning yellow', 'WarNing') %>.")
43
- assert_equal("This should be \e[1m\e[33mwarning yellow\e[0m.\n",@output.string)
44
- @output.rewind
45
-
46
- # Check that keys are available, and as expected
47
- assert_equal ["critical", "error", "warning", "notice", "info", "debug", "row_even", "row_odd"].sort,
48
- HighLine.color_scheme.keys.sort
49
-
50
- # Color scheme doesn't care if we use symbols or strings, and is case-insensitive
51
- warning1 = HighLine.color_scheme[:warning]
52
- warning2 = HighLine.color_scheme["warning"]
53
- warning3 = HighLine.color_scheme[:wArning]
54
- warning4 = HighLine.color_scheme["warniNg"]
55
- assert_instance_of HighLine::Style, warning1
56
- assert_instance_of HighLine::Style, warning2
57
- assert_instance_of HighLine::Style, warning3
58
- assert_instance_of HighLine::Style, warning4
59
- assert_equal warning1, warning2
60
- assert_equal warning1, warning3
61
- assert_equal warning1, warning4
62
-
63
- # Nonexistent keys return nil
64
- assert_nil HighLine.color_scheme[:nonexistent]
65
-
66
- # Same as above, for definitions
67
- defn1 = HighLine.color_scheme.definition(:warning)
68
- defn2 = HighLine.color_scheme.definition("warning")
69
- defn3 = HighLine.color_scheme.definition(:wArning)
70
- defn4 = HighLine.color_scheme.definition("warniNg")
71
- assert_instance_of Array, defn1
72
- assert_instance_of Array, defn2
73
- assert_instance_of Array, defn3
74
- assert_instance_of Array, defn4
75
- assert_equal [:bold, :yellow], defn1
76
- assert_equal [:bold, :yellow], defn2
77
- assert_equal [:bold, :yellow], defn3
78
- assert_equal [:bold, :yellow], defn4
79
- assert_nil HighLine.color_scheme.definition(:nonexistent)
80
-
81
- color_scheme_hash = HighLine.color_scheme.to_hash
82
- assert_instance_of Hash, color_scheme_hash
83
- assert_equal ["critical", "error", "warning", "notice", "info", "debug", "row_even", "row_odd"].sort,
84
- color_scheme_hash.keys.sort
85
- assert_instance_of Array, HighLine.color_scheme.definition(:warning)
86
- assert_equal [:bold, :yellow], HighLine.color_scheme.definition(:warning)
87
-
88
- # turn it back off, should raise an exception
89
- HighLine.color_scheme = nil
90
- assert_raises(NameError) {
91
- @terminal.say("This should be <%= color('nothing at all', :error) %>.")
92
- }
93
- end
94
- end
data/test/test_helper.rb DELETED
@@ -1,22 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
-
4
- # Run code coverage only for mri
5
- require 'simplecov' if RUBY_ENGINE == 'ruby'
6
-
7
- # Compatibility module for StringIO, File
8
- # and Tempfile. Necessary for some tests.
9
- require "io_console_compatible"
10
-
11
- require 'highline'
12
- debug_message = "Tests will be run under:\n"
13
- debug_message << " - #{HighLine.new.terminal.class}\n"
14
- debug_message << " - HighLine::VERSION #{HighLine::VERSION}\n"
15
-
16
- if defined? RUBY_DESCRIPTION
17
- debug_message << " - #{RUBY_DESCRIPTION}\n"
18
- end
19
-
20
- puts debug_message
21
-
22
- require "minitest/autorun"
@@ -1,1627 +0,0 @@
1
- #!/usr/bin/env ruby
2
- # coding: utf-8
3
-
4
- # tc_highline.rb
5
- #
6
- # Created by James Edward Gray II on 2005-04-26.
7
- # Copyright 2005 Gray Productions. All rights reserved.
8
- #
9
- # This is Free Software. See LICENSE and COPYING for details.
10
-
11
- require "test_helper"
12
-
13
- require "highline"
14
- require "stringio"
15
- require "readline"
16
- require "tempfile"
17
-
18
- =begin
19
- if HighLine::CHARACTER_MODE == "Win32API"
20
- class HighLine
21
- # Override Windows' character reading so it's not tied to STDIN.
22
- def get_character( input = STDIN )
23
- input.getc
24
- end
25
- end
26
- end
27
- =end
28
-
29
- class TestHighLine < Minitest::Test
30
- def setup
31
- HighLine.reset
32
- @input = StringIO.new
33
- @output = StringIO.new
34
- @terminal = HighLine.new(@input, @output)
35
- end
36
-
37
- def test_agree_valid_yes_answers
38
- valid_yes_answers = %w{ y yes Y YES }
39
-
40
- valid_yes_answers.each do |user_input|
41
- @input << "#{user_input}\n"
42
- @input.rewind
43
-
44
- assert_equal true, @terminal.agree("Yes or no? ")
45
- assert_equal "Yes or no? ", @output.string
46
-
47
- @input.truncate(@input.rewind)
48
- @output.truncate(@output.rewind)
49
- end
50
- end
51
-
52
- def test_agree_valid_no_answers
53
- valid_no_answers = %w{ n no N NO }
54
-
55
- valid_no_answers.each do |user_input|
56
- @input << "#{user_input}\n"
57
- @input.rewind
58
-
59
- assert_equal false, @terminal.agree("Yes or no? ")
60
- assert_equal "Yes or no? ", @output.string
61
-
62
- @input.truncate(@input.rewind)
63
- @output.truncate(@output.rewind)
64
- end
65
- end
66
-
67
- def test_agree_invalid_answers
68
- invalid_answers = [ "ye", "yuk", "nope", "Oh yes", "Oh no", "Hell no!"]
69
-
70
- invalid_answers.each do |user_input|
71
- # Each invalid answer, should be followed by a 'y' (as the question is reasked)
72
- @input << "#{user_input}\ny\n"
73
- @input.rewind
74
-
75
- assert_equal true, @terminal.agree("Yes or no? ")
76
-
77
- # It reasks the question if the answer is invalid
78
- assert_equal "Yes or no? Please enter \"yes\" or \"no\".\nYes or no? ", @output.string
79
-
80
- @input.truncate(@input.rewind)
81
- @output.truncate(@output.rewind)
82
- end
83
- end
84
-
85
- def test_agree_with_getc
86
- @input.truncate(@input.rewind)
87
- @input << "yellow"
88
- @input.rewind
89
-
90
- assert_equal(true, @terminal.agree("Yes or no? ", :getc))
91
- end
92
-
93
- def test_agree_with_block
94
- @input << "\n\n"
95
- @input.rewind
96
-
97
- assert_equal(true, @terminal.agree("Yes or no? ") { |q| q.default = "y" })
98
- assert_equal(false, @terminal.agree("Yes or no? ") { |q| q.default = "n" })
99
- end
100
-
101
- def test_ask
102
- name = "James Edward Gray II"
103
- @input << name << "\n"
104
- @input.rewind
105
-
106
- assert_equal(name, @terminal.ask("What is your name? "))
107
-
108
- assert_raises(EOFError) { @terminal.ask("Any input left? ") }
109
- end
110
-
111
- def test_ask_string
112
- name = "James Edward Gray II"
113
- @input << name << "\n"
114
- @input.rewind
115
-
116
- assert_equal(name, @terminal.ask("What is your name? ", String))
117
-
118
- assert_raises(EOFError) { @terminal.ask("Any input left? ", String) }
119
- end
120
-
121
- def test_ask_string_converting
122
- name = "James Edward Gray II"
123
- @input << name << "\n"
124
- @input.rewind
125
-
126
- answer = @terminal.ask("What is your name? ", String)
127
-
128
- assert_instance_of HighLine::String, answer
129
-
130
- @input.rewind
131
-
132
- answer = @terminal.ask("What is your name? ", HighLine::String)
133
-
134
- assert_instance_of HighLine::String, answer
135
-
136
- assert_raises(EOFError) { @terminal.ask("Any input left? ", HighLine::String) }
137
- end
138
-
139
- def test_indent
140
- text = "Testing...\n"
141
- @terminal.indent_level=1
142
- @terminal.say(text)
143
- assert_equal(' '*3+text, @output.string)
144
-
145
- @output.truncate(@output.rewind)
146
- @terminal.indent_level=3
147
- @terminal.say(text)
148
- assert_equal(' '*9+text, @output.string)
149
-
150
- @output.truncate(@output.rewind)
151
- @terminal.indent_level=0
152
- @terminal.indent_size=5
153
- @terminal.indent(2, text)
154
- assert_equal(' '*10+text, @output.string)
155
-
156
- @output.truncate(@output.rewind)
157
- @terminal.indent_level=0
158
- @terminal.indent_size=4
159
- @terminal.indent {
160
- @terminal.say(text)
161
- }
162
- assert_equal(' '*4+text, @output.string)
163
-
164
- @output.truncate(@output.rewind)
165
- @terminal.indent_size=2
166
- @terminal.indent(3) { |t|
167
- t.say(text)
168
- }
169
- assert_equal(' '*6+text, @output.string)
170
-
171
- @output.truncate(@output.rewind)
172
- @terminal.indent { |t|
173
- t.indent {
174
- t.indent {
175
- t.indent { |tt|
176
- tt.say(text)
177
- }
178
- }
179
- }
180
- }
181
- assert_equal(' '*8+text, @output.string)
182
-
183
- text = "Multi\nLine\nIndentation\n"
184
- indent = ' '*4
185
- @terminal.indent_level=2
186
- @output.truncate(@output.rewind)
187
- @terminal.say(text)
188
- assert_equal("#{indent}Multi\n#{indent}Line\n#{indent}Indentation\n", @output.string)
189
-
190
- @output.truncate(@output.rewind)
191
- @terminal.multi_indent = false
192
- @terminal.say(text)
193
- assert_equal("#{indent}Multi\nLine\nIndentation\n", @output.string)
194
-
195
- @output.truncate(@output.rewind)
196
- @terminal.indent(0, text, true)
197
- assert_equal("#{indent}Multi\n#{indent}Line\n#{indent}Indentation\n", @output.string)
198
- end
199
-
200
- def test_newline
201
- @terminal.newline
202
- @terminal.newline
203
- assert_equal("\n\n", @output.string)
204
- end
205
-
206
- def test_bug_fixes
207
- # auto-complete bug
208
- @input << "ruby\nRuby\n"
209
- @input.rewind
210
-
211
- languages = [:Perl, :Python, :Ruby]
212
- answer = @terminal.ask( "What is your favorite programming language? ",
213
- languages )
214
- assert_equal(languages.last, answer)
215
-
216
- @input.truncate(@input.rewind)
217
- @input << "ruby\n"
218
- @input.rewind
219
-
220
- answer = @terminal.ask( "What is your favorite programming language? ",
221
- languages ) do |q|
222
- q.case = :capitalize
223
- end
224
- assert_equal(languages.last, answer)
225
-
226
- # poor auto-complete error message
227
- @input.truncate(@input.rewind)
228
- @input << "lisp\nruby\n"
229
- @input.rewind
230
- @output.truncate(@output.rewind)
231
-
232
- answer = @terminal.ask( "What is your favorite programming language? ",
233
- languages ) do |q|
234
- q.case = :capitalize
235
- end
236
- assert_equal(languages.last, answer)
237
- assert_equal( "What is your favorite programming language? " +
238
- "You must choose one of [Perl, Python, Ruby].\n" +
239
- "? ", @output.string )
240
- end
241
-
242
- def test_case_changes
243
- @input << "jeg2\n"
244
- @input.rewind
245
-
246
- answer = @terminal.ask("Enter your initials ") do |q|
247
- q.case = :up
248
- end
249
- assert_equal("JEG2", answer)
250
-
251
- @input.truncate(@input.rewind)
252
- @input << "cRaZY\n"
253
- @input.rewind
254
-
255
- answer = @terminal.ask("Enter a search string: ") do |q|
256
- q.case = :down
257
- end
258
- assert_equal("crazy", answer)
259
- end
260
-
261
- def test_ask_with_overwrite
262
- @input << "Yes, sure!\n"
263
- @input.rewind
264
-
265
- answer = @terminal.ask("Do you like Ruby? ") do |q|
266
- q.overwrite = true
267
- q.echo = false
268
- end
269
-
270
- assert_equal("Yes, sure!", answer)
271
- erase_sequence = "\r#{HighLine.Style(:erase_line).code}"
272
- assert_equal("Do you like Ruby? #{erase_sequence}", @output.string)
273
- end
274
-
275
- def test_ask_with_overwrite_and_character_mode
276
- @input << "Y"
277
- @input.rewind
278
-
279
- answer = @terminal.ask("Do you like Ruby (Y/N)? ") do |q|
280
- q.overwrite = true
281
- q.echo = false
282
- q.character = true
283
- end
284
-
285
- assert_equal("Y", answer)
286
- erase_sequence = "\r#{HighLine.Style(:erase_line).code}"
287
- assert_equal("Do you like Ruby (Y/N)? #{erase_sequence}", @output.string)
288
- end
289
-
290
- def test_character_echo
291
- @input << "password\r"
292
- @input.rewind
293
-
294
- answer = @terminal.ask("Please enter your password: ") do |q|
295
- q.echo = "*"
296
- end
297
- assert_equal("password", answer)
298
- assert_equal("Please enter your password: ********\n", @output.string)
299
-
300
- @input.truncate(@input.rewind)
301
- @input << "2"
302
- @input.rewind
303
- @output.truncate(@output.rewind)
304
-
305
- answer = @terminal.ask( "Select an option (1, 2 or 3): ",
306
- Integer ) do |q|
307
- q.echo = "*"
308
- q.character = true
309
- end
310
- assert_equal(2, answer)
311
- assert_equal("Select an option (1, 2 or 3): *\n", @output.string)
312
- end
313
-
314
- def test_backspace_does_not_enter_prompt
315
- @input << "\b\b"
316
- @input.rewind
317
- answer = @terminal.ask("Please enter your password: ") do |q|
318
- q.echo = "*"
319
- end
320
- assert_equal("", answer)
321
- assert_equal("Please enter your password: \n", @output.string)
322
- end
323
-
324
- def test_after_some_chars_backspace_does_not_enter_prompt_when_ascii
325
- @input << "apple\b\b\b\b\b\b\b\b\b\b"
326
- @input.rewind
327
- answer = @terminal.ask("Please enter your password: ") do |q|
328
- q.echo = "*"
329
- end
330
- assert_equal("", answer)
331
- assert_equal("apple".size, @output.string.count("\b"))
332
- end
333
-
334
- def test_after_some_chars_backspace_does_not_enter_prompt_when_utf8
335
- @input << "maçã\b\b\b\b\b\b\b\b"
336
- @input.rewind
337
- answer = @terminal.ask("Please enter your password: ") do |q|
338
- q.echo = "*"
339
- end
340
- assert_equal("", answer)
341
- assert_equal("maçã".size, @output.string.count("\b"))
342
- end
343
-
344
- def test_readline_mode
345
- #
346
- # Rubinius (and JRuby) seems to be ignoring
347
- # Readline input and output assignments. This
348
- # ruins testing.
349
- #
350
- # But it doesn't mean readline is not working
351
- # properly on rubinius or jruby.
352
- #
353
-
354
- terminal = @terminal.terminal
355
-
356
- if terminal.jruby? or terminal.rubinius? or terminal.windows?
357
- skip "We can't test Readline on JRuby, Rubinius and Windows yet"
358
- end
359
-
360
- # Creating Tempfiles here because Readline.input
361
- # and Readline.output only accepts a File object
362
- # as argument (not any duck type as StringIO)
363
-
364
- temp_stdin = Tempfile.new "temp_stdin"
365
- temp_stdout = Tempfile.new "temp_stdout"
366
-
367
- Readline.input = @input = File.open(temp_stdin.path, 'w+')
368
- Readline.output = @output = File.open(temp_stdout.path, 'w+')
369
-
370
- @terminal = HighLine.new(@input, @output)
371
-
372
- @input << "any input\n"
373
- @input.rewind
374
-
375
- answer = @terminal.ask("Prompt: ") do |q|
376
- q.readline = true
377
- end
378
-
379
- @output.rewind
380
- output = @output.read
381
-
382
- assert_equal "any input", answer
383
- assert_match "Prompt: any input\n", output
384
-
385
- @input.close
386
- @output.close
387
- Readline.input = STDIN
388
- Readline.output = STDOUT
389
- end
390
-
391
- def test_readline_mode_with_limit_set
392
- temp_stdin = Tempfile.new "temp_stdin"
393
- temp_stdout = Tempfile.new "temp_stdout"
394
-
395
- Readline.input = @input = File.open(temp_stdin.path, 'w+')
396
- Readline.output = @output = File.open(temp_stdout.path, 'w+')
397
-
398
- @terminal = HighLine.new(@input, @output)
399
-
400
- @input << "any input\n"
401
- @input.rewind
402
-
403
- answer = @terminal.ask("Prompt: ") do |q|
404
- q.limit = 50
405
- q.readline = true
406
- end
407
-
408
- @output.rewind
409
- output = @output.read
410
-
411
- assert_equal "any input", answer
412
- assert_equal "Prompt: any input\n", output
413
-
414
- @input.close
415
- @output.close
416
- Readline.input = STDIN
417
- Readline.output = STDOUT
418
- end
419
-
420
- def test_readline_on_non_echo_question_has_prompt
421
- @input << "you can't see me"
422
- @input.rewind
423
- answer = @terminal.ask("Please enter some hidden text: ") do |q|
424
- q.readline = true
425
- q.echo = "*"
426
- end
427
- assert_equal("you can't see me", answer)
428
- assert_equal("Please enter some hidden text: ****************\n", @output.string)
429
- end
430
-
431
- def test_character_reading
432
- # WARNING: This method does NOT cover Unix and Windows savvy testing!
433
- @input << "12345"
434
- @input.rewind
435
-
436
- answer = @terminal.ask("Enter a single digit: ", Integer) do |q|
437
- q.character = :getc
438
- end
439
- assert_equal(1, answer)
440
- end
441
-
442
- def test_frozen_statement
443
- @terminal.say('This is a frozen statement'.freeze)
444
- assert_equal("This is a frozen statement\n", @output.string)
445
- end
446
-
447
- def test_color
448
- @terminal.say("This should be <%= BLUE %>blue<%= CLEAR %>!")
449
- assert_equal("This should be \e[34mblue\e[0m!\n", @output.string)
450
-
451
- @output.truncate(@output.rewind)
452
-
453
- @terminal.say( "This should be " +
454
- "<%= BOLD + ON_WHITE %>bold on white<%= CLEAR %>!" )
455
- assert_equal( "This should be \e[1m\e[47mbold on white\e[0m!\n",
456
- @output.string )
457
-
458
- @output.truncate(@output.rewind)
459
-
460
- @terminal.say("This should be <%= color('cyan', CYAN) %>!")
461
- assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string)
462
-
463
- @output.truncate(@output.rewind)
464
-
465
- @terminal.say( "This should be " +
466
- "<%= color('blinking on red', :blink, :on_red) %>!" )
467
- assert_equal( "This should be \e[5m\e[41mblinking on red\e[0m!\n",
468
- @output.string )
469
-
470
- @output.truncate(@output.rewind)
471
-
472
- @terminal.say("This should be <%= NONE %>none<%= CLEAR %>!")
473
- assert_equal("This should be \e[38mnone\e[0m!\n", @output.string)
474
-
475
- @output.truncate(@output.rewind)
476
-
477
- @terminal.say("This should be <%= RGB_906030 %>rgb_906030<%= CLEAR %>!")
478
- assert_equal("This should be \e[38;5;137mrgb_906030\e[0m!\n", @output.string)
479
-
480
- @output.truncate(@output.rewind)
481
-
482
- @terminal.say("This should be <%= ON_RGB_C06030 %>on_rgb_c06030<%= CLEAR %>!")
483
- assert_equal("This should be \e[48;5;173mon_rgb_c06030\e[0m!\n", @output.string)
484
-
485
- # Relying on const_missing
486
- assert_instance_of HighLine::Style, HighLine::ON_RGB_C06031_STYLE
487
- assert_instance_of String , HighLine::ON_RGB_C06032
488
- assert_raises(NameError) { HighLine::ON_RGB_ZZZZZZ }
489
-
490
- # Retrieving color_code from a style
491
- assert_equal "\e[41m", @terminal.color_code([HighLine::ON_RED_STYLE])
492
-
493
- @output.truncate(@output.rewind)
494
-
495
- # Does class method work, too?
496
- @terminal.say("This should be <%= HighLine.color('reverse underlined magenta', :reverse, :underline, :magenta) %>!")
497
- assert_equal( "This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n",
498
- @output.string )
499
-
500
- @output.truncate(@output.rewind)
501
-
502
- # turn off color
503
- old_setting = HighLine.use_color?
504
- HighLine.use_color = false
505
- @terminal.use_color = false
506
- @terminal.say("This should be <%= color('cyan', CYAN) %>!")
507
- assert_equal("This should be cyan!\n", @output.string)
508
- HighLine.use_color = old_setting
509
- @terminal.use_color = old_setting
510
- end
511
-
512
- def test_color_setting_per_instance
513
- require 'highline/import'
514
- old_glob_term = $terminal
515
- old_setting = HighLine.use_color?
516
-
517
- gterm_input = StringIO.new
518
- gterm_output = StringIO.new
519
- $terminal = HighLine.new(gterm_input, gterm_output)
520
-
521
- # It can set coloring at HighLine class
522
- cli_input = StringIO.new
523
- cli_output = StringIO.new
524
-
525
- cli = HighLine.new(cli_input, cli_output)
526
-
527
- # Testing with both use_color setted to true
528
- HighLine.use_color = true
529
- @terminal.use_color = true
530
- cli.use_color = true
531
-
532
- say("This should be <%= color('cyan', CYAN) %>!")
533
- assert_equal("This should be \e[36mcyan\e[0m!\n", gterm_output.string)
534
-
535
- @terminal.say("This should be <%= color('cyan', CYAN) %>!")
536
- assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string)
537
-
538
- cli.say("This should be <%= color('cyan', CYAN) %>!")
539
- assert_equal("This should be \e[36mcyan\e[0m!\n", cli_output.string)
540
-
541
- gterm_output.truncate(gterm_output.rewind)
542
- @output.truncate(@output.rewind)
543
- cli_output.truncate(cli_output.rewind)
544
-
545
- # Testing with both use_color setted to false
546
- HighLine.use_color = false
547
- @terminal.use_color = false
548
- cli.use_color = false
549
-
550
- say("This should be <%= color('cyan', CYAN) %>!")
551
- assert_equal("This should be cyan!\n", gterm_output.string)
552
-
553
- @terminal.say("This should be <%= color('cyan', CYAN) %>!")
554
- assert_equal("This should be cyan!\n", @output.string)
555
-
556
- cli.say("This should be <%= color('cyan', CYAN) %>!")
557
- assert_equal("This should be cyan!\n", cli_output.string)
558
-
559
- gterm_output.truncate(gterm_output.rewind)
560
- @output.truncate(@output.rewind)
561
- cli_output.truncate(cli_output.rewind)
562
-
563
- # Now check when class and instance doesn't agree about use_color
564
-
565
- # Class false, instance true
566
- HighLine.use_color = false
567
- @terminal.use_color = false
568
- cli.use_color = true
569
-
570
- say("This should be <%= color('cyan', CYAN) %>!")
571
- assert_equal("This should be cyan!\n", gterm_output.string)
572
-
573
- @terminal.say("This should be <%= color('cyan', CYAN) %>!")
574
- assert_equal("This should be cyan!\n", @output.string)
575
-
576
- cli.say("This should be <%= color('cyan', CYAN) %>!")
577
- assert_equal("This should be \e[36mcyan\e[0m!\n", cli_output.string)
578
-
579
- gterm_output.truncate(gterm_output.rewind)
580
- @output.truncate(@output.rewind)
581
- cli_output.truncate(cli_output.rewind)
582
-
583
- # Class true, instance false
584
- HighLine.use_color = true
585
- @terminal.use_color = true
586
- cli.use_color = false
587
-
588
- say("This should be <%= color('cyan', CYAN) %>!")
589
- assert_equal("This should be \e[36mcyan\e[0m!\n", gterm_output.string)
590
-
591
- @terminal.say("This should be <%= color('cyan', CYAN) %>!")
592
- assert_equal("This should be \e[36mcyan\e[0m!\n", @output.string)
593
-
594
- cli.say("This should be <%= color('cyan', CYAN) %>!")
595
- assert_equal("This should be cyan!\n", cli_output.string )
596
-
597
- gterm_output.truncate(gterm_output.rewind)
598
- @output.truncate(@output.rewind)
599
- cli_output.truncate(cli_output.rewind)
600
-
601
- HighLine.use_color = old_setting
602
- @terminal.use_color = old_setting
603
- $terminal = old_glob_term
604
- end
605
-
606
- def test_reset_use_color
607
- HighLine.use_color = false
608
- refute HighLine.use_color?
609
- HighLine.reset_use_color
610
- assert HighLine.use_color?
611
- end
612
-
613
- def test_reset_use_color_when_highline_reset
614
- HighLine.use_color = false
615
- refute HighLine.use_color?
616
- HighLine.reset
617
- assert HighLine.use_color?
618
- end
619
-
620
- def test_uncolor
621
- # instance method
622
- assert_equal( "This should be reverse underlined magenta!\n",
623
- @terminal.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n")
624
- )
625
-
626
- @output.truncate(@output.rewind)
627
-
628
- # class method
629
- assert_equal( "This should be reverse underlined magenta!\n",
630
- HighLine.uncolor("This should be \e[7m\e[4m\e[35mreverse underlined magenta\e[0m!\n")
631
- )
632
-
633
- @output.truncate(@output.rewind)
634
-
635
- # RGB color
636
- assert_equal( "This should be rgb_906030!\n",
637
- @terminal.uncolor("This should be \e[38;5;137mrgb_906030\e[0m!\n")
638
- )
639
- end
640
-
641
- def test_grey_is_the_same_of_gray
642
- @terminal.say("<%= GRAY %>")
643
- gray_code = @output.string.dup
644
- @output.truncate(@output.rewind)
645
-
646
- @terminal.say("<%= GREY %>")
647
- grey_code = @output.string.dup
648
- @output.truncate(@output.rewind)
649
-
650
- assert_equal gray_code, grey_code
651
- end
652
-
653
- def test_light_is_the_same_as_bright
654
- @terminal.say("<%= BRIGHT_BLUE %>")
655
- bright_blue_code = @output.string.dup
656
- @output.truncate(@output.rewind)
657
-
658
- @terminal.say("<%= LIGHT_BLUE %>")
659
- light_blue_code = @output.string.dup
660
- @output.truncate(@output.rewind)
661
-
662
- assert_equal bright_blue_code, light_blue_code
663
- end
664
-
665
- def test_confirm
666
- @input << "junk.txt\nno\nsave.txt\ny\n"
667
- @input.rewind
668
-
669
- answer = @terminal.ask("Enter a filename: ") do |q|
670
- q.confirm = "Are you sure you want to overwrite <%= answer %>? "
671
- q.responses[:ask_on_error] = :question
672
- end
673
- assert_equal("save.txt", answer)
674
- assert_equal( "Enter a filename: " +
675
- "Are you sure you want to overwrite junk.txt? " +
676
- "Enter a filename: " +
677
- "Are you sure you want to overwrite save.txt? ",
678
- @output.string )
679
-
680
- @input.truncate(@input.rewind)
681
- @input << "junk.txt\nyes\nsave.txt\nn\n"
682
- @input.rewind
683
- @output.truncate(@output.rewind)
684
-
685
- answer = @terminal.ask("Enter a filename: ") do |q|
686
- q.confirm = "Are you sure you want to overwrite <%= answer %>? "
687
- end
688
- assert_equal("junk.txt", answer)
689
- assert_equal( "Enter a filename: " +
690
- "Are you sure you want to overwrite junk.txt? ",
691
- @output.string )
692
-
693
- @input.truncate(@input.rewind)
694
- @input << "junk.txt\nyes\nsave.txt\nn\n"
695
- @input.rewind
696
- @output.truncate(@output.rewind)
697
-
698
- scoped_variable = { "junk.txt" => '20mb' }
699
- answer = @terminal.ask("Enter a filename: ") do |q|
700
- q.confirm = Proc.new do |answer|
701
- "Are you sure you want to overwrite #{answer} with size " +
702
- "of #{scoped_variable[answer]}? "
703
- end
704
- end
705
- assert_equal("junk.txt", answer)
706
- assert_equal( "Enter a filename: " +
707
- "Are you sure you want to overwrite junk.txt " +
708
- "with size of 20mb? ",
709
- @output.string )
710
- end
711
-
712
- def test_generic_confirm_with_true
713
- @input << "junk.txt\nno\nsave.txt\ny\n"
714
- @input.rewind
715
-
716
- answer = @terminal.ask("Enter a filename: ") do |q|
717
- q.confirm = true
718
- q.responses[:ask_on_error] = :question
719
- end
720
- assert_equal("save.txt", answer)
721
- assert_equal( "Enter a filename: " +
722
- "Are you sure? " +
723
- "Enter a filename: " +
724
- "Are you sure? ",
725
- @output.string )
726
-
727
- @input.truncate(@input.rewind)
728
- @input << "junk.txt\nyes\nsave.txt\nn\n"
729
- @input.rewind
730
- @output.truncate(@output.rewind)
731
-
732
- answer = @terminal.ask("Enter a filename: ") do |q|
733
- q.confirm = true
734
- end
735
- assert_equal("junk.txt", answer)
736
- assert_equal( "Enter a filename: " +
737
- "Are you sure? ",
738
- @output.string )
739
- end
740
-
741
- def test_defaults
742
- @input << "\nNo Comment\n"
743
- @input.rewind
744
-
745
- answer = @terminal.ask("Are you sexually active? ") do |q|
746
- q.validate = /\Ay(?:es)?|no?|no comment\Z/i
747
- end
748
- assert_equal("No Comment", answer)
749
-
750
- @input.truncate(@input.rewind)
751
- @input << "\nYes\n"
752
- @input.rewind
753
- @output.truncate(@output.rewind)
754
-
755
- answer = @terminal.ask("Are you sexually active? ") do |q|
756
- q.default = "No Comment"
757
- q.validate = /\Ay(?:es)?|no?|no comment\Z/i
758
- end
759
- assert_equal("No Comment", answer)
760
- assert_equal( "Are you sexually active? |No Comment| ",
761
- @output.string )
762
- end
763
-
764
- def test_default_with_String
765
- @input << "\n"
766
- @input.rewind
767
-
768
- answer = @terminal.ask("Question: ") do |q|
769
- q.default = "string"
770
- end
771
-
772
- assert_equal "string", answer
773
- assert_equal "Question: |string| ", @output.string
774
- end
775
-
776
- def test_default_with_Symbol
777
- # With a Symbol, it should show up the String version
778
- # at prompt, but return the Symbol as answer
779
-
780
- @input << "\n"
781
- @input.rewind
782
-
783
- answer = @terminal.ask("Question: ") do |q|
784
- q.default = :string
785
- end
786
-
787
- assert_equal :string, answer
788
- assert_equal "Question: |string| ", @output.string
789
- end
790
-
791
- def test_default_with_non_String_objects
792
- # With a non-string object, it should not show
793
- # any 'default' at prompt line. And should
794
- # return the "default" object, without conversion.
795
-
796
- @input << "\n"
797
- @input.rewind
798
-
799
- default_non_string_object = Object.new
800
-
801
- answer = @terminal.ask("Question: ") do |q|
802
- q.default = default_non_string_object
803
- end
804
-
805
- assert_equal default_non_string_object, answer
806
- assert_equal "Question: ", @output.string
807
- end
808
-
809
- def test_string_preservation
810
- @input << "Maybe\nYes\n"
811
- @input.rewind
812
-
813
- my_string = "Is that your final answer? "
814
-
815
- @terminal.ask(my_string) { |q| q.default = "Possibly" }
816
- @terminal.ask(my_string) { |q| q.default = "Maybe" }
817
-
818
- assert_equal("Is that your final answer? ", my_string)
819
- end
820
-
821
- def test_empty
822
- @input << "\n"
823
- @input.rewind
824
-
825
- answer = @terminal.ask("") do |q|
826
- q.default = "yes"
827
- q.validate = /\Ay(?:es)?|no?\Z/i
828
- end
829
- assert_equal("yes", answer)
830
- end
831
-
832
- def test_erb
833
- @terminal.say( "The integers from 1 to 10 are:\n" +
834
- "% (1...10).each do |n|\n" +
835
- "\t<%= n %>,\n" +
836
- "% end\n" +
837
- "\tand 10" )
838
- assert_equal( "The integers from 1 to 10 are:\n" +
839
- "\t1,\n\t2,\n\t3,\n\t4,\n\t5,\n" +
840
- "\t6,\n\t7,\n\t8,\n\t9,\n\tand 10\n",
841
- @output.string )
842
- end
843
-
844
- def test_files
845
- @input << "#{File.basename(__FILE__)[0, 7]}\n"
846
- @input.rewind
847
-
848
- assert_equal "test_hi\n",@input.read
849
- @input.rewind
850
-
851
- file = @terminal.ask("Select a file: ", File) do |q|
852
- q.directory = File.expand_path(File.dirname(__FILE__))
853
- q.glob = "*.rb"
854
- end
855
- assert_instance_of(File, file)
856
- assert_equal("#!/usr/bin/env ruby\n", file.gets)
857
- file.close
858
-
859
- @input.rewind
860
-
861
- pathname = @terminal.ask("Select a file: ", Pathname) do |q|
862
- q.directory = File.expand_path(File.dirname(__FILE__))
863
- q.glob = "*.rb"
864
- end
865
- assert_instance_of(Pathname, pathname)
866
- assert_equal(File.size(__FILE__), pathname.size)
867
- end
868
-
869
- def test_gather_with_integer
870
- @input << "James\nDana\nStorm\nGypsy\n\n"
871
- @input.rewind
872
-
873
- answers = @terminal.ask("Enter four names:") do |q|
874
- q.gather = 4
875
- end
876
- assert_equal(%w{James Dana Storm Gypsy}, answers)
877
- assert_equal("\n", @input.gets)
878
- assert_equal("Enter four names:\n", @output.string)
879
- end
880
-
881
- def test_gather_with_an_empty_string
882
- @input << "James\nDana\nStorm\nGypsy\n\n"
883
- @input.rewind
884
-
885
- answers = @terminal.ask("Enter four names:") do |q|
886
- q.gather = ""
887
- end
888
- assert_equal(%w{James Dana Storm Gypsy}, answers)
889
- end
890
-
891
- def test_gather_with_regexp
892
- @input << "James\nDana\nStorm\nGypsy\n\n"
893
- @input.rewind
894
-
895
- answers = @terminal.ask("Enter four names:") do |q|
896
- q.gather = /^\s*$/
897
- end
898
- assert_equal(%w{James Dana Storm Gypsy}, answers)
899
- end
900
-
901
- def test_gather_with_hash
902
- @input << "29\n49\n30\n"
903
- @input.rewind
904
-
905
- answers = @terminal.ask("<%= key %>: ", Integer) do |q|
906
- q.gather = { "Age" => 0, "Wife's Age" => 0, "Father's Age" => 0}
907
- end
908
- assert_equal( { "Age" => 29, "Wife's Age" => 30, "Father's Age" => 49},
909
- answers )
910
- assert_equal("Age: Father's Age: Wife's Age: ", @output.string)
911
- end
912
-
913
- def test_typing_verification
914
- @input << "all work and no play makes jack a dull boy\n" * 3
915
- @input.rewind
916
-
917
- answer = @terminal.ask("How's work? ") do |q|
918
- q.gather = 3
919
- q.verify_match = true
920
- end
921
- assert_equal("all work and no play makes jack a dull boy", answer)
922
-
923
- @input.truncate(@input.rewind)
924
- @input << "all play and no work makes jack a mere toy\n"
925
- @input << "all work and no play makes jack a dull boy\n" * 5
926
- @input.rewind
927
- @output.truncate(@output.rewind)
928
-
929
- answer = @terminal.ask("How are things going? ") do |q|
930
- q.gather = 3
931
- q.verify_match = true
932
- q.responses[:mismatch] = 'Typing mismatch!'
933
- q.responses[:ask_on_error] = ''
934
- end
935
- assert_equal("all work and no play makes jack a dull boy", answer)
936
-
937
- # now try using a hash for gather
938
-
939
- @input.truncate(@input.rewind)
940
- @input << "Password\nPassword\n"
941
- @input.rewind
942
- @output.truncate(@output.rewind)
943
-
944
- answer = @terminal.ask("<%= key %>: ") do |q|
945
- q.verify_match = true
946
- q.gather = {"Enter a password" => '', "Please type it again" => ''}
947
- end
948
- assert_equal("Password", answer)
949
-
950
- @input.truncate(@input.rewind)
951
- @input << "Password\nMistake\nPassword\nPassword\n"
952
- @input.rewind
953
- @output.truncate(@output.rewind)
954
-
955
- answer = @terminal.ask("<%= key %>: ") do |q|
956
- q.verify_match = true
957
- q.responses[:mismatch] = 'Typing mismatch!'
958
- q.responses[:ask_on_error] = ''
959
- q.gather = {"Enter a password" => '', "Please type it again" => ''}
960
- end
961
-
962
- assert_equal("Password", answer)
963
- assert_equal( "Enter a password: " +
964
- "Please type it again: " +
965
- "Typing mismatch!\n" +
966
- "Enter a password: " +
967
- "Please type it again: ", @output.string )
968
- end
969
-
970
- def test_lists
971
- digits = %w{Zero One Two Three Four Five Six Seven Eight Nine}
972
- erb_digits = digits.dup
973
- erb_digits[erb_digits.index("Five")] = "<%= color('Five', :blue) %%>"
974
-
975
- @terminal.say("<%= list(#{digits.inspect}) %>")
976
- assert_equal(digits.map { |d| "#{d}\n" }.join, @output.string)
977
-
978
- @output.truncate(@output.rewind)
979
-
980
- @terminal.say("<%= list(#{digits.inspect}, :inline) %>")
981
- assert_equal( digits[0..-2].join(", ") + " or #{digits.last}\n",
982
- @output.string )
983
-
984
- @output.truncate(@output.rewind)
985
-
986
- @terminal.say("<%= list(#{digits.inspect}, :inline, ' and ') %>")
987
- assert_equal( digits[0..-2].join(", ") + " and #{digits.last}\n",
988
- @output.string )
989
-
990
- @output.truncate(@output.rewind)
991
-
992
- @terminal.say("<%= list(#{digits.inspect}, :columns_down, 3) %>")
993
- assert_equal( "Zero Four Eight\n" +
994
- "One Five Nine \n" +
995
- "Two Six \n" +
996
- "Three Seven\n",
997
- @output.string )
998
-
999
- @output.truncate(@output.rewind)
1000
-
1001
- @terminal.say("<%= list(#{erb_digits.inspect}, :columns_down, 3) %>")
1002
- assert_equal( "Zero Four Eight\n" +
1003
- "One \e[34mFive\e[0m Nine \n" +
1004
- "Two Six \n" +
1005
- "Three Seven\n",
1006
- @output.string )
1007
-
1008
- colums_of_twenty = ["12345678901234567890"] * 5
1009
-
1010
- @output.truncate(@output.rewind)
1011
-
1012
- @terminal.say("<%= list(#{colums_of_twenty.inspect}, :columns_down) %>")
1013
- assert_equal( "12345678901234567890 12345678901234567890 " +
1014
- "12345678901234567890\n" +
1015
- "12345678901234567890 12345678901234567890\n",
1016
- @output.string )
1017
-
1018
- @output.truncate(@output.rewind)
1019
-
1020
- @terminal.say("<%= list(#{digits.inspect}, :columns_across, 3) %>")
1021
- assert_equal( "Zero One Two \n" +
1022
- "Three Four Five \n" +
1023
- "Six Seven Eight\n" +
1024
- "Nine \n",
1025
- @output.string )
1026
-
1027
- colums_of_twenty.pop
1028
-
1029
- @output.truncate(@output.rewind)
1030
-
1031
- @terminal.say("<%= list( #{colums_of_twenty.inspect}, :columns_across ) %>")
1032
- assert_equal( "12345678901234567890 12345678901234567890 " +
1033
- "12345678901234567890\n" +
1034
- "12345678901234567890\n",
1035
- @output.string )
1036
-
1037
- @output.truncate(@output.rewind)
1038
-
1039
- 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]
1040
-
1041
- @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_across ) %>")
1042
- assert_equal( "0123456789 a b c d e f g h i j k l m n o " +
1043
- "p q r s t u v w\n" +
1044
- "x y z\n",
1045
- @output.string )
1046
-
1047
- @output.truncate(@output.rewind)
1048
-
1049
- @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_across, 10 ) %>")
1050
- assert_equal( "0123456789 a b c d e f g h i\n" +
1051
- "j k l m n o p q r s\n" +
1052
- "t u v w x y z\n",
1053
- @output.string )
1054
-
1055
- @output.truncate(@output.rewind)
1056
-
1057
- @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_down ) %>")
1058
- assert_equal( "0123456789 b d f h j l n p r t v x z\n" +
1059
- "a c e g i k m o q s u w y\n",
1060
- @output.string )
1061
-
1062
- @output.truncate(@output.rewind)
1063
-
1064
- @terminal.say("<%= list( #{wide.inspect}, :uneven_columns_down, 10 ) %>")
1065
- assert_equal( "0123456789 c f i l o r u x\n" +
1066
- "a d g j m p s v y\n" +
1067
- "b e h k n q t w z\n",
1068
- @output.string )
1069
- end
1070
-
1071
- def test_lists_with_zero_items
1072
- modes = [nil, :rows, :inline, :columns_across, :columns_down]
1073
- modes.each do |mode|
1074
- result = @terminal.list([], mode)
1075
- assert_equal("", result)
1076
- end
1077
- end
1078
-
1079
- def test_lists_with_nil_items
1080
- modes = [nil]
1081
- modes.each do |mode|
1082
- result = @terminal.list([nil], mode)
1083
- assert_equal("\n", result)
1084
- end
1085
- end
1086
-
1087
- def test_lists_with_one_item
1088
- items = ['Zero']
1089
- modes = { nil => "Zero\n",
1090
- :rows => "Zero\n",
1091
- :inline => "Zero",
1092
- :columns_across => "Zero\n",
1093
- :columns_down => "Zero\n" }
1094
-
1095
- modes.each do |mode, expected|
1096
- result = @terminal.list(items, mode)
1097
- assert_equal(expected, result)
1098
- end
1099
- end
1100
-
1101
- def test_lists_with_two_items
1102
- items = ['Zero', 'One']
1103
- modes = { nil => "Zero\nOne\n",
1104
- :rows => "Zero\nOne\n",
1105
- :inline => "Zero or One",
1106
- :columns_across => "Zero One \n",
1107
- :columns_down => "Zero One \n" }
1108
-
1109
- modes.each do |mode, expected|
1110
- result = @terminal.list(items, mode)
1111
- assert_equal(expected, result)
1112
- end
1113
- end
1114
-
1115
- def test_lists_with_three_items
1116
- items = ['Zero', 'One', 'Two']
1117
- modes = { nil => "Zero\nOne\nTwo\n",
1118
- :rows => "Zero\nOne\nTwo\n",
1119
- :inline => "Zero, One or Two",
1120
- :columns_across => "Zero One Two \n",
1121
- :columns_down => "Zero One Two \n" }
1122
-
1123
- modes.each do |mode, expected|
1124
- result = @terminal.list(items, mode)
1125
- assert_equal(expected, result)
1126
- end
1127
- end
1128
-
1129
- def test_mode
1130
- assert(%w[HighLine::Terminal::IOConsole HighLine::Terminal::NCurses HighLine::Terminal::UnixStty].include?(@terminal.terminal.character_mode),
1131
- "#{@terminal.terminal.character_mode} not in list")
1132
- end
1133
-
1134
- class NameClass
1135
- def self.parse( string )
1136
- if string =~ /^\s*(\w+),\s*(\w+)\s+(\w+)\s*$/
1137
- self.new($2, $3, $1)
1138
- else
1139
- raise ArgumentError, "Invalid name format."
1140
- end
1141
- end
1142
-
1143
- def initialize(first, middle, last)
1144
- @first, @middle, @last = first, middle, last
1145
- end
1146
-
1147
- attr_reader :first, :middle, :last
1148
- end
1149
-
1150
- def test_my_class_conversion
1151
- @input << "Gray, James Edward\n"
1152
- @input.rewind
1153
-
1154
- answer = @terminal.ask("Your name? ", NameClass) do |q|
1155
- q.validate = lambda do |name|
1156
- names = name.split(/,\s*/)
1157
- return false unless names.size == 2
1158
- return false if names.first =~ /\s/
1159
- names.last.split.size == 2
1160
- end
1161
- end
1162
- assert_instance_of(NameClass, answer)
1163
- assert_equal("Gray", answer.last)
1164
- assert_equal("James", answer.first)
1165
- assert_equal("Edward", answer.middle)
1166
- end
1167
-
1168
- def test_no_echo
1169
- @input << "password\r"
1170
- @input.rewind
1171
-
1172
- answer = @terminal.ask("Please enter your password: ") do |q|
1173
- q.echo = false
1174
- end
1175
- assert_equal("password", answer)
1176
- assert_equal("Please enter your password: \n", @output.string)
1177
-
1178
- @input.rewind
1179
- @output.truncate(@output.rewind)
1180
-
1181
- answer = @terminal.ask("Pick a letter or number: ") do |q|
1182
- q.character = true
1183
- q.echo = false
1184
- end
1185
- assert_equal("p", answer)
1186
- assert_equal("a", @input.getc.chr)
1187
- assert_equal("Pick a letter or number: \n", @output.string)
1188
- end
1189
-
1190
- def test_correct_string_encoding_when_echo_false
1191
- @input << "ação\r" # An UTF-8 portuguese word for 'action'
1192
- @input.rewind
1193
-
1194
- answer = @terminal.ask("Please enter your password: ") do |q|
1195
- q.echo = false
1196
- end
1197
-
1198
- assert_equal "ação", answer
1199
- assert_equal Encoding::UTF_8, answer.encoding
1200
- end
1201
-
1202
- def test_backspace_with_ascii_when_echo_false
1203
- @input << "password\b\r"
1204
- @input.rewind
1205
-
1206
- answer = @terminal.ask("Please enter your password: ") do |q|
1207
- q.echo = false
1208
- end
1209
-
1210
- refute_equal("password", answer)
1211
- assert_equal("passwor", answer)
1212
- end
1213
-
1214
- def test_backspace_with_utf8_when_echo_false
1215
- @input << "maçã\b\r"
1216
- @input.rewind
1217
-
1218
- answer = @terminal.ask("Please enter your password: ") do |q|
1219
- q.echo = false
1220
- end
1221
-
1222
- refute_equal("maçã", answer)
1223
- assert_equal("maç", answer)
1224
- end
1225
-
1226
- def test_echoing_with_utf8_when_echo_is_star
1227
- @input << "maçã\r"
1228
- @input.rewind
1229
-
1230
- answer = @terminal.ask("Type: ") do |q|
1231
- q.echo = "*"
1232
- end
1233
-
1234
- assert_equal("Type: ****\n", @output.string)
1235
- assert_equal("maçã", answer)
1236
- end
1237
-
1238
- def test_range_requirements
1239
- @input << "112\n-541\n28\n"
1240
- @input.rewind
1241
-
1242
- answer = @terminal.ask("Tell me your age.", Integer) do |q|
1243
- q.in = 0..105
1244
- end
1245
- assert_equal(28, answer)
1246
- assert_equal( "Tell me your age.\n" +
1247
- "Your answer isn't within the expected range " +
1248
- "(included in 0..105).\n" +
1249
- "? " +
1250
- "Your answer isn't within the expected range " +
1251
- "(included in 0..105).\n" +
1252
- "? ", @output.string )
1253
-
1254
- @input.truncate(@input.rewind)
1255
- @input << "1\n-541\n28\n"
1256
- @input.rewind
1257
- @output.truncate(@output.rewind)
1258
-
1259
- answer = @terminal.ask("Tell me your age.", Integer) do |q|
1260
- q.above = 3
1261
- end
1262
- assert_equal(28, answer)
1263
- assert_equal( "Tell me your age.\n" +
1264
- "Your answer isn't within the expected range " +
1265
- "(above 3).\n" +
1266
- "? " +
1267
- "Your answer isn't within the expected range " +
1268
- "(above 3).\n" +
1269
- "? ", @output.string )
1270
-
1271
- @input.truncate(@input.rewind)
1272
- @input << "1\n28\n-541\n"
1273
- @input.rewind
1274
- @output.truncate(@output.rewind)
1275
-
1276
- answer = @terminal.ask("Lowest numer you can think of?", Integer) do |q|
1277
- q.below = 0
1278
- end
1279
- assert_equal(-541, answer)
1280
- assert_equal( "Lowest numer you can think of?\n" +
1281
- "Your answer isn't within the expected range " +
1282
- "(below 0).\n" +
1283
- "? " +
1284
- "Your answer isn't within the expected range " +
1285
- "(below 0).\n" +
1286
- "? ", @output.string )
1287
-
1288
- @input.truncate(@input.rewind)
1289
- @input << "-541\n11\n6\n"
1290
- @input.rewind
1291
- @output.truncate(@output.rewind)
1292
-
1293
- answer = @terminal.ask("Enter a low even number: ", Integer) do |q|
1294
- q.above = 0
1295
- q.below = 10
1296
- end
1297
- assert_equal(6, answer)
1298
- assert_equal( "Enter a low even number: " +
1299
- "Your answer isn't within the expected range " +
1300
- "(above 0 and below 10).\n" +
1301
- "? " +
1302
- "Your answer isn't within the expected range " +
1303
- "(above 0 and below 10).\n" +
1304
- "? ", @output.string )
1305
-
1306
- @input.truncate(@input.rewind)
1307
- @input << "1\n-541\n6\n"
1308
- @input.rewind
1309
- @output.truncate(@output.rewind)
1310
-
1311
- answer = @terminal.ask("Enter a low even number: ", Integer) do |q|
1312
- q.above = 0
1313
- q.below = 10
1314
- q.in = [2, 4, 6, 8]
1315
- end
1316
- assert_equal(6, answer)
1317
- assert_equal( "Enter a low even number: " +
1318
- "Your answer isn't within the expected range " +
1319
- "(above 0, below 10, and included in [2, 4, 6, 8]).\n" +
1320
- "? " +
1321
- "Your answer isn't within the expected range " +
1322
- "(above 0, below 10, and included in [2, 4, 6, 8]).\n" +
1323
- "? ", @output.string )
1324
- end
1325
-
1326
- def test_reask
1327
- number = 61676
1328
- @input << "Junk!\n" << number << "\n"
1329
- @input.rewind
1330
-
1331
- answer = @terminal.ask("Favorite number? ", Integer)
1332
- assert_kind_of(Integer, number)
1333
- assert_instance_of(Fixnum, number)
1334
- assert_equal(number, answer)
1335
- assert_equal( "Favorite number? " +
1336
- "You must enter a valid Integer.\n" +
1337
- "? ", @output.string )
1338
-
1339
- @input.rewind
1340
- @output.truncate(@output.rewind)
1341
-
1342
- answer = @terminal.ask("Favorite number? ", Integer) do |q|
1343
- q.responses[:ask_on_error] = :question
1344
- q.responses[:invalid_type] = "Not a valid number!"
1345
- end
1346
- assert_kind_of(Integer, number)
1347
- assert_instance_of(Fixnum, number)
1348
- assert_equal(number, answer)
1349
- assert_equal( "Favorite number? " +
1350
- "Not a valid number!\n" +
1351
- "Favorite number? ", @output.string )
1352
-
1353
- @input.truncate(@input.rewind)
1354
- @input << "gen\ngene\n"
1355
- @input.rewind
1356
- @output.truncate(@output.rewind)
1357
-
1358
- answer = @terminal.ask("Select a mode: ", [:generate, :gentle])
1359
- assert_instance_of(Symbol, answer)
1360
- assert_equal(:generate, answer)
1361
- assert_equal( "Select a mode: " +
1362
- "Ambiguous choice. " +
1363
- "Please choose one of [generate, gentle].\n" +
1364
- "? ", @output.string )
1365
- end
1366
-
1367
- def test_response_embedding
1368
- @input << "112\n-541\n28\n"
1369
- @input.rewind
1370
-
1371
- answer = @terminal.ask("Tell me your age.", Integer) do |q|
1372
- q.in = 0..105
1373
- q.responses[:not_in_range] = "Need a #{q.answer_type}" +
1374
- " #{q.expected_range}."
1375
- end
1376
- assert_equal(28, answer)
1377
- assert_equal( "Tell me your age.\n" +
1378
- "Need a Integer included in 0..105.\n" +
1379
- "? " +
1380
- "Need a Integer included in 0..105.\n" +
1381
- "? ", @output.string )
1382
- end
1383
-
1384
- def test_say
1385
- @terminal.say("This will have a newline.")
1386
- assert_equal("This will have a newline.\n", @output.string)
1387
-
1388
- @output.truncate(@output.rewind)
1389
-
1390
- @terminal.say("This will also have one newline.\n")
1391
- assert_equal("This will also have one newline.\n", @output.string)
1392
-
1393
- @output.truncate(@output.rewind)
1394
-
1395
- @terminal.say("This will not have a newline. ")
1396
- assert_equal("This will not have a newline. ", @output.string)
1397
-
1398
- @output.truncate(@output.rewind)
1399
-
1400
- @terminal.say("This will not have a newline.\t")
1401
- assert_equal("This will not have a newline.\t", @output.string)
1402
-
1403
- @output.truncate(@output.rewind)
1404
-
1405
- @terminal.say("This will not\n end with a newline. ")
1406
- assert_equal("This will not\n end with a newline. ", @output.string)
1407
-
1408
- @output.truncate(@output.rewind)
1409
-
1410
- @terminal.say("This will \nend with a newline.")
1411
- assert_equal("This will \nend with a newline.\n", @output.string)
1412
-
1413
- @output.truncate(@output.rewind)
1414
-
1415
- colorized = @terminal.color("This will not have a newline. ", :green)
1416
- @terminal.say(colorized)
1417
- assert_equal("\e[32mThis will not have a newline. \e[0m", @output.string)
1418
-
1419
- @output.truncate(@output.rewind)
1420
-
1421
- colorized = @terminal.color("This will have a newline.", :green)
1422
- @terminal.say(colorized)
1423
- assert_equal("\e[32mThis will have a newline.\e[0m\n", @output.string)
1424
-
1425
- @output.truncate(@output.rewind)
1426
-
1427
- @terminal.say(nil)
1428
- assert_equal("", @output.string)
1429
- end
1430
-
1431
- def test_say_handles_non_string_argument
1432
- integer = 10
1433
- hash = { :a => 20 }
1434
-
1435
- @terminal.say(integer)
1436
- assert_equal String(integer), @output.string.chomp
1437
-
1438
- @output.truncate(@output.rewind)
1439
-
1440
- @terminal.say(hash)
1441
- assert_equal String(hash), @output.string.chomp
1442
- end
1443
-
1444
- def test_terminal_size
1445
- assert_instance_of(Fixnum, @terminal.terminal.terminal_size[0])
1446
- assert_instance_of(Fixnum, @terminal.terminal.terminal_size[1])
1447
- end
1448
-
1449
- def test_type_conversion
1450
- number = 61676
1451
- @input << number << "\n"
1452
- @input.rewind
1453
-
1454
- answer = @terminal.ask("Favorite number? ", Integer)
1455
- assert_kind_of(Integer, answer)
1456
- assert_instance_of(Fixnum, answer)
1457
- assert_equal(number, answer)
1458
-
1459
- @input.truncate(@input.rewind)
1460
- number = 1_000_000_000_000_000_000_000_000_000_000
1461
- @input << number << "\n"
1462
- @input.rewind
1463
-
1464
- answer = @terminal.ask("Favorite number? ", Integer)
1465
- assert_kind_of(Integer, answer)
1466
- assert_instance_of(Bignum, answer)
1467
- assert_equal(number, answer)
1468
-
1469
- @input.truncate(@input.rewind)
1470
- number = 10.5002
1471
- @input << number << "\n"
1472
- @input.rewind
1473
-
1474
- answer = @terminal.ask( "Favorite number? ",
1475
- lambda { |n| n.to_f.abs.round } )
1476
- assert_kind_of(Integer, answer)
1477
- assert_instance_of(Fixnum, answer)
1478
- assert_equal(11, answer)
1479
-
1480
- @input.truncate(@input.rewind)
1481
- animal = :dog
1482
- @input << animal << "\n"
1483
- @input.rewind
1484
-
1485
- answer = @terminal.ask("Favorite animal? ", Symbol)
1486
- assert_instance_of(Symbol, answer)
1487
- assert_equal(animal, answer)
1488
-
1489
- @input.truncate(@input.rewind)
1490
- @input << "16th June 1976\n"
1491
- @input.rewind
1492
-
1493
- answer = @terminal.ask("Enter your birthday.", Date)
1494
- assert_instance_of(Date, answer)
1495
- assert_equal(16, answer.day)
1496
- assert_equal(6, answer.month)
1497
- assert_equal(1976, answer.year)
1498
-
1499
- @input.truncate(@input.rewind)
1500
- pattern = "^yes|no$"
1501
- @input << pattern << "\n"
1502
- @input.rewind
1503
-
1504
- answer = @terminal.ask("Give me a pattern to match with: ", Regexp)
1505
- assert_instance_of(Regexp, answer)
1506
- assert_equal(/#{pattern}/, answer)
1507
-
1508
- @input.truncate(@input.rewind)
1509
- @input << "gen\n"
1510
- @input.rewind
1511
-
1512
- answer = @terminal.ask("Select a mode: ", [:generate, :run])
1513
- assert_instance_of(Symbol, answer)
1514
- assert_equal(:generate, answer)
1515
- end
1516
-
1517
- def test_validation
1518
- @input << "system 'rm -rf /'\n105\n0b101_001\n"
1519
- @input.rewind
1520
-
1521
- answer = @terminal.ask("Enter a binary number: ") do |q|
1522
- q.validate = /\A(?:0b)?[01_]+\Z/
1523
- end
1524
- assert_equal("0b101_001", answer)
1525
- assert_equal( "Enter a binary number: " +
1526
- "Your answer isn't valid " +
1527
- "(must match /\\A(?:0b)?[01_]+\\Z/).\n" +
1528
- "? " +
1529
- "Your answer isn't valid " +
1530
- "(must match /\\A(?:0b)?[01_]+\\Z/).\n" +
1531
- "? ", @output.string )
1532
-
1533
- @input.truncate(@input.rewind)
1534
- @input << "Gray II, James Edward\n" +
1535
- "Gray, Dana Ann Leslie\n" +
1536
- "Gray, James Edward\n"
1537
- @input.rewind
1538
-
1539
- answer = @terminal.ask("Your name? ") do |q|
1540
- q.validate = lambda do |name|
1541
- names = name.split(/,\s*/)
1542
- return false unless names.size == 2
1543
- return false if names.first =~ /\s/
1544
- names.last.split.size == 2
1545
- end
1546
- end
1547
- assert_equal("Gray, James Edward", answer)
1548
- end
1549
-
1550
- def test_whitespace
1551
- @input << " A lot\tof \t space\t \there! \n"
1552
- @input.rewind
1553
-
1554
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1555
- q.whitespace = :chomp
1556
- end
1557
- assert_equal(" A lot\tof \t space\t \there! ", answer)
1558
-
1559
- @input.rewind
1560
-
1561
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1562
- q.whitespace = :strip
1563
- end
1564
- assert_equal("A lot\tof \t space\t \there!", answer)
1565
-
1566
- @input.rewind
1567
-
1568
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1569
- q.whitespace = :collapse
1570
- end
1571
- assert_equal(" A lot of space here! ", answer)
1572
-
1573
- @input.rewind
1574
-
1575
- answer = @terminal.ask("Enter a whitespace filled string: ")
1576
- assert_equal("A lot\tof \t space\t \there!", answer)
1577
-
1578
- @input.rewind
1579
-
1580
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1581
- q.whitespace = :strip_and_collapse
1582
- end
1583
- assert_equal("A lot of space here!", answer)
1584
-
1585
- @input.rewind
1586
-
1587
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1588
- q.whitespace = :remove
1589
- end
1590
- assert_equal("Alotofspacehere!", answer)
1591
-
1592
- @input.rewind
1593
-
1594
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1595
- q.whitespace = :none
1596
- end
1597
- assert_equal(" A lot\tof \t space\t \there! \n", answer)
1598
-
1599
- @input.rewind
1600
-
1601
- answer = @terminal.ask("Enter a whitespace filled string: ") do |q|
1602
- q.whitespace = nil
1603
- end
1604
- assert_equal(" A lot\tof \t space\t \there! \n", answer)
1605
- end
1606
-
1607
- def test_track_eof
1608
- assert_raises(EOFError) { @terminal.ask("Any input left? ") }
1609
-
1610
- # turn EOF tracking
1611
- old_setting = HighLine.track_eof?
1612
- HighLine.track_eof = false
1613
- begin
1614
- @terminal.ask("And now? ") # this will still blow up, nothing available
1615
- rescue
1616
- refute_equal(EOFError, $!.class) # but HighLine's safe guards are off
1617
- end
1618
- HighLine.track_eof = old_setting
1619
- end
1620
-
1621
- def test_version
1622
- refute_nil(HighLine::VERSION)
1623
- assert_instance_of(String, HighLine::VERSION)
1624
- assert(HighLine::VERSION.frozen?)
1625
- assert_match(/\A\d+\.\d+\.\d+(-.*)?/, HighLine::VERSION)
1626
- end
1627
- end