forked from junegunn/fzf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_go.rb
executable file
·2374 lines (2092 loc) · 76.1 KB
/
test_go.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'minitest/autorun'
require 'fileutils'
require 'English'
require 'shellwords'
require 'erb'
require 'tempfile'
TEMPLATE = DATA.read
UNSETS = %w[
FZF_DEFAULT_COMMAND FZF_DEFAULT_OPTS
FZF_TMUX FZF_TMUX_OPTS
FZF_CTRL_T_COMMAND FZF_CTRL_T_OPTS
FZF_ALT_C_COMMAND
FZF_ALT_C_OPTS FZF_CTRL_R_OPTS
fish_history
].freeze
DEFAULT_TIMEOUT = 10
FILE = File.expand_path(__FILE__)
BASE = File.expand_path('..', __dir__)
Dir.chdir(BASE)
FZF = "FZF_DEFAULT_OPTS= FZF_DEFAULT_COMMAND= #{BASE}/bin/fzf"
def wait
since = Time.now
begin
yield or raise Minitest::Assertion, 'Assertion failure'
rescue Minitest::Assertion
raise if Time.now - since > DEFAULT_TIMEOUT
sleep(0.05)
retry
end
end
class Shell
class << self
def bash
@bash ||=
begin
bashrc = '/tmp/fzf.bash'
File.open(bashrc, 'w') do |f|
f.puts ERB.new(TEMPLATE).result(binding)
end
"bash --rcfile #{bashrc}"
end
end
def zsh
@zsh ||=
begin
zdotdir = '/tmp/fzf-zsh'
FileUtils.rm_rf(zdotdir)
FileUtils.mkdir_p(zdotdir)
File.open("#{zdotdir}/.zshrc", 'w') do |f|
f.puts ERB.new(TEMPLATE).result(binding)
end
"ZDOTDIR=#{zdotdir} zsh"
end
end
def fish
UNSETS.map { |v| v + '= ' }.join + 'fish'
end
end
end
class Tmux
attr_reader :win
def initialize(shell = :bash)
@win = go(%W[new-window -d -P -F #I #{Shell.send(shell)}]).first
go(%W[set-window-option -t #{@win} pane-base-index 0])
return unless shell == :fish
send_keys 'function fish_prompt; end; clear', :Enter
self.until(&:empty?)
end
def kill
go(%W[kill-window -t #{win}])
end
def focus
go(%W[select-window -t #{win}])
end
def send_keys(*args)
go(%W[send-keys -t #{win}] + args.map(&:to_s))
end
def paste(str)
system('tmux', 'setb', str, ';', 'pasteb', '-t', win, ';', 'send-keys', '-t', win, 'Enter')
end
def capture
go(%W[capture-pane -p -J -t #{win}]).map(&:rstrip).reverse.drop_while(&:empty?).reverse
end
def until(refresh = false)
lines = nil
begin
wait do
lines = capture
class << lines
def counts
lazy
.map { |l| l.scan(%r{^. ([0-9]+)/([0-9]+)( \(([0-9]+)\))?}) }
.reject(&:empty?)
.first&.first&.map(&:to_i)&.values_at(0, 1, 3) || [0, 0, 0]
end
def match_count
counts[0]
end
def item_count
counts[1]
end
def select_count
counts[2]
end
def any_include?(val)
method = val.is_a?(Regexp) ? :match : :include?
find { |line| line.send(method, val) }
end
end
yield(lines).tap do |ok|
send_keys 'C-l' if refresh && !ok
end
end
rescue Minitest::Assertion
puts $ERROR_INFO.backtrace
puts '>' * 80
puts lines
puts '<' * 80
raise
end
lines
end
def prepare
tries = 0
begin
self.until(true) do |lines|
message = "Prepare[#{tries}]"
send_keys ' ', 'C-u', :Enter, message, :Left, :Right
lines[-1] == message
end
rescue Minitest::Assertion
(tries += 1) < 5 ? retry : raise
end
send_keys 'C-u', 'C-l'
end
private
def go(args)
IO.popen(%w[tmux] + args) { |io| io.readlines(chomp: true) }
end
end
class TestBase < Minitest::Test
TEMPNAME = '/tmp/output'
attr_reader :tmux
def tempname
@temp_suffix ||= 0
[TEMPNAME,
caller_locations.map(&:label).find { |l| l.start_with?('test_') },
@temp_suffix].join('-')
end
def writelines(path, lines)
File.unlink(path) while File.exist?(path)
File.open(path, 'w') { |f| f.puts lines }
end
def readonce
wait { assert_path_exists tempname }
File.read(tempname)
ensure
File.unlink(tempname) while File.exist?(tempname)
@temp_suffix += 1
tmux.prepare
end
def fzf(*opts)
fzf!(*opts) + " > #{tempname}.tmp; mv #{tempname}.tmp #{tempname}"
end
def fzf!(*opts)
opts = opts.map do |o|
case o
when Symbol
o = o.to_s
o.length > 1 ? "--#{o.tr('_', '-')}" : "-#{o}"
when String, Numeric
o.to_s
end
end.compact
"#{FZF} #{opts.join(' ')}"
end
end
class TestGoFZF < TestBase
def setup
super
@tmux = Tmux.new
end
def teardown
@tmux.kill
end
def test_vanilla
tmux.send_keys "seq 1 100000 | #{fzf}", :Enter
tmux.until do |lines|
assert_equal '>', lines.last
assert_equal ' 100000/100000', lines[-2]
end
lines = tmux.capture
assert_equal ' 2', lines[-4]
assert_equal '> 1', lines[-3]
assert_equal ' 100000/100000', lines[-2]
assert_equal '>', lines[-1]
# Testing basic key bindings
tmux.send_keys '99', 'C-a', '1', 'C-f', '3', 'C-b', 'C-h', 'C-u', 'C-e', 'C-y', 'C-k', 'Tab', 'BTab'
tmux.until do |lines|
assert_equal '> 3910', lines[-4]
assert_equal ' 391', lines[-3]
assert_equal ' 856/100000', lines[-2]
assert_equal '> 391', lines[-1]
end
tmux.send_keys :Enter
assert_equal '3910', readonce.chomp
end
def test_fzf_default_command
tmux.send_keys fzf.sub('FZF_DEFAULT_COMMAND=', "FZF_DEFAULT_COMMAND='echo hello'"), :Enter
tmux.until { |lines| assert_equal '> hello', lines[-3] }
tmux.send_keys :Enter
assert_equal 'hello', readonce.chomp
end
def test_fzf_default_command_failure
tmux.send_keys fzf.sub('FZF_DEFAULT_COMMAND=', 'FZF_DEFAULT_COMMAND=false'), :Enter
tmux.until { |lines| assert_equal ' [Command failed: false]', lines[-2] }
tmux.send_keys :Enter
end
def test_key_bindings
tmux.send_keys "#{FZF} -q 'foo bar foo-bar'", :Enter
tmux.until { |lines| assert_equal '> foo bar foo-bar', lines.last }
# CTRL-A
tmux.send_keys 'C-A', '('
tmux.until { |lines| assert_equal '> (foo bar foo-bar', lines.last }
# META-F
tmux.send_keys :Escape, :f, ')'
tmux.until { |lines| assert_equal '> (foo) bar foo-bar', lines.last }
# CTRL-B
tmux.send_keys 'C-B', 'var'
tmux.until { |lines| assert_equal '> (foovar) bar foo-bar', lines.last }
# Left, CTRL-D
tmux.send_keys :Left, :Left, 'C-D'
tmux.until { |lines| assert_equal '> (foovr) bar foo-bar', lines.last }
# META-BS
tmux.send_keys :Escape, :BSpace
tmux.until { |lines| assert_equal '> (r) bar foo-bar', lines.last }
# CTRL-Y
tmux.send_keys 'C-Y', 'C-Y'
tmux.until { |lines| assert_equal '> (foovfoovr) bar foo-bar', lines.last }
# META-B
tmux.send_keys :Escape, :b, :Space, :Space
tmux.until { |lines| assert_equal '> ( foovfoovr) bar foo-bar', lines.last }
# CTRL-F / Right
tmux.send_keys 'C-F', :Right, '/'
tmux.until { |lines| assert_equal '> ( fo/ovfoovr) bar foo-bar', lines.last }
# CTRL-H / BS
tmux.send_keys 'C-H', :BSpace
tmux.until { |lines| assert_equal '> ( fovfoovr) bar foo-bar', lines.last }
# CTRL-E
tmux.send_keys 'C-E', 'baz'
tmux.until { |lines| assert_equal '> ( fovfoovr) bar foo-barbaz', lines.last }
# CTRL-U
tmux.send_keys 'C-U'
tmux.until { |lines| assert_equal '>', lines.last }
# CTRL-Y
tmux.send_keys 'C-Y'
tmux.until { |lines| assert_equal '> ( fovfoovr) bar foo-barbaz', lines.last }
# CTRL-W
tmux.send_keys 'C-W', 'bar-foo'
tmux.until { |lines| assert_equal '> ( fovfoovr) bar bar-foo', lines.last }
# META-D
tmux.send_keys :Escape, :b, :Escape, :b, :Escape, :d, 'C-A', 'C-Y'
tmux.until { |lines| assert_equal '> bar( fovfoovr) bar -foo', lines.last }
# CTRL-M
tmux.send_keys 'C-M'
tmux.until { |lines| refute_equal '>', lines.last }
end
def test_file_word
tmux.send_keys "#{FZF} -q '--/foo bar/foo-bar/baz' --filepath-word", :Enter
tmux.until { |lines| assert_equal '> --/foo bar/foo-bar/baz', lines.last }
tmux.send_keys :Escape, :b
tmux.send_keys :Escape, :b
tmux.send_keys :Escape, :b
tmux.send_keys :Escape, :d
tmux.send_keys :Escape, :f
tmux.send_keys :Escape, :BSpace
tmux.until { |lines| assert_equal '> --///baz', lines.last }
end
def test_multi_order
tmux.send_keys "seq 1 10 | #{fzf(:multi)}", :Enter
tmux.until { |lines| assert_equal '>', lines.last }
tmux.send_keys :Tab, :Up, :Up, :Tab, :Tab, :Tab, # 3, 2
'C-K', 'C-K', 'C-K', 'C-K', :BTab, :BTab, # 5, 6
:PgUp, 'C-J', :Down, :Tab, :Tab # 8, 7
tmux.until { |lines| assert_equal ' 10/10 (6)', lines[-2] }
tmux.send_keys 'C-M'
assert_equal %w[3 2 5 6 8 7], readonce.lines(chomp: true)
end
def test_multi_max
tmux.send_keys "seq 1 10 | #{FZF} -m 3 --bind A:select-all,T:toggle-all --preview 'echo [{+}]/{}'", :Enter
tmux.until { |lines| assert_equal 10, lines.item_count }
tmux.send_keys '1'
tmux.until do |lines|
assert_includes lines[1], ' [1]/1 '
assert lines[-2]&.start_with?(' 2/10 ')
end
tmux.send_keys 'A'
tmux.until do |lines|
assert_includes lines[1], ' [1 10]/1 '
assert lines[-2]&.start_with?(' 2/10 (2/3)')
end
tmux.send_keys :BSpace
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 (2/3)') }
tmux.send_keys 'T'
tmux.until do |lines|
assert_includes lines[1], ' [2 3 4]/1 '
assert lines[-2]&.start_with?(' 10/10 (3/3)')
end
%w[T A].each do |key|
tmux.send_keys key
tmux.until do |lines|
assert_includes lines[1], ' [1 5 6]/1 '
assert lines[-2]&.start_with?(' 10/10 (3/3)')
end
end
tmux.send_keys :BTab
tmux.until do |lines|
assert_includes lines[1], ' [5 6]/2 '
assert lines[-2]&.start_with?(' 10/10 (2/3)')
end
[:BTab, :BTab, 'A'].each do |key|
tmux.send_keys key
tmux.until do |lines|
assert_includes lines[1], ' [5 6 2]/3 '
assert lines[-2]&.start_with?(' 10/10 (3/3)')
end
end
tmux.send_keys '2'
tmux.until { |lines| assert lines[-2]&.start_with?(' 1/10 (3/3)') }
tmux.send_keys 'T'
tmux.until do |lines|
assert_includes lines[1], ' [5 6]/2 '
assert lines[-2]&.start_with?(' 1/10 (2/3)')
end
tmux.send_keys :BSpace
tmux.until { |lines| assert lines[-2]&.start_with?(' 10/10 (2/3)') }
tmux.send_keys 'A'
tmux.until do |lines|
assert_includes lines[1], ' [5 6 1]/1 '
assert lines[-2]&.start_with?(' 10/10 (3/3)')
end
end
def test_with_nth
[true, false].each do |multi|
tmux.send_keys "(echo ' 1st 2nd 3rd/';
echo ' first second third/') |
#{fzf(multi && :multi, :x, :nth, 2, :with_nth, '2,-1,1')}",
:Enter
tmux.until { |lines| assert_equal multi ? ' 2/2 (0)' : ' 2/2', lines[-2] }
# Transformed list
lines = tmux.capture
assert_equal ' second third/first', lines[-4]
assert_equal '> 2nd 3rd/1st', lines[-3]
# However, the output must not be transformed
if multi
tmux.send_keys :BTab, :BTab
tmux.until { |lines| assert_equal ' 2/2 (2)', lines[-2] }
tmux.send_keys :Enter
assert_equal [' 1st 2nd 3rd/', ' first second third/'], readonce.lines(chomp: true)
else
tmux.send_keys '^', '3'
tmux.until { |lines| assert_equal ' 1/2', lines[-2] }
tmux.send_keys :Enter
assert_equal [' 1st 2nd 3rd/'], readonce.lines(chomp: true)
end
end
end
def test_scroll
[true, false].each do |rev|
tmux.send_keys "seq 1 100 | #{fzf(rev && :reverse)}", :Enter
tmux.until { |lines| assert_includes lines, ' 100/100' }
tmux.send_keys(*Array.new(110) { rev ? :Down : :Up })
tmux.until { |lines| assert_includes lines, '> 100' }
tmux.send_keys :Enter
assert_equal '100', readonce.chomp
end
end
def test_select_1
tmux.send_keys "seq 1 100 | #{fzf(:with_nth, '..,..', :print_query, :q, 5555, :'1')}", :Enter
assert_equal %w[5555 55], readonce.lines(chomp: true)
end
def test_exit_0
tmux.send_keys "seq 1 100 | #{fzf(:with_nth, '..,..', :print_query, :q, 555_555, :'0')}", :Enter
assert_equal %w[555555], readonce.lines(chomp: true)
end
def test_select_1_exit_0_fail
[:'0', :'1', %i[1 0]].each do |opt|
tmux.send_keys "seq 1 100 | #{fzf(:print_query, :multi, :q, 5, *opt)}", :Enter
tmux.until { |lines| assert_equal '> 5', lines.last }
tmux.send_keys :BTab, :BTab, :BTab
tmux.until { |lines| assert_equal ' 19/100 (3)', lines[-2] }
tmux.send_keys :Enter
assert_equal %w[5 5 50 51], readonce.lines(chomp: true)
end
end
def test_query_unicode
tmux.paste "(echo abc; echo $'\\352\\260\\200\\353\\202\\230\\353\\213\\244') | #{fzf(:query, "$'\\352\\260\\200\\353\\213\\244'")}"
tmux.until { |lines| assert_equal ' 1/2', lines[-2] }
tmux.send_keys :Enter
assert_equal %w[가나다], readonce.lines(chomp: true)
end
def test_sync
tmux.send_keys "seq 1 100 | #{fzf!(:multi)} | awk '{print $1 $1}' | #{fzf(:sync)}", :Enter
tmux.until { |lines| assert_equal '>', lines[-1] }
tmux.send_keys 9
tmux.until { |lines| assert_equal ' 19/100 (0)', lines[-2] }
tmux.send_keys :BTab, :BTab, :BTab
tmux.until { |lines| assert_equal ' 19/100 (3)', lines[-2] }
tmux.send_keys :Enter
tmux.until { |lines| assert_equal '>', lines[-1] }
tmux.send_keys 'C-K', :Enter
assert_equal %w[9090], readonce.lines(chomp: true)
end
def test_tac
tmux.send_keys "seq 1 1000 | #{fzf(:tac, :multi)}", :Enter
tmux.until { |lines| assert_equal ' 1000/1000 (0)', lines[-2] }
tmux.send_keys :BTab, :BTab, :BTab
tmux.until { |lines| assert_equal ' 1000/1000 (3)', lines[-2] }
tmux.send_keys :Enter
assert_equal %w[1000 999 998], readonce.lines(chomp: true)
end
def test_tac_sort
tmux.send_keys "seq 1 1000 | #{fzf(:tac, :multi)}", :Enter
tmux.until { |lines| assert_equal ' 1000/1000 (0)', lines[-2] }
tmux.send_keys '99'
tmux.until { |lines| assert_equal ' 28/1000 (0)', lines[-2] }
tmux.send_keys :BTab, :BTab, :BTab
tmux.until { |lines| assert_equal ' 28/1000 (3)', lines[-2] }
tmux.send_keys :Enter
assert_equal %w[99 999 998], readonce.lines(chomp: true)
end
def test_tac_nosort
tmux.send_keys "seq 1 1000 | #{fzf(:tac, :no_sort, :multi)}", :Enter
tmux.until { |lines| assert_equal ' 1000/1000 (0)', lines[-2] }
tmux.send_keys '00'
tmux.until { |lines| assert_equal ' 10/1000 (0)', lines[-2] }
tmux.send_keys :BTab, :BTab, :BTab
tmux.until { |lines| assert_equal ' 10/1000 (3)', lines[-2] }
tmux.send_keys :Enter
assert_equal %w[1000 900 800], readonce.lines(chomp: true)
end
def test_expect
test = lambda do |key, feed, expected = key|
tmux.send_keys "seq 1 100 | #{fzf(:expect, key)}", :Enter
tmux.until { |lines| assert_equal ' 100/100', lines[-2] }
tmux.send_keys '55'
tmux.until { |lines| assert_equal ' 1/100', lines[-2] }
tmux.send_keys(*feed)
tmux.prepare
assert_equal [expected, '55'], readonce.lines(chomp: true)
end
test.call('ctrl-t', 'C-T')
test.call('ctrl-t', 'Enter', '')
test.call('alt-c', %i[Escape c])
test.call('f1', 'f1')
test.call('f2', 'f2')
test.call('f3', 'f3')
test.call('f2,f4', 'f2', 'f2')
test.call('f2,f4', 'f4', 'f4')
test.call('alt-/', %i[Escape /])
%w[f5 f6 f7 f8 f9 f10].each do |key|
test.call('f5,f6,f7,f8,f9,f10', key, key)
end
test.call('@', '@')
end
def test_expect_print_query
tmux.send_keys "seq 1 100 | #{fzf('--expect=alt-z', :print_query)}", :Enter
tmux.until { |lines| assert_equal ' 100/100', lines[-2] }
tmux.send_keys '55'
tmux.until { |lines| assert_equal ' 1/100', lines[-2] }
tmux.send_keys :Escape, :z
assert_equal %w[55 alt-z 55], readonce.lines(chomp: true)
end
def test_expect_printable_character_print_query
tmux.send_keys "seq 1 100 | #{fzf('--expect=z --print-query')}", :Enter
tmux.until { |lines| assert_equal ' 100/100', lines[-2] }
tmux.send_keys '55'
tmux.until { |lines| assert_equal ' 1/100', lines[-2] }
tmux.send_keys 'z'
assert_equal %w[55 z 55], readonce.lines(chomp: true)
end
def test_expect_print_query_select_1
tmux.send_keys "seq 1 100 | #{fzf('-q55 -1 --expect=alt-z --print-query')}", :Enter
assert_equal ['55', '', '55'], readonce.lines(chomp: true)
end
def test_toggle_sort
['--toggle-sort=ctrl-r', '--bind=ctrl-r:toggle-sort'].each do |opt|
tmux.send_keys "seq 1 111 | #{fzf("-m +s --tac #{opt} -q11")}", :Enter
tmux.until { |lines| assert_equal '> 111', lines[-3] }
tmux.send_keys :Tab
tmux.until { |lines| assert_equal ' 4/111 -S (1)', lines[-2] }
tmux.send_keys 'C-R'
tmux.until { |lines| assert_equal '> 11', lines[-3] }
tmux.send_keys :Tab
tmux.until { |lines| assert_equal ' 4/111 +S (2)', lines[-2] }
tmux.send_keys :Enter
assert_equal %w[111 11], readonce.lines(chomp: true)
end
end
def test_unicode_case
writelines(tempname, %w[строКА1 СТРОКА2 строка3 Строка4])
assert_equal %w[СТРОКА2 Строка4], `#{FZF} -fС < #{tempname}`.lines(chomp: true)
assert_equal %w[строКА1 СТРОКА2 строка3 Строка4], `#{FZF} -fс < #{tempname}`.lines(chomp: true)
end
def test_tiebreak
input = %w[
--foobar--------
-----foobar---
----foobar--
-------foobar-
]
writelines(tempname, input)
assert_equal input, `#{FZF} -ffoobar --tiebreak=index < #{tempname}`.lines(chomp: true)
by_length = %w[
----foobar--
-----foobar---
-------foobar-
--foobar--------
]
assert_equal by_length, `#{FZF} -ffoobar < #{tempname}`.lines(chomp: true)
assert_equal by_length, `#{FZF} -ffoobar --tiebreak=length < #{tempname}`.lines(chomp: true)
by_begin = %w[
--foobar--------
----foobar--
-----foobar---
-------foobar-
]
assert_equal by_begin, `#{FZF} -ffoobar --tiebreak=begin < #{tempname}`.lines(chomp: true)
assert_equal by_begin, `#{FZF} -f"!z foobar" -x --tiebreak begin < #{tempname}`.lines(chomp: true)
assert_equal %w[
-------foobar-
----foobar--
-----foobar---
--foobar--------
], `#{FZF} -ffoobar --tiebreak end < #{tempname}`.lines(chomp: true)
assert_equal input, `#{FZF} -f"!z" -x --tiebreak end < #{tempname}`.lines(chomp: true)
end
def test_tiebreak_index_begin
writelines(tempname, [
'xoxxxxxoxx',
'xoxxxxxox',
'xxoxxxoxx',
'xxxoxoxxx',
'xxxxoxox',
' xxoxoxxx'
])
assert_equal [
'xxxxoxox',
' xxoxoxxx',
'xxxoxoxxx',
'xxoxxxoxx',
'xoxxxxxox',
'xoxxxxxoxx'
], `#{FZF} -foo < #{tempname}`.lines(chomp: true)
assert_equal [
'xxxoxoxxx',
'xxxxoxox',
' xxoxoxxx',
'xxoxxxoxx',
'xoxxxxxoxx',
'xoxxxxxox'
], `#{FZF} -foo --tiebreak=index < #{tempname}`.lines(chomp: true)
# Note that --tiebreak=begin is now based on the first occurrence of the
# first character on the pattern
assert_equal [
' xxoxoxxx',
'xxxoxoxxx',
'xxxxoxox',
'xxoxxxoxx',
'xoxxxxxoxx',
'xoxxxxxox'
], `#{FZF} -foo --tiebreak=begin < #{tempname}`.lines(chomp: true)
assert_equal [
' xxoxoxxx',
'xxxoxoxxx',
'xxxxoxox',
'xxoxxxoxx',
'xoxxxxxox',
'xoxxxxxoxx'
], `#{FZF} -foo --tiebreak=begin,length < #{tempname}`.lines(chomp: true)
end
def test_tiebreak_begin_algo_v2
writelines(tempname, [
'baz foo bar',
'foo bar baz'
])
assert_equal [
'foo bar baz',
'baz foo bar'
], `#{FZF} -fbar --tiebreak=begin --algo=v2 < #{tempname}`.lines(chomp: true)
end
def test_tiebreak_end
writelines(tempname, [
'xoxxxxxxxx',
'xxoxxxxxxx',
'xxxoxxxxxx',
'xxxxoxxxx',
'xxxxxoxxx',
' xxxxoxxx'
])
assert_equal [
' xxxxoxxx',
'xxxxoxxxx',
'xxxxxoxxx',
'xoxxxxxxxx',
'xxoxxxxxxx',
'xxxoxxxxxx'
], `#{FZF} -fo < #{tempname}`.lines(chomp: true)
assert_equal [
'xxxxxoxxx',
' xxxxoxxx',
'xxxxoxxxx',
'xxxoxxxxxx',
'xxoxxxxxxx',
'xoxxxxxxxx'
], `#{FZF} -fo --tiebreak=end < #{tempname}`.lines(chomp: true)
assert_equal [
'xxxxxoxxx',
' xxxxoxxx',
'xxxxoxxxx',
'xxxoxxxxxx',
'xxoxxxxxxx',
'xoxxxxxxxx'
], `#{FZF} -fo --tiebreak=end,length,begin < #{tempname}`.lines(chomp: true)
end
def test_tiebreak_length_with_nth
input = %w[
1:hell
123:hello
12345:he
1234567:h
]
writelines(tempname, input)
output = %w[
1:hell
12345:he
123:hello
1234567:h
]
assert_equal output, `#{FZF} -fh < #{tempname}`.lines(chomp: true)
# Since 0.16.8, --nth doesn't affect --tiebreak
assert_equal output, `#{FZF} -fh -n2 -d: < #{tempname}`.lines(chomp: true)
end
def test_invalid_cache
tmux.send_keys "(echo d; echo D; echo x) | #{fzf('-q d')}", :Enter
tmux.until { |lines| assert_equal ' 2/3', lines[-2] }
tmux.send_keys :BSpace
tmux.until { |lines| assert_equal ' 3/3', lines[-2] }
tmux.send_keys :D
tmux.until { |lines| assert_equal ' 1/3', lines[-2] }
tmux.send_keys :Enter
end
def test_invalid_cache_query_type
command = %[(echo 'foo$bar'; echo 'barfoo'; echo 'foo^bar'; echo "foo'1-2"; seq 100) | #{fzf}]
# Suffix match
tmux.send_keys command, :Enter
tmux.until { |lines| assert_equal 104, lines.match_count }
tmux.send_keys 'foo$'
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.send_keys 'bar'
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.send_keys :Enter
# Prefix match
tmux.prepare
tmux.send_keys command, :Enter
tmux.until { |lines| assert_equal 104, lines.match_count }
tmux.send_keys '^bar'
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.send_keys 'C-a', 'foo'
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.send_keys :Enter
# Exact match
tmux.prepare
tmux.send_keys command, :Enter
tmux.until { |lines| assert_equal 104, lines.match_count }
tmux.send_keys "'12"
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.send_keys 'C-a', 'foo'
tmux.until { |lines| assert_equal 1, lines.match_count }
end
def test_smart_case_for_each_term
assert_equal 1, `echo Foo bar | #{FZF} -x -f "foo Fbar" | wc -l`.to_i
end
def test_bind
tmux.send_keys "seq 1 1000 | #{fzf('-m --bind=ctrl-j:accept,u:up,T:toggle-up,t:toggle')}", :Enter
tmux.until { |lines| assert_equal ' 1000/1000 (0)', lines[-2] }
tmux.send_keys 'uuu', 'TTT', 'tt', 'uu', 'ttt', 'C-j'
assert_equal %w[4 5 6 9], readonce.lines(chomp: true)
end
def test_bind_print_query
tmux.send_keys "seq 1 1000 | #{fzf('-m --bind=ctrl-j:print-query')}", :Enter
tmux.until { |lines| assert_equal ' 1000/1000 (0)', lines[-2] }
tmux.send_keys 'print-my-query', 'C-j'
assert_equal %w[print-my-query], readonce.lines(chomp: true)
end
def test_bind_replace_query
tmux.send_keys "seq 1 1000 | #{fzf('--print-query --bind=ctrl-j:replace-query')}", :Enter
tmux.send_keys '1'
tmux.until { |lines| assert_equal ' 272/1000', lines[-2] }
tmux.send_keys 'C-k', 'C-j'
tmux.until { |lines| assert_equal ' 29/1000', lines[-2] }
tmux.until { |lines| assert_equal '> 10', lines[-1] }
end
def test_long_line
data = '.' * 256 * 1024
File.open(tempname, 'w') do |f|
f << data
end
assert_equal data, `#{FZF} -f . < #{tempname}`.chomp
end
def test_read0
lines = `find .`.lines(chomp: true)
assert_equal lines.last, `find . | #{FZF} -e -f "^#{lines.last}$"`.chomp
assert_equal \
lines.last,
`find . -print0 | #{FZF} --read0 -e -f "^#{lines.last}$"`.chomp
end
def test_select_all_deselect_all_toggle_all
tmux.send_keys "seq 100 | #{fzf('--bind ctrl-a:select-all,ctrl-d:deselect-all,ctrl-t:toggle-all --multi')}", :Enter
tmux.until { |lines| assert_equal ' 100/100 (0)', lines[-2] }
tmux.send_keys :BTab, :BTab, :BTab
tmux.until { |lines| assert_equal ' 100/100 (3)', lines[-2] }
tmux.send_keys 'C-t'
tmux.until { |lines| assert_equal ' 100/100 (97)', lines[-2] }
tmux.send_keys 'C-a'
tmux.until { |lines| assert_equal ' 100/100 (100)', lines[-2] }
tmux.send_keys :Tab, :Tab
tmux.until { |lines| assert_equal ' 100/100 (98)', lines[-2] }
tmux.send_keys '100'
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.send_keys 'C-d'
tmux.until { |lines| assert_equal ' 1/100 (97)', lines[-2] }
tmux.send_keys 'C-u'
tmux.until { |lines| assert_equal 100, lines.match_count }
tmux.send_keys 'C-d'
tmux.until { |lines| assert_equal ' 100/100 (0)', lines[-2] }
tmux.send_keys :BTab, :BTab
tmux.until { |lines| assert_equal ' 100/100 (2)', lines[-2] }
tmux.send_keys 0
tmux.until { |lines| assert_equal ' 10/100 (2)', lines[-2] }
tmux.send_keys 'C-a'
tmux.until { |lines| assert_equal ' 10/100 (12)', lines[-2] }
tmux.send_keys :Enter
assert_equal %w[1 2 10 20 30 40 50 60 70 80 90 100],
readonce.lines(chomp: true)
end
def test_history
history_file = '/tmp/fzf-test-history'
# History with limited number of entries
begin
File.unlink(history_file)
rescue StandardError
nil
end
opts = "--history=#{history_file} --history-size=4"
input = %w[00 11 22 33 44]
input.each do |keys|
tmux.prepare
tmux.send_keys "seq 100 | #{fzf(opts)}", :Enter
tmux.until { |lines| assert_equal ' 100/100', lines[-2] }
tmux.send_keys keys
tmux.until { |lines| assert_equal ' 1/100', lines[-2] }
tmux.send_keys :Enter
end
wait do
assert_path_exists history_file
assert_equal input[1..-1], File.readlines(history_file, chomp: true)
end
# Update history entries (not changed on disk)
tmux.send_keys "seq 100 | #{fzf(opts)}", :Enter
tmux.until { |lines| assert_equal ' 100/100', lines[-2] }
tmux.send_keys 'C-p'
tmux.until { |lines| assert_equal '> 44', lines[-1] }
tmux.send_keys 'C-p'
tmux.until { |lines| assert_equal '> 33', lines[-1] }
tmux.send_keys :BSpace
tmux.until { |lines| assert_equal '> 3', lines[-1] }
tmux.send_keys 1
tmux.until { |lines| assert_equal '> 31', lines[-1] }
tmux.send_keys 'C-p'
tmux.until { |lines| assert_equal '> 22', lines[-1] }
tmux.send_keys 'C-n'
tmux.until { |lines| assert_equal '> 31', lines[-1] }
tmux.send_keys 0
tmux.until { |lines| assert_equal '> 310', lines[-1] }
tmux.send_keys :Enter
wait do
assert_path_exists history_file
assert_equal %w[22 33 44 310], File.readlines(history_file, chomp: true)
end
# Respect --bind option
tmux.send_keys "seq 100 | #{fzf(opts + ' --bind ctrl-p:next-history,ctrl-n:previous-history')}", :Enter
tmux.until { |lines| assert_equal ' 100/100', lines[-2] }
tmux.send_keys 'C-n', 'C-n', 'C-n', 'C-n', 'C-p'
tmux.until { |lines| assert_equal '> 33', lines[-1] }
tmux.send_keys :Enter
ensure
File.unlink(history_file)
end
def test_execute
output = '/tmp/fzf-test-execute'
opts = %[--bind "alt-a:execute(echo /{}/ >> #{output}),alt-b:execute[echo /{}{}/ >> #{output}],C:execute:echo /{}{}{}/ >> #{output}"]
writelines(tempname, %w[foo'bar foo"bar foo$bar])
tmux.send_keys "cat #{tempname} | #{fzf(opts)}", :Enter
tmux.until { |lines| assert_equal ' 3/3', lines[-2] }
tmux.send_keys :Escape, :a
tmux.send_keys :Escape, :a
tmux.send_keys :Up
tmux.send_keys :Escape, :b
tmux.send_keys :Escape, :b
tmux.send_keys :Up
tmux.send_keys :C
tmux.send_keys 'barfoo'
tmux.until { |lines| assert_equal ' 0/3', lines[-2] }
tmux.send_keys :Escape, :a
tmux.send_keys :Escape, :b
wait do
assert_path_exists output
assert_equal %w[
/foo'bar/ /foo'bar/
/foo"barfoo"bar/ /foo"barfoo"bar/
/foo$barfoo$barfoo$bar/
], File.readlines(output, chomp: true)
end
ensure
begin
File.unlink(output)
rescue StandardError
nil
end
end
def test_execute_multi
output = '/tmp/fzf-test-execute-multi'
opts = %[--multi --bind "alt-a:execute-multi(echo {}/{+} >> #{output})"]
writelines(tempname, %w[foo'bar foo"bar foo$bar foobar])
tmux.send_keys "cat #{tempname} | #{fzf(opts)}", :Enter
tmux.until { |lines| assert_equal ' 4/4 (0)', lines[-2] }
tmux.send_keys :Escape, :a
tmux.send_keys :BTab, :BTab, :BTab
tmux.until { |lines| assert_equal ' 4/4 (3)', lines[-2] }
tmux.send_keys :Escape, :a
tmux.send_keys :Tab, :Tab
tmux.until { |lines| assert_equal ' 4/4 (3)', lines[-2] }
tmux.send_keys :Escape, :a
wait do
assert_path_exists output
assert_equal [
%(foo'bar/foo'bar),
%(foo'bar foo"bar foo$bar/foo'bar foo"bar foo$bar),
%(foo'bar foo"bar foobar/foo'bar foo"bar foobar)
], File.readlines(output, chomp: true)
end
ensure
begin
File.unlink(output)
rescue StandardError
nil
end
end
def test_execute_plus_flag
output = tempname + '.tmp'
begin
File.unlink(output)
rescue StandardError
nil
end
writelines(tempname, ['foo bar', '123 456'])
tmux.send_keys "cat #{tempname} | #{FZF} --multi --bind 'x:execute-silent(echo {+}/{}/{+2}/{2} >> #{output})'", :Enter