Commit fc880278 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add tests for the priority command.

parent 2d4fff19
import Command from Command import *
from PrettyPrinter import pretty_print from PrettyPrinter import pretty_print
from Utils import convert_todo_number, is_valid_priority from TodoList import InvalidTodoException
from Utils import *
class PriorityCommand(Command.Command): class PriorityCommand(Command):
def __init__(self, p_args, p_todolist, def __init__(self, p_args, p_todolist,
p_out=lambda a: None, p_out=lambda a: None,
p_err=lambda a: None, p_err=lambda a: None,
p_prompt=lambda a: None): p_prompt=lambda a: None):
super(PriorityCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt) super(PriorityCommand, 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.priority = self.argument(1)
def execute(self): def execute(self):
if is_valid_priority(self.priority): number = None
old_priority = self.todo.priority() priority = None
self.todolist.set_priority(self.number, self.priority) try:
number = convert_todo_number(self.argument(0))
priority = self.argument(1)
todo = self.todolist.todo(number)
self.out("Priority changed from %s to %s" % (old_priority, self.priority)) if is_valid_priority(priority):
self.out(pretty_print(self.todo)) old_priority = todo.priority()
else: self.todolist.set_priority(todo, priority)
self.error("Invalid priority given.")
if old_priority and priority:
self.out("Priority changed from %s to %s" % (old_priority, priority))
else:
self.out("Priority set to %s." % priority)
self.out(pretty_print(todo))
else:
self.error("Invalid priority given.")
except InvalidCommandArgument:
self.error(self.usage())
except (InvalidTodoNumberException, InvalidTodoException):
if number and priority:
self.error( "Invalid todo number given.")
else:
self.error(self.usage())
...@@ -39,4 +39,4 @@ def convert_todo_number(p_number): ...@@ -39,4 +39,4 @@ def convert_todo_number(p_number):
return p_number return p_number
def is_valid_priority(p_priority): def is_valid_priority(p_priority):
return re.match(r'^[A-Z]$', p_priority) != None return p_priority != None and re.match(r'^[A-Z]$', p_priority) != None
import CommandTest
import PriorityCommand
import TodoList
class PriorityCommandTest(CommandTest.CommandTest):
def setUp(self):
todos = [
"(A) Foo",
"Bar",
]
self.todolist = TodoList.TodoList(todos)
def test_set_prio1(self):
command = PriorityCommand.PriorityCommand(["1", "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_set_prio2(self):
command = PriorityCommand.PriorityCommand(["2", "Z"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, "Priority set to Z.\n(Z) Bar\n")
self.assertEquals(self.errors, "")
def test_invalid1(self):
command = PriorityCommand.PriorityCommand(["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.\n")
def test_invalid2(self):
command = PriorityCommand.PriorityCommand(["1", "ZZ"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertFalse(self.output)
self.assertEquals(self.errors, "Invalid priority given.\n")
def test_invalid3(self):
command = PriorityCommand.PriorityCommand(["A"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertFalse(self.output)
self.assertEquals(self.errors, command.usage() + "\n")
def test_invalid4(self):
command = PriorityCommand.PriorityCommand(["1"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertFalse(self.output)
self.assertEquals(self.errors, command.usage() + "\n")
def test_empty(self):
command = PriorityCommand.PriorityCommand([], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
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