mirror of
https://github.com/nvbn/thefuck.git
synced 2025-02-21 20:38:54 +00:00
Make CorrectedCommand
ignore priority when checking equality
This commit is contained in:
parent
12394ca842
commit
5d74344994
@ -44,3 +44,16 @@ class TestSortedCorrectedCommandsSequence(object):
|
|||||||
settings)
|
settings)
|
||||||
assert set(seq) == {CorrectedCommand('ls', priority=100),
|
assert set(seq) == {CorrectedCommand('ls', priority=100),
|
||||||
CorrectedCommand('ls', side_effect, 300)}
|
CorrectedCommand('ls', side_effect, 300)}
|
||||||
|
|
||||||
|
|
||||||
|
class TestCorrectedCommand(object):
|
||||||
|
|
||||||
|
def test_equality(self):
|
||||||
|
assert CorrectedCommand('ls', None, 100) == \
|
||||||
|
CorrectedCommand('ls', None, 200)
|
||||||
|
assert CorrectedCommand('ls', None, 100) != \
|
||||||
|
CorrectedCommand('ls', lambda *_: _, 100)
|
||||||
|
|
||||||
|
def test_hashable(self):
|
||||||
|
assert {CorrectedCommand('ls', None, 100),
|
||||||
|
CorrectedCommand('ls', None, 200)} == {CorrectedCommand('ls')}
|
||||||
|
@ -4,12 +4,31 @@ from .logs import debug
|
|||||||
|
|
||||||
Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
Command = namedtuple('Command', ('script', 'stdout', 'stderr'))
|
||||||
|
|
||||||
CorrectedCommand = namedtuple('CorrectedCommand', ('script', 'side_effect', 'priority'))
|
|
||||||
|
|
||||||
Rule = namedtuple('Rule', ('name', 'match', 'get_new_command',
|
Rule = namedtuple('Rule', ('name', 'match', 'get_new_command',
|
||||||
'enabled_by_default', 'side_effect',
|
'enabled_by_default', 'side_effect',
|
||||||
'priority', 'requires_output'))
|
'priority', 'requires_output'))
|
||||||
|
|
||||||
|
class CorrectedCommand(object):
|
||||||
|
def __init__(self, script, side_effect, priority):
|
||||||
|
self.script = script
|
||||||
|
self.side_effect = side_effect
|
||||||
|
self.priority = priority
|
||||||
|
|
||||||
|
def __eq__(self, other):
|
||||||
|
"""Ignores `priority` field."""
|
||||||
|
if isinstance(other, CorrectedCommand):
|
||||||
|
return (other.script, other.side_effect) ==\
|
||||||
|
(self.script, self.side_effect)
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
def __hash__(self):
|
||||||
|
return (self.script, self.side_effect).__hash__()
|
||||||
|
|
||||||
|
def __repr__(self):
|
||||||
|
return 'CorrectedCommand(script={}, side_effect={}, priority={})'.format(
|
||||||
|
self.script, self.side_effect, self.priority)
|
||||||
|
|
||||||
|
|
||||||
class RulesNamesList(list):
|
class RulesNamesList(list):
|
||||||
"""Wrapper a top of list for storing rules names."""
|
"""Wrapper a top of list for storing rules names."""
|
||||||
@ -54,19 +73,17 @@ class SortedCorrectedCommandsSequence(object):
|
|||||||
return []
|
return []
|
||||||
|
|
||||||
for command in self._commands:
|
for command in self._commands:
|
||||||
if command.script != first.script or \
|
if command != first:
|
||||||
command.side_effect != first.side_effect:
|
|
||||||
return [first, command]
|
return [first, command]
|
||||||
return [first]
|
return [first]
|
||||||
|
|
||||||
def _remove_duplicates(self, corrected_commands):
|
def _remove_duplicates(self, corrected_commands):
|
||||||
"""Removes low-priority duplicates."""
|
"""Removes low-priority duplicates."""
|
||||||
commands = {(command.script, command.side_effect): command
|
commands = {command
|
||||||
for command in sorted(corrected_commands,
|
for command in sorted(corrected_commands,
|
||||||
key=lambda command: -command.priority)
|
key=lambda command: -command.priority)
|
||||||
if command.script != self._cached[0].script
|
if command.script != self._cached[0]}
|
||||||
or command.side_effect != self._cached[0].side_effect}
|
return commands
|
||||||
return commands.values()
|
|
||||||
|
|
||||||
def _realise(self):
|
def _realise(self):
|
||||||
"""Realises generator, removes duplicates and sorts commands."""
|
"""Realises generator, removes duplicates and sorts commands."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user