tty-prompt 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. checksums.yaml +4 -4
  2. data/.travis.yml +9 -6
  3. data/CHANGELOG.md +40 -3
  4. data/Gemfile +0 -1
  5. data/README.md +246 -65
  6. data/examples/ask.rb +7 -0
  7. data/examples/echo.rb +7 -0
  8. data/examples/in.rb +7 -0
  9. data/examples/mask.rb +9 -0
  10. data/examples/multi_select.rb +8 -0
  11. data/examples/select.rb +8 -0
  12. data/examples/validation.rb +9 -0
  13. data/examples/yes_no.rb +7 -0
  14. data/lib/tty-prompt.rb +6 -4
  15. data/lib/tty/prompt.rb +100 -25
  16. data/lib/tty/prompt/choice.rb +1 -1
  17. data/lib/tty/prompt/converter_dsl.rb +19 -0
  18. data/lib/tty/prompt/converter_registry.rb +56 -0
  19. data/lib/tty/prompt/converters.rb +77 -0
  20. data/lib/tty/prompt/evaluator.rb +29 -0
  21. data/lib/tty/prompt/list.rb +38 -36
  22. data/lib/tty/prompt/mask_question.rb +85 -0
  23. data/lib/tty/prompt/multi_list.rb +21 -32
  24. data/lib/tty/prompt/question.rb +184 -162
  25. data/lib/tty/prompt/question/checks.rb +85 -0
  26. data/lib/tty/prompt/question/modifier.rb +4 -5
  27. data/lib/tty/prompt/question/validation.rb +29 -35
  28. data/lib/tty/prompt/reader.rb +98 -52
  29. data/lib/tty/prompt/reader/codes.rb +63 -0
  30. data/lib/tty/prompt/reader/key_event.rb +67 -0
  31. data/lib/tty/prompt/reader/mode.rb +66 -0
  32. data/lib/tty/prompt/reader/mode/echo.rb +43 -0
  33. data/lib/tty/prompt/reader/mode/raw.rb +43 -0
  34. data/lib/tty/prompt/result.rb +42 -0
  35. data/lib/tty/prompt/statement.rb +9 -14
  36. data/lib/tty/prompt/suggestion.rb +4 -2
  37. data/lib/tty/prompt/symbols.rb +13 -0
  38. data/lib/tty/prompt/test.rb +3 -2
  39. data/lib/tty/prompt/utils.rb +1 -1
  40. data/lib/tty/prompt/version.rb +1 -1
  41. data/spec/unit/ask_spec.rb +31 -48
  42. data/spec/unit/choice/eql_spec.rb +0 -2
  43. data/spec/unit/choice/from_spec.rb +0 -2
  44. data/spec/unit/choices/add_spec.rb +0 -2
  45. data/spec/unit/choices/each_spec.rb +0 -2
  46. data/spec/unit/choices/new_spec.rb +0 -2
  47. data/spec/unit/choices/pluck_spec.rb +0 -2
  48. data/spec/unit/converters/convert_bool_spec.rb +58 -0
  49. data/spec/unit/{response/read_char_spec.rb → converters/convert_char_spec.rb} +2 -4
  50. data/spec/unit/converters/convert_custom_spec.rb +14 -0
  51. data/spec/unit/converters/convert_date_spec.rb +25 -0
  52. data/spec/unit/converters/convert_file_spec.rb +14 -0
  53. data/spec/unit/{response/read_number_spec.rb → converters/convert_number_spec.rb} +5 -7
  54. data/spec/unit/converters/convert_path_spec.rb +15 -0
  55. data/spec/unit/{response/read_range_spec.rb → converters/convert_range_spec.rb} +3 -5
  56. data/spec/unit/converters/convert_regex_spec.rb +12 -0
  57. data/spec/unit/converters/convert_string_spec.rb +21 -0
  58. data/spec/unit/distance/distance_spec.rb +0 -2
  59. data/spec/unit/error_spec.rb +0 -6
  60. data/spec/unit/evaluator_spec.rb +67 -0
  61. data/spec/unit/keypress_spec.rb +19 -0
  62. data/spec/unit/mask_spec.rb +95 -0
  63. data/spec/unit/multi_select_spec.rb +36 -24
  64. data/spec/unit/multiline_spec.rb +19 -0
  65. data/spec/unit/new_spec.rb +18 -0
  66. data/spec/unit/ok_spec.rb +10 -0
  67. data/spec/unit/question/default_spec.rb +17 -4
  68. data/spec/unit/question/echo_spec.rb +31 -0
  69. data/spec/unit/question/in_spec.rb +48 -16
  70. data/spec/unit/question/initialize_spec.rb +2 -9
  71. data/spec/unit/question/modifier/apply_to_spec.rb +9 -16
  72. data/spec/unit/question/modifier/letter_case_spec.rb +0 -2
  73. data/spec/unit/question/modifier/whitespace_spec.rb +12 -20
  74. data/spec/unit/question/modify_spec.rb +3 -7
  75. data/spec/unit/question/required_spec.rb +20 -14
  76. data/spec/unit/question/validate_spec.rb +20 -19
  77. data/spec/unit/question/validation/call_spec.rb +15 -6
  78. data/spec/unit/question/validation/coerce_spec.rb +17 -11
  79. data/spec/unit/reader/publish_keypress_event_spec.rb +81 -0
  80. data/spec/unit/reader/read_keypress_spec.rb +22 -0
  81. data/spec/unit/reader/read_line_spec.rb +31 -0
  82. data/spec/unit/reader/read_multiline_spec.rb +37 -0
  83. data/spec/unit/result_spec.rb +40 -0
  84. data/spec/unit/say_spec.rb +18 -23
  85. data/spec/unit/select_spec.rb +37 -32
  86. data/spec/unit/statement/initialize_spec.rb +4 -4
  87. data/spec/unit/suggest_spec.rb +0 -2
  88. data/spec/unit/warn_spec.rb +0 -5
  89. data/spec/unit/yes_no_spec.rb +70 -0
  90. data/tty-prompt.gemspec +7 -4
  91. metadata +123 -40
  92. data/lib/tty/prompt/codes.rb +0 -32
  93. data/lib/tty/prompt/cursor.rb +0 -131
  94. data/lib/tty/prompt/error.rb +0 -26
  95. data/lib/tty/prompt/mode.rb +0 -64
  96. data/lib/tty/prompt/mode/echo.rb +0 -41
  97. data/lib/tty/prompt/mode/raw.rb +0 -41
  98. data/lib/tty/prompt/response.rb +0 -247
  99. data/lib/tty/prompt/response_delegation.rb +0 -42
  100. data/spec/unit/cursor/new_spec.rb +0 -74
  101. data/spec/unit/question/character_spec.rb +0 -13
  102. data/spec/unit/reader/getc_spec.rb +0 -42
  103. data/spec/unit/response/read_bool_spec.rb +0 -58
  104. data/spec/unit/response/read_date_spec.rb +0 -16
  105. data/spec/unit/response/read_email_spec.rb +0 -45
  106. data/spec/unit/response/read_multiple_spec.rb +0 -21
  107. data/spec/unit/response/read_spec.rb +0 -69
  108. data/spec/unit/response/read_string_spec.rb +0 -14
@@ -1,12 +1,6 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'spec_helper'
4
-
5
3
  RSpec.describe TTY::Prompt do
6
- let(:color) { Pastel.new(enabled: true) }
7
-
8
- before { allow(Pastel).to receive(:new).and_return(color) }
9
-
10
4
  it "selects nothing when return pressed immediately" do
11
5
  prompt = TTY::TestPrompt.new
12
6
  choices = %w(vodka beer wine whisky bourbon)
@@ -19,8 +13,8 @@ RSpec.describe TTY::Prompt do
19
13
  " ⬡ beer\n",
20
14
  " ⬡ wine\n",
21
15
  " ⬡ whisky\n",
22
- " ⬡ bourbon\n",
23
- "\e[1A\e[1000D\e[K" * 6,
16
+ " ⬡ bourbon",
17
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
24
18
  "Select drinks? \n\e[?25h"
25
19
  ].join)
26
20
  end
@@ -37,15 +31,15 @@ RSpec.describe TTY::Prompt do
37
31
  " ⬡ beer\n",
38
32
  " ⬡ wine\n",
39
33
  " ⬡ whisky\n",
40
- " ⬡ bourbon\n",
41
- "\e[1A\e[1000D\e[K" * 6,
34
+ " ⬡ bourbon",
35
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
42
36
  "Select drinks? vodka\n",
43
37
  "‣ \e[32m⬢\e[0m vodka\n",
44
38
  " ⬡ beer\n",
45
39
  " ⬡ wine\n",
46
40
  " ⬡ whisky\n",
47
- " ⬡ bourbon\n",
48
- "\e[1A\e[1000D\e[K" * 6,
41
+ " ⬡ bourbon",
42
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
49
43
  "Select drinks? \e[32mvodka\e[0m\n\e[?25h"
50
44
  ].join)
51
45
  end
@@ -62,15 +56,15 @@ RSpec.describe TTY::Prompt do
62
56
  " ⬡ beer\n",
63
57
  " ⬡ wine\n",
64
58
  " ⬡ whisky\n",
65
- " ⬡ bourbon\n",
66
- "\e[1A\e[1000D\e[K" * 6,
59
+ " ⬡ bourbon",
60
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
67
61
  "Select drinks? vodka\n",
68
62
  "‣ \e[32m⬢\e[0m vodka\n",
69
63
  " ⬡ beer\n",
70
64
  " ⬡ wine\n",
71
65
  " ⬡ whisky\n",
72
- " ⬡ bourbon\n",
73
- "\e[1A\e[1000D\e[K" * 6,
66
+ " ⬡ bourbon",
67
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
74
68
  "Select drinks? \e[32mvodka\e[0m\n\e[?25h"
75
69
  ].join)
76
70
  end
@@ -92,15 +86,15 @@ RSpec.describe TTY::Prompt do
92
86
  " ⬡ beer\n",
93
87
  " ⬡ wine\n",
94
88
  " ⬡ whisky\n",
95
- " ⬡ bourbon\n",
96
- "\e[1A\e[1000D\e[K" * 6,
89
+ " ⬡ bourbon",
90
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
97
91
  "Select drinks? vodka\n",
98
92
  "‣ \e[32m⬢\e[0m vodka\n",
99
93
  " ⬡ beer\n",
100
94
  " ⬡ wine\n",
101
95
  " ⬡ whisky\n",
102
- " ⬡ bourbon\n",
103
- "\e[1A\e[1000D\e[K" * 6,
96
+ " ⬡ bourbon",
97
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
104
98
  "Select drinks? \e[32mvodka\e[0m\n\e[?25h"
105
99
  ].join)
106
100
  end
@@ -125,8 +119,8 @@ RSpec.describe TTY::Prompt do
125
119
  " \e[32m⬢\e[0m beer\n",
126
120
  " ⬡ wine\n",
127
121
  " ⬡ whisky\n",
128
- "‣ \e[32m⬢\e[0m bourbon\n",
129
- "\e[1A\e[1000D\e[K" * 6,
122
+ "‣ \e[32m⬢\e[0m bourbon",
123
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
130
124
  "Select drinks? \e[32mbeer, bourbon\e[0m\n\e[?25h",
131
125
  ].join)
132
126
  end
@@ -135,7 +129,7 @@ RSpec.describe TTY::Prompt do
135
129
  prompt = TTY::TestPrompt.new
136
130
  prompt.input << "\r"
137
131
  prompt.input.rewind
138
- value = prompt.multi_select("Select drinks?",default: [2, 5]) do |menu|
132
+ value = prompt.multi_select("Select drinks?", default: [2, 5]) do |menu|
139
133
  menu.choice :vodka, {score: 10}
140
134
  menu.choice :beer, {score: 20}
141
135
  menu.choice :wine, {score: 30}
@@ -150,7 +144,7 @@ RSpec.describe TTY::Prompt do
150
144
  prompt.input << "\r"
151
145
  prompt.input.rewind
152
146
  expect {
153
- prompt.multi_select("Select drinks?",default: [2, 6]) do |menu|
147
+ prompt.multi_select("Select drinks?", default: [2, 6]) do |menu|
154
148
  menu.choice :vodka, {score: 10}
155
149
  menu.choice :beer, {score: 20}
156
150
  menu.choice :wine, {score: 30}
@@ -160,4 +154,22 @@ RSpec.describe TTY::Prompt do
160
154
  }.to raise_error(TTY::PromptConfigurationError,
161
155
  /default index `6` out of range \(1 - 5\)/)
162
156
  end
157
+
158
+ it "sets prompt prefix" do
159
+ prompt = TTY::TestPrompt.new(prefix: '[?] ')
160
+ choices = %w(vodka beer wine whisky bourbon)
161
+ prompt.input << "\r"
162
+ prompt.input.rewind
163
+ expect(prompt.multi_select("Select drinks?", choices)). to eq([])
164
+ expect(prompt.output.string).to eq([
165
+ "\e[?25l[?] Select drinks? \e[90m(Use arrow keys, press Space to select and Enter to finish)\e[0m\n",
166
+ "‣ ⬡ vodka\n",
167
+ " ⬡ beer\n",
168
+ " ⬡ wine\n",
169
+ " ⬡ whisky\n",
170
+ " ⬡ bourbon",
171
+ "\e[1000D\e[K\e[1A" * 5, "\e[1000D\e[K",
172
+ "[?] Select drinks? \n\e[?25h"
173
+ ].join)
174
+ end
163
175
  end
@@ -0,0 +1,19 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.describe TTY::Prompt::Question, 'ask multiline' do
4
+ it 'reads multiple lines with :read option' do
5
+ prompt = TTY::TestPrompt.new
6
+ prompt.input << "First line\nSecond line\nThird line"
7
+ prompt.input.rewind
8
+ answer = prompt.ask("Provide description?", read: :multiline)
9
+ expect(answer).to eq(['First line', 'Second line', 'Third line'])
10
+ end
11
+
12
+ it 'reads multiple lines with method' do
13
+ prompt = TTY::TestPrompt.new
14
+ prompt.input << "First\nSecond\nThird"
15
+ prompt.input.rewind
16
+ answer = prompt.multiline("Provide description?")
17
+ expect(answer).to eq(['First', 'Second', 'Third'])
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.describe TTY::Prompt, '#new' do
4
+ it "sets prefix" do
5
+ prompt = described_class.new(prefix: "[?]")
6
+ expect(prompt.prefix).to eq("[?]")
7
+ end
8
+
9
+ it "sets input stream" do
10
+ prompt = described_class.new(input: :stream1)
11
+ expect(prompt.input).to eq(:stream1)
12
+ end
13
+
14
+ it "sets output stream" do
15
+ prompt = described_class.new(output: :stream2)
16
+ expect(prompt.output).to eq(:stream2)
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.describe TTY::Prompt, 'ok' do
4
+ subject(:prompt) { TTY::TestPrompt.new }
5
+
6
+ it 'prints text in green' do
7
+ prompt.ok("All is fine")
8
+ expect(prompt.output.string).to eq("\e[32mAll is fine\e[0m\n")
9
+ end
10
+ end
@@ -1,19 +1,32 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'spec_helper'
4
-
5
3
  RSpec.describe TTY::Prompt::Question, '#default' do
4
+
5
+ subject(:prompt) { TTY::TestPrompt.new }
6
+
6
7
  it 'uses default value' do
7
- prompt = TTY::TestPrompt.new
8
8
  name = 'Anonymous'
9
+ prompt.input << "\n"
10
+ prompt.input.rewind
9
11
  answer = prompt.ask('What is your name?', default: name)
10
12
  expect(answer).to eq(name)
13
+ expect(prompt.output.string).to eq([
14
+ "What is your name? \e[90m(Anonymous)\e[0m ",
15
+ "\e[1000D\e[K\e[1A",
16
+ "\e[1000D\e[K",
17
+ "What is your name? \e[32mAnonymous\e[0m\n"
18
+ ].join)
11
19
  end
12
20
 
13
21
  it 'uses default value in block' do
14
- prompt = TTY::TestPrompt.new
15
22
  name = 'Anonymous'
16
23
  answer = prompt.ask('What is your name?') { |q| q.default(name) }
17
24
  expect(answer).to eq(name)
25
+ expect(prompt.output.string).to eq([
26
+ "What is your name? \e[90m(Anonymous)\e[0m ",
27
+ "\e[1000D\e[K\e[1A",
28
+ "\e[1000D\e[K",
29
+ "What is your name? \e[32mAnonymous\e[0m\n"
30
+ ].join)
18
31
  end
19
32
  end
@@ -0,0 +1,31 @@
1
+ # encoding: utf-8
2
+
3
+ RSpec.describe TTY::Prompt::Question, '#echo' do
4
+
5
+ subject(:prompt) { TTY::TestPrompt.new }
6
+
7
+ it 'asks with echo on' do
8
+ prompt.input << "password"
9
+ prompt.input.rewind
10
+ answer = prompt.ask("What is your password?") { |q| q.echo(true) }
11
+ expect(answer).to eql("password")
12
+ expect(prompt.output.string).to eq([
13
+ "What is your password? ",
14
+ "\e[1000D\e[K\e[1A",
15
+ "\e[1000D\e[K",
16
+ "What is your password? \e[32mpassword\e[0m\n"
17
+ ].join)
18
+ end
19
+
20
+ it 'asks with echo off' do
21
+ prompt.input << "password"
22
+ prompt.input.rewind
23
+ answer = prompt.ask("What is your password?", echo: false)
24
+ expect(answer).to eql("password")
25
+ expect(prompt.output.string).to eq([
26
+ "What is your password? ",
27
+ "\e[1000D\e[K",
28
+ "What is your password? \n"
29
+ ].join)
30
+ end
31
+ end
@@ -1,26 +1,58 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'spec_helper'
4
-
5
3
  RSpec.describe TTY::Prompt::Question, '#in' do
6
- it 'reads number within range' do
7
- prompt = TTY::TestPrompt.new
8
- prompt.input << 8
4
+
5
+ subject(:prompt) { TTY::TestPrompt.new }
6
+
7
+ it "reads range from option" do
8
+ prompt.input << '8'
9
9
  prompt.input.rewind
10
- answer = prompt.ask("How do you like it on scale 1-10", read: :int) { |q|
10
+ answer = prompt.ask("How do you like it on scale 1-10?", in: '1-10')
11
+ expect(answer).to eq('8')
12
+ end
13
+
14
+ it 'reads number within string range' do
15
+ prompt.input << '8'
16
+ prompt.input.rewind
17
+ answer = prompt.ask("How do you like it on scale 1-10?") do |q|
11
18
  q.in('1-10')
12
- }
13
- expect(answer).to eq(8)
19
+ end
20
+ expect(answer).to eq('8')
21
+ expect(prompt.output.string).to eq([
22
+ "How do you like it on scale 1-10? ",
23
+ "\e[1000D\e[K\e[1A",
24
+ "\e[1000D\e[K",
25
+ "How do you like it on scale 1-10? \e[32m8\e[0m\n",
26
+ ].join)
27
+ end
28
+
29
+ it 'reads number within digit range' do
30
+ prompt.input << '8.1'
31
+ prompt.input.rewind
32
+ answer = prompt.ask("How do you like it on scale 1-10?") do |q|
33
+ q.in(1.0..11.5)
34
+ end
35
+ expect(answer).to eq('8.1')
36
+ expect(prompt.output.string).to eq([
37
+ "How do you like it on scale 1-10? ",
38
+ "\e[1000D\e[K\e[1A",
39
+ "\e[1000D\e[K",
40
+ "How do you like it on scale 1-10? \e[32m8.1\e[0m\n",
41
+ ].join)
14
42
  end
15
43
 
16
- it "reads number outside of range" do
17
- prompt = TTY::TestPrompt.new
18
- prompt.input << 12
44
+ it 'reads letters within range' do
45
+ prompt.input << 'E'
19
46
  prompt.input.rewind
20
- expect {
21
- prompt.ask("How do you like it on scale 1-10", read: :int) { |q|
22
- q.in('1-10')
23
- }
24
- }.to raise_error(ArgumentError)
47
+ answer = prompt.ask("Your favourite vitamin? (A-K)") do |q|
48
+ q.in('A-K')
49
+ end
50
+ expect(answer).to eq('E')
51
+ expect(prompt.output.string).to eq([
52
+ "Your favourite vitamin? (A-K) ",
53
+ "\e[1000D\e[K\e[1A",
54
+ "\e[1000D\e[K",
55
+ "Your favourite vitamin? (A-K) \e[32mE\e[0m\n"
56
+ ].join)
25
57
  end
26
58
  end
@@ -1,19 +1,12 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'spec_helper'
4
-
5
3
  RSpec.describe TTY::Prompt::Question, '#initialize' do
6
- let(:message) { 'Do you like me?' }
7
4
 
8
5
  subject(:question) { described_class.new(TTY::TestPrompt.new)}
9
6
 
10
7
  it { expect(question.echo).to eq(true) }
11
8
 
12
- it { expect(question.mask).to eq(false) }
13
-
14
- it { expect(question.character).to eq(false) }
15
-
16
- it { expect(question.modifier).to be_kind_of(TTY::Prompt::Question::Modifier) }
9
+ it { expect(question.modifier).to eq([]) }
17
10
 
18
- it { expect(question.validation).to be_kind_of(TTY::Prompt::Question::Validation) }
11
+ it { expect(question.validation).to eq(TTY::Prompt::Question::UndefinedSetting) }
19
12
  end
@@ -1,31 +1,24 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'spec_helper'
4
-
5
3
  RSpec.describe TTY::Prompt::Question::Modifier, '#apply_to' do
6
- let(:instance) { described_class.new modifiers }
7
- let(:string) { "Text to be modified"}
4
+ let(:string) { "text to be modified"}
8
5
 
9
6
  it "doesn't apply modifiers" do
10
7
  modifier = described_class.new([])
11
8
  expect(modifier.apply_to(string)).to eq(string)
12
9
  end
13
10
 
14
- it 'applies letter case modifications' do
15
- modifiers = [:down, :capitalize]
16
- allow(described_class).to receive(:letter_case)
11
+ it 'combines whitespace & letter case modifications' do
12
+ modifiers = [:collapse, :capitalize]
17
13
  modifier = described_class.new(modifiers)
18
- modifier.apply_to(string)
19
- expect(described_class).to have_received(:letter_case).
20
- with(modifiers, string)
14
+ modified = modifier.apply_to(string)
15
+ expect(modified).to eq('Text to be modified')
21
16
  end
22
17
 
23
- it 'applies whitespace modifications' do
24
- modifiers = [:up, :capitalize]
25
- allow(described_class).to receive(:whitespace)
18
+ it 'combines letter case & whitespace modifications' do
19
+ modifiers = [:up, :collapse]
26
20
  modifier = described_class.new(modifiers)
27
- modifier.apply_to(string)
28
- expect(described_class).to have_received(:whitespace).
29
- with(modifiers, string)
21
+ modified = modifier.apply_to(string)
22
+ expect(modified).to eq('TEXT TO BE MODIFIED')
30
23
  end
31
24
  end
@@ -1,7 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'spec_helper'
4
-
5
3
  RSpec.describe TTY::Prompt::Question::Modifier, '#letter_case' do
6
4
  let(:string) { 'text to modify' }
7
5
 
@@ -1,33 +1,25 @@
1
1
  # encoding: utf-8
2
2
 
3
- require 'spec_helper'
4
-
5
3
  RSpec.describe TTY::Prompt::Question::Modifier, '#whitespace' do
6
4
  let(:string) { " text\t \n to\t modify\r\n" }
7
5
 
8
- subject { described_class.whitespace modifier, string}
9
-
10
- context 'when stripping whitespace' do
11
- let(:modifier) { :trim }
12
-
13
- it { is_expected.to eq("text\t \n to\t modify") }
6
+ it "trims whitespace" do
7
+ modified = described_class.whitespace(:trim, string)
8
+ expect(modified).to eq("text\t \n to\t modify")
14
9
  end
15
10
 
16
- context 'when chomping whitespace' do
17
- let(:modifier) { :chomp }
18
-
19
- it { is_expected.to eq(" text\t \n to\t modify") }
11
+ it "chomps whitespace" do
12
+ modified = described_class.whitespace(:chomp, string)
13
+ expect(modified).to eq(" text\t \n to\t modify")
20
14
  end
21
15
 
22
- context 'when capitalize' do
23
- let(:modifier) { :collapse }
24
-
25
- it { is_expected.to eq(" text to modify ") }
16
+ it "collapses text" do
17
+ modified = described_class.whitespace(:collapse, string)
18
+ expect(modified).to eq(" text to modify ")
26
19
  end
27
20
 
28
- context 'when removing whitespace' do
29
- let(:modifier) { :remove }
30
-
31
- it { is_expected.to eq("texttomodify") }
21
+ it "removes whitespace" do
22
+ modified = described_class.whitespace(:remove, string)
23
+ expect(modified).to eq("texttomodify")
32
24
  end
33
25
  end