From ec4b8a59fad7247922444b80525215605fd9b9dc Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Sat, 29 Mar 2014 17:05:35 +0900 Subject: Implement --nth and --delimiter option --- test/test_fzf.rb | 64 +++++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 59 insertions(+), 5 deletions(-) (limited to 'test') diff --git a/test/test_fzf.rb b/test/test_fzf.rb index b236d6ea..c9ac9047 100644 --- a/test/test_fzf.rb +++ b/test/test_fzf.rb @@ -27,8 +27,10 @@ class TestFZF < MiniTest::Unit::TestCase ENV['FZF_DEFAULT_SORT'] = '20000' fzf = FZF.new [] assert_equal 20000, fzf.sort + assert_equal nil, fzf.nth - ENV['FZF_DEFAULT_OPTS'] = '-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black' + ENV['FZF_DEFAULT_OPTS'] = + '-x -m -s 10000 -q " hello world " +c +2 --no-mouse -f "goodbye world" --black --nth=3' fzf = FZF.new [] assert_equal 10000, fzf.sort assert_equal ' hello world ', @@ -41,12 +43,13 @@ class TestFZF < MiniTest::Unit::TestCase assert_equal false, fzf.ansi256 assert_equal true, fzf.black assert_equal false, fzf.mouse + assert_equal 3, fzf.nth end def test_option_parser # Long opts fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello - --filter=howdy --extended-exact --no-mouse --no-256] + --filter=howdy --extended-exact --no-mouse --no-256 --nth=1] assert_equal 2000, fzf.sort assert_equal true, fzf.multi assert_equal false, fzf.color @@ -57,9 +60,10 @@ class TestFZF < MiniTest::Unit::TestCase assert_equal 'hello', fzf.query.get assert_equal 'howdy', fzf.filter assert_equal :exact, fzf.extended + assert_equal 1, fzf.nth fzf = FZF.new %w[--sort=2000 --no-color --multi +i --query hello - --filter a --filter b --no-256 --black + --filter a --filter b --no-256 --black --nth 2 --no-sort -i --color --no-multi --256] assert_equal nil, fzf.sort assert_equal false, fzf.multi @@ -71,9 +75,10 @@ class TestFZF < MiniTest::Unit::TestCase assert_equal 'b', fzf.filter assert_equal 'hello', fzf.query.get assert_equal nil, fzf.extended + assert_equal 2, fzf.nth # Short opts - fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy +2] + fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fhowdy +2 -n3] assert_equal 2000, fzf.sort assert_equal true, fzf.multi assert_equal false, fzf.color @@ -82,9 +87,10 @@ class TestFZF < MiniTest::Unit::TestCase assert_equal 'hello', fzf.query.get assert_equal 'howdy', fzf.filter assert_equal :fuzzy, fzf.extended + assert_equal 3, fzf.nth # Left-to-right - fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 + fzf = FZF.new %w[-s 2000 +c -m +i -qhello -x -fgoodbye +2 -n3 -n4 -s 3000 -c +m -i -q world +x -fworld -2 --black --no-black] assert_equal 3000, fzf.sort assert_equal false, fzf.multi @@ -95,6 +101,7 @@ class TestFZF < MiniTest::Unit::TestCase assert_equal 'world', fzf.query.get assert_equal 'world', fzf.filter assert_equal nil, fzf.extended + assert_equal 4, fzf.nth fzf = FZF.new %w[--query hello +s -s 2000 --query=world] assert_equal 2000, fzf.sort @@ -109,6 +116,12 @@ class TestFZF < MiniTest::Unit::TestCase fzf = FZF.new argv end end + assert_raises(SystemExit) do + fzf = FZF.new %w[--nth=0] + end + assert_raises(SystemExit) do + fzf = FZF.new %w[-n 0] + end end # FIXME Only on 1.9 or above @@ -476,5 +489,46 @@ class TestFZF < MiniTest::Unit::TestCase sleep interval assert_equal false, me.double?(20) end + + def test_nth_match + list = [ + ' first second third', + 'fourth fifth sixth', + ] + + matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE + assert_equal list, matcher.match(list, 'f', '', '').map(&:first) + assert_equal [ + [list[0], [[2, 5]]], + [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '') + + matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2 + assert_equal [[list[1], [[8, 9]]]], matcher.match(list, 'f', '', '') + assert_equal [[list[0], [[8, 9]]]], matcher.match(list, 's', '', '') + + matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 3 + assert_equal [[list[0], [[19, 20]]]], matcher.match(list, 'r', '', '') + + regex = FZF.build_delim_regex "\t" + matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 1, regex + assert_equal [[list[0], [[3, 10]]]], matcher.match(list, 're', '', '') + + matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2, regex + assert_equal [], matcher.match(list, 'r', '', '') + assert_equal [[list[1], [[9, 17]]]], matcher.match(list, 'is', '', '') + + # Negative indexing + matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, -1, regex + assert_equal [[list[0], [[3, 6]]]], matcher.match(list, 'rt', '', '') + assert_equal [[list[0], [[2, 5]]], [list[1], [[9, 17]]]], matcher.match(list, 'is', '', '') + + # Regex delimiter + regex = FZF.build_delim_regex "[ \t]+" + matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 1, regex + assert_equal [list[1]], matcher.match(list, 'f', '', '').map(&:first) + + matcher = FZF::FuzzyMatcher.new Regexp::IGNORECASE, 2, regex + assert_equal [[list[0], [[1, 2]]], [list[1], [[8, 9]]]], matcher.match(list, 'f', '', '') + end end -- cgit v1.2.3