Skip to content

Commit

Permalink
Change register to an instance method to bind run to func.
Browse files Browse the repository at this point in the history
  • Loading branch information
mblayman committed Jan 5, 2016
1 parent f91ed48 commit baece8b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 7 deletions.
15 changes: 11 additions & 4 deletions handroll/commands/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,20 @@ def finish():
class Command(object):
"""A command class with the minimal interface required for each command."""

@classmethod
def register(cls, parser):
name = 'command'
description = 'the command description'
help = 'the command help'

def register(self, subparsers):
"""Register required options.
The provided parser is a subparser from ``subparsers.add_parser``.
Commands should invoke this method and use the returned parser to
add command specific options.
"""
raise NotImplementedError()
parser = subparsers.add_parser(
self.name, description=self.description, help=self.help)
parser.set_defaults(func=self.run)
return parser

def run(self, args):
"""Run whatever action the command intends."""
Expand Down
24 changes: 21 additions & 3 deletions handroll/tests/test_commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,27 @@

class TestCommand(TestCase):

def test_register_not_implemented(self):
parser = mock.Mock()
self.assertRaises(NotImplementedError, Command.register, parser)
def test_register_returns_parser(self):
subparsers = mock.Mock()
expected_parser = mock.Mock()
subparsers.add_parser.return_value = expected_parser
command = Command()
parser = command.register(subparsers)
self.assertEqual(expected_parser, parser)

def test_register_command_attributes(self):
subparsers = mock.Mock()
command = Command()
command.register(subparsers)
subparsers.add_parser.assert_called_once_with(
'command', description='the command description',
help='the command help')

def test_register_run_as_func(self):
subparsers = mock.Mock()
command = Command()
parser = command.register(subparsers)
parser.set_defaults.assert_called_once_with(func=command.run)

def test_run_not_implemented(self):
args = mock.Mock()
Expand Down

0 comments on commit baece8b

Please sign in to comment.