1
0
mirror of https://github.com/nvbn/thefuck.git synced 2024-10-06 02:41:10 +01:00
thefuck/tests/test_corrector.py

64 lines
2.7 KiB
Python
Raw Normal View History

# -*- coding: utf-8 -*-
2015-07-28 20:04:27 +01:00
import pytest
2016-02-22 15:40:28 +00:00
from thefuck import corrector, const
2016-08-14 13:15:03 +01:00
from thefuck.system import Path
2015-08-01 17:16:22 +01:00
from tests.utils import Rule, Command, CorrectedCommand
2015-09-08 12:18:11 +01:00
from thefuck.corrector import get_corrected_commands, organize_commands
2015-07-28 20:04:27 +01:00
class TestGetRules(object):
@pytest.fixture
2015-07-28 20:04:27 +01:00
def glob(self, mocker):
results = {}
2016-08-14 13:15:03 +01:00
mocker.patch('thefuck.system.Path.glob',
new_callable=lambda: lambda *_: results.pop('value', []))
return lambda value: results.update({'value': value})
2015-07-28 20:04:27 +01:00
@pytest.fixture(autouse=True)
def load_source(self, monkeypatch):
2015-09-08 12:18:11 +01:00
monkeypatch.setattr('thefuck.types.load_source',
2015-07-28 20:04:27 +01:00
lambda x, _: Rule(x))
def _compare_names(self, rules, names):
assert {r.name for r in rules} == set(names)
@pytest.mark.parametrize('paths, conf_rules, exclude_rules, loaded_rules', [
2016-02-22 15:40:28 +00:00
(['git.py', 'bash.py'], const.DEFAULT_RULES, [], ['git', 'bash']),
(['git.py', 'bash.py'], ['git'], [], ['git']),
2016-02-22 15:40:28 +00:00
(['git.py', 'bash.py'], const.DEFAULT_RULES, ['git'], ['bash']),
(['git.py', 'bash.py'], ['git'], ['git'], [])])
2015-09-07 10:12:16 +01:00
def test_get_rules(self, glob, settings, paths, conf_rules, exclude_rules,
loaded_rules):
2016-03-30 03:34:08 +01:00
glob([Path(path) for path in paths])
settings.update(rules=conf_rules,
priority={},
exclude_rules=exclude_rules)
2015-09-07 16:59:10 +01:00
rules = corrector.get_rules()
self._compare_names(rules, loaded_rules)
2015-07-28 20:04:27 +01:00
def test_get_corrected_commands(mocker):
command = Command('test', 'test', 'test')
rules = [Rule(match=lambda _: False),
Rule(match=lambda _: True,
get_new_command=lambda x: x.script + '!', priority=100),
Rule(match=lambda _: True,
get_new_command=lambda x: [x.script + '@', x.script + ';'],
2015-07-28 20:04:27 +01:00
priority=60)]
mocker.patch('thefuck.corrector.get_rules', return_value=rules)
2015-09-07 16:59:10 +01:00
assert [cmd.script for cmd in get_corrected_commands(command)] \
== ['test!', 'test@', 'test;']
def test_organize_commands():
"""Ensures that the function removes duplicates and sorts commands."""
commands = [CorrectedCommand('ls'), CorrectedCommand('ls -la', priority=9000),
CorrectedCommand('ls -lh', priority=100),
CorrectedCommand(u'echo café', priority=200),
CorrectedCommand('ls -lh', priority=9999)]
assert list(organize_commands(iter(commands))) \
== [CorrectedCommand('ls'), CorrectedCommand('ls -lh', priority=100),
CorrectedCommand(u'echo café', priority=200),
CorrectedCommand('ls -la', priority=9000)]