Commit a390086f authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add test for DepCommand.

parent fea6ad10
import Command
from Command import *
import Config
import Filter
import Sorter
from Utils import convert_todo_number
import TodoList
from Utils import convert_todo_number, InvalidTodoNumberException
import View
class DepCommand(Command.Command):
class DepCommand(Command):
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
......@@ -13,45 +15,68 @@ class DepCommand(Command.Command):
self.subsubcommand = self.argument(0)
def _handle_add(self):
(from_todo, to_todo) = self._get_todo_numbers()
self.todolist.add_dependency(from_todo, to_todo)
(from_todo, to_todo) = self._get_todos()
if from_todo and to_todo:
self.todolist.add_dependency(from_todo, to_todo)
def _handle_rm(self):
(from_todo, to_todo) = self._get_todo_numbers()
self.todolist.remove_dependency(from_todo, to_todo)
(from_todo, to_todo) = self._get_todos()
def _get_todo_numbers(self):
from_todonumber = convert_todo_number(self.argument(1))
to_todonumber = self.argument(2)
if from_todo and to_todo:
self.todolist.remove_dependency(from_todo, to_todo)
if to_todonumber == 'to':
to_todonumber = convert_todo_number(self.argument(3))
else:
to_todonumber = convert_todo_number(to_todonumber)
def _get_todos(self):
from_todo = None
to_todo = None
try:
from_todo_nr = convert_todo_number(self.argument(1))
to_todo_nr = self.argument(2)
if to_todo_nr == 'to':
to_todo_nr = convert_todo_number(self.argument(3))
else:
to_todo_nr = convert_todo_number(to_todo_nr)
return (from_todonumber, to_todonumber)
from_todo = self.todolist.todo(from_todo_nr)
to_todo = self.todolist.todo(to_todo_nr)
except (InvalidTodoNumberException, TodoList.InvalidTodoException):
self.error("Invalid todo number given.")
except InvalidCommandArgument:
self.error(self.usage())
return (from_todo, to_todo)
def _handle_ls(self):
""" Handles the ls subsubcommand. """
arg1 = self.argument(1)
arg2 = self.argument(2)
todos = []
if arg2 == 'to':
# dep ls 1 to ...
number = convert_todo_number(arg1)
todo = self.todolist.todo(number)
todos = self.todolist.children(todo)
elif arg1 == 'to':
# dep ls ... to 1
todos = self.todolist.parents(convert_todo_number(arg2))
else:
self.errors.append(self.usage())
try:
arg1 = self.argument(1)
arg2 = self.argument(2)
if todos:
sorter = Sorter.Sorter(Config.SORT_STRING)
view = View.View(sorter, [], todos)
self.out(view.pretty_print())
todos = []
if arg2 == 'to':
# dep ls 1 to ...
number = convert_todo_number(arg1)
todo = self.todolist.todo(number)
todos = self.todolist.children(todo)
elif arg1 == 'to':
# dep ls ... to 1
number = convert_todo_number(arg2)
todo = self.todolist.todo(number)
todos = self.todolist.parents(todo)
else:
self.error(self.usage())
if todos:
sorter = Sorter.Sorter(Config.SORT_STRING)
instance_filter = Filter.InstanceFilter(todos)
view = View.View(sorter, [instance_filter], self.todolist)
self.out(view.pretty_print())
except (TodoList.InvalidTodoException, InvalidTodoNumberException):
self.error("Invalid todo number given.")
except InvalidCommandArgument:
self.error(self.usage())
def execute(self):
dispatch = {
......@@ -65,4 +90,6 @@ class DepCommand(Command.Command):
if self.subsubcommand in dispatch:
dispatch[self.subsubcommand]()
else:
self.error(self.usage())
import CommandTest
import DepCommand
import TodoList
class DepCommandTest(CommandTest.CommandTest):
def setUp(self):
todos = [
"Foo id:1",
"Bar p:1",
"Baz p:1",
"Fnord id:2",
"Garbage dependency p:99",
"Fart p:2",
]
self.todolist = TodoList.TodoList(todos)
def test_add1(self):
command = DepCommand.DepCommand(["add", "1", "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 test_add2(self):
command = DepCommand.DepCommand(["add", "1", "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 test_add3(self):
command = DepCommand.DepCommand(["add", "99", "3"], 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.\n")
def test_add4(self):
command = DepCommand.DepCommand(["add", "A", "3"], 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.\n")
def test_add5(self):
command = DepCommand.DepCommand(["add", "1"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "")
self.assertEquals(self.errors, command.usage() + "\n")
def rm_helper(self, p_args):
"""
Helper function that checks the removal of the dependency from todo 1
to todo 3.
"""
command = DepCommand.DepCommand(p_args, self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertTrue(self.todolist.todo(1).has_tag('id', '1'))
self.assertFalse(self.todolist.todo(3).has_tag('p', '1'))
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "")
def test_rm1(self):
self.rm_helper(["rm", "1", "to", "3"])
def test_rm2(self):
self.rm_helper(["rm", "1", "3"])
def test_del1(self):
self.rm_helper(["del", "1", "to", "3"])
def test_del2(self):
self.rm_helper(["del", "1", "3"])
def test_rm3(self):
command = DepCommand.DepCommand(["rm", "99", "3"], 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.\n")
def test_rm4(self):
command = DepCommand.DepCommand(["rm", "A", "3"], 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.\n")
def test_rm5(self):
command = DepCommand.DepCommand(["rm", "1"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "")
self.assertEquals(self.errors, command.usage() + "\n")
def test_ls1(self):
command = DepCommand.DepCommand(["ls", "1", "to"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, " 2 Bar p:1\n 3 Baz p:1\n")
self.assertEquals(self.errors, "")
def test_ls2(self):
command = DepCommand.DepCommand(["ls", "99", "to"], 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.\n")
def test_ls3(self):
command = DepCommand.DepCommand(["ls", "to", "3"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, " 1 Foo id:1\n")
self.assertEquals(self.errors, "")
def test_ls4(self):
command = DepCommand.DepCommand(["ls", "to", "99"], 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.\n")
def test_ls5(self):
command = DepCommand.DepCommand(["ls", "1"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "")
self.assertEquals(self.errors, command.usage() + "\n")
def test_ls6(self):
command = DepCommand.DepCommand(["ls"], 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 gc_helper(self, p_subcommand):
command = DepCommand.DepCommand([p_subcommand], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertFalse(self.output)
self.assertFalse(self.errors)
self.assertFalse(self.todolist.todo(5).has_tag('p', '99'))
def test_clean(self):
self.gc_helper("clean")
def test_gc(self):
self.gc_helper("gc")
def test_invalid_subsubcommand(self):
command = DepCommand.DepCommand(["foo"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.output)
self.assertEqual(self.errors, command.usage() + "\n")
self.assertFalse(self.todolist.is_dirty())
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