1
0
mirror of https://github.com/wting/autojump synced 2024-09-28 14:00:46 +00:00
wting_autojump/tests/autojump_utils_test.py

133 lines
3.3 KiB
Python
Raw Normal View History

2014-01-02 21:56:43 +00:00
#!/usr/bin/env python
2013-12-31 22:33:32 +00:00
# -*- coding: utf-8 -*-
2013-12-31 23:15:25 +00:00
import os
import sys
2013-12-31 23:15:25 +00:00
2013-12-31 22:34:35 +00:00
import mock
import pytest
2013-12-31 22:33:32 +00:00
# Python 3 compatibility
2014-01-11 13:09:19 +00:00
if sys.version_info[0] == 3:
os.getcwdu = os.getcwd
sys.path.append(os.path.join(os.getcwd(), 'bin'))
2013-12-31 22:34:35 +00:00
import autojump_utils
from autojump_utils import encode_local
2013-12-31 22:33:32 +00:00
from autojump_utils import first
2014-01-01 00:39:45 +00:00
from autojump_utils import get_tab_entry_info
2013-12-31 23:41:12 +00:00
from autojump_utils import has_uppercase
2013-12-31 23:15:25 +00:00
from autojump_utils import in_bash
2013-12-31 22:33:32 +00:00
from autojump_utils import last
2013-12-31 22:34:35 +00:00
from autojump_utils import sanitize
from autojump_utils import second
from autojump_utils import surround_quotes
2013-12-31 22:33:32 +00:00
from autojump_utils import take
from autojump_utils import unico
2013-12-31 22:33:32 +00:00
# strings
@mock.patch.object(sys, 'getfilesystemencoding', return_value='ascii')
def test_encode_local_ascii(_):
assert encode_local(u'foo') == b'foo'
@pytest.mark.xfail(reason='issue #246')
def test_encode_local_ascii_fails():
with pytest.raises(UnicodeDecodeError):
with mock.patch.object(
sys,
'getfilesystemencoding',
return_value='ascii'):
encode_local(u'日本語')
@mock.patch.object(sys, 'getfilesystemencoding', return_value=None)
def test_encode_local_empty(_):
assert encode_local(b'foo') == u'foo'
@mock.patch.object(sys, 'getfilesystemencoding', return_value='utf-8')
def test_encode_local_unicode(_):
assert encode_local(b'foo') == u'foo'
assert encode_local(u'foo') == u'foo'
def test_has_uppercase():
assert has_uppercase('Foo')
assert has_uppercase('foO')
assert not has_uppercase('foo')
assert not has_uppercase('')
@mock.patch.object(autojump_utils, 'in_bash', return_value=True)
def test_surround_quotes_in_bash(_):
assert surround_quotes('foo') == '"foo"'
@mock.patch.object(autojump_utils, 'in_bash', return_value=False)
def test_dont_surround_quotes_not_in_bash(_):
assert surround_quotes('foo') == 'foo'
def test_sanitize():
assert sanitize([]) == []
assert sanitize([r'/foo/bar/', r'/']) == [u'/foo/bar', u'/']
def test_unico():
assert unico(b'blah') == u'blah'
assert unico(b'日本語') == u'日本語'
assert unico(u'でもおれは中国人だ。') == u'でもおれは中国人だ。'
# iteration
def test_first():
assert first(xrange(5)) == 0
assert first([]) is None
def test_second():
assert second(xrange(5)) == 1
assert second([]) is None
def test_last():
assert last(xrange(4)) == 3
assert last([]) is None
def test_take():
assert list(take(1, xrange(3))) == [0]
assert list(take(2, xrange(3))) == [0, 1]
assert list(take(4, xrange(3))) == [0, 1, 2]
assert list(take(10, [])) == []
# environment variables
def test_in_bash():
for path in ['/bin/bash', '/usr/bin/bash']:
os.environ['SHELL'] = path
assert in_bash()
for path in ['/bin/zsh', '/usr/bin/zsh']:
2013-12-31 23:15:25 +00:00
os.environ['SHELL'] = '/usr/bin/zsh'
assert not in_bash()
# helper functions
def test_get_needle():
assert get_tab_entry_info('foo__', '__') == ('foo', None, None)
def test_get_index():
assert get_tab_entry_info('foo__2', '__') == ('foo', 2, None)
def test_get_path():
assert get_tab_entry_info('foo__3__/foo/bar', '__') \
== ('foo', 3, '/foo/bar')
def test_get_none():
assert get_tab_entry_info('gibberish content', '__') == (None, None, None)