Browse code

Reduce boilerplate in acceptance test

John Hawthorn authored on 09/09/2018 08:04:57
Showing 1 changed files

... ...
@@ -6,7 +6,7 @@ class FzyTest < Minitest::Test
6 6
   FZY_PATH = File.expand_path('../../../fzy', __FILE__)
7 7
 
8 8
   def test_empty_list
9
-    @tty = TTYtest.new_terminal(%{echo placeholder;echo -n "" | #{FZY_PATH}})
9
+    @tty = interactive_fzy(input: %w[], before: "placeholder")
10 10
     @tty.assert_cursor_position(y: 1, x: 2)
11 11
     @tty.assert_matches <<~TTY
12 12
       placeholder
... ...
@@ -36,7 +36,7 @@ class FzyTest < Minitest::Test
36 36
   end
37 37
 
38 38
   def test_one_item
39
-    @tty = TTYtest.new_terminal(%{echo placeholder;echo -n "test" | #{FZY_PATH}})
39
+    @tty = interactive_fzy(input: %w[test], before: "placeholder")
40 40
     @tty.assert_matches <<~TTY
41 41
       placeholder
42 42
       >
... ...
@@ -68,7 +68,7 @@ class FzyTest < Minitest::Test
68 68
   end
69 69
 
70 70
   def test_two_items
71
-    @tty = TTYtest.new_terminal(%{echo placeholder;echo -n "test\nfoo" | #{FZY_PATH}})
71
+    @tty = interactive_fzy(input: %w[test foo], before: "placeholder")
72 72
     @tty.assert_cursor_position(y: 1, x: 2)
73 73
     @tty.assert_matches <<~TTY
74 74
       placeholder
... ...
@@ -105,7 +105,7 @@ class FzyTest < Minitest::Test
105 105
   end
106 106
 
107 107
   def test_editing
108
-    @tty = TTYtest.new_terminal(%{echo placeholder;echo -n "test\nfoo" | #{FZY_PATH}})
108
+    @tty = interactive_fzy(input: %w[test foo], before: "placeholder")
109 109
     @tty.assert_cursor_position(y: 1, x: 2)
110 110
     @tty.assert_matches <<~TTY
111 111
       placeholder
... ...
@@ -146,7 +146,7 @@ class FzyTest < Minitest::Test
146 146
   end
147 147
 
148 148
   def test_ctrl_d
149
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}})
149
+    @tty = interactive_fzy(input: %w[foo bar])
150 150
     @tty.assert_matches ">\nfoo\nbar"
151 151
 
152 152
     @tty.send_keys('foo')
... ...
@@ -158,7 +158,7 @@ class FzyTest < Minitest::Test
158 158
   end
159 159
 
160 160
   def test_ctrl_c
161
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}})
161
+    @tty = interactive_fzy(input: %w[foo bar])
162 162
     @tty.assert_matches ">\nfoo\nbar"
163 163
 
164 164
     @tty.send_keys('foo')
... ...
@@ -170,25 +170,25 @@ class FzyTest < Minitest::Test
170 170
   end
171 171
 
172 172
   def test_down_arrow
173
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}})
173
+    @tty = interactive_fzy(input: %w[foo bar])
174 174
     @tty.assert_matches ">\nfoo\nbar"
175 175
     @tty.send_keys("\e[A\r")
176 176
     @tty.assert_matches "bar"
177 177
 
178
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}})
178
+    @tty = interactive_fzy(input: %w[foo bar])
179 179
     @tty.assert_matches ">\nfoo\nbar"
180 180
     @tty.send_keys("\eOA\r")
181 181
     @tty.assert_matches "bar"
182 182
   end
183 183
 
184 184
   def test_up_arrow
185
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}})
185
+    @tty = interactive_fzy(input: %w[foo bar])
186 186
     @tty.assert_matches ">\nfoo\nbar"
187 187
     @tty.send_keys("\e[A")   # first down
188 188
     @tty.send_keys("\e[B\r") # and back up
189 189
     @tty.assert_matches "foo"
190 190
 
191
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}})
191
+    @tty = interactive_fzy(input: %w[foo bar])
192 192
     @tty.assert_matches ">\nfoo\nbar"
193 193
     @tty.send_keys("\eOA")   # first down
194 194
     @tty.send_keys("\e[B\r") # and back up
... ...
@@ -196,45 +196,48 @@ class FzyTest < Minitest::Test
196 196
   end
197 197
 
198 198
   def test_lines
199
-    @tty = TTYtest.new_terminal(%{seq 10 | #{FZY_PATH}})
199
+    input10 = (1..10).map(&:to_s)
200
+    input20 = (1..20).map(&:to_s)
201
+
202
+    @tty = interactive_fzy(input: input10)
200 203
     @tty.assert_matches ">\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10"
201 204
 
202
-    @tty = TTYtest.new_terminal(%{seq 20 | #{FZY_PATH}})
205
+    @tty = interactive_fzy(input: input20)
203 206
     @tty.assert_matches ">\n1\n2\n3\n4\n5\n6\n7\n8\n9\n10"
204 207
 
205
-    @tty = TTYtest.new_terminal(%{seq 10 | #{FZY_PATH} -l 5})
208
+    @tty = interactive_fzy(input: input10, args: "-l 5")
206 209
     @tty.assert_matches ">\n1\n2\n3\n4\n5"
207 210
 
208
-    @tty = TTYtest.new_terminal(%{seq 10 | #{FZY_PATH} --lines=5})
211
+    @tty = interactive_fzy(input: input10, args: "--lines=5")
209 212
     @tty.assert_matches ">\n1\n2\n3\n4\n5"
210 213
   end
211 214
 
212 215
   def test_prompt
213
-    @tty = TTYtest.new_terminal(%{echo -n "" | #{FZY_PATH}})
216
+    @tty = interactive_fzy
214 217
     @tty.send_keys("foo")
215 218
     @tty.assert_matches '> foo'
216 219
 
217
-    @tty = TTYtest.new_terminal(%{echo -n "" | #{FZY_PATH} -p 'C:\\'})
220
+    @tty = interactive_fzy(args: "-p 'C:\\'")
218 221
     @tty.send_keys("foo")
219 222
     @tty.assert_matches 'C:\foo'
220 223
 
221
-    @tty = TTYtest.new_terminal(%{echo -n "" | #{FZY_PATH} --prompt="foo bar "})
224
+    @tty = interactive_fzy(args: "--prompt=\"foo bar \"")
222 225
     @tty.send_keys("baz")
223 226
     @tty.assert_matches "foo bar baz"
224 227
   end
225 228
 
226 229
   def test_show_scores
227 230
     expected_score = '(  inf)'
228
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} -s})
231
+    @tty = interactive_fzy(input: %w[foo bar], args: "-s")
229 232
     @tty.send_keys('foo')
230 233
     @tty.assert_matches "> foo\n#{expected_score} foo"
231 234
 
232
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} --show-scores})
235
+    @tty = interactive_fzy(input: %w[foo bar], args: "--show-scores")
233 236
     @tty.send_keys('foo')
234 237
     @tty.assert_matches "> foo\n#{expected_score} foo"
235 238
 
236 239
     expected_score = '( 0.89)'
237
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} -s})
240
+    @tty = interactive_fzy(input: %w[foo bar], args: "-s")
238 241
     @tty.send_keys('f')
239 242
     @tty.assert_matches "> f\n#{expected_score} foo"
240 243
   end
... ...
@@ -252,7 +255,7 @@ class FzyTest < Minitest::Test
252 255
   end
253 256
 
254 257
   def test_worker_count
255
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} -j1})
258
+    @tty = interactive_fzy(input: %w[foo bar], args: "-j1")
256 259
     @tty.send_keys('foo')
257 260
     @tty.assert_matches "> foo\nfoo"
258 261
 
... ...
@@ -266,24 +269,24 @@ class FzyTest < Minitest::Test
266 269
   end
267 270
 
268 271
   def test_initial_query
269
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} -q fo})
272
+    @tty = interactive_fzy(input: %w[foo bar], args: "-q fo")
270 273
     @tty.assert_matches "> fo\nfoo"
271 274
     @tty.send_keys("o")
272 275
     @tty.assert_matches "> foo\nfoo"
273 276
     @tty.send_keys("o")
274 277
     @tty.assert_matches "> fooo"
275 278
 
276
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH} -q asdf})
279
+    @tty = interactive_fzy(input: %w[foo bar], args: "-q asdf")
277 280
     @tty.assert_matches "> asdf"
278 281
   end
279 282
 
280 283
   def test_non_interactive
281
-    @tty = TTYtest.new_terminal(%{echo before; echo -n "foo\nbar" | #{FZY_PATH} -e foo; echo after})
284
+    @tty = interactive_fzy(input: %w[foo bar], args: "-e foo", before: "before", after: "after")
282 285
     @tty.assert_matches "before\nfoo\nafter"
283 286
   end
284 287
 
285 288
   def test_moving_text_cursor
286
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}; echo after})
289
+    @tty = interactive_fzy(input: %w[foo bar])
287 290
     @tty.send_keys("br")
288 291
     @tty.assert_matches "> br\nbar"
289 292
     @tty.assert_cursor_position(y: 0, x: 4)
... ...
@@ -314,7 +317,7 @@ class FzyTest < Minitest::Test
314 317
   # https://github.com/jhawthorn/fzy/issues/42
315 318
   # https://cirw.in/blog/bracketed-paste
316 319
   def test_bracketed_paste_characters
317
-    @tty = TTYtest.new_terminal(%{echo -n "foo\nbar" | #{FZY_PATH}})
320
+    @tty = interactive_fzy(input: %w[foo bar])
318 321
     @tty.assert_matches ">\nfoo\nbar"
319 322
     @tty.send_keys("\e[200~foo\e[201~")
320 323
     @tty.assert_matches "> foo\nfoo"
... ...
@@ -342,4 +345,15 @@ Usage: fzy [OPTION]...
342 345
  -v, --version  Output version information and exit
343 346
 TTY
344 347
   end
348
+
349
+  private
350
+
351
+  def interactive_fzy(input: [], before: nil, after: nil, args: "")
352
+    cmd = []
353
+    cmd << %{echo "#{before}"} if before
354
+    cmd << %{echo -n "#{input.join("\\n")}" | #{FZY_PATH} #{args}}
355
+    cmd << %{echo "#{after}"} if after
356
+    cmd = cmd.join("; ")
357
+    TTYtest.new_terminal(cmd)
358
+  end
345 359
 end