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):
self.assertEquals(self.output, "")
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):
"""
Helper function that checks the removal of the dependency from todo 1
......
......@@ -45,6 +45,15 @@ class DepriCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "")
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):
command = DepriCommand.DepriCommand(["99"], self.todolist, self.out, self.error)
command.execute()
......
......@@ -152,6 +152,16 @@ class PostponeCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "")
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):
command = PostponeCommand.PostponeCommand(["help"], self.todolist, self.out, self.error)
command.execute()
......
......@@ -43,6 +43,15 @@ class PriorityCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "Priority set to Z.\n(Z) Bar\n")
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):
command = PriorityCommand.PriorityCommand(["99", "A"], self.todolist, self.out, self.error)
command.execute()
......
......@@ -182,9 +182,6 @@ class TodoListTester(unittest.TestCase):
self.assertTrue(self.todolist.is_dirty())
def test_regex1(self):
self.assertRaises(TodoList.InvalidTodoException, self.todolist.todo, "1")
def test_regex2(self):
""" Multiple hits should result in None. """
self.assertRaises(TodoList.InvalidTodoException, self.todolist.todo, "Project1")
......
......@@ -17,7 +17,6 @@
from Command import *
from PrettyPrinter import pretty_print
import TodoList
from Utils import convert_todo_number, InvalidTodoNumberException
class AppendCommand(Command):
def __init__(self, p_args, p_todolist,
......@@ -31,7 +30,7 @@ class AppendCommand(Command):
return False
try:
number = convert_todo_number(self.argument(0))
number = self.argument(0)
text = " ".join(self.args[1:])
if text:
......@@ -40,7 +39,7 @@ class AppendCommand(Command):
self.out(pretty_print(todo, [self.todolist.pp_number()]))
else:
self.error(self.usage())
except (InvalidCommandArgument, InvalidTodoNumberException):
except InvalidCommandArgument:
self.error(self.usage())
except TodoList.InvalidTodoException:
self.error("Invalid todo number given.")
......
......@@ -17,7 +17,6 @@
from Command import *
from PrettyPrinter import *
from TodoList import InvalidTodoException
from Utils import convert_todo_number, InvalidTodoNumberException
class DCommand(Command):
"""
......@@ -31,20 +30,12 @@ class DCommand(Command):
p_prompt=lambda a: None):
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.force = self.argument_shift("--force") or self.argument_shift("-f")
try:
self.number = convert_todo_number(self.argument(0))
self.todo = self.todolist.todo(self.number)
except (InvalidCommandArgument, InvalidTodoException):
self.todo = None
except InvalidTodoNumberException:
try:
self.todo = self.todolist.todo(self.argument(0))
self.number = self.todolist.number(self.todo)
except InvalidTodoException:
except (InvalidCommandArgument, InvalidTodoException):
self.todo = None
def _uncompleted_children(self, p_todo):
......
......@@ -19,7 +19,6 @@ from Config import config
import Filter
import Sorter
import TodoList
from Utils import convert_todo_number, InvalidTodoNumberException
import View
class DepCommand(Command):
......@@ -54,20 +53,20 @@ class DepCommand(Command):
operator = self.argument(2)
if operator == 'before' or operator == 'partof':
from_todo_nr = convert_todo_number(self.argument(3))
to_todo_nr = convert_todo_number(self.argument(1))
from_todo_nr = self.argument(3)
to_todo_nr = self.argument(1)
elif operator == 'to' or operator == 'after':
from_todo_nr = convert_todo_number(self.argument(1))
to_todo_nr = convert_todo_number(self.argument(3))
from_todo_nr = self.argument(1)
to_todo_nr = self.argument(3)
else:
# the operator was omitted, assume 2nd argument is target task
# default to 'to' behavior
from_todo_nr = convert_todo_number(self.argument(1))
to_todo_nr = convert_todo_number(self.argument(2))
from_todo_nr = self.argument(1)
to_todo_nr = self.argument(2)
from_todo = self.todolist.todo(from_todo_nr)
to_todo = self.todolist.todo(to_todo_nr)
except (InvalidTodoNumberException, TodoList.InvalidTodoException):
except (TodoList.InvalidTodoException):
self.error("Invalid todo number given.")
except InvalidCommandArgument:
self.error(self.usage())
......@@ -83,12 +82,12 @@ class DepCommand(Command):
todos = []
if arg2 == 'to':
# dep ls 1 to ...
number = convert_todo_number(arg1)
number = arg1
todo = self.todolist.todo(number)
todos = self.todolist.children(todo)
elif arg1 == 'to':
# dep ls ... to 1
number = convert_todo_number(arg2)
number = arg2
todo = self.todolist.todo(number)
todos = self.todolist.parents(todo)
else:
......@@ -99,7 +98,7 @@ class DepCommand(Command):
instance_filter = Filter.InstanceFilter(todos)
view = View.View(sorter, [instance_filter], self.todolist)
self.out(view.pretty_print())
except (TodoList.InvalidTodoException, InvalidTodoNumberException):
except TodoList.InvalidTodoException:
self.error("Invalid todo number given.")
except InvalidCommandArgument:
self.error(self.usage())
......
......@@ -17,7 +17,6 @@
from Command import *
from PrettyPrinter import pretty_print
from TodoList import InvalidTodoException
from Utils import *
class DepriCommand(Command):
def __init__(self, p_args, p_todolist,
......@@ -30,11 +29,9 @@ class DepriCommand(Command):
if not super(DepriCommand, self).execute():
return False
number = None
todo = None
try:
number = convert_todo_number(self.argument(0))
todo = self.todolist.todo(number)
todo = self.todolist.todo(self.argument(0))
if todo.priority() != None:
self.todolist.set_priority(todo, None)
......@@ -42,7 +39,7 @@ class DepriCommand(Command):
self.out(pretty_print(todo))
except InvalidCommandArgument:
self.error(self.usage())
except (InvalidTodoNumberException, InvalidTodoException):
except (InvalidTodoException):
if not todo:
self.error( "Invalid todo number given.")
else:
......
......@@ -21,7 +21,7 @@ from Config import config
from PrettyPrinter import *
from RelativeDate import relative_date_to_date
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):
def __init__(self, p_args, p_todolist,
......@@ -57,8 +57,7 @@ class PostponeCommand(Command):
self._process_flags()
try:
number = convert_todo_number(self.argument(0))
todo = self.todolist.todo(number)
todo = self.todolist.todo(self.argument(0))
pattern = self.argument(1)
# pdb.set_trace()
......@@ -80,7 +79,7 @@ class PostponeCommand(Command):
except InvalidCommandArgument:
self.error(self.usage())
except (InvalidTodoNumberException, InvalidTodoException):
except (InvalidTodoException):
self.error("Invalid todo number given.")
def usage(self):
......
......@@ -17,7 +17,7 @@
from Command import *
from PrettyPrinter import pretty_print
from TodoList import InvalidTodoException
from Utils import *
from Utils import is_valid_priority
class PriorityCommand(Command):
def __init__(self, p_args, p_todolist,
......@@ -33,7 +33,7 @@ class PriorityCommand(Command):
number = None
priority = None
try:
number = convert_todo_number(self.argument(0))
number = self.argument(0)
priority = self.argument(1)
todo = self.todolist.todo(number)
......@@ -51,7 +51,7 @@ class PriorityCommand(Command):
self.error("Invalid priority given.")
except InvalidCommandArgument:
self.error(self.usage())
except (InvalidTodoNumberException, InvalidTodoException):
except (InvalidTodoException):
if number and priority:
self.error( "Invalid todo number given.")
else:
......
......@@ -17,7 +17,6 @@
from Command import *
import TodoList
from PrettyPrinter import pretty_print
from Utils import convert_todo_number, InvalidTodoNumberException
class TagCommand(Command):
def __init__(self, p_args, p_todolist,
......@@ -43,18 +42,10 @@ class TagCommand(Command):
def _process_args(self):
self._process_flags()
try:
number = convert_todo_number(self.argument(0))
self.todo = self.todolist.todo(number)
self.tag = self.argument(1)
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):
self.error("Invalid todo number.")
......
......@@ -59,10 +59,10 @@ class TodoList(object):
"""
result = None
try:
result = self._todos[p_identifier - 1]
result = self._todos[int(p_identifier) - 1]
except IndexError:
raise InvalidTodoException
except TypeError:
except (TypeError, ValueError):
result = self.todo_by_regexp(p_identifier)
return result
......
......@@ -42,18 +42,6 @@ def date_string_to_date(p_date):
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):
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