Commit 1f594ca6 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Let TodoList throw an exception when invalid todos are requested.

parent 09584282
...@@ -7,6 +7,7 @@ import Config ...@@ -7,6 +7,7 @@ import Config
import Command import Command
from PrettyPrinter import pretty_print from PrettyPrinter import pretty_print
from RelativeDate import relative_date_to_date from RelativeDate import relative_date_to_date
import TodoList
class AddCommand(Command.Command): class AddCommand(Command.Command):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
...@@ -46,14 +47,15 @@ class AddCommand(Command.Command): ...@@ -46,14 +47,15 @@ class AddCommand(Command.Command):
try: try:
value = int(raw_value) value = int(raw_value)
dep = self.todolist.todo(value) dep = self.todolist.todo(value)
except ValueError:
continue
if dep:
if p_tag == 'after': if p_tag == 'after':
self.todolist.add_dependency(self.todo, dep) self.todolist.add_dependency(self.todo, dep)
elif p_tag == 'before' or p_tag == 'partof': elif p_tag == 'before' or p_tag == 'partof':
self.todolist.add_dependency(dep, self.todo) self.todolist.add_dependency(dep, self.todo)
except ValueError:
continue
except TodoList.InvalidTodoException:
pass
self.todo.remove_tag(p_tag, raw_value) self.todo.remove_tag(p_tag, raw_value)
......
import Command import Command
from PrettyPrinter import pretty_print from PrettyPrinter import pretty_print
import TodoList
from Utils import convert_todo_number from Utils import convert_todo_number
class AppendCommand(Command.Command): class AppendCommand(Command.Command):
...@@ -16,11 +17,11 @@ class AppendCommand(Command.Command): ...@@ -16,11 +17,11 @@ class AppendCommand(Command.Command):
text = " ".join(self.args[1:]) text = " ".join(self.args[1:])
if number and text: if number and text:
todo = self.todolist.todo(number) try:
if todo: todo = self.todolist.todo(number)
self.todolist.append(todo, text) self.todolist.append(todo, text)
self.out(pretty_print(todo, [self.todolist.pp_number()])) self.out(pretty_print(todo, [self.todolist.pp_number()]))
else: except TodoList.InvalidTodoException:
self.error("Invalid todo number given.") self.error("Invalid todo number given.")
else: else:
self.error(self.usage()) self.error(self.usage())
...@@ -9,6 +9,9 @@ from PrettyPrinter import pretty_print_list ...@@ -9,6 +9,9 @@ from PrettyPrinter import pretty_print_list
import Todo import Todo
import View import View
class InvalidTodoException(Exception):
pass
class TodoList(object): class TodoList(object):
""" """
Provides operations for a todo list, such as adding items, removing them, Provides operations for a todo list, such as adding items, removing them,
...@@ -41,7 +44,7 @@ class TodoList(object): ...@@ -41,7 +44,7 @@ class TodoList(object):
try: try:
result = self._todos[p_number - 1] result = self._todos[p_number - 1]
except IndexError: except IndexError:
result = None raise InvalidTodoException
return result return result
...@@ -284,7 +287,10 @@ class TodoList(object): ...@@ -284,7 +287,10 @@ class TodoList(object):
self.dirty = True self.dirty = True
def number(self, p_todo): def number(self, p_todo):
return self._todos.index(p_todo) + 1 try:
return self._todos.index(p_todo) + 1
except ValueError:
raise InvalidTodoException
def pp_number(self): def pp_number(self):
""" """
......
...@@ -78,7 +78,7 @@ class TodoListTester(unittest.TestCase): ...@@ -78,7 +78,7 @@ class TodoListTester(unittest.TestCase):
"(C) Baz @Context1 +Project1 key:value") "(C) Baz @Context1 +Project1 key:value")
self.assertEquals(self.todolist.count(), count - 1) self.assertEquals(self.todolist.count(), count - 1)
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
self.assertRaises(ValueError, self.todolist.number, todo) self.assertRaises(TodoList.InvalidTodoException, self.todolist.number, todo)
def test_append1(self): def test_append1(self):
todo = self.todolist.todo(3) todo = self.todolist.todo(3)
...@@ -108,9 +108,8 @@ class TodoListTester(unittest.TestCase): ...@@ -108,9 +108,8 @@ class TodoListTester(unittest.TestCase):
def test_todo(self): def test_todo(self):
count = self.todolist.count() count = self.todolist.count()
todo = self.todolist.todo(count+100)
self.assertEquals(todo, None) self.assertRaises(TodoList.InvalidTodoException, self.todolist.todo, count + 100)
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
def test_string(self): def test_string(self):
...@@ -139,7 +138,7 @@ class TodoListTester(unittest.TestCase): ...@@ -139,7 +138,7 @@ class TodoListTester(unittest.TestCase):
def test_todo_number2(self): def test_todo_number2(self):
todo = Todo.Todo("Non-existent") todo = Todo.Todo("Non-existent")
self.assertRaises(ValueError, self.todolist.number, todo) self.assertRaises(TodoList.InvalidTodoException, self.todolist.number, todo)
def test_todo_complete(self): def test_todo_complete(self):
todo = self.todolist.todo(1) todo = self.todolist.todo(1)
......
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