Commit 2dc073a9 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Implement del subcommand.

parent a053904c
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2014 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from Command import *
from TodoList import InvalidTodoException
from Utils import convert_todo_number, InvalidTodoNumberException
class DeleteCommand(Command):
def __init__(self, p_args, p_todolist,
p_out=lambda a: None,
p_err=lambda a: None,
p_prompt=lambda a: None):
super(DeleteCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.number = None
try:
self.number = convert_todo_number(self.argument(0))
self.todo = self.todolist.todo(self.number)
except (InvalidCommandArgument, InvalidTodoNumberException, InvalidTodoException):
self.todo = None
def execute(self):
if not super(DeleteCommand, self).execute():
return False
if not self.number:
self.error(self.usage())
elif self.todo:
self.todolist.delete(self.todo)
self.out("Todo %d removed." % self.number)
else:
self.error("Invalid todo number given.")
def usage(self):
return """Synopsis: del <NUMBER>"""
def help(self):
return """Deletes the todo item with the given number from the list."""
......@@ -23,6 +23,7 @@ import sys
from AddCommand import AddCommand
from AppendCommand import AppendCommand
from ArchiveCommand import ArchiveCommand
from DeleteCommand import DeleteCommand
from DepCommand import DepCommand
from DepriCommand import DepriCommand
import Config
......@@ -102,6 +103,7 @@ class CLIApplication(object):
'add': AddCommand,
'app': AppendCommand,
'append': AppendCommand,
'del': DeleteCommand,
'dep': DepCommand,
'depri': DepriCommand,
'do': DoCommand,
......@@ -115,6 +117,7 @@ class CLIApplication(object):
'listproject': ListProjectCommand,
'listprojects': ListProjectCommand,
'pri': PriorityCommand,
'rm': DeleteCommand,
}
args = arguments()
......
# Topydo - A todo.txt client written in Python.
# Copyright (C) 2014 Bram Schoenmakers <me@bramschoenmakers.nl>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import CommandTest
import DeleteCommand
import TodoList
class DeleteCommandTest(CommandTest.CommandTest):
def setUp(self):
todos = [
"Foo id:1",
"Bar p:1",
]
self.todolist = TodoList.TodoList(todos)
def test_del1(self):
command = DeleteCommand.DeleteCommand(["1"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.todolist.todo(1).source(), "Bar")
self.assertEquals(self.output, "Todo 1 removed.\n")
self.assertEquals(self.errors, "")
def test_del2(self):
command = DeleteCommand.DeleteCommand(["2"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.todolist.todo(1).source(), "Foo")
self.assertEquals(self.output, "Todo 2 removed.\n")
self.assertEquals(self.errors, "")
def test_del3(self):
command = DeleteCommand.DeleteCommand(["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_del4(self):
command = DeleteCommand.DeleteCommand(["A"], 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_empty(self):
command = DeleteCommand.DeleteCommand([], 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_help(self):
command = DeleteCommand.DeleteCommand(["help"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(self.output, "")
self.assertEquals(self.errors, command.usage() + "\n\n" + command.help() + "\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