Commit 57e89685 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Throw InvalidTodoException when the regex doesn't have exactly 1 hit.

parent a7d51550
...@@ -86,7 +86,7 @@ class DeleteCommandTest(CommandTest.CommandTest): ...@@ -86,7 +86,7 @@ class DeleteCommandTest(CommandTest.CommandTest):
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "") self.assertEquals(self.output, "")
self.assertEquals(self.errors, command.usage() + "\n") self.assertEquals(self.errors, "Invalid todo number given.\n")
def test_empty(self): def test_empty(self):
command = DeleteCommand.DeleteCommand([], self.todolist, self.out, self.error) command = DeleteCommand.DeleteCommand([], self.todolist, self.out, self.error)
......
...@@ -138,7 +138,7 @@ class DoCommandTest(CommandTest.CommandTest): ...@@ -138,7 +138,7 @@ class DoCommandTest(CommandTest.CommandTest):
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertFalse(self.output) self.assertFalse(self.output)
self.assertEquals(self.errors, command.usage() + "\n") self.assertEquals(self.errors, "Invalid todo number given.\n")
def test_activated_todos1(self): def test_activated_todos1(self):
command = DoCommand.DoCommand(["2"], self.todolist, self.out, self.error) command = DoCommand.DoCommand(["2"], self.todolist, self.out, self.error)
......
...@@ -182,11 +182,11 @@ class TodoListTester(unittest.TestCase): ...@@ -182,11 +182,11 @@ class TodoListTester(unittest.TestCase):
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
def test_regex1(self): def test_regex1(self):
self.assertFalse(self.todolist.todo("1")) self.assertRaises(TodoList.InvalidTodoException, self.todolist.todo, "1")
def test_regex2(self): def test_regex2(self):
""" Multiple hits should result in None. """ """ Multiple hits should result in None. """
self.assertFalse(self.todolist.todo("Project1")) self.assertRaises(TodoList.InvalidTodoException, self.todolist.todo, "Project1")
def test_regex3(self): def test_regex3(self):
todo = self.todolist.todo("project2") todo = self.todolist.todo("project2")
......
...@@ -41,9 +41,11 @@ class DCommand(Command): ...@@ -41,9 +41,11 @@ class DCommand(Command):
except (InvalidCommandArgument, InvalidTodoException): except (InvalidCommandArgument, InvalidTodoException):
self.todo = None self.todo = None
except InvalidTodoNumberException: except InvalidTodoNumberException:
self.todo = self.todolist.todo(self.argument(0)) try:
if self.todo: self.todo = self.todolist.todo(self.argument(0))
self.number = self.todolist.number(self.todo) self.number = self.todolist.number(self.todo)
except InvalidTodoException:
self.todo = None
def _uncompleted_children(self, p_todo): def _uncompleted_children(self, p_todo):
return sorted([t for t in self.todolist.children(p_todo) if not t.is_completed()]) return sorted([t for t in self.todolist.children(p_todo) if not t.is_completed()])
...@@ -115,16 +117,16 @@ class DCommand(Command): ...@@ -115,16 +117,16 @@ class DCommand(Command):
if not super(DCommand, self).execute(): if not super(DCommand, self).execute():
return False return False
if not self.number: if len(self.args) == 0:
self.error(self.usage()) self.error(self.usage())
elif not self.todo:
self.error("Invalid todo number given.")
elif self.todo and self.condition(): elif self.todo and self.condition():
old_active = self._active_todos() old_active = self._active_todos()
self._process_subtasks() self._process_subtasks()
self.execute_specific() self.execute_specific()
current_active = self._active_todos() current_active = self._active_todos()
self._print_unlocked_todos(old_active, current_active) self._print_unlocked_todos(old_active, current_active)
elif not self.todo:
self.error("Invalid todo number given.")
else: else:
self.error(self.condition_failed_text()) self.error(self.condition_failed_text())
...@@ -78,6 +78,8 @@ class TodoList(object): ...@@ -78,6 +78,8 @@ class TodoList(object):
if len(candidates) == 1: if len(candidates) == 1:
result = candidates[0] result = candidates[0]
else:
raise InvalidTodoException
return result return result
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment