1
0
mirror of https://github.com/wting/autojump synced 2024-10-27 20:34:07 +00:00

Add a u() wrapper to enable testing to work in both Python2 and Python3.

This commit is contained in:
William Ting 2014-06-28 19:34:45 -07:00
parent 567ae121ab
commit 921b52e9d5

View File

@ -6,16 +6,14 @@ import sys
import mock import mock
import pytest import pytest
if sys.version_info[0] == 3:
os.getcwdu = os.getcwd
sys.path.append(os.path.join(os.getcwd(), 'bin')) sys.path.append(os.path.join(os.getcwd(), 'bin'))
import autojump_utils import autojump_utils
from autojump_utils import encode_local from autojump_utils import encode_local
from autojump_utils import first from autojump_utils import first
from autojump_utils import get_tab_entry_info from autojump_utils import get_tab_entry_info
from autojump_utils import has_uppercase from autojump_utils import has_uppercase
from autojump_utils import in_bash from autojump_utils import in_bash
from autojump_utils import is_python3
from autojump_utils import last from autojump_utils import last
from autojump_utils import sanitize from autojump_utils import sanitize
from autojump_utils import second from autojump_utils import second
@ -24,10 +22,23 @@ from autojump_utils import take
from autojump_utils import unico from autojump_utils import unico
if is_python3():
os.getcwdu = os.getcwd
def u(string):
"""
This is a unicode() wrapper since u'string' fails in Python3.
"""
if is_python3():
return string
return unicode(string, encoding='utf-8', errors='strict')
# strings # strings
@mock.patch.object(sys, 'getfilesystemencoding', return_value='ascii') @mock.patch.object(sys, 'getfilesystemencoding', return_value='ascii')
def test_encode_local_ascii(_): def test_encode_local_ascii(_):
assert encode_local(u'foo') == b'foo' assert encode_local(u('foo')) == b'foo'
@pytest.mark.xfail(reason='issue #246') @pytest.mark.xfail(reason='issue #246')
@ -37,18 +48,18 @@ def test_encode_local_ascii_fails():
sys, sys,
'getfilesystemencoding', 'getfilesystemencoding',
return_value='ascii'): return_value='ascii'):
encode_local(u'日本語') encode_local(u('日本語'))
@mock.patch.object(sys, 'getfilesystemencoding', return_value=None) @mock.patch.object(sys, 'getfilesystemencoding', return_value=None)
def test_encode_local_empty(_): def test_encode_local_empty(_):
assert encode_local(b'foo') == u'foo' assert encode_local(b'foo') == u('foo')
@mock.patch.object(sys, 'getfilesystemencoding', return_value='utf-8') @mock.patch.object(sys, 'getfilesystemencoding', return_value='utf-8')
def test_encode_local_unicode(_): def test_encode_local_u(_):
assert encode_local(b'foo') == u'foo' assert encode_local(b'foo') == u('foo')
assert encode_local(u'foo') == u'foo' assert encode_local(u('foo')) == u('foo')
def test_has_uppercase(): def test_has_uppercase():
@ -70,13 +81,13 @@ def test_dont_surround_quotes_not_in_bash(_):
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('/')]
def test_unico(): def test_unico():
assert unico(b'blah') == u'blah' assert unico(str('blah')) == u('blah')
assert unico(b'日本語') == u'日本語' assert unico(str('日本語')) == u('日本語')
assert unico(u'でもおれは中国人だ。') == u'でもおれは中国人だ。' assert unico(u('でもおれは中国人だ。')) == u('でもおれは中国人だ。')
# iteration # iteration