pull/561/merge
Chris 9 months ago committed by GitHub
commit 5a9bf21aaf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -75,9 +75,10 @@ def match_consecutive(needles, haystack, ignore_case=False):
(path='/foo/baz', weight=10), (path='/foo/baz', weight=10),
] ]
""" """
regex_no_sep = '[^' + os.sep + ']*' sep = '\\\\' if os.sep == '\\' else os.sep
regex_no_sep = '[^' + sep + ']*'
regex_no_sep_end = regex_no_sep + '$' regex_no_sep_end = regex_no_sep + '$'
regex_one_sep = regex_no_sep + os.sep + regex_no_sep regex_one_sep = regex_no_sep + sep + regex_no_sep
regex_needle = regex_one_sep.join(imap(re.escape, needles)) + regex_no_sep_end regex_needle = regex_one_sep.join(imap(re.escape, needles)) + regex_no_sep_end
regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE regex_flags = re.IGNORECASE | re.UNICODE if ignore_case else re.UNICODE
found = lambda entry: re.search( found = lambda entry: re.search(

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import sys import sys
import platform
import pytest import pytest
@ -10,6 +11,7 @@ from autojump_data import Entry
from autojump_match import match_anywhere from autojump_match import match_anywhere
from autojump_match import match_consecutive from autojump_match import match_consecutive
is_windows = platform.system() == 'Windows'
class TestMatchAnywhere(object): class TestMatchAnywhere(object):
@ -19,8 +21,8 @@ class TestMatchAnywhere(object):
entry4 = Entry('/中/zhong/国/guo', 10) entry4 = Entry('/中/zhong/国/guo', 10)
entry5 = Entry('/is\'t/this/a/b*tchin/edge/case?', 10) entry5 = Entry('/is\'t/this/a/b*tchin/edge/case?', 10)
win_entry1 = Entry('C:\\foo\\bar\\baz', 10) win_entry1 = Entry('C:\\foo\\bar\\baz', 10)
win_entry2 = Entry('D:\Program Files (x86)\GIMP', 10) win_entry2 = Entry(r'D:\Program Files (x86)\GIMP', 10)
win_entry3 = Entry('C:\Windows\System32', 10) win_entry3 = Entry(r'C:\Windows\System32', 10)
@pytest.fixture @pytest.fixture
def haystack(self): def haystack(self):
@ -75,9 +77,10 @@ class TestMatchConsecutive(object):
entry4 = Entry('/中/zhong/国/guo', 10) entry4 = Entry('/中/zhong/国/guo', 10)
entry5 = Entry('/日/本', 10) entry5 = Entry('/日/本', 10)
entry6 = Entry('/is\'t/this/a/b*tchin/edge/case?', 10) entry6 = Entry('/is\'t/this/a/b*tchin/edge/case?', 10)
win_entry1 = Entry('C:\Foo\Bar\Baz', 10) win_entry1 = Entry(r'C:\Foo\Bar\Baz', 10)
win_entry2 = Entry('D:\Program Files (x86)\GIMP', 10) win_entry2 = Entry(r'D:\Program Files (x86)\GIMP', 10)
win_entry3 = Entry('C:\Windows\System32', 10) win_entry3 = Entry(r'C:\Windows\System32', 10)
win_entry4 = Entry('C:\\is\'t\\this\\a\\b*tchin\\edge\\case?', 10)
@pytest.fixture @pytest.fixture
def haystack(self): def haystack(self):
@ -91,18 +94,21 @@ class TestMatchConsecutive(object):
@pytest.fixture @pytest.fixture
def windows_haystack(self): def windows_haystack(self):
return [self.win_entry1, self.win_entry2, self.win_entry3] return [self.win_entry1, self.win_entry2, self.win_entry3, self.win_entry4]
@pytest.mark.skipif(is_windows, reason='Different reference data for path.')
def test_single_needle(self, haystack): def test_single_needle(self, haystack):
assert list(match_consecutive(['baz'], haystack)) == [self.entry1, self.entry3] assert list(match_consecutive(['baz'], haystack)) == [self.entry1, self.entry3]
assert list(match_consecutive([''], haystack)) == [self.entry5] assert list(match_consecutive([''], haystack)) == [self.entry5]
@pytest.mark.skipif(is_windows, reason='Different reference data for path.')
def test_consecutive(self, haystack): def test_consecutive(self, haystack):
assert list(match_consecutive(['bar', 'baz'], haystack)) == [self.entry1] assert list(match_consecutive(['bar', 'baz'], haystack)) == [self.entry1]
assert list(match_consecutive(['foo', 'bar'], haystack)) == [self.entry2] assert list(match_consecutive(['foo', 'bar'], haystack)) == [self.entry2]
assert list(match_consecutive(['', 'guo'], haystack)) == [self.entry4] assert list(match_consecutive(['', 'guo'], haystack)) == [self.entry4]
assert list(match_consecutive(['bar', 'foo'], haystack)) == [] assert list(match_consecutive(['bar', 'foo'], haystack)) == []
@pytest.mark.skipif(is_windows, reason='Different reference data for path.')
def test_ignore_case(self, haystack): def test_ignore_case(self, haystack):
assert list(match_consecutive(['FoO', 'bAR'], haystack, ignore_case=True)) \ assert list(match_consecutive(['FoO', 'bAR'], haystack, ignore_case=True)) \
== [self.entry2] == [self.entry2]
@ -110,22 +116,29 @@ class TestMatchConsecutive(object):
def test_windows_ignore_case(self, windows_haystack): def test_windows_ignore_case(self, windows_haystack):
assert list(match_consecutive(['gimp'], windows_haystack, True)) == [self.win_entry2] assert list(match_consecutive(['gimp'], windows_haystack, True)) == [self.win_entry2]
@pytest.mark.xfail(reason='https://github.com/wting/autojump/issues/418') @pytest.mark.skipif(not is_windows, reason='Different path seperator')
def test_backslashes_for_windows_paths(self, windows_haystack): def test_backslashes_for_windows_paths(self, windows_haystack):
# https://github.com/wting/autojump/issues/418
assert list(match_consecutive(['program', 'gimp'], windows_haystack, True)) \ assert list(match_consecutive(['program', 'gimp'], windows_haystack, True)) \
== [self.win_entry2] == [self.win_entry2]
@pytest.mark.xfail(reason='https://github.com/wting/autojump/issues/418') @pytest.mark.skipif(not is_windows, reason='Different path seperator')
def test_foo_bar_baz(self, windows_haystack): def test_foo_bar_baz(self, windows_haystack):
# https://github.com/wting/autojump/issues/418
assert list(match_consecutive(['bar', 'baz'], windows_haystack, ignore_case=True)) \ assert list(match_consecutive(['bar', 'baz'], windows_haystack, ignore_case=True)) \
== [self.win_entry1] == [self.win_entry1]
@pytest.mark.xfail(reason='https://github.com/wting/autojump/issues/402') @pytest.mark.skipif(not is_windows, reason='Different path seperator')
def test_thing(self, windows_haystack): def test_thing(self, windows_haystack):
assert list(match_consecutive(['win', '32'], windows_haystack, True)) \ assert list(match_consecutive(['win', '32'], windows_haystack, True)) \
== [self.win_entry3] == [self.win_entry3]
@pytest.mark.xfail(reason='https://github.com/wting/autojump/issues/402') @pytest.mark.skipif(is_windows, reason='Different reference data for path.')
def test_wildcard_in_needle(self, haystack): def test_wildcard_in_needle(self, haystack):
assert list(match_consecutive(['*', 'this'], haystack)) == [] assert list(match_consecutive(['*', 'this'], haystack)) == []
assert list(match_consecutive(['*', 'edge', 'case'], haystack)) == [self.entry6] assert list(match_consecutive(['*', 'edge', 'case'], haystack)) == [self.entry6]
@pytest.mark.skipif(not is_windows, reason='Different path seperator')
def test_wildcard_in_needle(self, windows_haystack):
assert list(match_consecutive(['*', 'this'], windows_haystack)) == []
assert list(match_consecutive(['*', 'edge', 'case'], windows_haystack)) == [self.win_entry4]

@ -2,6 +2,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import sys import sys
import platform
import mock import mock
import pytest import pytest
@ -21,6 +22,7 @@ from autojump_utils import surround_quotes
from autojump_utils import take from autojump_utils import take
from autojump_utils import unico from autojump_utils import unico
is_windows = platform.system() == 'Windows'
if is_python3(): if is_python3():
os.getcwdu = os.getcwd os.getcwdu = os.getcwd
@ -80,11 +82,15 @@ def test_surround_quotes_in_bash(_):
def test_dont_surround_quotes_not_in_bash(_): def test_dont_surround_quotes_not_in_bash(_):
assert surround_quotes('foo') == 'foo' assert surround_quotes('foo') == 'foo'
@pytest.mark.skipif(is_windows, reason='Different reference data for path.')
def test_sanitize(): def test_sanitize():
assert sanitize([]) == [] assert sanitize([]) == []
assert sanitize([r'/foo/bar/', r'/']) == [u('/foo/bar'), u('/')] assert sanitize([r'/foo/bar/', r'/']) == [u('/foo/bar'), u('/')]
@pytest.mark.skipif(not is_windows, reason='Different reference data for path.')
def test_sanitize_on_windows():
assert sanitize([]) == []
assert sanitize(['C:\\foo\\bar\\', 'C:\\']) == [u('C:\\foo\\bar'), u('C:')]
@pytest.mark.skipif(is_python3(), reason='Unicode sucks.') @pytest.mark.skipif(is_python3(), reason='Unicode sucks.')
def test_unico(): def test_unico():

Loading…
Cancel
Save