From 698451f65dd35c3dc0a150118f421548b71a0d51 Mon Sep 17 00:00:00 2001 From: nvbn Date: Sat, 25 Apr 2015 02:54:39 +0200 Subject: [PATCH] Use parametrized tests where it possible --- tests/rules/test_cd_mkdir.py | 34 +++++++++++++++++------------ tests/rules/test_mkdir_p.py | 12 ++++++++--- tests/rules/test_rm_dir.py | 19 +++++++++++------ tests/rules/test_rm_root.py | 25 ++++++++++++---------- tests/rules/test_sudo.py | 13 +++++++---- tests/rules/test_switch_lang.py | 38 +++++++++++++++++---------------- 6 files changed, 85 insertions(+), 56 deletions(-) diff --git a/tests/rules/test_cd_mkdir.py b/tests/rules/test_cd_mkdir.py index b3db2926..ae5449ff 100644 --- a/tests/rules/test_cd_mkdir.py +++ b/tests/rules/test_cd_mkdir.py @@ -1,19 +1,25 @@ -from mock import Mock +import pytest from thefuck.rules.cd_mkdir import match, get_new_command +from tests.utils import Command -def test_match(): - assert match(Mock(script='cd foo', stderr='cd: foo: No such file or directory'), - None) - assert match(Mock(script='cd foo/bar/baz', stderr='cd: foo: No such file or directory'), - None) - assert match(Mock(script='cd foo/bar/baz', stderr='cd: can\'t cd to foo/bar/baz'), - None) - assert not match(Mock(script='cd foo', - stderr=''), None) - assert not match(Mock(script='', stderr=''), None) +@pytest.mark.parametrize('command', [ + Command(script='cd foo', stderr='cd: foo: No such file or directory'), + Command(script='cd foo/bar/baz', + stderr='cd: foo: No such file or directory'), + Command(script='cd foo/bar/baz', stderr='cd: can\'t cd to foo/bar/baz')]) +def test_match(command): + assert match(command, None) -def test_get_new_command(): - assert get_new_command(Mock(script='cd foo'), None) == 'mkdir -p foo && cd foo' - assert get_new_command(Mock(script='cd foo/bar/baz'), None) == 'mkdir -p foo/bar/baz && cd foo/bar/baz' +@pytest.mark.parametrize('command', [ + Command(script='cd foo', stderr=''), Command()]) +def test_not_match(command): + assert not match(command, None) + + +@pytest.mark.parametrize('command, new_command', [ + (Command('cd foo'), 'mkdir -p foo && cd foo'), + (Command('cd foo/bar/baz'), 'mkdir -p foo/bar/baz && cd foo/bar/baz')]) +def test_get_new_command(command, new_command): + assert get_new_command(command, None) == new_command diff --git a/tests/rules/test_mkdir_p.py b/tests/rules/test_mkdir_p.py index 60b6b657..d1b91a77 100644 --- a/tests/rules/test_mkdir_p.py +++ b/tests/rules/test_mkdir_p.py @@ -1,3 +1,4 @@ +import pytest from thefuck.rules.mkdir_p import match, get_new_command from tests.utils import Command @@ -6,9 +7,14 @@ def test_match(): assert match(Command('mkdir foo/bar/baz', stderr='mkdir: foo/bar: No such file or directory'), None) - assert not match(Command('mkdir foo/bar/baz'), None) - assert not match(Command('mkdir foo/bar/baz', stderr='foo bar baz'), None) - assert not match(Command(), None) + + +@pytest.mark.parametrize('command', [ + Command('mkdir foo/bar/baz'), + Command('mkdir foo/bar/baz', stderr='foo bar baz'), + Command()]) +def test_not_match(command): + assert not match(command, None) def test_get_new_command(): diff --git a/tests/rules/test_rm_dir.py b/tests/rules/test_rm_dir.py index 573bbdc4..c334d2ff 100644 --- a/tests/rules/test_rm_dir.py +++ b/tests/rules/test_rm_dir.py @@ -1,13 +1,20 @@ +import pytest from thefuck.rules.rm_dir import match, get_new_command from tests.utils import Command -def test_match(): - assert match(Command('rm foo', stderr='rm: foo: is a directory'), None) - assert match(Command('rm foo', stderr='rm: foo: Is a directory'), None) - assert not match(Command('rm foo'), None) - assert not match(Command('rm foo', stderr='foo bar baz'), None) - assert not match(Command(), None) +@pytest.mark.parametrize('command', [ + Command('rm foo', stderr='rm: foo: is a directory'), + Command('rm foo', stderr='rm: foo: Is a directory')]) +def test_match(command): + assert match(command, None) + assert match(command, None) + + +@pytest.mark.parametrize('command', [ + Command('rm foo'), Command('rm foo'), Command()]) +def test_not_match(command): + assert not match(command, None) def test_get_new_command(): diff --git a/tests/rules/test_rm_root.py b/tests/rules/test_rm_root.py index 003cb800..f56595fc 100644 --- a/tests/rules/test_rm_root.py +++ b/tests/rules/test_rm_root.py @@ -1,18 +1,21 @@ -from mock import Mock +import pytest from thefuck.rules.rm_root import match, get_new_command +from tests.utils import Command def test_match(): - assert match(Mock(script='rm -rf /', - stderr='add --no-preserve-root'), None) - assert not match(Mock(script='ls', - stderr='add --no-preserve-root'), None) - assert not match(Mock(script='rm --no-preserve-root /', - stderr='add --no-preserve-root'), None) - assert not match(Mock(script='rm -rf /', - stderr=''), None) + assert match(Command(script='rm -rf /', + stderr='add --no-preserve-root'), None) + + +@pytest.mark.parametrize('command', [ + Command(script='ls', stderr='add --no-preserve-root'), + Command(script='rm --no-preserve-root /', stderr='add --no-preserve-root'), + Command(script='rm -rf /', stderr='')]) +def test_not_match(command): + assert not match(command, None) def test_get_new_command(): - assert get_new_command(Mock(script='rm -rf /'), None) \ - == 'rm -rf / --no-preserve-root' + assert get_new_command(Command(script='rm -rf /'), None) \ + == 'rm -rf / --no-preserve-root' diff --git a/tests/rules/test_sudo.py b/tests/rules/test_sudo.py index 47542c89..6bac8825 100644 --- a/tests/rules/test_sudo.py +++ b/tests/rules/test_sudo.py @@ -1,11 +1,16 @@ +import pytest from thefuck.rules.sudo import match, get_new_command from tests.utils import Command -def test_match(): - assert match(Command(stderr='Permission denied'), None) - assert match(Command(stderr='permission denied'), None) - assert match(Command(stderr="npm ERR! Error: EACCES, unlink"), None) +@pytest.mark.parametrize('stderr', ['Permission denied', + 'permission denied', + "npm ERR! Error: EACCES, unlink"]) +def test_match(stderr): + assert match(Command(stderr=stderr), None) + + +def test_not_match(): assert not match(Command(), None) diff --git a/tests/rules/test_switch_lang.py b/tests/rules/test_switch_lang.py index 991f3983..12163d4b 100644 --- a/tests/rules/test_switch_lang.py +++ b/tests/rules/test_switch_lang.py @@ -1,25 +1,27 @@ # -*- encoding: utf-8 -*- -from mock import Mock +import pytest from thefuck.rules import switch_lang +from tests.utils import Command -def test_match(): - assert switch_lang.match(Mock(stderr='command not found: фзе-пуе', - script=u'фзе-пуе'), None) - assert switch_lang.match(Mock(stderr='command not found: λσ', - script=u'λσ'), None) - - assert not switch_lang.match(Mock(stderr='command not found: pat-get', - script=u'pat-get'), None) - assert not switch_lang.match(Mock(stderr='command not found: ls', - script=u'ls'), None) - assert not switch_lang.match(Mock(stderr='some info', - script=u'фзе-пуе'), None) +@pytest.mark.parametrize('command', [ + Command(stderr='command not found: фзе-пуе', script=u'фзе-пуе'), + Command(stderr='command not found: λσ', script=u'λσ')]) +def test_match(command): + assert switch_lang.match(command, None) -def test_get_new_command(): - assert switch_lang.get_new_command( - Mock(script=u'фзе-пуе штыефдд мшь'), None) == 'apt-get install vim' - assert switch_lang.get_new_command( - Mock(script=u'λσ -λα'), None) == 'ls -la' +@pytest.mark.parametrize('command', [ + Command(stderr='command not found: pat-get', script=u'pat-get'), + Command(stderr='command not found: ls', script=u'ls'), + Command(stderr='some info', script=u'фзе-пуе')]) +def test_not_match(command): + assert not switch_lang.match(command, None) + + +@pytest.mark.parametrize('command, new_command', [ + (Command(u'фзе-пуе штыефдд мшь'), 'apt-get install vim'), + (Command(u'λσ -λα'), 'ls -la')]) +def test_get_new_command(command, new_command): + assert switch_lang.get_new_command(command, None) == new_command