Commit f5d8302b authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add tests for the 'do' subcommand.

parent 1f594ca6
import Command
from PrettyPrinter import pretty_print
import TodoList
from Utils import convert_todo_number
from Utils import convert_todo_number, InvalidTodoNumberException
class AppendCommand(Command.Command):
def __init__(self, p_args, p_todolist,
......@@ -13,15 +13,18 @@ class AppendCommand(Command.Command):
def execute(self):
number = self.argument(0)
if number:
number = convert_todo_number(number)
text = " ".join(self.args[1:])
try:
number = convert_todo_number(number)
text = " ".join(self.args[1:])
if number and text:
try:
if text:
todo = self.todolist.todo(number)
self.todolist.append(todo, text)
self.out(pretty_print(todo, [self.todolist.pp_number()]))
except TodoList.InvalidTodoException:
self.error("Invalid todo number given.")
else:
else:
self.error(self.usage())
except InvalidTodoNumberException:
self.error(self.usage())
except TodoList.InvalidTodoException:
self.error("Invalid todo number given.")
......@@ -3,7 +3,8 @@ import re
import Command
from PrettyPrinter import *
from Recurrence import advance_recurring_todo
from Utils import convert_todo_number
from TodoList import InvalidTodoException
from Utils import convert_todo_number, InvalidTodoNumberException
class DoCommand(Command.Command):
def __init__(self, p_args, p_todolist,
......@@ -11,11 +12,17 @@ class DoCommand(Command.Command):
p_err=lambda a: None,
p_prompt=lambda a: None):
super(DoCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.number = convert_todo_number(self.argument(0))
self.todo = self.todolist.todo(self.number)
self.number = None
try:
self.number = convert_todo_number(self.argument(0))
self.todo = self.todolist.todo(self.number)
except (InvalidTodoNumberException, InvalidTodoException):
self.todo = None
def _complete_children(self):
children = [t for t in self.todolist.children(self.todo) if not t.is_completed()]
children = sorted([t for t in self.todolist.children(self.todo) if not t.is_completed()])
if children:
self.out("\n".join(pretty_print_list(children, [self.todolist.pp_number()])))
......@@ -24,7 +31,7 @@ class DoCommand(Command.Command):
if re.match('^y(es)?$', confirmation, re.I):
for child in children:
self.todolist.set_todo_completed(child)
self.out(pretty_print(child, [self.todolist.pp_number()]))
self.out(pretty_print(child))
def _handle_recurrence(self):
if self.todo.has_tag('rec'):
......@@ -33,8 +40,14 @@ class DoCommand(Command.Command):
self.out(pretty_print(new_todo, [self.todolist.pp_number()]))
def execute(self):
if self.todo and not self.todo.is_completed():
if not self.number:
self.error(self.usage())
elif self.todo and not self.todo.is_completed():
self._complete_children()
self._handle_recurrence()
self.todolist.set_todo_completed(self.number)
self.todolist.set_todo_completed(self.todo)
self.out(pretty_print(self.todo))
elif not self.todo:
self.error("Invalid todo number given.")
else:
self.error("Todo has already been completed.")
......@@ -26,12 +26,15 @@ 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:
p_number = None
raise InvalidTodoNumberException
return p_number
......
from datetime import date
import CommandTest
import DoCommand
import TodoList
def _yes_prompt(self):
return "y"
def _no_prompt(self):
return "n"
class DoCommandTest(CommandTest.CommandTest):
def setUp(self):
self.todolist = TodoList.TodoList([])
self.today = date.today().isoformat()
self.todolist.add("Foo id:1")
self.todolist.add("Bar p:1")
self.todolist.add("Baz p:1")
self.todolist.add("Recurring! rec:1d")
self.todolist.add("x 2014-10-18 Already complete")
def test_do1(self):
command = DoCommand.DoCommand(["3"], self.todolist, self.out, self.error, _no_prompt)
command.execute()
self.assertEquals(self.output, "x %s Baz p:1\n" % self.today)
self.assertEquals(self.errors, "")
def test_do_children1(self):
command = DoCommand.DoCommand(["1"], self.todolist, self.out, self.error, _yes_prompt)
command.execute()
result = " 2 Bar p:1\n 3 Baz p:1\nx %s Bar p:1\nx %s Baz p:1\nx %s Foo id:1\n" % (self.today, self.today, self.today)
self.assertEquals(self.output, result)
self.assertEquals(self.errors, "")
def test_do_children2(self):
command = DoCommand.DoCommand(["1"], self.todolist, self.out, self.error, _no_prompt)
command.execute()
result = " 2 Bar p:1\n 3 Baz p:1\nx %s Foo id:1\n" % self.today
self.assertEquals(self.output, result)
self.assertEquals(self.errors, "")
def test_recurrence(self):
command = DoCommand.DoCommand(["4"], self.todolist, self.out, self.error)
command.execute()
result = " 6 2014-10-18 Recurring! rec:1d due:2014-10-19\nx 2014-10-18 Recurring! rec:1d\n"
self.assertEquals(self.output, result)
self.assertEquals(self.errors, "")
self.assertEquals(self.todolist.count(), 6)
self.assertTrue(self.todolist.todo(4).is_completed())
self.assertFalse(self.todolist.todo(6).is_completed())
def test_invalid1(self):
command = DoCommand.DoCommand(["99"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.output)
self.assertEquals(self.errors, "Invalid todo number given.\n")
def test_invalid2(self):
command = DoCommand.DoCommand(["A"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.output)
self.assertEquals(self.errors, command.usage() + "\n")
def test_already_complete(self):
command = DoCommand.DoCommand(["5"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.output)
self.assertEquals(self.errors, "Todo has already been completed.\n")
def test_empty(self):
command = DoCommand.DoCommand([""], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.output)
self.assertEquals(self.errors, command.usage() + "\n")
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