Commit 4c7e5c54 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Handle recurrence properly custom completion date is given.

Also added testcases to handle the combination with strict recurrence
well.
parent d73b5128
......@@ -43,6 +43,9 @@ class DoCommandTest(CommandTest.CommandTest):
self.todolist = TodoList(todos)
self.today = date.today()
self.tomorrow = self.today + timedelta(1)
self.yesterday = self.today - timedelta(1)
self.yesterday = self.yesterday.isoformat()
self.today = self.today.isoformat()
self.tomorrow = self.tomorrow.isoformat()
......@@ -239,6 +242,43 @@ class DoCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "Completed: x {} Baz p:1\n".format(self.today))
self.assertEquals(self.errors, "")
def test_do_custom_date5(self):
"""
Make sure that the new recurrence date is correct when a custom
date is given.
"""
command = DoCommand(["-d", self.yesterday, "4"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "| 9| {today} Recurring! rec:1d due:{today}\nCompleted: x {yesterday} Recurring! rec:1d\n".format(today=self.today, yesterday=self.yesterday))
self.assertEquals(self.errors, "")
def test_do_custom_date6(self):
"""
When a custom date is set, strict recurrence must still hold on to the
due date as the offset. This todo item however, has no due date, then
the completion date must be used as an offset.
"""
command = DoCommand(["-s", "-d", self.yesterday, "4"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "| 9| {today} Recurring! rec:1d due:{today}\nCompleted: x {yesterday} Recurring! rec:1d\n".format(today=self.today, yesterday=self.yesterday))
self.assertEquals(self.errors, "")
def test_do_custom_date7(self):
"""
When a custom date is set, strict recurrence must still hold on to the
due date as the offset.
"""
command = DoCommand(["-s", "-d", self.yesterday, "8"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "| 9| {today} Strict due:2014-01-02 rec:1d\nCompleted: x {yesterday} Strict due:2014-01-01 rec:1d\n".format(today=self.today, yesterday=self.yesterday))
self.assertEquals(self.errors, "")
def test_empty(self):
command = DoCommand([], self.todolist, self.out, self.error)
command.execute()
......
......@@ -46,9 +46,11 @@ class DoCommand(DCommand):
def _handle_recurrence(self):
if self.todo.has_tag('rec'):
if self.strict_recurrence:
new_todo = strict_advance_recurring_todo(self.todo)
new_todo = strict_advance_recurring_todo(self.todo,
self.completion_date)
else:
new_todo = advance_recurring_todo(self.todo)
new_todo = advance_recurring_todo(self.todo,
self.completion_date)
self.todolist.add_todo(new_todo)
self.out(pretty_print(new_todo, [self.todolist.pp_number()]))
......
......@@ -30,6 +30,8 @@ def _advance_recurring_todo_helper(p_todo, p_offset):
Given a Todo item, return a new instance of a Todo item with the dates
shifted according to the recurrence rule.
The new date is calculated from the given p_offset value.
When no recurrence tag is present, an exception is raised.
"""
......@@ -52,10 +54,10 @@ def _advance_recurring_todo_helper(p_todo, p_offset):
return todo
def advance_recurring_todo(p_todo):
return _advance_recurring_todo_helper(p_todo, date.today())
def advance_recurring_todo(p_todo, p_offset=date.today()):
return _advance_recurring_todo_helper(p_todo, p_offset)
def strict_advance_recurring_todo(p_todo):
def strict_advance_recurring_todo(p_todo, p_offset=date.today()):
"""
Given a Todo item, return a new instance of a Todo item with the dates
shifted according to the recurrence rule.
......@@ -63,7 +65,10 @@ def strict_advance_recurring_todo(p_todo):
Strict means that the real due date is taken as a offset, not today or a
future date to determine the offset.
When the todo item has no due date, then the date is used passed by the
caller (defaulting to today).
When no recurrence tag is present, an exception is raised.
"""
offset = p_todo.due_date() or date.today()
offset = p_todo.due_date() or p_offset
return _advance_recurring_todo_helper(p_todo, offset)
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