2015-05-02 04:29:55 +02:00
|
|
|
import pytest
|
2015-04-22 16:45:38 +02:00
|
|
|
from mock import Mock
|
2015-07-08 21:30:24 +03:00
|
|
|
from thefuck.utils import sudo_support, wrap_settings, memoize, get_closest
|
2015-04-25 02:35:26 +02:00
|
|
|
from thefuck.types import Settings
|
2015-07-10 17:58:41 +03:00
|
|
|
from tests.utils import Command
|
2015-04-22 20:18:53 +02:00
|
|
|
|
|
|
|
|
2015-05-02 04:29:55 +02:00
|
|
|
@pytest.mark.parametrize('override, old, new', [
|
|
|
|
({'key': 'val'}, {}, {'key': 'val'}),
|
|
|
|
({'key': 'new-val'}, {'key': 'val'}, {'key': 'new-val'})])
|
|
|
|
def test_wrap_settings(override, old, new):
|
2015-04-22 23:04:22 +02:00
|
|
|
fn = lambda _, settings: settings
|
2015-05-02 04:29:55 +02:00
|
|
|
assert wrap_settings(override)(fn)(None, Settings(old)) == new
|
2015-04-22 16:45:38 +02:00
|
|
|
|
|
|
|
|
2015-05-02 04:29:55 +02:00
|
|
|
@pytest.mark.parametrize('return_value, command, called, result', [
|
|
|
|
('ls -lah', 'sudo ls', 'ls', 'sudo ls -lah'),
|
|
|
|
('ls -lah', 'ls', 'ls', 'ls -lah'),
|
|
|
|
(True, 'sudo ls', 'ls', True),
|
|
|
|
(True, 'ls', 'ls', True),
|
|
|
|
(False, 'sudo ls', 'ls', False),
|
|
|
|
(False, 'ls', 'ls', False)])
|
|
|
|
def test_sudo_support(return_value, command, called, result):
|
|
|
|
fn = Mock(return_value=return_value, __name__='')
|
|
|
|
assert sudo_support(fn)(Command(command), None) == result
|
|
|
|
fn.assert_called_once_with(Command(called), None)
|
2015-05-22 17:07:01 +03:00
|
|
|
|
|
|
|
|
|
|
|
def test_memoize():
|
|
|
|
fn = Mock(__name__='fn')
|
|
|
|
memoized = memoize(fn)
|
|
|
|
memoized()
|
|
|
|
memoized()
|
|
|
|
fn.assert_called_once_with()
|
2015-07-08 21:30:24 +03:00
|
|
|
|
|
|
|
|
2015-07-10 17:06:05 +03:00
|
|
|
@pytest.mark.usefixtures('no_memoize')
|
|
|
|
def test_no_memoize():
|
|
|
|
fn = Mock(__name__='fn')
|
|
|
|
memoized = memoize(fn)
|
|
|
|
memoized()
|
|
|
|
memoized()
|
|
|
|
assert fn.call_count == 2
|
|
|
|
|
|
|
|
|
2015-07-08 21:30:24 +03:00
|
|
|
class TestGetClosest(object):
|
|
|
|
|
|
|
|
def test_when_can_match(self):
|
|
|
|
assert 'branch' == get_closest('brnch', ['branch', 'status'])
|
|
|
|
|
|
|
|
def test_when_cant_match(self):
|
|
|
|
assert 'status' == get_closest('st', ['status', 'reset'])
|