Skip to content
Myr

Merge pull request #117 from junegunn/fix-ctrl-y

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Fix CTRL-Y key binding

authored by Junegunn Choi · committed by Junegunn Choi · parents 62acb9a, 1990f3c · Browse files · 7 files+1072 added−862 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

7 files+1072 added−862 removed

  • tests:+1043 added−850 removed
  • other:+29 added−12 removed

7 files+1072 added−862 removed

modified:src/Makefile+2 added-2 removed
20 unmodified lines
21VERSION = $(shell fzf/$(BINARY64) --version)22RELEASE32 = fzf-$(VERSION)-$(GOOS)_38623RELEASE64 = fzf-$(VERSION)-$(GOOS)_amd642425all: test release25all: release2627release: build28 cd fzf && \29 cp $(BINARY32) $(RELEASE32) && tar -czf $(RELEASE32).tgz $(RELEASE32) && \30 cp $(BINARY64) $(RELEASE64) && tar -czf $(RELEASE64).tgz $(RELEASE64) && \31 rm $(RELEASE32) $(RELEASE64)3233build: fzf/$(BINARY32) fzf/$(BINARY64)33build: test fzf/$(BINARY32) fzf/$(BINARY64)3435test:36 go get37 go test -v ./...
37 unmodified lines
modified:src/terminal.go+2 added-1 removed
513 unmodified lines
514 if t.cx > 0 {515 t.rubout("[^[:alnum:]][[:alnum:]]")516 }517 case C.CtrlY:518 suffix := copySlice(t.input[t.cx:])518 t.input = append(append(t.input[:t.cx], t.yanked...), t.input[t.cx:]...)519 t.input = append(append(t.input[:t.cx], t.yanked...), suffix...)520 t.cx += len(t.yanked)521 case C.Del:522 t.delChar()523 case C.PgUp:
105 unmodified lines
deleted:test/test_fzf.rb-850 removed
1#!/usr/bin/env ruby2# encoding: utf-834require 'rubygems'5require 'curses'6require 'timeout'7require 'stringio'8require 'minitest/autorun'9require 'tempfile'10$LOAD_PATH.unshift File.expand_path('../..', __FILE__)11ENV['FZF_EXECUTABLE'] = '0'12load 'fzf'1314class MockTTY15 def initialize16 @buffer = ''17 @mutex = Mutex.new18 @condv = ConditionVariable.new19 end2021 def read_nonblock sz22 @mutex.synchronize do23 take sz24 end25 end2627 def take sz28 if @buffer.length >= sz29 ret = @buffer[0, sz]30 @buffer = @buffer[sz..-1]31 ret32 end33 end3435 def getc36 sleep 0.137 while true38 @mutex.synchronize do39 if char = take(1)40 return char41 else42 @condv.wait(@mutex)43 end44 end45 end46 end4748 def << str49 @mutex.synchronize do50 @buffer << str51 @condv.broadcast52 end53 self54 end55end5657class TestFZF < MiniTest::Unit::TestCase58 def setup59 ENV.delete 'FZF_DEFAULT_SORT'60 ENV.delete 'FZF_DEFAULT_OPTS'61 ENV.delete 'FZF_DEFAULT_COMMAND'62 end6364 def test_default_options65 fzf = FZF.new []66 assert_equal 1000, fzf.sort67 assert_equal false, fzf.multi68 assert_equal true, fzf.color69 assert_equal nil, fzf.rxflag70 assert_equal true, fzf.mouse71 assert_equal nil, fzf.nth72 assert_equal nil, fzf.with_nth73 assert_equal true, fzf.color74 assert_equal false, fzf.black75 assert_equal true, fzf.ansi25676 assert_equal '', fzf.query77 assert_equal false, fzf.select178 assert_equal false, fzf.exit079 assert_equal nil, fzf.filter80 assert_equal nil, fzf.extended81 assert_equal false, fzf.reverse82 assert_equal '> ', fzf.prompt83 assert_equal false, fzf.print_query84 end8586 def test_environment_variables87 # Deprecated88 ENV['FZF_DEFAULT_SORT'] = '20000'89 fzf = FZF.new []90 assert_equal 20000, fzf.sort91 assert_equal nil, fzf.nth9293 ENV['FZF_DEFAULT_OPTS'] =94 '-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' <<95 '--no-mouse -f "goodbye world" --black --with-nth=3,-3..,2 --nth=3,-1,2 --reverse --print-query'96 fzf = FZF.new []97 assert_equal 10000, fzf.sort98 assert_equal ' hello world ',99 fzf.query100 assert_equal 'goodbye world',101 fzf.filter102 assert_equal :fuzzy, fzf.extended103 assert_equal true, fzf.multi104 assert_equal false, fzf.color105 assert_equal false, fzf.ansi256106 assert_equal true, fzf.black107 assert_equal false, fzf.mouse108 assert_equal true, fzf.select1109 assert_equal true, fzf.exit0110 assert_equal true, fzf.reverse111 assert_equal true, fzf.print_query112 assert_equal [2..2, -1..-1, 1..1], fzf.nth113 assert_equal [2..2, -3..-1, 1..1], fzf.with_nth114 end115116 def test_option_parser117 # Long opts118 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1119 --exit-0 --filter=howdy --extended-exact120 --no-mouse --no-256 --nth=1 --with-nth=.. --reverse --prompt (hi)121 --print-query]122 assert_equal 2000, fzf.sort123 assert_equal true, fzf.multi124 assert_equal false, fzf.color125 assert_equal false, fzf.ansi256126 assert_equal false, fzf.black127 assert_equal false, fzf.mouse128 assert_equal 0, fzf.rxflag129 assert_equal 'hello', fzf.query130 assert_equal true, fzf.select1131 assert_equal true, fzf.exit0132 assert_equal 'howdy', fzf.filter133 assert_equal :exact, fzf.extended134 assert_equal [0..0], fzf.nth135 assert_equal nil, fzf.with_nth136 assert_equal true, fzf.reverse137 assert_equal '(hi)', fzf.prompt138 assert_equal true, fzf.print_query139140 # Long opts (left-to-right)141 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello142 --filter a --filter b --no-256 --black --nth -1 --nth -2143 --select-1 --exit-0 --no-select-1 --no-exit-0144 --no-sort -i --color --no-multi --256145 --reverse --no-reverse --prompt (hi) --prompt=(HI)146 --print-query --no-print-query]147 assert_equal nil, fzf.sort148 assert_equal false, fzf.multi149 assert_equal true, fzf.color150 assert_equal true, fzf.ansi256151 assert_equal true, fzf.black152 assert_equal true, fzf.mouse153 assert_equal 1, fzf.rxflag154 assert_equal 'b', fzf.filter155 assert_equal 'hello', fzf.query156 assert_equal false, fzf.select1157 assert_equal false, fzf.exit0158 assert_equal nil, fzf.extended159 assert_equal [-2..-2], fzf.nth160 assert_equal false, fzf.reverse161 assert_equal '(HI)', fzf.prompt162 assert_equal false, fzf.print_query163164 # Short opts165 fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]166 assert_equal 2000, fzf.sort167 assert_equal true, fzf.multi168 assert_equal false, fzf.color169 assert_equal false, fzf.ansi256170 assert_equal 0, fzf.rxflag171 assert_equal 'hello', fzf.query172 assert_equal 'howdy', fzf.filter173 assert_equal :fuzzy, fzf.extended174 assert_equal [2..2], fzf.nth175 assert_equal true, fzf.select1176 assert_equal true, fzf.exit0177178 # Left-to-right179 fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n4,5180 -s 3000 -c +m -i -q world +x -fworld -2 --black --no-black181 -1 -0 +1 +0182 ]183 assert_equal 3000, fzf.sort184 assert_equal false, fzf.multi185 assert_equal true, fzf.color186 assert_equal true, fzf.ansi256187 assert_equal false, fzf.black188 assert_equal 1, fzf.rxflag189 assert_equal 'world', fzf.query190 assert_equal false, fzf.select1191 assert_equal false, fzf.exit0192 assert_equal 'world', fzf.filter193 assert_equal nil, fzf.extended194 assert_equal [3..3, 4..4], fzf.nth195 rescue SystemExit => e196 assert false, "Exited"197 end198199 def test_invalid_option200 [201 %w[--unknown],202 %w[yo dawg],203 %w[--nth=0],204 %w[-n 0],205 %w[-n 1..2..3],206 %w[-n 1....],207 %w[-n ....3],208 %w[-n 1....3],209 %w[-n 1..0],210 %w[--nth ..0],211 ].each do |argv|212 assert_raises(SystemExit) do213 fzf = FZF.new argv214 end215 end216 end217218 def test_width219 fzf = FZF.new []220 assert_equal 5, fzf.width('abcde')221 assert_equal 4, fzf.width('한글')222 assert_equal 5, fzf.width('한글.')223 end if RUBY_VERSION >= '1.9'224225 def test_trim226 fzf = FZF.new []227 assert_equal ['사.', 6], fzf.trim('가나다라마바사.', 4, true)228 assert_equal ['바사.', 5], fzf.trim('가나다라마바사.', 5, true)229 assert_equal ['바사.', 5], fzf.trim('가나다라마바사.', 6, true)230 assert_equal ['마바사.', 4], fzf.trim('가나다라마바사.', 7, true)231 assert_equal ['가나', 6], fzf.trim('가나다라마바사.', 4, false)232 assert_equal ['가나', 6], fzf.trim('가나다라마바사.', 5, false)233 assert_equal ['가나a', 6], fzf.trim('가나ab라마바사.', 5, false)234 assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 6, false)235 assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 7, false)236 end if RUBY_VERSION >= '1.9'237238 def test_format239 fzf = FZF.new []240 assert_equal [['01234..', false]], fzf.format('0123456789', 7, [])241 assert_equal [['012', false], ['34', true], ['..', false]],242 fzf.format('0123456789', 7, [[3, 5]])243 assert_equal [['..56', false], ['789', true]],244 fzf.format('0123456789', 7, [[7, 10]])245 assert_equal [['..56', false], ['78', true], ['9', false]],246 fzf.format('0123456789', 7, [[7, 9]])247248 (3..5).each do |i|249 assert_equal [['..', false], ['567', true], ['89', false]],250 fzf.format('0123456789', 7, [[i, 8]])251 end252253 assert_equal [['..', false], ['345', true], ['..', false]],254 fzf.format('0123456789', 7, [[3, 6]])255 assert_equal [['012', false], ['34', true], ['..', false]],256 fzf.format('0123456789', 7, [[3, 5]])257258 # Multi-region259 assert_equal [["0", true], ["1", false], ["2", true], ["34..", false]],260 fzf.format('0123456789', 7, [[0, 1], [2, 3]])261262 assert_equal [["..", false], ["5", true], ["6", false], ["78", true], ["9", false]],263 fzf.format('0123456789', 7, [[3, 6], [7, 9]])264265 assert_equal [["..", false], ["3", true], ["4", false], ["5", true], ["..", false]],266 fzf.format('0123456789', 7, [[3, 4], [5, 6]])267268 # Multi-region Overlap269 assert_equal [["..", false], ["345", true], ["..", false]],270 fzf.format('0123456789', 7, [[4, 5], [3, 6]])271 end272273 def test_fuzzy_matcher274 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE275 list = %w[276 juice277 juiceful278 juiceless279 juicily280 juiciness281 juicy]282 assert matcher.caches.empty?283 assert_equal(284 [["juice", [[0, 1]]],285 ["juiceful", [[0, 1]]],286 ["juiceless", [[0, 1]]],287 ["juicily", [[0, 1]]],288 ["juiciness", [[0, 1]]],289 ["juicy", [[0, 1]]]], matcher.match(list, 'j', '', '').sort)290 assert !matcher.caches.empty?291 assert_equal [list.object_id], matcher.caches.keys292 assert_equal 1, matcher.caches[list.object_id].length293 assert_equal 6, matcher.caches[list.object_id]['j'].length294295 assert_equal(296 [["juicily", [[0, 5]]],297 ["juiciness", [[0, 5]]]], matcher.match(list, 'jii', '', '').sort)298299 assert_equal(300 [["juicily", [[2, 5]]],301 ["juiciness", [[2, 5]]]], matcher.match(list, 'ii', '', '').sort)302303 assert_equal 3, matcher.caches[list.object_id].length304 assert_equal 2, matcher.caches[list.object_id]['ii'].length305306 # TODO : partial_cache307 end308309 def test_fuzzy_matcher_rxflag310 assert_equal nil, FZF::FuzzyMatcher.new(nil).rxflag311 assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag312 assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag313314 assert_equal 1, FZF::FuzzyMatcher.new(nil).rxflag_for('abc')315 assert_equal 0, FZF::FuzzyMatcher.new(nil).rxflag_for('Abc')316 assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('abc')317 assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('Abc')318 assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('abc')319 assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('Abc')320 end321322 def test_fuzzy_matcher_case_sensitive323 # Smart-case match (Uppercase found)324 assert_equal [['Fruit', [[0, 5]]]],325 FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort326327 # Smart-case match (Uppercase not-found)328 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],329 FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'fruit', '', '').sort330331 # Case-sensitive match (-i)332 assert_equal [['Fruit', [[0, 5]]]],333 FZF::FuzzyMatcher.new(0).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort334335 # Case-insensitive match (+i)336 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],337 FZF::FuzzyMatcher.new(Regexp::IGNORECASE).338 match(%w[Fruit Grapefruit], 'Fruit', '', '').sort339 end340341 def test_extended_fuzzy_matcher_case_sensitive342 %w['Fruit Fruit$].each do |q|343 # Smart-case match (Uppercase found)344 assert_equal [['Fruit', [[0, 5]]]],345 FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q, '', '').sort346347 # Smart-case match (Uppercase not-found)348 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],349 FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q.downcase, '', '').sort350351 # Case-sensitive match (-i)352 assert_equal [['Fruit', [[0, 5]]]],353 FZF::ExtendedFuzzyMatcher.new(0).match(%w[Fruit Grapefruit], q, '', '').sort354355 # Case-insensitive match (+i)356 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],357 FZF::ExtendedFuzzyMatcher.new(Regexp::IGNORECASE).358 match(%w[Fruit Grapefruit], q, '', '').sort359 end360 end361362 def test_extended_fuzzy_matcher363 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE364 list = %w[365 juice366 juiceful367 juiceless368 juicily369 juiciness370 juicy371 _juice]372 match = proc { |q, prefix|373 matcher.match(list, q, prefix, '').sort.map { |p| [p.first, p.last.sort] }374 }375376 assert matcher.caches.empty?377 3.times do378 ['y j', 'j y'].each do |pat|379 (0..pat.length - 1).each do |prefix_length|380 prefix = pat[0, prefix_length]381 assert_equal(382 [["juicily", [[0, 1], [6, 7]]],383 ["juicy", [[0, 1], [4, 5]]]],384 match.call(pat, prefix))385 end386 end387388 # $389 assert_equal [["juiceful", [[7, 8]]]], match.call('l$', '')390 assert_equal [["juiceful", [[7, 8]]],391 ["juiceless", [[5, 6]]],392 ["juicily", [[5, 6]]]], match.call('l', '')393394 # ^395 assert_equal list.length, match.call('j', '').length396 assert_equal list.length - 1, match.call('^j', '').length397398 # ^ + $399 assert_equal 0, match.call('^juici$', '').length400 assert_equal 1, match.call('^juice$', '').length401 assert_equal 0, match.call('^.*$', '').length402403 # !404 assert_equal 0, match.call('!j', '').length405406 # ! + ^407 assert_equal [["_juice", []]], match.call('!^j', '')408409 # ! + $410 assert_equal list.length - 1, match.call('!l$', '').length411412 # ! + f413 assert_equal [["juicy", [[4, 5]]]], match.call('y !l', '')414415 # '416 assert_equal %w[juiceful juiceless juicily],417 match.call('il', '').map { |e| e.first }418 assert_equal %w[juicily],419 match.call("'il", '').map { |e| e.first }420 assert_equal (list - %w[juicily]).sort,421 match.call("!'il", '').map { |e| e.first }.sort422 end423 assert !matcher.caches.empty?424 end425426 def test_xfuzzy_matcher_prefix_cache427 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE428 list = %w[429 a.java430 b.java431 java.jive432 c.java$433 d.java434 ]435 2.times do436 assert_equal 5, matcher.match(list, 'java', 'java', '').length437 assert_equal 3, matcher.match(list, 'java$', 'java$', '').length438 assert_equal 1, matcher.match(list, 'java$$', 'java$$', '').length439440 assert_equal 0, matcher.match(list, '!java', '!java', '').length441 assert_equal 4, matcher.match(list, '!^jav', '!^jav', '').length442 assert_equal 4, matcher.match(list, '!^java', '!^java', '').length443 assert_equal 2, matcher.match(list, '!^java !b !c', '!^java', '').length444 end445 end446447 def test_sort_by_rank448 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE449 xmatcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE450 list = %w[451 0____1452 0_____1453 01454 ____0_1455 01_456 _01_457 0______1458 ___01___459 ]460 assert_equal(461 [["01", [[0, 2]]],462 ["01_", [[0, 2]]],463 ["_01_", [[1, 3]]],464 ["___01___", [[3, 5]]],465 ["____0_1", [[4, 7]]],466 ["0____1", [[0, 6]]],467 ["0_____1", [[0, 7]]],468 ["0______1", [[0, 8]]]],469 FZF.sort(matcher.match(list, '01', '', '')))470471 assert_equal(472 [["01", [[0, 1], [1, 2]]],473 ["01_", [[0, 1], [1, 2]]],474 ["_01_", [[1, 2], [2, 3]]],475 ["0____1", [[0, 1], [5, 6]]],476 ["0_____1", [[0, 1], [6, 7]]],477 ["____0_1", [[4, 5], [6, 7]]],478 ["0______1", [[0, 1], [7, 8]]],479 ["___01___", [[3, 4], [4, 5]]]],480 FZF.sort(xmatcher.match(list, '0 1', '', '')))481482 assert_equal(483 [["_01_", [[1, 3], [0, 4]], [4, 4, "_01_"]],484 ["___01___", [[3, 5], [0, 2]], [4, 8, "___01___"]],485 ["____0_1", [[4, 7], [0, 2]], [5, 7, "____0_1"]],486 ["0____1", [[0, 6], [1, 3]], [6, 6, "0____1"]],487 ["0_____1", [[0, 7], [1, 3]], [7, 7, "0_____1"]],488 ["0______1", [[0, 8], [1, 3]], [8, 8, "0______1"]]],489 FZF.sort(xmatcher.match(list, '01 __', '', '')).map { |tuple|490 tuple << FZF.rank(tuple)491 }492 )493 end494495 def test_extended_exact_mode496 exact = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :exact497 fuzzy = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy498 list = %w[499 extended-exact-mode-not-fuzzy500 extended'-fuzzy-mode501 ]502 assert_equal 2, fuzzy.match(list, 'extended', '', '').length503 assert_equal 2, fuzzy.match(list, 'mode extended', '', '').length504 assert_equal 2, fuzzy.match(list, 'xtndd', '', '').length505 assert_equal 2, fuzzy.match(list, "'-fuzzy", '', '').length506507 assert_equal 2, exact.match(list, 'extended', '', '').length508 assert_equal 2, exact.match(list, 'mode extended', '', '').length509 assert_equal 0, exact.match(list, 'xtndd', '', '').length510 assert_equal 1, exact.match(list, "'-fuzzy", '', '').length511 assert_equal 2, exact.match(list, "-fuzzy", '', '').length512 end513514 # ^$ -> matches empty item515 def test_format_empty_item516 fzf = FZF.new []517 item = ['', [[0, 0]]]518 line, offsets = item519 tokens = fzf.format line, 80, offsets520 assert_equal [], tokens521 end522523 def test_mouse_event524 interval = FZF::MouseEvent::DOUBLE_CLICK_INTERVAL525 me = FZF::MouseEvent.new nil526 me.v = 10527 assert_equal false, me.double?(10)528 assert_equal false, me.double?(20)529 me.v = 20530 assert_equal false, me.double?(10)531 assert_equal false, me.double?(20)532 me.v = 20533 assert_equal false, me.double?(10)534 assert_equal true, me.double?(20)535 sleep interval536 assert_equal false, me.double?(20)537 end538539 def test_nth_match540 list = [541 ' first second third',542 'fourth fifth sixth',543 ]544545 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE546 assert_equal list, matcher.match(list, 'f', '', '').map(&:first)547 assert_equal [548 [list[0], [[2, 5]]],549 [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')550551 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1]552 assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')553 assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')554555 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2..2]556 assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')557558 # Comma-separated559 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2..2, 0..0]560 assert_equal [[list[0], [[19, 20]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')561562 # Ordered563 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0, 2..2]564 assert_equal [[list[0], [[3, 4]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')565566 regex = FZF.build_delim_regex "\t"567 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0], regex568 assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')569570 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1], regex571 assert_equal [], matcher.match(list, 'r', '', '')572 assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')573574 # Negative indexing575 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [-1..-1], regex576 assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')577 assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')578579 # Regex delimiter580 regex = FZF.build_delim_regex "[ \t]+"581 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0], regex582 assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)583584 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1], regex585 assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')586 end587588 def test_nth_match_range589 list = [590 ' first second third',591 'fourth fifth sixth',592 ]593594 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..2]595 assert_equal [[list[0], [[8, 20]]]], matcher.match(list, 'sr', '', '')596 assert_equal [], matcher.match(list, 'fo', '', '')597598 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..-1, 0..0]599 assert_equal [[list[0], [[8, 20]]]], matcher.match(list, 'sr', '', '')600 assert_equal [[list[1], [[0, 2]]]], matcher.match(list, 'fo', '', '')601602 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [0..0, 1..2]603 assert_equal [], matcher.match(list, '^t', '', '')604605 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [0..1, 2..2]606 assert_equal [[list[0], [[16, 17]]]], matcher.match(list, '^t', '', '')607608 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [1..-1]609 assert_equal [[list[0], [[8, 9]]]], matcher.match(list, '^s', '', '')610 end611612 def stream_for str, delay = 0613 StringIO.new(str).tap do |sio|614 sio.instance_eval do615 alias org_gets gets616617 def gets618 org_gets.tap { |e| sleep(@delay) unless e.nil? }619 end620621 def reopen _622 end623 end624 sio.instance_variable_set :@delay, delay625 end626 end627628 def assert_fzf_output opts, given, expected629 stream = stream_for given630 output = stream_for ''631632 def sorted_lines line633 line.split($/).sort634 end635636 begin637 tty = MockTTY.new638 $stdout = output639 fzf = FZF.new(opts, stream)640 fzf.instance_variable_set :@tty, tty641 thr = block_given? && Thread.new { yield tty }642 fzf.start643 thr && thr.join644 rescue SystemExit => e645 assert_equal 0, e.status646 assert_equal sorted_lines(expected), sorted_lines(output.string)647 ensure648 $stdout = STDOUT649 end650 end651652 def test_filter653 {654 %w[--filter=ol] => 'World',655 %w[--filter=ol --print-query] => "ol\nWorld",656 }.each do |opts, expected|657 assert_fzf_output opts, "Hello\nWorld", expected658 end659 end660661 def test_select_1662 {663 %w[--query=ol --select-1] => 'World',664 %w[--query=ol --select-1 --print-query] => "ol\nWorld",665 }.each do |opts, expected|666 assert_fzf_output opts, "Hello\nWorld", expected667 end668 end669670 def test_select_1_without_query671 assert_fzf_output %w[--select-1], 'Hello World', 'Hello World'672 end673674 def test_select_1_ambiguity675 begin676 Timeout::timeout(0.5) do677 assert_fzf_output %w[--query=o --select-1], "hello\nworld", "should not match"678 end679 rescue Timeout::Error680 Curses.close_screen681 end682 end683684 def test_exit_0685 {686 %w[--query=zz --exit-0] => '',687 %w[--query=zz --exit-0 --print-query] => 'zz',688 }.each do |opts, expected|689 assert_fzf_output opts, "Hello\nWorld", expected690 end691 end692693 def test_exit_0_without_query694 assert_fzf_output %w[--exit-0], '', ''695 end696697 def test_with_nth698 source = "hello world\nbatman"699 assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q ^worl],700 source, 'hello world'701 assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q llo$],702 source, 'hello world'703 assert_fzf_output %w[-0 -1 --with-nth=.. -x -q llo$],704 source, ''705 assert_fzf_output %w[-0 -1 --with-nth=2,2,2,..,1 -x -q worlworlworlhellworlhell],706 source, 'hello world'707 assert_fzf_output %w[-0 -1 --with-nth=1,1,-1,1 -x -q batbatbatbat],708 source, 'batman'709 end710711 def test_with_nth_transform712 fzf = FZF.new %w[--with-nth 2..,1]713 assert_equal 'my world hello', fzf.transform('hello my world')714 assert_equal 'my world hello', fzf.transform('hello my world')715 assert_equal 'my world hello', fzf.transform('hello my world ')716717 fzf = FZF.new %w[--with-nth 2,-1,2]718 assert_equal 'my world my', fzf.transform('hello my world')719 assert_equal 'world world world', fzf.transform('hello world')720 assert_equal 'world world world', fzf.transform('hello world ')721 end722723 def test_ranking_overlap_match_regions724 list = [725 '1 3 4 2',726 '1 2 3 4'727 ]728 assert_equal [729 ['1 2 3 4', [[0, 13], [16, 22]]],730 ['1 3 4 2', [[0, 24], [12, 17]]],731 ], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))732 end733734 def test_constrain735 fzf = FZF.new []736737 # [#**** ]738 assert_equal [false, 0, 0], fzf.constrain(0, 0, 5, 100)739740 # *****[**#** ... ] => [**#******* ... ]741 assert_equal [true, 0, 2], fzf.constrain(5, 7, 10, 100)742743 # [**********]**#** => ***[*********#]**744 assert_equal [true, 3, 12], fzf.constrain(0, 12, 15, 10)745746 # *****[**#** ] => ***[**#****]747 assert_equal [true, 3, 5], fzf.constrain(5, 7, 10, 7)748749 # *****[**#** ] => ****[**#***]750 assert_equal [true, 4, 6], fzf.constrain(5, 7, 10, 6)751752 # ***** [#] => ****[#]753 assert_equal [true, 4, 4], fzf.constrain(10, 10, 5, 1)754755 # [ ] #**** => [#]****756 assert_equal [true, 0, 0], fzf.constrain(-5, 0, 5, 1)757758 # [ ] **#** => **[#]**759 assert_equal [true, 2, 2], fzf.constrain(-5, 2, 5, 1)760761 # [***** #] => [****# ]762 assert_equal [true, 0, 4], fzf.constrain(0, 7, 5, 10)763764 # **[***** #] => [******# ]765 assert_equal [true, 0, 6], fzf.constrain(2, 10, 7, 10)766 end767768 def test_invalid_utf8769 tmp = Tempfile.new('fzf')770 tmp << 'hello ' << [0xff].pack('C*') << ' world' << $/ << [0xff].pack('C*')771 tmp.close772 begin773 Timeout::timeout(0.5) do774 FZF.new(%w[-n..,1,2.. -q^ -x], File.open(tmp.path)).start775 end776 rescue Timeout::Error777 Curses.close_screen778 end779 ensure780 tmp.unlink781 end782783 def test_with_nth_mock_tty784 # Manual selection with input785 assert_fzf_output ["--with-nth=2,1"], "hello world", "hello world" do |tty|786 tty << "world"787 tty << "hell"788 tty << "\r"789 end790791 # Manual selection without input792 assert_fzf_output ["--with-nth=2,1"], "hello world", "hello world" do |tty|793 tty << "\r"794 end795796 # Manual selection with input and --multi797 lines = "hello world\ngoodbye world"798 assert_fzf_output %w[-m --with-nth=2,1], lines, lines do |tty|799 tty << "o"800 tty << "\e[Z\e[Z"801 tty << "\r"802 end803804 # Manual selection without input and --multi805 assert_fzf_output %w[-m --with-nth=2,1], lines, lines do |tty|806 tty << "\e[Z\e[Z"807 tty << "\r"808 end809810 # ALT-D811 assert_fzf_output %w[--print-query], "", "hello baby = world" do |tty|812 tty << "hello world baby"813 tty << alt(:b) << alt(:b) << alt(:d)814 tty << ctrl(:e) << " = " << ctrl(:y)815 tty << "\r"816 end817818 # ALT-BACKSPACE819 assert_fzf_output %w[--print-query], "", "hello baby = world " do |tty|820 tty << "hello world baby"821 tty << alt(:b) << alt(127.chr)822 tty << ctrl(:e) << " = " << ctrl(:y)823 tty << "\r"824 end825826 # Word-movements827 assert_fzf_output %w[--print-query], "", "ello!_orld!~ foo=?" do |tty|828 tty << "hello_world==baby?"829 tty << alt(:b) << ctrl(:d)830 tty << alt(:b) << ctrl(:d)831 tty << alt(:b) << ctrl(:d)832 tty << alt(:f) << '!'833 tty << alt(:f) << '!'834 tty << alt(:d) << '~'835 tty << " foo=bar foo=bar"836 tty << ctrl(:w)837 tty << alt(127.chr)838 tty << "\r"839 end840 end841842 def alt chr843 "\e#{chr}"844 end845846 def ctrl char847 char.to_s.ord - 'a'.ord + 1848 end849end850
added:test/test_go.rb+193 added
1#!/usr/bin/env ruby2# encoding: utf-834require 'minitest/autorun'56class Tmux7 TEMPNAME = '/tmp/fzf-test.txt'89 attr_reader :win1011 def initialize shell = 'bash'12 @win = go("new-window -P -F '#I' 'bash --rcfile ~/.fzf.#{shell}'").first13 @lines = `tput lines`.chomp.to_i14 end1516 def self.current17 `tmux display-message -p '#I'`.split($/).first18 end1920 def self.select id21 system "tmux select-window -t #{id}"22 end2324 def closed?25 !go("list-window -F '#I'").include?(win)26 end2728 def close timeout = 129 send_keys 'C-c', 'C-u', 'exit', :Enter30 wait(timeout) { closed? }31 end3233 def kill34 go("kill-window -t #{win} 2> /dev/null")35 end3637 def send_keys *args38 args = args.map { |a| %{"#{a}"} }.join ' '39 go("send-keys -t #{win} #{args}")40 end4142 def capture43 go("capture-pane -t #{win} \\; save-buffer #{TEMPNAME}")44 raise "Window not found" if $?.exitstatus != 045 File.read(TEMPNAME).split($/)[0, @lines]46 end4748 def until timeout = 149 wait(timeout) { yield capture }50 end5152private53 def wait timeout = 154 waited = 055 until yield56 waited += 0.157 sleep 0.158 if waited > timeout59 hl = '=' * 1060 puts hl61 capture.each_with_index do |line, idx|62 puts [idx.to_s.rjust(2), line].join(': ')63 end64 puts hl65 raise "timeout"66 end67 end68 end6970 def go *args71 %x[tmux #{args.join ' '}].split($/)72 end73end7475class TestGoFZF < MiniTest::Unit::TestCase76 attr_reader :tmux7778 def tempname79 '/tmp/output'80 end8182 def setup83 ENV.delete 'FZF_DEFAULT_OPTS'84 ENV.delete 'FZF_DEFAULT_COMMAND'85 @prev = Tmux.current86 @tmux = Tmux.new87 File.unlink tempname rescue nil88 end8990 def teardown91 @tmux.kill92 Tmux.select @prev93 end9495 def test_vanilla96 tmux.send_keys "seq 1 100000 | fzf > #{tempname}", :Enter97 tmux.until(10) { |lines| lines.last =~ /^>/ && lines[-2] =~ /^ 100000/ }98 lines = tmux.capture99 assert_equal ' 2', lines[-4]100 assert_equal '> 1', lines[-3]101 assert_equal ' 100000/100000', lines[-2]102 assert_equal '>', lines[-1]103104 # Testing basic key bindings105 tmux.send_keys '99', 'C-a', '1', 'C-f', '3', 'C-b', 'C-h', 'C-u', 'C-e', 'C-y', 'C-k', 'Tab', 'BTab'106 tmux.until { |lines| lines[-2] == ' 856/100000' }107 lines = tmux.capture108 assert_equal '> 1391', lines[-4]109 assert_equal ' 391', lines[-3]110 assert_equal ' 856/100000', lines[-2]111 assert_equal '> 391', lines[-1]112113 tmux.send_keys :Enter114 tmux.close115 assert_equal '1391', File.read(tempname).chomp116 end117118 def test_fzf_default_command119 tmux.send_keys "FZF_DEFAULT_COMMAND='echo hello' fzf > #{tempname}", :Enter120 tmux.until { |lines| lines.last =~ /^>/ }121122 tmux.send_keys :Enter123 tmux.close124 assert_equal 'hello', File.read(tempname).chomp125 end126127 def test_fzf_prompt128 tmux.send_keys "fzf -q 'foo bar foo-bar'", :Enter129 tmux.until { |lines| lines.last =~ /^>/ }130131 # CTRL-A132 tmux.send_keys "C-A", "("133 tmux.until { |lines| lines.last == '> (foo bar foo-bar' }134135 # META-F136 tmux.send_keys :Escape, :f, ")"137 tmux.until { |lines| lines.last == '> (foo) bar foo-bar' }138139 # CTRL-B140 tmux.send_keys "C-B", "var"141 tmux.until { |lines| lines.last == '> (foovar) bar foo-bar' }142143 # Left, CTRL-D144 tmux.send_keys :Left, :Left, "C-D"145 tmux.until { |lines| lines.last == '> (foovr) bar foo-bar' }146147 # META-BS148 tmux.send_keys :Escape, :BSpace149 tmux.until { |lines| lines.last == '> (r) bar foo-bar' }150151 # CTRL-Y152 tmux.send_keys "C-Y", "C-Y"153 tmux.until { |lines| lines.last == '> (foovfoovr) bar foo-bar' }154155 # META-B156 tmux.send_keys :Escape, :b, :Space, :Space157 tmux.until { |lines| lines.last == '> ( foovfoovr) bar foo-bar' }158159 # CTRL-F / Right160 tmux.send_keys 'C-F', :Right, '/'161 tmux.until { |lines| lines.last == '> ( fo/ovfoovr) bar foo-bar' }162163 # CTRL-H / BS164 tmux.send_keys 'C-H', :BSpace165 tmux.until { |lines| lines.last == '> ( fovfoovr) bar foo-bar' }166167 # CTRL-E168 tmux.send_keys "C-E", 'baz'169 tmux.until { |lines| lines.last == '> ( fovfoovr) bar foo-barbaz' }170171 # CTRL-U172 tmux.send_keys "C-U"173 tmux.until { |lines| lines.last == '>' }174175 # CTRL-Y176 tmux.send_keys "C-Y"177 tmux.until { |lines| lines.last == '> ( fovfoovr) bar foo-barbaz' }178179 # CTRL-W180 tmux.send_keys "C-W", "bar-foo"181 tmux.until { |lines| lines.last == '> ( fovfoovr) bar bar-foo' }182183 # META-D184 tmux.send_keys :Escape, :b, :Escape, :b, :Escape, :d, "C-A", "C-Y"185 tmux.until { |lines| lines.last == '> bar( fovfoovr) bar -foo' }186187 # CTRL-M188 tmux.send_keys "C-M"189 tmux.until { |lines| lines.last !~ /^>/ }190 tmux.close191 end192end193
added:test/test_ruby.rb+850 added
1#!/usr/bin/env ruby2# encoding: utf-834require 'rubygems'5require 'curses'6require 'timeout'7require 'stringio'8require 'minitest/autorun'9require 'tempfile'10$LOAD_PATH.unshift File.expand_path('../..', __FILE__)11ENV['FZF_EXECUTABLE'] = '0'12load 'fzf'1314class MockTTY15 def initialize16 @buffer = ''17 @mutex = Mutex.new18 @condv = ConditionVariable.new19 end2021 def read_nonblock sz22 @mutex.synchronize do23 take sz24 end25 end2627 def take sz28 if @buffer.length >= sz29 ret = @buffer[0, sz]30 @buffer = @buffer[sz..-1]31 ret32 end33 end3435 def getc36 sleep 0.137 while true38 @mutex.synchronize do39 if char = take(1)40 return char41 else42 @condv.wait(@mutex)43 end44 end45 end46 end4748 def << str49 @mutex.synchronize do50 @buffer << str51 @condv.broadcast52 end53 self54 end55end5657class TestRubyFZF < MiniTest::Unit::TestCase58 def setup59 ENV.delete 'FZF_DEFAULT_SORT'60 ENV.delete 'FZF_DEFAULT_OPTS'61 ENV.delete 'FZF_DEFAULT_COMMAND'62 end6364 def test_default_options65 fzf = FZF.new []66 assert_equal 1000, fzf.sort67 assert_equal false, fzf.multi68 assert_equal true, fzf.color69 assert_equal nil, fzf.rxflag70 assert_equal true, fzf.mouse71 assert_equal nil, fzf.nth72 assert_equal nil, fzf.with_nth73 assert_equal true, fzf.color74 assert_equal false, fzf.black75 assert_equal true, fzf.ansi25676 assert_equal '', fzf.query77 assert_equal false, fzf.select178 assert_equal false, fzf.exit079 assert_equal nil, fzf.filter80 assert_equal nil, fzf.extended81 assert_equal false, fzf.reverse82 assert_equal '> ', fzf.prompt83 assert_equal false, fzf.print_query84 end8586 def test_environment_variables87 # Deprecated88 ENV['FZF_DEFAULT_SORT'] = '20000'89 fzf = FZF.new []90 assert_equal 20000, fzf.sort91 assert_equal nil, fzf.nth9293 ENV['FZF_DEFAULT_OPTS'] =94 '-x -m -s 10000 -q " hello world " +c +2 --select-1 -0 ' <<95 '--no-mouse -f "goodbye world" --black --with-nth=3,-3..,2 --nth=3,-1,2 --reverse --print-query'96 fzf = FZF.new []97 assert_equal 10000, fzf.sort98 assert_equal ' hello world ',99 fzf.query100 assert_equal 'goodbye world',101 fzf.filter102 assert_equal :fuzzy, fzf.extended103 assert_equal true, fzf.multi104 assert_equal false, fzf.color105 assert_equal false, fzf.ansi256106 assert_equal true, fzf.black107 assert_equal false, fzf.mouse108 assert_equal true, fzf.select1109 assert_equal true, fzf.exit0110 assert_equal true, fzf.reverse111 assert_equal true, fzf.print_query112 assert_equal [2..2, -1..-1, 1..1], fzf.nth113 assert_equal [2..2, -3..-1, 1..1], fzf.with_nth114 end115116 def test_option_parser117 # Long opts118 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello --select-1119 --exit-0 --filter=howdy --extended-exact120 --no-mouse --no-256 --nth=1 --with-nth=.. --reverse --prompt (hi)121 --print-query]122 assert_equal 2000, fzf.sort123 assert_equal true, fzf.multi124 assert_equal false, fzf.color125 assert_equal false, fzf.ansi256126 assert_equal false, fzf.black127 assert_equal false, fzf.mouse128 assert_equal 0, fzf.rxflag129 assert_equal 'hello', fzf.query130 assert_equal true, fzf.select1131 assert_equal true, fzf.exit0132 assert_equal 'howdy', fzf.filter133 assert_equal :exact, fzf.extended134 assert_equal [0..0], fzf.nth135 assert_equal nil, fzf.with_nth136 assert_equal true, fzf.reverse137 assert_equal '(hi)', fzf.prompt138 assert_equal true, fzf.print_query139140 # Long opts (left-to-right)141 fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query=hello142 --filter a --filter b --no-256 --black --nth -1 --nth -2143 --select-1 --exit-0 --no-select-1 --no-exit-0144 --no-sort -i --color --no-multi --256145 --reverse --no-reverse --prompt (hi) --prompt=(HI)146 --print-query --no-print-query]147 assert_equal nil, fzf.sort148 assert_equal false, fzf.multi149 assert_equal true, fzf.color150 assert_equal true, fzf.ansi256151 assert_equal true, fzf.black152 assert_equal true, fzf.mouse153 assert_equal 1, fzf.rxflag154 assert_equal 'b', fzf.filter155 assert_equal 'hello', fzf.query156 assert_equal false, fzf.select1157 assert_equal false, fzf.exit0158 assert_equal nil, fzf.extended159 assert_equal [-2..-2], fzf.nth160 assert_equal false, fzf.reverse161 assert_equal '(HI)', fzf.prompt162 assert_equal false, fzf.print_query163164 # Short opts165 fzf = FZF.new %w[-s2000 +c -m +i -qhello -x -fhowdy +2 -n3 -1 -0]166 assert_equal 2000, fzf.sort167 assert_equal true, fzf.multi168 assert_equal false, fzf.color169 assert_equal false, fzf.ansi256170 assert_equal 0, fzf.rxflag171 assert_equal 'hello', fzf.query172 assert_equal 'howdy', fzf.filter173 assert_equal :fuzzy, fzf.extended174 assert_equal [2..2], fzf.nth175 assert_equal true, fzf.select1176 assert_equal true, fzf.exit0177178 # Left-to-right179 fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n4,5180 -s 3000 -c +m -i -q world +x -fworld -2 --black --no-black181 -1 -0 +1 +0182 ]183 assert_equal 3000, fzf.sort184 assert_equal false, fzf.multi185 assert_equal true, fzf.color186 assert_equal true, fzf.ansi256187 assert_equal false, fzf.black188 assert_equal 1, fzf.rxflag189 assert_equal 'world', fzf.query190 assert_equal false, fzf.select1191 assert_equal false, fzf.exit0192 assert_equal 'world', fzf.filter193 assert_equal nil, fzf.extended194 assert_equal [3..3, 4..4], fzf.nth195 rescue SystemExit => e196 assert false, "Exited"197 end198199 def test_invalid_option200 [201 %w[--unknown],202 %w[yo dawg],203 %w[--nth=0],204 %w[-n 0],205 %w[-n 1..2..3],206 %w[-n 1....],207 %w[-n ....3],208 %w[-n 1....3],209 %w[-n 1..0],210 %w[--nth ..0],211 ].each do |argv|212 assert_raises(SystemExit) do213 fzf = FZF.new argv214 end215 end216 end217218 def test_width219 fzf = FZF.new []220 assert_equal 5, fzf.width('abcde')221 assert_equal 4, fzf.width('한글')222 assert_equal 5, fzf.width('한글.')223 end if RUBY_VERSION >= '1.9'224225 def test_trim226 fzf = FZF.new []227 assert_equal ['사.', 6], fzf.trim('가나다라마바사.', 4, true)228 assert_equal ['바사.', 5], fzf.trim('가나다라마바사.', 5, true)229 assert_equal ['바사.', 5], fzf.trim('가나다라마바사.', 6, true)230 assert_equal ['마바사.', 4], fzf.trim('가나다라마바사.', 7, true)231 assert_equal ['가나', 6], fzf.trim('가나다라마바사.', 4, false)232 assert_equal ['가나', 6], fzf.trim('가나다라마바사.', 5, false)233 assert_equal ['가나a', 6], fzf.trim('가나ab라마바사.', 5, false)234 assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 6, false)235 assert_equal ['가나ab', 5], fzf.trim('가나ab라마바사.', 7, false)236 end if RUBY_VERSION >= '1.9'237238 def test_format239 fzf = FZF.new []240 assert_equal [['01234..', false]], fzf.format('0123456789', 7, [])241 assert_equal [['012', false], ['34', true], ['..', false]],242 fzf.format('0123456789', 7, [[3, 5]])243 assert_equal [['..56', false], ['789', true]],244 fzf.format('0123456789', 7, [[7, 10]])245 assert_equal [['..56', false], ['78', true], ['9', false]],246 fzf.format('0123456789', 7, [[7, 9]])247248 (3..5).each do |i|249 assert_equal [['..', false], ['567', true], ['89', false]],250 fzf.format('0123456789', 7, [[i, 8]])251 end252253 assert_equal [['..', false], ['345', true], ['..', false]],254 fzf.format('0123456789', 7, [[3, 6]])255 assert_equal [['012', false], ['34', true], ['..', false]],256 fzf.format('0123456789', 7, [[3, 5]])257258 # Multi-region259 assert_equal [["0", true], ["1", false], ["2", true], ["34..", false]],260 fzf.format('0123456789', 7, [[0, 1], [2, 3]])261262 assert_equal [["..", false], ["5", true], ["6", false], ["78", true], ["9", false]],263 fzf.format('0123456789', 7, [[3, 6], [7, 9]])264265 assert_equal [["..", false], ["3", true], ["4", false], ["5", true], ["..", false]],266 fzf.format('0123456789', 7, [[3, 4], [5, 6]])267268 # Multi-region Overlap269 assert_equal [["..", false], ["345", true], ["..", false]],270 fzf.format('0123456789', 7, [[4, 5], [3, 6]])271 end272273 def test_fuzzy_matcher274 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE275 list = %w[276 juice277 juiceful278 juiceless279 juicily280 juiciness281 juicy]282 assert matcher.caches.empty?283 assert_equal(284 [["juice", [[0, 1]]],285 ["juiceful", [[0, 1]]],286 ["juiceless", [[0, 1]]],287 ["juicily", [[0, 1]]],288 ["juiciness", [[0, 1]]],289 ["juicy", [[0, 1]]]], matcher.match(list, 'j', '', '').sort)290 assert !matcher.caches.empty?291 assert_equal [list.object_id], matcher.caches.keys292 assert_equal 1, matcher.caches[list.object_id].length293 assert_equal 6, matcher.caches[list.object_id]['j'].length294295 assert_equal(296 [["juicily", [[0, 5]]],297 ["juiciness", [[0, 5]]]], matcher.match(list, 'jii', '', '').sort)298299 assert_equal(300 [["juicily", [[2, 5]]],301 ["juiciness", [[2, 5]]]], matcher.match(list, 'ii', '', '').sort)302303 assert_equal 3, matcher.caches[list.object_id].length304 assert_equal 2, matcher.caches[list.object_id]['ii'].length305306 # TODO : partial_cache307 end308309 def test_fuzzy_matcher_rxflag310 assert_equal nil, FZF::FuzzyMatcher.new(nil).rxflag311 assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag312 assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag313314 assert_equal 1, FZF::FuzzyMatcher.new(nil).rxflag_for('abc')315 assert_equal 0, FZF::FuzzyMatcher.new(nil).rxflag_for('Abc')316 assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('abc')317 assert_equal 0, FZF::FuzzyMatcher.new(0).rxflag_for('Abc')318 assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('abc')319 assert_equal 1, FZF::FuzzyMatcher.new(1).rxflag_for('Abc')320 end321322 def test_fuzzy_matcher_case_sensitive323 # Smart-case match (Uppercase found)324 assert_equal [['Fruit', [[0, 5]]]],325 FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort326327 # Smart-case match (Uppercase not-found)328 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],329 FZF::FuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], 'fruit', '', '').sort330331 # Case-sensitive match (-i)332 assert_equal [['Fruit', [[0, 5]]]],333 FZF::FuzzyMatcher.new(0).match(%w[Fruit Grapefruit], 'Fruit', '', '').sort334335 # Case-insensitive match (+i)336 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],337 FZF::FuzzyMatcher.new(Regexp::IGNORECASE).338 match(%w[Fruit Grapefruit], 'Fruit', '', '').sort339 end340341 def test_extended_fuzzy_matcher_case_sensitive342 %w['Fruit Fruit$].each do |q|343 # Smart-case match (Uppercase found)344 assert_equal [['Fruit', [[0, 5]]]],345 FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q, '', '').sort346347 # Smart-case match (Uppercase not-found)348 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],349 FZF::ExtendedFuzzyMatcher.new(nil).match(%w[Fruit Grapefruit], q.downcase, '', '').sort350351 # Case-sensitive match (-i)352 assert_equal [['Fruit', [[0, 5]]]],353 FZF::ExtendedFuzzyMatcher.new(0).match(%w[Fruit Grapefruit], q, '', '').sort354355 # Case-insensitive match (+i)356 assert_equal [["Fruit", [[0, 5]]], ["Grapefruit", [[5, 10]]]],357 FZF::ExtendedFuzzyMatcher.new(Regexp::IGNORECASE).358 match(%w[Fruit Grapefruit], q, '', '').sort359 end360 end361362 def test_extended_fuzzy_matcher363 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE364 list = %w[365 juice366 juiceful367 juiceless368 juicily369 juiciness370 juicy371 _juice]372 match = proc { |q, prefix|373 matcher.match(list, q, prefix, '').sort.map { |p| [p.first, p.last.sort] }374 }375376 assert matcher.caches.empty?377 3.times do378 ['y j', 'j y'].each do |pat|379 (0..pat.length - 1).each do |prefix_length|380 prefix = pat[0, prefix_length]381 assert_equal(382 [["juicily", [[0, 1], [6, 7]]],383 ["juicy", [[0, 1], [4, 5]]]],384 match.call(pat, prefix))385 end386 end387388 # $389 assert_equal [["juiceful", [[7, 8]]]], match.call('l$', '')390 assert_equal [["juiceful", [[7, 8]]],391 ["juiceless", [[5, 6]]],392 ["juicily", [[5, 6]]]], match.call('l', '')393394 # ^395 assert_equal list.length, match.call('j', '').length396 assert_equal list.length - 1, match.call('^j', '').length397398 # ^ + $399 assert_equal 0, match.call('^juici$', '').length400 assert_equal 1, match.call('^juice$', '').length401 assert_equal 0, match.call('^.*$', '').length402403 # !404 assert_equal 0, match.call('!j', '').length405406 # ! + ^407 assert_equal [["_juice", []]], match.call('!^j', '')408409 # ! + $410 assert_equal list.length - 1, match.call('!l$', '').length411412 # ! + f413 assert_equal [["juicy", [[4, 5]]]], match.call('y !l', '')414415 # '416 assert_equal %w[juiceful juiceless juicily],417 match.call('il', '').map { |e| e.first }418 assert_equal %w[juicily],419 match.call("'il", '').map { |e| e.first }420 assert_equal (list - %w[juicily]).sort,421 match.call("!'il", '').map { |e| e.first }.sort422 end423 assert !matcher.caches.empty?424 end425426 def test_xfuzzy_matcher_prefix_cache427 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE428 list = %w[429 a.java430 b.java431 java.jive432 c.java$433 d.java434 ]435 2.times do436 assert_equal 5, matcher.match(list, 'java', 'java', '').length437 assert_equal 3, matcher.match(list, 'java$', 'java$', '').length438 assert_equal 1, matcher.match(list, 'java$$', 'java$$', '').length439440 assert_equal 0, matcher.match(list, '!java', '!java', '').length441 assert_equal 4, matcher.match(list, '!^jav', '!^jav', '').length442 assert_equal 4, matcher.match(list, '!^java', '!^java', '').length443 assert_equal 2, matcher.match(list, '!^java !b !c', '!^java', '').length444 end445 end446447 def test_sort_by_rank448 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE449 xmatcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE450 list = %w[451 0____1452 0_____1453 01454 ____0_1455 01_456 _01_457 0______1458 ___01___459 ]460 assert_equal(461 [["01", [[0, 2]]],462 ["01_", [[0, 2]]],463 ["_01_", [[1, 3]]],464 ["___01___", [[3, 5]]],465 ["____0_1", [[4, 7]]],466 ["0____1", [[0, 6]]],467 ["0_____1", [[0, 7]]],468 ["0______1", [[0, 8]]]],469 FZF.sort(matcher.match(list, '01', '', '')))470471 assert_equal(472 [["01", [[0, 1], [1, 2]]],473 ["01_", [[0, 1], [1, 2]]],474 ["_01_", [[1, 2], [2, 3]]],475 ["0____1", [[0, 1], [5, 6]]],476 ["0_____1", [[0, 1], [6, 7]]],477 ["____0_1", [[4, 5], [6, 7]]],478 ["0______1", [[0, 1], [7, 8]]],479 ["___01___", [[3, 4], [4, 5]]]],480 FZF.sort(xmatcher.match(list, '0 1', '', '')))481482 assert_equal(483 [["_01_", [[1, 3], [0, 4]], [4, 4, "_01_"]],484 ["___01___", [[3, 5], [0, 2]], [4, 8, "___01___"]],485 ["____0_1", [[4, 7], [0, 2]], [5, 7, "____0_1"]],486 ["0____1", [[0, 6], [1, 3]], [6, 6, "0____1"]],487 ["0_____1", [[0, 7], [1, 3]], [7, 7, "0_____1"]],488 ["0______1", [[0, 8], [1, 3]], [8, 8, "0______1"]]],489 FZF.sort(xmatcher.match(list, '01 __', '', '')).map { |tuple|490 tuple << FZF.rank(tuple)491 }492 )493 end494495 def test_extended_exact_mode496 exact = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :exact497 fuzzy = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy498 list = %w[499 extended-exact-mode-not-fuzzy500 extended'-fuzzy-mode501 ]502 assert_equal 2, fuzzy.match(list, 'extended', '', '').length503 assert_equal 2, fuzzy.match(list, 'mode extended', '', '').length504 assert_equal 2, fuzzy.match(list, 'xtndd', '', '').length505 assert_equal 2, fuzzy.match(list, "'-fuzzy", '', '').length506507 assert_equal 2, exact.match(list, 'extended', '', '').length508 assert_equal 2, exact.match(list, 'mode extended', '', '').length509 assert_equal 0, exact.match(list, 'xtndd', '', '').length510 assert_equal 1, exact.match(list, "'-fuzzy", '', '').length511 assert_equal 2, exact.match(list, "-fuzzy", '', '').length512 end513514 # ^$ -> matches empty item515 def test_format_empty_item516 fzf = FZF.new []517 item = ['', [[0, 0]]]518 line, offsets = item519 tokens = fzf.format line, 80, offsets520 assert_equal [], tokens521 end522523 def test_mouse_event524 interval = FZF::MouseEvent::DOUBLE_CLICK_INTERVAL525 me = FZF::MouseEvent.new nil526 me.v = 10527 assert_equal false, me.double?(10)528 assert_equal false, me.double?(20)529 me.v = 20530 assert_equal false, me.double?(10)531 assert_equal false, me.double?(20)532 me.v = 20533 assert_equal false, me.double?(10)534 assert_equal true, me.double?(20)535 sleep interval536 assert_equal false, me.double?(20)537 end538539 def test_nth_match540 list = [541 ' first second third',542 'fourth fifth sixth',543 ]544545 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE546 assert_equal list, matcher.match(list, 'f', '', '').map(&:first)547 assert_equal [548 [list[0], [[2, 5]]],549 [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')550551 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1]552 assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')553 assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '')554555 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2..2]556 assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '')557558 # Comma-separated559 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [2..2, 0..0]560 assert_equal [[list[0], [[19, 20]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')561562 # Ordered563 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0, 2..2]564 assert_equal [[list[0], [[3, 4]]], [list[1], [[3, 4]]]], matcher.match(list, 'r', '', '')565566 regex = FZF.build_delim_regex "\t"567 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0], regex568 assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '')569570 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1], regex571 assert_equal [], matcher.match(list, 'r', '', '')572 assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')573574 # Negative indexing575 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [-1..-1], regex576 assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '')577 assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '')578579 # Regex delimiter580 regex = FZF.build_delim_regex "[ \t]+"581 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [0..0], regex582 assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first)583584 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..1], regex585 assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '')586 end587588 def test_nth_match_range589 list = [590 ' first second third',591 'fourth fifth sixth',592 ]593594 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..2]595 assert_equal [[list[0], [[8, 20]]]], matcher.match(list, 'sr', '', '')596 assert_equal [], matcher.match(list, 'fo', '', '')597598 matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, [1..-1, 0..0]599 assert_equal [[list[0], [[8, 20]]]], matcher.match(list, 'sr', '', '')600 assert_equal [[list[1], [[0, 2]]]], matcher.match(list, 'fo', '', '')601602 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [0..0, 1..2]603 assert_equal [], matcher.match(list, '^t', '', '')604605 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [0..1, 2..2]606 assert_equal [[list[0], [[16, 17]]]], matcher.match(list, '^t', '', '')607608 matcher = FZF::ExtendedFuzzyMatcher.new Regexp::IGNORECASE, :fuzzy, [1..-1]609 assert_equal [[list[0], [[8, 9]]]], matcher.match(list, '^s', '', '')610 end611612 def stream_for str, delay = 0613 StringIO.new(str).tap do |sio|614 sio.instance_eval do615 alias org_gets gets616617 def gets618 org_gets.tap { |e| sleep(@delay) unless e.nil? }619 end620621 def reopen _622 end623 end624 sio.instance_variable_set :@delay, delay625 end626 end627628 def assert_fzf_output opts, given, expected629 stream = stream_for given630 output = stream_for ''631632 def sorted_lines line633 line.split($/).sort634 end635636 begin637 tty = MockTTY.new638 $stdout = output639 fzf = FZF.new(opts, stream)640 fzf.instance_variable_set :@tty, tty641 thr = block_given? && Thread.new { yield tty }642 fzf.start643 thr && thr.join644 rescue SystemExit => e645 assert_equal 0, e.status646 assert_equal sorted_lines(expected), sorted_lines(output.string)647 ensure648 $stdout = STDOUT649 end650 end651652 def test_filter653 {654 %w[--filter=ol] => 'World',655 %w[--filter=ol --print-query] => "ol\nWorld",656 }.each do |opts, expected|657 assert_fzf_output opts, "Hello\nWorld", expected658 end659 end660661 def test_select_1662 {663 %w[--query=ol --select-1] => 'World',664 %w[--query=ol --select-1 --print-query] => "ol\nWorld",665 }.each do |opts, expected|666 assert_fzf_output opts, "Hello\nWorld", expected667 end668 end669670 def test_select_1_without_query671 assert_fzf_output %w[--select-1], 'Hello World', 'Hello World'672 end673674 def test_select_1_ambiguity675 begin676 Timeout::timeout(0.5) do677 assert_fzf_output %w[--query=o --select-1], "hello\nworld", "should not match"678 end679 rescue Timeout::Error680 Curses.close_screen681 end682 end683684 def test_exit_0685 {686 %w[--query=zz --exit-0] => '',687 %w[--query=zz --exit-0 --print-query] => 'zz',688 }.each do |opts, expected|689 assert_fzf_output opts, "Hello\nWorld", expected690 end691 end692693 def test_exit_0_without_query694 assert_fzf_output %w[--exit-0], '', ''695 end696697 def test_with_nth698 source = "hello world\nbatman"699 assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q ^worl],700 source, 'hello world'701 assert_fzf_output %w[-0 -1 --with-nth=2,1 -x -q llo$],702 source, 'hello world'703 assert_fzf_output %w[-0 -1 --with-nth=.. -x -q llo$],704 source, ''705 assert_fzf_output %w[-0 -1 --with-nth=2,2,2,..,1 -x -q worlworlworlhellworlhell],706 source, 'hello world'707 assert_fzf_output %w[-0 -1 --with-nth=1,1,-1,1 -x -q batbatbatbat],708 source, 'batman'709 end710711 def test_with_nth_transform712 fzf = FZF.new %w[--with-nth 2..,1]713 assert_equal 'my world hello', fzf.transform('hello my world')714 assert_equal 'my world hello', fzf.transform('hello my world')715 assert_equal 'my world hello', fzf.transform('hello my world ')716717 fzf = FZF.new %w[--with-nth 2,-1,2]718 assert_equal 'my world my', fzf.transform('hello my world')719 assert_equal 'world world world', fzf.transform('hello world')720 assert_equal 'world world world', fzf.transform('hello world ')721 end722723 def test_ranking_overlap_match_regions724 list = [725 '1 3 4 2',726 '1 2 3 4'727 ]728 assert_equal [729 ['1 2 3 4', [[0, 13], [16, 22]]],730 ['1 3 4 2', [[0, 24], [12, 17]]],731 ], FZF.sort(FZF::ExtendedFuzzyMatcher.new(nil).match(list, '12 34', '', ''))732 end733734 def test_constrain735 fzf = FZF.new []736737 # [#**** ]738 assert_equal [false, 0, 0], fzf.constrain(0, 0, 5, 100)739740 # *****[**#** ... ] => [**#******* ... ]741 assert_equal [true, 0, 2], fzf.constrain(5, 7, 10, 100)742743 # [**********]**#** => ***[*********#]**744 assert_equal [true, 3, 12], fzf.constrain(0, 12, 15, 10)745746 # *****[**#** ] => ***[**#****]747 assert_equal [true, 3, 5], fzf.constrain(5, 7, 10, 7)748749 # *****[**#** ] => ****[**#***]750 assert_equal [true, 4, 6], fzf.constrain(5, 7, 10, 6)751752 # ***** [#] => ****[#]753 assert_equal [true, 4, 4], fzf.constrain(10, 10, 5, 1)754755 # [ ] #**** => [#]****756 assert_equal [true, 0, 0], fzf.constrain(-5, 0, 5, 1)757758 # [ ] **#** => **[#]**759 assert_equal [true, 2, 2], fzf.constrain(-5, 2, 5, 1)760761 # [***** #] => [****# ]762 assert_equal [true, 0, 4], fzf.constrain(0, 7, 5, 10)763764 # **[***** #] => [******# ]765 assert_equal [true, 0, 6], fzf.constrain(2, 10, 7, 10)766 end767768 def test_invalid_utf8769 tmp = Tempfile.new('fzf')770 tmp << 'hello ' << [0xff].pack('C*') << ' world' << $/ << [0xff].pack('C*')771 tmp.close772 begin773 Timeout::timeout(0.5) do774 FZF.new(%w[-n..,1,2.. -q^ -x], File.open(tmp.path)).start775 end776 rescue Timeout::Error777 Curses.close_screen778 end779 ensure780 tmp.unlink781 end782783 def test_with_nth_mock_tty784 # Manual selection with input785 assert_fzf_output ["--with-nth=2,1"], "hello world", "hello world" do |tty|786 tty << "world"787 tty << "hell"788 tty << "\r"789 end790791 # Manual selection without input792 assert_fzf_output ["--with-nth=2,1"], "hello world", "hello world" do |tty|793 tty << "\r"794 end795796 # Manual selection with input and --multi797 lines = "hello world\ngoodbye world"798 assert_fzf_output %w[-m --with-nth=2,1], lines, lines do |tty|799 tty << "o"800 tty << "\e[Z\e[Z"801 tty << "\r"802 end803804 # Manual selection without input and --multi805 assert_fzf_output %w[-m --with-nth=2,1], lines, lines do |tty|806 tty << "\e[Z\e[Z"807 tty << "\r"808 end809810 # ALT-D811 assert_fzf_output %w[--print-query], "", "hello baby = world" do |tty|812 tty << "hello world baby"813 tty << alt(:b) << alt(:b) << alt(:d)814 tty << ctrl(:e) << " = " << ctrl(:y)815 tty << "\r"816 end817818 # ALT-BACKSPACE819 assert_fzf_output %w[--print-query], "", "hello baby = world " do |tty|820 tty << "hello world baby"821 tty << alt(:b) << alt(127.chr)822 tty << ctrl(:e) << " = " << ctrl(:y)823 tty << "\r"824 end825826 # Word-movements827 assert_fzf_output %w[--print-query], "", "ello!_orld!~ foo=?" do |tty|828 tty << "hello_world==baby?"829 tty << alt(:b) << ctrl(:d)830 tty << alt(:b) << ctrl(:d)831 tty << alt(:b) << ctrl(:d)832 tty << alt(:f) << '!'833 tty << alt(:f) << '!'834 tty << alt(:d) << '~'835 tty << " foo=bar foo=bar"836 tty << ctrl(:w)837 tty << alt(127.chr)838 tty << "\r"839 end840 end841842 def alt chr843 "\e#{chr}"844 end845846 def ctrl char847 char.to_s.ord - 'a'.ord + 1848 end849end850
modified:.travis.yml+20 added-7 removed
modified:Rakefile+5 added-2 removed
1require "bundler/gem_tasks"2require 'rake/testtask'34Rake::TestTask.new(:test) do |test|5 test.pattern = 'test/**/test_*.rb'6 test.verbose = true5 test.pattern = 'test/test_go.rb'6end78Rake::TestTask.new(:testall) do |test|9 test.pattern = 'test/test_*.rb'10end1112task :default => :test