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