Commit 19deba13 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Merge pull request #10 from mruwek/multi-output-consistency

More output and behavior consistency between 'del', 'do', 'pri and 'postpone'
parents 43da2315 64f1ad6a
...@@ -133,11 +133,22 @@ class DeleteCommandTest(CommandTest.CommandTest): ...@@ -133,11 +133,22 @@ class DeleteCommandTest(CommandTest.CommandTest):
self.assertEquals(self.todolist.count(), 0) self.assertEquals(self.todolist.count(), 0)
def test_multi_del3(self): def test_multi_del3(self):
""" Test deletion of multiple items. """ """ Fail if any of supplied todo numbers is invalid. """
command = DeleteCommand(["99", "2"], self.todolist, self.out, self.error, _yes_prompt) command = DeleteCommand(["99", "2"], self.todolist, self.out, self.error, _yes_prompt)
command.execute() command.execute()
self.assertEquals(self.todolist.count(), 1) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Invalid todo number given: 99.\n")
def test_multi_del4(self):
""" Check output when all supplied todo numbers are invalid. """
command = DeleteCommand(["99", "A"], self.todolist, self.out, self.error, _yes_prompt)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Invalid todo number given: 99.\nInvalid todo number given: A.\n")
def test_empty(self): def test_empty(self):
command = DeleteCommand([], self.todolist, self.out, self.error) command = DeleteCommand([], self.todolist, self.out, self.error)
......
...@@ -312,20 +312,17 @@ class DoCommandTest(CommandTest.CommandTest): ...@@ -312,20 +312,17 @@ class DoCommandTest(CommandTest.CommandTest):
command = DoCommand(["99", "3"], self.todolist, self.out, self.error, _no_prompt) command = DoCommand(["99", "3"], self.todolist, self.out, self.error, _no_prompt)
command.execute() command.execute()
self.assertTrue(self.todolist.todo(3).is_completed()) self.assertFalse(self.todolist.todo(3).is_completed())
self.assertEquals(self.output, "Completed: x {} Baz p:1\n".format(self.today)) self.assertEquals(self.errors, "Invalid todo number given: 99.\n")
self.assertEquals(self.errors, "Invalid todo number given.\n")
def test_multi_do5(self): def test_multi_do5(self):
""" """
When a todo item was generated by a recurring todo item, make sure Check output when all supplied todo numbers are invalid.
it cannot be completed in the same invocation.
""" """
command = DoCommand(["4", "10"], self.todolist, self.out, self.error, _no_prompt) command = DoCommand(["99", "10"], self.todolist, self.out, self.error, _no_prompt)
command.execute() command.execute()
self.assertTrue(self.todolist.todo(4).is_completed()) self.assertEquals(self.errors, "Invalid todo number given: 99.\nInvalid todo number given: 10.\n")
self.assertFalse(self.todolist.todo(10).is_completed())
def test_invalid_recurrence(self): def test_invalid_recurrence(self):
""" Show error message when an item has an invalid recurrence pattern. """ """ Show error message when an item has an invalid recurrence pattern. """
......
...@@ -211,6 +211,14 @@ class PostponeCommandTest(CommandTest.CommandTest): ...@@ -211,6 +211,14 @@ class PostponeCommandTest(CommandTest.CommandTest):
self.assertEquals(self.output, "") self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Invalid todo number given: 99.\nInvalid todo number given: 123.\n") self.assertEquals(self.errors, "Invalid todo number given: 99.\nInvalid todo number given: 123.\n")
def test_postpone19(self):
command = PostponeCommand(["Zoo", "99", "123", "1w"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Invalid todo number given: Zoo.\nInvalid todo number given: 99.\nInvalid todo number given: 123.\n")
def test_help(self): def test_help(self):
command = PostponeCommand(["help"], self.todolist, self.out, self.error) command = PostponeCommand(["help"], self.todolist, self.out, self.error)
command.execute() command.execute()
......
...@@ -35,7 +35,7 @@ class PriorityCommandTest(CommandTest.CommandTest): ...@@ -35,7 +35,7 @@ class PriorityCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "Priority changed from A to B\n(B) Foo\n") self.assertEquals(self.output, "Priority changed from A to B\n| 1| (B) Foo\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_prio2(self): def test_set_prio2(self):
...@@ -43,7 +43,7 @@ class PriorityCommandTest(CommandTest.CommandTest): ...@@ -43,7 +43,7 @@ class PriorityCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "Priority set to Z.\n(Z) Bar\n") self.assertEquals(self.output, "Priority set to Z.\n| 2| (Z) Bar\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_prio3(self): def test_set_prio3(self):
...@@ -51,7 +51,7 @@ class PriorityCommandTest(CommandTest.CommandTest): ...@@ -51,7 +51,7 @@ class PriorityCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "Priority changed from A to B\n(B) Foo\n") self.assertEquals(self.output, "Priority changed from A to B\n| 1| (B) Foo\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_prio4(self): def test_set_prio4(self):
...@@ -59,7 +59,7 @@ class PriorityCommandTest(CommandTest.CommandTest): ...@@ -59,7 +59,7 @@ class PriorityCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "(A) Foo\n") self.assertEquals(self.output, "| 1| (A) Foo\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_prio5(self): def test_set_prio5(self):
...@@ -67,7 +67,7 @@ class PriorityCommandTest(CommandTest.CommandTest): ...@@ -67,7 +67,7 @@ class PriorityCommandTest(CommandTest.CommandTest):
command.execute() command.execute()
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "Priority changed from A to C\n(C) Foo\nPriority set to C.\n(C) Bar\n") self.assertEquals(self.output, "Priority changed from A to C\n| 1| (C) Foo\nPriority set to C.\n| 2| (C) Bar\n")
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_invalid1(self): def test_invalid1(self):
...@@ -79,6 +79,22 @@ class PriorityCommandTest(CommandTest.CommandTest): ...@@ -79,6 +79,22 @@ class PriorityCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "Invalid todo number given.\n") self.assertEquals(self.errors, "Invalid todo number given.\n")
def test_invalid2(self): def test_invalid2(self):
command = PriorityCommand(["1", "99", "A"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertFalse(self.output)
self.assertEquals(self.errors, "Invalid todo number given: 99.\n")
def test_invalid3(self):
command = PriorityCommand(["98", "99", "A"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertFalse(self.output)
self.assertEquals(self.errors, "Invalid todo number given: 98.\nInvalid todo number given: 99.\n")
def test_invalid4(self):
command = PriorityCommand(["1", "ZZ"], self.todolist, self.out, self.error) command = PriorityCommand(["1", "ZZ"], self.todolist, self.out, self.error)
command.execute() command.execute()
...@@ -86,7 +102,7 @@ class PriorityCommandTest(CommandTest.CommandTest): ...@@ -86,7 +102,7 @@ class PriorityCommandTest(CommandTest.CommandTest):
self.assertFalse(self.output) self.assertFalse(self.output)
self.assertEquals(self.errors, "Invalid priority given.\n") self.assertEquals(self.errors, "Invalid priority given.\n")
def test_invalid3(self): def test_invalid5(self):
command = PriorityCommand(["A"], self.todolist, self.out, self.error) command = PriorityCommand(["A"], self.todolist, self.out, self.error)
command.execute() command.execute()
...@@ -94,7 +110,7 @@ class PriorityCommandTest(CommandTest.CommandTest): ...@@ -94,7 +110,7 @@ class PriorityCommandTest(CommandTest.CommandTest):
self.assertFalse(self.output) self.assertFalse(self.output)
self.assertEquals(self.errors, command.usage() + "\n") self.assertEquals(self.errors, command.usage() + "\n")
def test_invalid4(self): def test_invalid6(self):
command = PriorityCommand(["1"], self.todolist, self.out, self.error) command = PriorityCommand(["1"], self.todolist, self.out, self.error)
command.execute() command.execute()
......
...@@ -40,11 +40,12 @@ class DCommand(Command): ...@@ -40,11 +40,12 @@ class DCommand(Command):
self.length = len(self.todolist.todos()) # to determine newly activated todos self.length = len(self.todolist.todos()) # to determine newly activated todos
self.todos = [] self.todos = []
self.invalid_numbers = []
for number in self.args: for number in self.args:
try: try:
self.todos.append(self.todolist.todo(number)) self.todos.append(self.todolist.todo(number))
except InvalidTodoException: except InvalidTodoException:
self.todos.append(None) self.invalid_numbers.append(number)
def get_flags(self): def get_flags(self):
""" Default implementation of getting specific flags. """ """ Default implementation of getting specific flags. """
...@@ -141,13 +142,16 @@ class DCommand(Command): ...@@ -141,13 +142,16 @@ class DCommand(Command):
if len(self.args) == 0: if len(self.args) == 0:
self.error(self.usage()) self.error(self.usage())
elif len(self.invalid_numbers) > 1 or len(self.invalid_numbers) > 0 and len(self.todos) > 0:
for number in self.invalid_numbers:
self.error("Invalid todo number given: {}.".format(number))
elif len(self.invalid_numbers) == 1 and len(self.todos) == 0:
self.error("Invalid todo number given.")
else: else:
old_active = self._active_todos() old_active = self._active_todos()
for todo in self.todos: for todo in self.todos:
if not todo: if todo and self.condition(todo):
self.error("Invalid todo number given.")
elif todo and self.condition(todo):
self._process_subtasks(todo) self._process_subtasks(todo)
self.execute_specific(todo) self.execute_specific(todo)
else: else:
......
...@@ -60,22 +60,21 @@ class PostponeCommand(Command): ...@@ -60,22 +60,21 @@ class PostponeCommand(Command):
todos = [] todos = []
invalid_numbers = [] invalid_numbers = []
for number in self.args[:-1]: for number in self.args[:-1]:
try: try:
todos.append(self.todolist.todo(number)) todos.append(self.todolist.todo(number))
except InvalidTodoException: except InvalidTodoException:
invalid_numbers.append(number) invalid_numbers.append(number)
if len(invalid_numbers) > 0 and len(todos) > 0: if len(invalid_numbers) > 1 or len(invalid_numbers) > 0 and len(todos) > 0:
for number in invalid_numbers: for number in invalid_numbers:
self.error("Invalid todo number given: {}.".format(number)) self.error("Invalid todo number given: {}.".format(number))
elif len(invalid_numbers) == 1 and len(todos) == 0: elif len(invalid_numbers) == 1 and len(todos) == 0:
self.error("Invalid todo number given.") self.error("Invalid todo number given.")
else: else:
try: try:
pattern = self.args[-1] pattern = self.args[-1]
self.printer.add_filter(PrettyPrinterNumbers(self.todolist)) self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
if len(todos) > 0: if len(todos) > 0:
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
from topydo.lib.Command import Command, InvalidCommandArgument from topydo.lib.Command import Command, InvalidCommandArgument
from topydo.lib.PrettyPrinterFilter import PrettyPrinterNumbers
from topydo.lib.TodoListBase import InvalidTodoException from topydo.lib.TodoListBase import InvalidTodoException
from topydo.lib.Utils import is_valid_priority from topydo.lib.Utils import is_valid_priority
...@@ -30,37 +31,44 @@ class PriorityCommand(Command): ...@@ -30,37 +31,44 @@ class PriorityCommand(Command):
if not super(PriorityCommand, self).execute(): if not super(PriorityCommand, self).execute():
return False return False
numbers = None
priority = None priority = None
try: todos = []
numbers = self.args[:-1] invalid_numbers = []
priority = self.args[-1]
if len(numbers) > 0: for number in self.args[:-1]:
todos = [self.todolist.todo(number) for number in numbers] try:
todos.append(self.todolist.todo(number))
except InvalidTodoException:
invalid_numbers.append(number)
if is_valid_priority(priority): if len(invalid_numbers) > 1 or len(invalid_numbers) > 0 and len(todos) > 0:
for todo in todos: for number in invalid_numbers:
old_priority = todo.priority() self.error("Invalid todo number given: {}.".format(number))
self.todolist.set_priority(todo, priority) elif len(invalid_numbers) == 1 and len(todos) == 0:
self.error("Invalid todo number given.")
else:
try:
priority = self.args[-1]
self.printer.add_filter(PrettyPrinterNumbers(self.todolist))
if old_priority and priority and old_priority != priority: if len(todos) > 0:
self.out("Priority changed from {} to {}".format( if is_valid_priority(priority):
old_priority, priority)) for todo in todos:
elif not old_priority: old_priority = todo.priority()
self.out("Priority set to {}.".format(priority)) self.todolist.set_priority(todo, priority)
self.out(self.printer.print_todo(todo)) if old_priority and priority and old_priority != priority:
self.out("Priority changed from {} to {}".format(
old_priority, priority))
elif not old_priority:
self.out("Priority set to {}.".format(priority))
self.out(self.printer.print_todo(todo))
else:
self.error("Invalid priority given.")
else: else:
self.error("Invalid priority given.") self.error(self.usage())
else: except (IndexError, InvalidCommandArgument):
self.error(self.usage())
except (IndexError, InvalidCommandArgument):
self.error(self.usage())
except (InvalidTodoException):
if len(numbers) > 0 and priority:
self.error( "Invalid todo number given.")
else:
self.error(self.usage()) self.error(self.usage())
def usage(self): def usage(self):
......
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