| 1 |
# frozen_string_literal: true |
| 2 |
|
| 3 |
require_relative 'lib/common' |
| 4 |
|
| 5 |
# Non-interactive tests |
| 6 |
class TestFilter < TestBase |
| 7 |
def test_default_extended |
| 8 |
assert_equal '100', `seq 100 | #{FZF} -f "1 00$"`.chomp |
| 9 |
assert_equal '', `seq 100 | #{FZF} -f "1 00$" +x`.chomp |
| 10 |
end |
| 11 |
|
| 12 |
def test_exact |
| 13 |
assert_equal 4, `seq 123 | #{FZF} -f 13`.lines.length |
| 14 |
assert_equal 2, `seq 123 | #{FZF} -f 13 -e`.lines.length |
| 15 |
assert_equal 4, `seq 123 | #{FZF} -f 13 +e`.lines.length |
| 16 |
end |
| 17 |
|
| 18 |
def test_or_operator |
| 19 |
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 |
end |
| 23 |
|
| 24 |
def test_smart_case_for_each_term |
| 25 |
assert_equal 1, `echo Foo bar | #{FZF} -x -f "foo Fbar" | wc -l`.to_i |
| 26 |
end |
| 27 |
|
| 28 |
def test_filter_exitstatus |
| 29 |
# filter / streaming filter |
| 30 |
['', '--no-sort'].each do |opts| |
| 31 |
assert_includes `echo foo | #{FZF} -f foo #{opts}`, 'foo' |
| 32 |
assert_equal 0, $CHILD_STATUS.exitstatus |
| 33 |
|
| 34 |
assert_empty `echo foo | #{FZF} -f bar #{opts}` |
| 35 |
assert_equal 1, $CHILD_STATUS.exitstatus |
| 36 |
end |
| 37 |
end |
| 38 |
|
| 39 |
def test_long_line |
| 40 |
data = '.' * 256 * 1024 |
| 41 |
File.open(tempname, 'w') do |f| |
| 42 |
f << data |
| 43 |
end |
| 44 |
assert_equal data, `#{FZF} -f . < #{tempname}`.chomp |
| 45 |
end |
| 46 |
|
| 47 |
def test_read0 |
| 48 |
lines = `find .`.lines(chomp: true) |
| 49 |
assert_equal lines.last, `find . | #{FZF} -e -f "^#{lines.last}$"`.chomp |
| 50 |
assert_equal \ |
| 51 |
lines.last, |
| 52 |
`find . -print0 | #{FZF} --read0 -e -f "^#{lines.last}$"`.chomp |
| 53 |
end |
| 54 |
|
| 55 |
def test_nth_suffix_match |
| 56 |
assert_equal \ |
| 57 |
'foo,bar,baz', |
| 58 |
`echo foo,bar,baz | #{FZF} -d, -f'bar$' -n2`.chomp |
| 59 |
end |
| 60 |
|
| 61 |
def test_with_nth_basic |
| 62 |
writelines(['hello world ', 'byebye']) |
| 63 |
assert_equal \ |
| 64 |
'hello world ', |
| 65 |
`#{FZF} -f"^he hehe" -x -n 2.. --with-nth 2,1,1 < #{tempname}`.chomp |
| 66 |
end |
| 67 |
|
| 68 |
def test_with_nth_template |
| 69 |
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}`.chomp |
| 73 |
end |
| 74 |
|
| 75 |
def test_with_nth_ansi |
| 76 |
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}`.chomp |
| 80 |
end |
| 81 |
|
| 82 |
def test_with_nth_no_ansi |
| 83 |
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}`.chomp |
| 88 |
end |
| 89 |
|
| 90 |
def test_escaped_meta_characters |
| 91 |
input = [ |
| 92 |
'foo^bar', |
| 93 |
'foo$bar', |
| 94 |
'foo!bar', |
| 95 |
"foo'bar", |
| 96 |
'foo bar', |
| 97 |
'bar foo' |
| 98 |
] |
| 99 |
writelines(input) |
| 100 |
|
| 101 |
assert_equal input.length, `#{FZF} -f'foo bar' < #{tempname}`.lines.length |
| 102 |
assert_equal input.length - 1, `#{FZF} -f'^foo bar$' < #{tempname}`.lines.length |
| 103 |
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.length |
| 106 |
end |
| 107 |
|
| 108 |
def test_normalized_match |
| 109 |
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 |
end |
| 115 |
|
| 116 |
def test_unicode_case |
| 117 |
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 |
end |
| 121 |
|
| 122 |
def test_tiebreak |
| 123 |
input = %w[ |
| 124 |
--foobar-------- |
| 125 |
-----foobar--- |
| 126 |
----foobar-- |
| 127 |
-------foobar- |
| 128 |
] |
| 129 |
writelines(input) |
| 130 |
|
| 131 |
assert_equal input, `#{FZF} -ffoobar --tiebreak=index < #{tempname}`.lines(chomp: true) |
| 132 |
|
| 133 |
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) |
| 141 |
|
| 142 |
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) |
| 150 |
|
| 151 |
assert_equal %w[ |
| 152 |
-------foobar- |
| 153 |
----foobar-- |
| 154 |
-----foobar--- |
| 155 |
--foobar-------- |
| 156 |
], `#{FZF} -ffoobar --tiebreak end < #{tempname}`.lines(chomp: true) |
| 157 |
|
| 158 |
assert_equal input, `#{FZF} -f"!z" -x --tiebreak end < #{tempname}`.lines(chomp: true) |
| 159 |
end |
| 160 |
|
| 161 |
def test_tiebreak_index_begin |
| 162 |
writelines([ |
| 163 |
'xoxxxxxoxx', |
| 164 |
'xoxxxxxox', |
| 165 |
'xxoxxxoxx', |
| 166 |
'xxxoxoxxx', |
| 167 |
'xxxxoxox', |
| 168 |
' xxoxoxxx' |
| 169 |
]) |
| 170 |
|
| 171 |
assert_equal [ |
| 172 |
'xxxxoxox', |
| 173 |
' xxoxoxxx', |
| 174 |
'xxxoxoxxx', |
| 175 |
'xxoxxxoxx', |
| 176 |
'xoxxxxxox', |
| 177 |
'xoxxxxxoxx' |
| 178 |
], `#{FZF} -foo < #{tempname}`.lines(chomp: true) |
| 179 |
|
| 180 |
assert_equal [ |
| 181 |
'xxxoxoxxx', |
| 182 |
'xxxxoxox', |
| 183 |
' xxoxoxxx', |
| 184 |
'xxoxxxoxx', |
| 185 |
'xoxxxxxoxx', |
| 186 |
'xoxxxxxox' |
| 187 |
], `#{FZF} -foo --tiebreak=index < #{tempname}`.lines(chomp: true) |
| 188 |
|
| 189 |
# Note that --tiebreak=begin is now based on the first occurrence of the |
| 190 |
# first character on the pattern |
| 191 |
assert_equal [ |
| 192 |
' xxoxoxxx', |
| 193 |
'xxxoxoxxx', |
| 194 |
'xxxxoxox', |
| 195 |
'xxoxxxoxx', |
| 196 |
'xoxxxxxoxx', |
| 197 |
'xoxxxxxox' |
| 198 |
], `#{FZF} -foo --tiebreak=begin < #{tempname}`.lines(chomp: true) |
| 199 |
|
| 200 |
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 |
end |
| 209 |
|
| 210 |
def test_tiebreak_begin_algo_v2 |
| 211 |
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 |
end |
| 218 |
|
| 219 |
def test_tiebreak_end |
| 220 |
writelines(['xoxxxxxxxx', |
| 221 |
'xxoxxxxxxx', |
| 222 |
'xxxoxxxxxx', |
| 223 |
'xxxxoxxxx', |
| 224 |
'xxxxxoxxx', |
| 225 |
' xxxxoxxx']) |
| 226 |
|
| 227 |
assert_equal [ |
| 228 |
' xxxxoxxx', |
| 229 |
'xxxxoxxxx', |
| 230 |
'xxxxxoxxx', |
| 231 |
'xoxxxxxxxx', |
| 232 |
'xxoxxxxxxx', |
| 233 |
'xxxoxxxxxx' |
| 234 |
], `#{FZF} -fo < #{tempname}`.lines(chomp: true) |
| 235 |
|
| 236 |
assert_equal [ |
| 237 |
'xxxxxoxxx', |
| 238 |
' xxxxoxxx', |
| 239 |
'xxxxoxxxx', |
| 240 |
'xxxoxxxxxx', |
| 241 |
'xxoxxxxxxx', |
| 242 |
'xoxxxxxxxx' |
| 243 |
], `#{FZF} -fo --tiebreak=end < #{tempname}`.lines(chomp: true) |
| 244 |
|
| 245 |
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) |
| 253 |
|
| 254 |
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 |
end |
| 260 |
|
| 261 |
def test_tiebreak_length_with_nth |
| 262 |
input = %w[ |
| 263 |
1:hell |
| 264 |
123:hello |
| 265 |
12345:he |
| 266 |
1234567:h |
| 267 |
] |
| 268 |
writelines(input) |
| 269 |
|
| 270 |
output = %w[ |
| 271 |
1:hell |
| 272 |
12345:he |
| 273 |
123:hello |
| 274 |
1234567:h |
| 275 |
] |
| 276 |
assert_equal output, `#{FZF} -fh < #{tempname}`.lines(chomp: true) |
| 277 |
|
| 278 |
# Since 0.16.8, --nth doesn't affect --tiebreak |
| 279 |
assert_equal output, `#{FZF} -fh -n2 -d: < #{tempname}`.lines(chomp: true) |
| 280 |
end |
| 281 |
|
| 282 |
def test_tiebreak_chunk |
| 283 |
writelines(['1 foobarbaz ba', |
| 284 |
'2 foobar baz', |
| 285 |
'3 foo barbaz']) |
| 286 |
|
| 287 |
assert_equal [ |
| 288 |
'3 foo barbaz', |
| 289 |
'2 foobar baz', |
| 290 |
'1 foobarbaz ba' |
| 291 |
], `#{FZF} -fo --tiebreak=chunk < #{tempname}`.lines(chomp: true) |
| 292 |
|
| 293 |
assert_equal [ |
| 294 |
'1 foobarbaz ba', |
| 295 |
'2 foobar baz', |
| 296 |
'3 foo barbaz' |
| 297 |
], `#{FZF} -fba --tiebreak=chunk < #{tempname}`.lines(chomp: true) |
| 298 |
|
| 299 |
assert_equal [ |
| 300 |
'3 foo barbaz' |
| 301 |
], `#{FZF} -f'!foobar' --tiebreak=chunk < #{tempname}`.lines(chomp: true) |
| 302 |
end |
| 303 |
|
| 304 |
def test_boundary_match |
| 305 |
# Underscore boundaries should be ranked lower |
| 306 |
{ |
| 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, result |
| 313 |
end |
| 314 |
end |
| 315 |
|
| 316 |
def test_accept_nth |
| 317 |
# Single field selection |
| 318 |
assert_equal 'three', `echo 'one two three' | #{FZF} -d' ' --with-nth 1 --accept-nth -1 -f one`.chomp |
| 319 |
|
| 320 |
# Multiple field selection |
| 321 |
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}`.chomp |
| 323 |
assert_equal 'ID002:Manager', `#{FZF} -d: --with-nth 2 --accept-nth 1,3 -f Jane < #{tempname}`.chomp |
| 324 |
|
| 325 |
# Test with different delimiters |
| 326 |
writelines(['emp001 Alice Engineering', 'emp002 Bob Marketing']) |
| 327 |
assert_equal 'emp001', `#{FZF} -d' ' --with-nth 2 --accept-nth 1 -f Alice < #{tempname}`.chomp |
| 328 |
end |
| 329 |
|
| 330 |
def test_header_lines_filter |
| 331 |
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 matched |
| 336 |
assert_empty `seq 10 | #{FZF} --header-lines 3 -f "^1$"`.lines(chomp: true) |
| 337 |
end |
| 338 |
|
| 339 |
def test_header_lines_filter_with_nth |
| 340 |
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 |
end |
| 346 |
|
| 347 |
def test_header_lines_all_headers |
| 348 |
# When all lines are header lines, no results |
| 349 |
assert_empty `seq 3 | #{FZF} --header-lines 10 -f ""`.chomp |
| 350 |
assert_equal 1, $CHILD_STATUS.exitstatus |
| 351 |
end |
| 352 |
end |