Commit 625ebcde authored by Bram Schoenmakers's avatar Bram Schoenmakers

Eliminate convert_todo_number.

Commands shouldn't care about the format of the todo number, TodoList
will take care of that now.

Added a bunch of tests for the commands to make sure they can deal with
regexp values.
parent bab8dc24
...@@ -100,6 +100,15 @@ class DepCommandTest(CommandTest.CommandTest): ...@@ -100,6 +100,15 @@ class DepCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "") self.assertEquals(self.output, "")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_add9(self):
command = DepCommand.DepCommand(["add", "Foo", "to", "4"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertTrue(self.todolist.todo(4).has_tag('p', '1'))
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "")
def rm_helper(self, p_args): def rm_helper(self, p_args):
""" """
Helper function that checks the removal of the dependency from todo 1 Helper function that checks the removal of the dependency from todo 1
......
...@@ -45,6 +45,15 @@ class DepriCommandTest(CommandTest.CommandTest): ...@@ -45,6 +45,15 @@ class DepriCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "") self.assertEquals(self.output, "")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_prio3(self):
command = DepriCommand.DepriCommand(["Foo"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.todolist.todo(1).priority(), None)
self.assertEquals(self.output, "Priority removed.\nFoo\n")
self.assertEquals(self.errors, "")
def test_invalid1(self): def test_invalid1(self):
command = DepriCommand.DepriCommand(["99"], self.todolist, self.out, self.error) command = DepriCommand.DepriCommand(["99"], self.todolist, self.out, self.error)
command.execute() command.execute()
......
...@@ -152,6 +152,16 @@ class PostponeCommandTest(CommandTest.CommandTest): ...@@ -152,6 +152,16 @@ class PostponeCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "") self.assertEquals(self.output, "")
self.assertEquals(self.errors, command.usage() + "\n") self.assertEquals(self.errors, command.usage() + "\n")
def test_postpone13(self):
command = PostponeCommand.PostponeCommand(["Foo", "1w"], self.todolist, self.out, self.error)
command.execute()
due = self.today + timedelta(7)
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, " 1 Foo due:%s\n" % due.isoformat())
self.assertEquals(self.errors, "")
def test_help(self): def test_help(self):
command = PostponeCommand.PostponeCommand(["help"], self.todolist, self.out, self.error) command = PostponeCommand.PostponeCommand(["help"], self.todolist, self.out, self.error)
command.execute() command.execute()
......
...@@ -43,6 +43,15 @@ class PriorityCommandTest(CommandTest.CommandTest): ...@@ -43,6 +43,15 @@ class PriorityCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "Priority set to Z.\n(Z) Bar\n") self.assertEquals(self.output, "Priority set to Z.\n(Z) Bar\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_prio3(self):
command = PriorityCommand.PriorityCommand(["Foo", "B"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "Priority changed from A to B\n(B) Foo\n")
self.assertEquals(self.errors, "")
def test_invalid1(self): def test_invalid1(self):
command = PriorityCommand.PriorityCommand(["99", "A"], self.todolist, self.out, self.error) command = PriorityCommand.PriorityCommand(["99", "A"], self.todolist, self.out, self.error)
command.execute() command.execute()
......
...@@ -182,9 +182,6 @@ class TodoListTester(unittest.TestCase): ...@@ -182,9 +182,6 @@ 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.assertRaises(TodoList.InvalidTodoException, self.todolist.todo, "1")
def test_regex2(self):
""" Multiple hits should result in None. """ """ Multiple hits should result in None. """
self.assertRaises(TodoList.InvalidTodoException, self.todolist.todo, "Project1") self.assertRaises(TodoList.InvalidTodoException, self.todolist.todo, "Project1")
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
from Command import * from Command import *
from PrettyPrinter import pretty_print from PrettyPrinter import pretty_print
import TodoList import TodoList
from Utils import convert_todo_number, InvalidTodoNumberException
class AppendCommand(Command): class AppendCommand(Command):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
...@@ -31,7 +30,7 @@ class AppendCommand(Command): ...@@ -31,7 +30,7 @@ class AppendCommand(Command):
return False return False
try: try:
number = convert_todo_number(self.argument(0)) number = self.argument(0)
text = " ".join(self.args[1:]) text = " ".join(self.args[1:])
if text: if text:
...@@ -40,7 +39,7 @@ class AppendCommand(Command): ...@@ -40,7 +39,7 @@ class AppendCommand(Command):
self.out(pretty_print(todo, [self.todolist.pp_number()])) self.out(pretty_print(todo, [self.todolist.pp_number()]))
else: else:
self.error(self.usage()) self.error(self.usage())
except (InvalidCommandArgument, InvalidTodoNumberException): except InvalidCommandArgument:
self.error(self.usage()) self.error(self.usage())
except TodoList.InvalidTodoException: except TodoList.InvalidTodoException:
self.error("Invalid todo number given.") self.error("Invalid todo number given.")
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
from Command import * from Command import *
from PrettyPrinter import * from PrettyPrinter import *
from TodoList import InvalidTodoException from TodoList import InvalidTodoException
from Utils import convert_todo_number, InvalidTodoNumberException
class DCommand(Command): class DCommand(Command):
""" """
...@@ -31,21 +30,13 @@ class DCommand(Command): ...@@ -31,21 +30,13 @@ class DCommand(Command):
p_prompt=lambda a: None): p_prompt=lambda a: None):
super(DCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt) super(DCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.number = None
self.length = len(self.todolist.todos()) # to determine newly activated todos self.length = len(self.todolist.todos()) # to determine newly activated todos
self.force = self.argument_shift("--force") or self.argument_shift("-f") self.force = self.argument_shift("--force") or self.argument_shift("-f")
try: try:
self.number = convert_todo_number(self.argument(0)) self.todo = self.todolist.todo(self.argument(0))
self.todo = self.todolist.todo(self.number)
except (InvalidCommandArgument, InvalidTodoException): except (InvalidCommandArgument, InvalidTodoException):
self.todo = None self.todo = None
except InvalidTodoNumberException:
try:
self.todo = self.todolist.todo(self.argument(0))
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()])
......
...@@ -19,7 +19,6 @@ from Config import config ...@@ -19,7 +19,6 @@ from Config import config
import Filter import Filter
import Sorter import Sorter
import TodoList import TodoList
from Utils import convert_todo_number, InvalidTodoNumberException
import View import View
class DepCommand(Command): class DepCommand(Command):
...@@ -54,20 +53,20 @@ class DepCommand(Command): ...@@ -54,20 +53,20 @@ class DepCommand(Command):
operator = self.argument(2) operator = self.argument(2)
if operator == 'before' or operator == 'partof': if operator == 'before' or operator == 'partof':
from_todo_nr = convert_todo_number(self.argument(3)) from_todo_nr = self.argument(3)
to_todo_nr = convert_todo_number(self.argument(1)) to_todo_nr = self.argument(1)
elif operator == 'to' or operator == 'after': elif operator == 'to' or operator == 'after':
from_todo_nr = convert_todo_number(self.argument(1)) from_todo_nr = self.argument(1)
to_todo_nr = convert_todo_number(self.argument(3)) to_todo_nr = self.argument(3)
else: else:
# the operator was omitted, assume 2nd argument is target task # the operator was omitted, assume 2nd argument is target task
# default to 'to' behavior # default to 'to' behavior
from_todo_nr = convert_todo_number(self.argument(1)) from_todo_nr = self.argument(1)
to_todo_nr = convert_todo_number(self.argument(2)) to_todo_nr = self.argument(2)
from_todo = self.todolist.todo(from_todo_nr) from_todo = self.todolist.todo(from_todo_nr)
to_todo = self.todolist.todo(to_todo_nr) to_todo = self.todolist.todo(to_todo_nr)
except (InvalidTodoNumberException, TodoList.InvalidTodoException): except (TodoList.InvalidTodoException):
self.error("Invalid todo number given.") self.error("Invalid todo number given.")
except InvalidCommandArgument: except InvalidCommandArgument:
self.error(self.usage()) self.error(self.usage())
...@@ -83,12 +82,12 @@ class DepCommand(Command): ...@@ -83,12 +82,12 @@ class DepCommand(Command):
todos = [] todos = []
if arg2 == 'to': if arg2 == 'to':
# dep ls 1 to ... # dep ls 1 to ...
number = convert_todo_number(arg1) number = arg1
todo = self.todolist.todo(number) todo = self.todolist.todo(number)
todos = self.todolist.children(todo) todos = self.todolist.children(todo)
elif arg1 == 'to': elif arg1 == 'to':
# dep ls ... to 1 # dep ls ... to 1
number = convert_todo_number(arg2) number = arg2
todo = self.todolist.todo(number) todo = self.todolist.todo(number)
todos = self.todolist.parents(todo) todos = self.todolist.parents(todo)
else: else:
...@@ -99,7 +98,7 @@ class DepCommand(Command): ...@@ -99,7 +98,7 @@ class DepCommand(Command):
instance_filter = Filter.InstanceFilter(todos) instance_filter = Filter.InstanceFilter(todos)
view = View.View(sorter, [instance_filter], self.todolist) view = View.View(sorter, [instance_filter], self.todolist)
self.out(view.pretty_print()) self.out(view.pretty_print())
except (TodoList.InvalidTodoException, InvalidTodoNumberException): except TodoList.InvalidTodoException:
self.error("Invalid todo number given.") self.error("Invalid todo number given.")
except InvalidCommandArgument: except InvalidCommandArgument:
self.error(self.usage()) self.error(self.usage())
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
from Command import * from Command import *
from PrettyPrinter import pretty_print from PrettyPrinter import pretty_print
from TodoList import InvalidTodoException from TodoList import InvalidTodoException
from Utils import *
class DepriCommand(Command): class DepriCommand(Command):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
...@@ -30,11 +29,9 @@ class DepriCommand(Command): ...@@ -30,11 +29,9 @@ class DepriCommand(Command):
if not super(DepriCommand, self).execute(): if not super(DepriCommand, self).execute():
return False return False
number = None
todo = None todo = None
try: try:
number = convert_todo_number(self.argument(0)) todo = self.todolist.todo(self.argument(0))
todo = self.todolist.todo(number)
if todo.priority() != None: if todo.priority() != None:
self.todolist.set_priority(todo, None) self.todolist.set_priority(todo, None)
...@@ -42,7 +39,7 @@ class DepriCommand(Command): ...@@ -42,7 +39,7 @@ class DepriCommand(Command):
self.out(pretty_print(todo)) self.out(pretty_print(todo))
except InvalidCommandArgument: except InvalidCommandArgument:
self.error(self.usage()) self.error(self.usage())
except (InvalidTodoNumberException, InvalidTodoException): except (InvalidTodoException):
if not todo: if not todo:
self.error( "Invalid todo number given.") self.error( "Invalid todo number given.")
else: else:
......
...@@ -21,7 +21,7 @@ from Config import config ...@@ -21,7 +21,7 @@ from Config import config
from PrettyPrinter import * from PrettyPrinter import *
from RelativeDate import relative_date_to_date from RelativeDate import relative_date_to_date
from TodoList import InvalidTodoException from TodoList import InvalidTodoException
from Utils import convert_todo_number, InvalidTodoNumberException, date_string_to_date from Utils import date_string_to_date
class PostponeCommand(Command): class PostponeCommand(Command):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
...@@ -57,8 +57,7 @@ class PostponeCommand(Command): ...@@ -57,8 +57,7 @@ class PostponeCommand(Command):
self._process_flags() self._process_flags()
try: try:
number = convert_todo_number(self.argument(0)) todo = self.todolist.todo(self.argument(0))
todo = self.todolist.todo(number)
pattern = self.argument(1) pattern = self.argument(1)
# pdb.set_trace() # pdb.set_trace()
...@@ -80,7 +79,7 @@ class PostponeCommand(Command): ...@@ -80,7 +79,7 @@ class PostponeCommand(Command):
except InvalidCommandArgument: except InvalidCommandArgument:
self.error(self.usage()) self.error(self.usage())
except (InvalidTodoNumberException, InvalidTodoException): except (InvalidTodoException):
self.error("Invalid todo number given.") self.error("Invalid todo number given.")
def usage(self): def usage(self):
......
...@@ -17,7 +17,7 @@ ...@@ -17,7 +17,7 @@
from Command import * from Command import *
from PrettyPrinter import pretty_print from PrettyPrinter import pretty_print
from TodoList import InvalidTodoException from TodoList import InvalidTodoException
from Utils import * from Utils import is_valid_priority
class PriorityCommand(Command): class PriorityCommand(Command):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
...@@ -33,7 +33,7 @@ class PriorityCommand(Command): ...@@ -33,7 +33,7 @@ class PriorityCommand(Command):
number = None number = None
priority = None priority = None
try: try:
number = convert_todo_number(self.argument(0)) number = self.argument(0)
priority = self.argument(1) priority = self.argument(1)
todo = self.todolist.todo(number) todo = self.todolist.todo(number)
...@@ -51,7 +51,7 @@ class PriorityCommand(Command): ...@@ -51,7 +51,7 @@ class PriorityCommand(Command):
self.error("Invalid priority given.") self.error("Invalid priority given.")
except InvalidCommandArgument: except InvalidCommandArgument:
self.error(self.usage()) self.error(self.usage())
except (InvalidTodoNumberException, InvalidTodoException): except (InvalidTodoException):
if number and priority: if number and priority:
self.error( "Invalid todo number given.") self.error( "Invalid todo number given.")
else: else:
......
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
from Command import * from Command import *
import TodoList import TodoList
from PrettyPrinter import pretty_print from PrettyPrinter import pretty_print
from Utils import convert_todo_number, InvalidTodoNumberException
class TagCommand(Command): class TagCommand(Command):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
...@@ -44,17 +43,9 @@ class TagCommand(Command): ...@@ -44,17 +43,9 @@ class TagCommand(Command):
self._process_flags() self._process_flags()
try: try:
number = convert_todo_number(self.argument(0)) self.todo = self.todolist.todo(self.argument(0))
self.todo = self.todolist.todo(number)
self.tag = self.argument(1) self.tag = self.argument(1)
self.current_values = self.todo.tag_values(self.tag) self.current_values = self.todo.tag_values(self.tag)
except InvalidTodoNumberException:
try:
self.todo = self.todolist.todo(self.argument(0))
self.tag = self.argument(1)
self.current_values = self.todo.tag_values(self.tag)
except TodoList.InvalidTodoException:
self.error("Invalid todo number.")
except (InvalidCommandArgument, TodoList.InvalidTodoException): except (InvalidCommandArgument, TodoList.InvalidTodoException):
self.error("Invalid todo number.") self.error("Invalid todo number.")
......
...@@ -59,10 +59,10 @@ class TodoList(object): ...@@ -59,10 +59,10 @@ class TodoList(object):
""" """
result = None result = None
try: try:
result = self._todos[p_identifier - 1] result = self._todos[int(p_identifier) - 1]
except IndexError: except IndexError:
raise InvalidTodoException raise InvalidTodoException
except TypeError: except (TypeError, ValueError):
result = self.todo_by_regexp(p_identifier) result = self.todo_by_regexp(p_identifier)
return result return result
......
...@@ -42,18 +42,6 @@ def date_string_to_date(p_date): ...@@ -42,18 +42,6 @@ def date_string_to_date(p_date):
return result return result
class InvalidTodoNumberException(Exception):
pass
def convert_todo_number(p_number):
""" Converts a string number to an integer. """
try:
p_number = int(p_number)
except ValueError:
raise InvalidTodoNumberException
return p_number
def is_valid_priority(p_priority): def is_valid_priority(p_priority):
return p_priority != None and re.match(r'^[A-Z]$', p_priority) != None return p_priority != None and re.match(r'^[A-Z]$', p_priority) != None
......
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