Commit c4189bb0 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Tests and adjustments for AppendCommand.

parent 9d97eb31
...@@ -10,10 +10,17 @@ class AppendCommand(Command.Command): ...@@ -10,10 +10,17 @@ class AppendCommand(Command.Command):
super(AppendCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt=lambda a: None) super(AppendCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt=lambda a: None)
def execute(self): def execute(self):
number = convert_todo_number(self.argument(0)) number = self.argument(0)
text = self.argument(1) if number:
number = convert_todo_number(number)
text = " ".join(self.args[1:])
todo = self.todolist.todo(number) if number and text:
self.todolist.append(number, text) todo = self.todolist.todo(number)
if todo:
self.out(pretty_print(todo, [self.todolist.pp_number()])) self.todolist.append(number, text)
self.out(pretty_print(todo, [self.todolist.pp_number()]))
else:
self.error("Invalid todo number given.")
else:
self.error(self.usage())
...@@ -39,10 +39,11 @@ class Command(object): ...@@ -39,10 +39,11 @@ class Command(object):
def argument(self, p_number): def argument(self, p_number):
""" Retrieves a value from the argument list at the given position. """ """ Retrieves a value from the argument list at the given position. """
value = None
try: try:
value = self.args[p_number] value = self.args[p_number]
except IndexError: except IndexError:
self.usage() self.error(self.usage())
return value return value
......
...@@ -31,7 +31,7 @@ def convert_todo_number(p_number): ...@@ -31,7 +31,7 @@ def convert_todo_number(p_number):
try: try:
p_number = int(p_number) p_number = int(p_number)
except ValueError: except ValueError:
error("Invalid todo number given.") p_number = None
return p_number return p_number
......
import AppendCommand
import CommandTest
import TodoList
class AppendCommandTest(CommandTest.CommandTest):
def setUp(self):
self.todolist = TodoList.TodoList([])
self.todolist.add("Foo")
def test_append1(self):
command = AppendCommand.AppendCommand([1, "Bar"], self.todolist, self.out, self.err)
command.execute()
self.assertEqual(self.output, " 1 Foo Bar\n")
self.assertEqual(self.errors, "")
def test_append2(self):
command = AppendCommand.AppendCommand([2, "Bar"], self.todolist, self.out, self.err)
command.execute()
self.assertEqual(self.output, "")
self.assertEqual(self.errors, "Invalid todo number given.\n")
def test_append3(self):
command = AppendCommand.AppendCommand([1, ""], self.todolist, self.out, self.err)
command.execute()
self.assertEqual(self.output, "")
self.assertEqual(self.output, "")
def test_append4(self):
command = AppendCommand.AppendCommand([1], self.todolist, self.out, self.err)
command.execute()
self.assertEqual(self.output, "")
self.assertEqual(self.errors, command.usage() + "\n")
def test_append5(self):
command = AppendCommand.AppendCommand([1, "Bar", "Baz"], self.todolist, self.out, self.err)
command.execute()
self.assertEqual(self.output, " 1 Foo Bar Baz\n")
self.assertEqual(self.errors, "")
def test_append6(self):
command = AppendCommand.AppendCommand([], self.todolist, self.out, self.err)
command.execute()
self.assertEqual(self.output, "")
self.assertEqual(self.errors, command.usage() + "\n")
def test_append7(self):
command = AppendCommand.AppendCommand(["Bar"], self.todolist, self.out, self.err)
command.execute()
self.assertEqual(self.output, "")
self.assertEqual(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