replaced custom command pattern exceptions with attributeerror exceptions.

This commit is contained in:
tylerlaberge
2016-08-28 20:27:18 -04:00
parent ccc203f1e6
commit 7501604bbf
2 changed files with 5 additions and 20 deletions

View File

@@ -1,20 +1,6 @@
from abc import ABCMeta, abstractmethod
class InvalidActionException(Exception):
"""
Exception for when an invalid action is called on a Receiver.
"""
pass
class InvalidInvokerCommandException(Exception):
"""
Exception for when an invalid command is given to an Invoker to execute.
"""
pass
class Receiver(object, metaclass=ABCMeta):
"""
Abstract receiver class as part of the Command pattern.
@@ -33,7 +19,7 @@ class Receiver(object, metaclass=ABCMeta):
try:
return getattr(self, name)(*args, **kwargs)
except AttributeError:
raise InvalidActionException
raise AttributeError('Invalid Action.')
class Command(object, metaclass=ABCMeta):
@@ -89,7 +75,7 @@ class Invoker(object, metaclass=ABCMeta):
@type command: Command
"""
if command.__class__ not in self._valid_commands:
raise InvalidInvokerCommandException
raise AttributeError('Invalid Command')
else:
self._history.append(command)
return command.execute()

View File

@@ -1,7 +1,6 @@
from unittest import TestCase
from pypatterns.behavioral.command import InvalidActionException, InvalidInvokerCommandException, \
Receiver, Command, Invoker
from pypatterns.behavioral.command import Receiver, Command, Invoker
class ReceiverTestCase(TestCase):
@@ -38,7 +37,7 @@ class ReceiverTestCase(TestCase):
@raise AssertionError: If the test fails.
"""
with self.assertRaises(InvalidActionException):
with self.assertRaises(AttributeError):
self.thermostat.action('foo')
@@ -176,7 +175,7 @@ class InvokerTestCase(TestCase):
def unexecute(self):
return self._receiver.action('turn_off')
with self.assertRaises(InvalidInvokerCommandException):
with self.assertRaises(AttributeError):
self.worker.execute(TurnOnLightCommand(Light))
def test_undo(self):