test_filter.rb
This file
Path
test/test_filter.rb
352 lines · 10.3 KiB · Ruby
Last changed by
Not known.
History of this fileTake it
1# frozen_string_literal: true23require_relative 'lib/common'45# Non-interactive tests6class TestFilter < TestBase7 def test_default_extended8 assert_equal '100', `seq 100 | #{FZF} -f "1 00$"`.chomp9 assert_equal '', `seq 100 | #{FZF} -f "1 00$" +x`.chomp10 end1112 def test_exact13 assert_equal 4, `seq 123 | #{FZF} -f 13`.lines.length14 assert_equal 2, `seq 123 | #{FZF} -f 13 -e`.lines.length15 assert_equal 4, `seq 123 | #{FZF} -f 13 +e`.lines.length16 end1718 def test_or_operator19 assert_equal %w[1 5 10], `seq 10 | #{FZF} -f "1 | 5"`.lines(chomp: true)20 assert_equal %w[1 10 2 3 4 5 6 7 8 9],21 `seq 10 | #{FZF} -f '1 | !1'`.lines(chomp: true)22 end2324 def test_smart_case_for_each_term25 assert_equal 1, `echo Foo bar | #{FZF} -x -f "foo Fbar" | wc -l`.to_i26 end2728 def test_filter_exitstatus29 # filter / streaming filter30 ['', '--no-sort'].each do |opts|31 assert_includes `echo foo | #{FZF} -f foo #{opts}`, 'foo'32 assert_equal 0, $CHILD_STATUS.exitstatus3334 assert_empty `echo foo | #{FZF} -f bar #{opts}`35 assert_equal 1, $CHILD_STATUS.exitstatus36 end37 end3839 def test_long_line40 data = '.' * 256 * 102441 File.open(tempname, 'w') do |f|42 f << data43 end44 assert_equal data, `#{FZF} -f . < #{tempname}`.chomp45 end4647 def test_read048 lines = `find .`.lines(chomp: true)49 assert_equal lines.last, `find . | #{FZF} -e -f "^#{lines.last}$"`.chomp50 assert_equal \51 lines.last,52 `find . -print0 | #{FZF} --read0 -e -f "^#{lines.last}$"`.chomp53 end5455 def test_nth_suffix_match56 assert_equal \57 'foo,bar,baz',58 `echo foo,bar,baz | #{FZF} -d, -f'bar$' -n2`.chomp59 end6061 def test_with_nth_basic62 writelines(['hello world ', 'byebye'])63 assert_equal \64 'hello world ',65 `#{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1 < #{tempname}`.chomp66 end6768 def test_with_nth_template69 writelines(['hello world ', 'byebye'])70 assert_equal \71 'hello world ',72 `#{FZF} -f"^he he.he." -x -n 2.. --with-nth '{2} {1}. {1}.' < #{tempname}`.chomp73 end7475 def test_with_nth_ansi76 writelines(["\x1b[33mhello \x1b[34;1mworld\x1b[m ", 'byebye'])77 assert_equal \78 'hello world ',79 `#{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1 --ansi < #{tempname}`.chomp80 end8182 def test_with_nth_no_ansi83 src = "\x1b[33mhello \x1b[34;1mworld\x1b[m "84 writelines([src, 'byebye'])85 assert_equal \86 src,87 `#{FZF} -fhehe -x -n 2.. --with-nth 2,1,1 --no-ansi < #{tempname}`.chomp88 end8990 def test_escaped_meta_characters91 input = [92 'foo^bar',93 'foo$bar',94 'foo!bar',95 "foo'bar",96 'foo bar',97 'bar foo'98 ]99 writelines(input)100101 assert_equal input.length, `#{FZF} -f'foo bar' < #{tempname}`.lines.length102 assert_equal input.length - 1, `#{FZF} -f'^foo bar$' < #{tempname}`.lines.length103 assert_equal ['foo bar'], `#{FZF} -f'foo\\ bar' < #{tempname}`.lines(chomp: true)104 assert_equal ['foo bar'], `#{FZF} -f'^foo\\ bar$' < #{tempname}`.lines(chomp: true)105 assert_equal input.length - 1, `#{FZF} -f'!^foo\\ bar$' < #{tempname}`.lines.length106 end107108 def test_normalized_match109 echoes = '(echo a; echo á; echo A; echo Á;)'110 assert_equal %w[a á A Á], `#{echoes} | #{FZF} -f a`.lines.map(&:chomp)111 assert_equal %w[á Á], `#{echoes} | #{FZF} -f á`.lines.map(&:chomp)112 assert_equal %w[A Á], `#{echoes} | #{FZF} -f A`.lines.map(&:chomp)113 assert_equal %w[Á], `#{echoes} | #{FZF} -f Á`.lines.map(&:chomp)114 end115116 def test_unicode_case117 writelines(%w[строКА1 СТРОКА2 строка3 Строка4])118 assert_equal %w[СТРОКА2 Строка4], `#{FZF} -fС < #{tempname}`.lines(chomp: true)119 assert_equal %w[строКА1 СТРОКА2 строка3 Строка4], `#{FZF} -fс < #{tempname}`.lines(chomp: true)120 end121122 def test_tiebreak123 input = %w[124 --foobar--------125 -----foobar---126 ----foobar--127 -------foobar-128 ]129 writelines(input)130131 assert_equal input, `#{FZF} -ffoobar --tiebreak=index < #{tempname}`.lines(chomp: true)132133 by_length = %w[134 ----foobar--135 -----foobar---136 -------foobar-137 --foobar--------138 ]139 assert_equal by_length, `#{FZF} -ffoobar < #{tempname}`.lines(chomp: true)140 assert_equal by_length, `#{FZF} -ffoobar --tiebreak=length < #{tempname}`.lines(chomp: true)141142 by_begin = %w[143 --foobar--------144 ----foobar--145 -----foobar---146 -------foobar-147 ]148 assert_equal by_begin, `#{FZF} -ffoobar --tiebreak=begin < #{tempname}`.lines(chomp: true)149 assert_equal by_begin, `#{FZF} -f"!z foobar" -x --tiebreak begin < #{tempname}`.lines(chomp: true)150151 assert_equal %w[152 -------foobar-153 ----foobar--154 -----foobar---155 --foobar--------156 ], `#{FZF} -ffoobar --tiebreak end < #{tempname}`.lines(chomp: true)157158 assert_equal input, `#{FZF} -f"!z" -x --tiebreak end < #{tempname}`.lines(chomp: true)159 end160161 def test_tiebreak_index_begin162 writelines([163 'xoxxxxxoxx',164 'xoxxxxxox',165 'xxoxxxoxx',166 'xxxoxoxxx',167 'xxxxoxox',168 ' xxoxoxxx'169 ])170171 assert_equal [172 'xxxxoxox',173 ' xxoxoxxx',174 'xxxoxoxxx',175 'xxoxxxoxx',176 'xoxxxxxox',177 'xoxxxxxoxx'178 ], `#{FZF} -foo < #{tempname}`.lines(chomp: true)179180 assert_equal [181 'xxxoxoxxx',182 'xxxxoxox',183 ' xxoxoxxx',184 'xxoxxxoxx',185 'xoxxxxxoxx',186 'xoxxxxxox'187 ], `#{FZF} -foo --tiebreak=index < #{tempname}`.lines(chomp: true)188189 # Note that --tiebreak=begin is now based on the first occurrence of the190 # first character on the pattern191 assert_equal [192 ' xxoxoxxx',193 'xxxoxoxxx',194 'xxxxoxox',195 'xxoxxxoxx',196 'xoxxxxxoxx',197 'xoxxxxxox'198 ], `#{FZF} -foo --tiebreak=begin < #{tempname}`.lines(chomp: true)199200 assert_equal [201 ' xxoxoxxx',202 'xxxoxoxxx',203 'xxxxoxox',204 'xxoxxxoxx',205 'xoxxxxxox',206 'xoxxxxxoxx'207 ], `#{FZF} -foo --tiebreak=begin,length < #{tempname}`.lines(chomp: true)208 end209210 def test_tiebreak_begin_algo_v2211 writelines(['baz foo bar',212 'foo bar baz'])213 assert_equal [214 'foo bar baz',215 'baz foo bar'216 ], `#{FZF} -fbar --tiebreak=begin --algo=v2 < #{tempname}`.lines(chomp: true)217 end218219 def test_tiebreak_end220 writelines(['xoxxxxxxxx',221 'xxoxxxxxxx',222 'xxxoxxxxxx',223 'xxxxoxxxx',224 'xxxxxoxxx',225 ' xxxxoxxx'])226227 assert_equal [228 ' xxxxoxxx',229 'xxxxoxxxx',230 'xxxxxoxxx',231 'xoxxxxxxxx',232 'xxoxxxxxxx',233 'xxxoxxxxxx'234 ], `#{FZF} -fo < #{tempname}`.lines(chomp: true)235236 assert_equal [237 'xxxxxoxxx',238 ' xxxxoxxx',239 'xxxxoxxxx',240 'xxxoxxxxxx',241 'xxoxxxxxxx',242 'xoxxxxxxxx'243 ], `#{FZF} -fo --tiebreak=end < #{tempname}`.lines(chomp: true)244245 assert_equal [246 'xxxxxoxxx',247 ' xxxxoxxx',248 'xxxxoxxxx',249 'xxxoxxxxxx',250 'xxoxxxxxxx',251 'xoxxxxxxxx'252 ], `#{FZF} -fo --tiebreak=end,length,begin < #{tempname}`.lines(chomp: true)253254 writelines(['/bar/baz', '/foo/bar/baz'])255 assert_equal [256 '/foo/bar/baz',257 '/bar/baz'258 ], `#{FZF} -fbaz --tiebreak=end < #{tempname}`.lines(chomp: true)259 end260261 def test_tiebreak_length_with_nth262 input = %w[263 1:hell264 123:hello265 12345:he266 1234567:h267 ]268 writelines(input)269270 output = %w[271 1:hell272 12345:he273 123:hello274 1234567:h275 ]276 assert_equal output, `#{FZF} -fh < #{tempname}`.lines(chomp: true)277278 # Since 0.16.8, --nth doesn't affect --tiebreak279 assert_equal output, `#{FZF} -fh -n2 -d: < #{tempname}`.lines(chomp: true)280 end281282 def test_tiebreak_chunk283 writelines(['1 foobarbaz ba',284 '2 foobar baz',285 '3 foo barbaz'])286287 assert_equal [288 '3 foo barbaz',289 '2 foobar baz',290 '1 foobarbaz ba'291 ], `#{FZF} -fo --tiebreak=chunk < #{tempname}`.lines(chomp: true)292293 assert_equal [294 '1 foobarbaz ba',295 '2 foobar baz',296 '3 foo barbaz'297 ], `#{FZF} -fba --tiebreak=chunk < #{tempname}`.lines(chomp: true)298299 assert_equal [300 '3 foo barbaz'301 ], `#{FZF} -f'!foobar' --tiebreak=chunk < #{tempname}`.lines(chomp: true)302 end303304 def test_boundary_match305 # Underscore boundaries should be ranked lower306 {307 default: [' xyz '] + %w[/xyz/ [xyz] -xyz- -xyz_ _xyz- _xyz_],308 path: ['/xyz/', ' xyz '] + %w[[xyz] -xyz- -xyz_ _xyz- _xyz_],309 history: ['[xyz]', '-xyz-', ' xyz '] + %w[/xyz/ -xyz_ _xyz- _xyz_]310 }.each do |scheme, expected|311 result = `printf -- 'xxyzx\n-xxyz\nxyzx-\n_xyz_\n_xyz-\n-xyz_\n[xyz]\n-xyz-\n xyz \n/xyz/\n' | #{FZF} -f"'xyz'" --scheme=#{scheme}`.lines(chomp: true)312 assert_equal expected, result313 end314 end315316 def test_accept_nth317 # Single field selection318 assert_equal 'three', `echo 'one two three' | #{FZF} -d' ' --with-nth 1 --accept-nth -1 -f one`.chomp319320 # Multiple field selection321 writelines(['ID001:John:Developer', 'ID002:Jane:Manager', 'ID003:Bob:Designer'])322 assert_equal 'ID001', `#{FZF} -d: --with-nth 2 --accept-nth 1 -f John < #{tempname}`.chomp323 assert_equal 'ID002:Manager', `#{FZF} -d: --with-nth 2 --accept-nth 1,3 -f Jane < #{tempname}`.chomp324325 # Test with different delimiters326 writelines(['emp001 Alice Engineering', 'emp002 Bob Marketing'])327 assert_equal 'emp001', `#{FZF} -d' ' --with-nth 2 --accept-nth 1 -f Alice < #{tempname}`.chomp328 end329330 def test_header_lines_filter331 assert_equal %w[4 5 6 7 8 9 10],332 `seq 10 | #{FZF} --header-lines 3 -f ""`.lines(chomp: true)333 assert_equal %w[5],334 `seq 10 | #{FZF} --header-lines 3 -f 5`.lines(chomp: true)335 # Header items should not be matched336 assert_empty `seq 10 | #{FZF} --header-lines 3 -f "^1$"`.lines(chomp: true)337 end338339 def test_header_lines_filter_with_nth340 writelines(%w[a:1 b:2 c:3 d:4 e:5])341 assert_equal %w[c:3 d:4 e:5],342 `#{FZF} --header-lines 2 -d: --with-nth 2 -f "" < #{tempname}`.lines(chomp: true)343 assert_equal %w[d:4],344 `#{FZF} --header-lines 2 -d: --with-nth 2 -f 4 < #{tempname}`.lines(chomp: true)345 end346347 def test_header_lines_all_headers348 # When all lines are header lines, no results349 assert_empty `seq 3 | #{FZF} --header-lines 10 -f ""`.chomp350 assert_equal 1, $CHILD_STATUS.exitstatus351 end352end