Commit a7d51550 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Simplify TagCommand by eliminating the subsubcommands.

parent 30c961bf
...@@ -30,7 +30,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -30,7 +30,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.todolist = TodoList.TodoList(todos) self.todolist = TodoList.TodoList(todos)
def test_add_tag1(self): def test_add_tag1(self):
command = TagCommand.TagCommand(["add", "1", "due", "2014-10-22"], self.todolist, self.out, self.error) command = TagCommand.TagCommand(["1", "due", "2014-10-22"], self.todolist, self.out, self.error)
command.execute() command.execute()
self.assertEquals(self.todolist.todo(1).source(), "Foo due:2014-10-22") self.assertEquals(self.todolist.todo(1).source(), "Foo due:2014-10-22")
...@@ -38,16 +38,8 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -38,16 +38,8 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
def test_add_tag2(self):
command = TagCommand.TagCommand(["add", "1", "due"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Missing value for tag.\n" + command.usage() + "\n")
self.assertFalse(self.todolist.is_dirty())
def test_add_tag3(self): def test_add_tag3(self):
command = TagCommand.TagCommand(["add", "2", "due", "2014-10-19"], self.todolist, self.out, self.error) command = TagCommand.TagCommand(["-f", "2", "due", "2014-10-19"], self.todolist, self.out, self.error)
command.execute() command.execute()
self.assertEquals(self.todolist.todo(2).source(), "Bar due:2014-10-22 due:2014-10-19") self.assertEquals(self.todolist.todo(2).source(), "Bar due:2014-10-22 due:2014-10-19")
...@@ -55,40 +47,8 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -55,40 +47,8 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
def test_add_tag4(self):
command = TagCommand.TagCommand(["add", "1", "due"], self.todolist, self.out, self.error)
command.execute()
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Missing value for tag.\n" + command.usage() + "\n")
self.assertFalse(self.todolist.is_dirty())
def test_set_tag1(self):
command = TagCommand.TagCommand(["set", "1", "due", "2014-10-22"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, " 1 Foo due:2014-10-22\n")
self.assertEquals(self.errors, "")
def test_set_tag2(self):
command = TagCommand.TagCommand(["set", "1", "due"], self.todolist, self.out, self.error)
command.execute()
self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Missing value for tag.\n" + command.usage() + "\n")
def test_set_tag3(self):
command = TagCommand.TagCommand(["set", "2", "due", "2014-10-20"], self.todolist, self.out, self.error)
command.execute()
self.assertTrue(self.todolist.is_dirty())
self.assertEquals(self.output, " 2 Bar due:2014-10-20\n")
self.assertEquals(self.errors, "")
def test_set_tag4(self): def test_set_tag4(self):
command = TagCommand.TagCommand(["set", "3", "due", "2014-10-20"], self.todolist, self.out, self.error) command = TagCommand.TagCommand(["3", "due", "2014-10-20"], self.todolist, self.out, self.error)
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
...@@ -96,7 +56,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -96,7 +56,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_tag5(self): def test_set_tag5(self):
command = TagCommand.TagCommand(["set", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "all") command = TagCommand.TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "all")
command.execute() command.execute()
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
...@@ -104,7 +64,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -104,7 +64,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_tag6(self): def test_set_tag6(self):
command = TagCommand.TagCommand(["set", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "1") command = TagCommand.TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "1")
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
...@@ -112,7 +72,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -112,7 +72,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_tag7(self): def test_set_tag7(self):
command = TagCommand.TagCommand(["set", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "2") command = TagCommand.TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "2")
command.execute() command.execute()
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
...@@ -120,7 +80,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -120,7 +80,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_tag8(self): def test_set_tag8(self):
command = TagCommand.TagCommand(["set", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "") command = TagCommand.TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "")
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
...@@ -128,7 +88,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -128,7 +88,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_set_tag9(self): def test_set_tag9(self):
command = TagCommand.TagCommand(["set", "4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "99") command = TagCommand.TagCommand(["4", "due", "2014-10-20"], self.todolist, self.out, self.error, lambda t: "99")
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
...@@ -136,7 +96,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -136,7 +96,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_rm_tag1(self): def test_rm_tag1(self):
command = TagCommand.TagCommand(["rm", "1", "due"], self.todolist, self.out, self.error) command = TagCommand.TagCommand(["1", "due"], self.todolist, self.out, self.error)
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
...@@ -144,7 +104,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -144,7 +104,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_rm_tag2(self): def test_rm_tag2(self):
command = TagCommand.TagCommand(["rm", "2", "due"], self.todolist, self.out, self.error) command = TagCommand.TagCommand(["2", "due"], self.todolist, self.out, self.error)
command.execute() command.execute()
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
...@@ -152,7 +112,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -152,7 +112,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_rm_tag3(self): def test_rm_tag3(self):
command = TagCommand.TagCommand(["rm", "4", "due"], self.todolist, self.out, self.error, lambda t: "all") command = TagCommand.TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "all")
command.execute() command.execute()
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
...@@ -160,7 +120,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -160,7 +120,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_rm_tag4(self): def test_rm_tag4(self):
command = TagCommand.TagCommand(["rm", "4", "due"], self.todolist, self.out, self.error, lambda t: "1") command = TagCommand.TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "1")
command.execute() command.execute()
self.assertTrue(self.todolist.is_dirty()) self.assertTrue(self.todolist.is_dirty())
...@@ -168,7 +128,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -168,7 +128,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_rm_tag6(self): def test_rm_tag6(self):
command = TagCommand.TagCommand(["rm", "4", "due"], self.todolist, self.out, self.error, lambda t: "99") command = TagCommand.TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "99")
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
...@@ -176,7 +136,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -176,7 +136,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_rm_tag7(self): def test_rm_tag7(self):
command = TagCommand.TagCommand(["rm", "4", "due"], self.todolist, self.out, self.error, lambda t: "A") command = TagCommand.TagCommand(["4", "due"], self.todolist, self.out, self.error, lambda t: "A")
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
...@@ -184,7 +144,7 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -184,7 +144,7 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "") self.assertEquals(self.errors, "")
def test_rm_tag8(self): def test_rm_tag8(self):
command = TagCommand.TagCommand(["rm", "5", "due"], self.todolist, self.out, self.error) command = TagCommand.TagCommand(["5", "due"], self.todolist, self.out, self.error)
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
...@@ -192,21 +152,13 @@ class TagCommandTest(CommandTest.CommandTest): ...@@ -192,21 +152,13 @@ class TagCommandTest(CommandTest.CommandTest):
self.assertEquals(self.errors, "Invalid todo number.\n") self.assertEquals(self.errors, "Invalid todo number.\n")
def test_rm_tag9(self): def test_rm_tag9(self):
command = TagCommand.TagCommand(["rm", "A", "due"], self.todolist, self.out, self.error) command = TagCommand.TagCommand(["A", "due"], self.todolist, self.out, self.error)
command.execute() command.execute()
self.assertFalse(self.todolist.is_dirty()) self.assertFalse(self.todolist.is_dirty())
self.assertEquals(self.output, "") self.assertEquals(self.output, "")
self.assertEquals(self.errors, "Invalid todo number.\n") self.assertEquals(self.errors, "Invalid todo number.\n")
def test_invalid_subsubcommand(self):
command = TagCommand.TagCommand(["foo", "bar"], 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_help(self): def test_help(self):
command = TagCommand.TagCommand(["help"], self.todolist, self.out, self.error) command = TagCommand.TagCommand(["help"], self.todolist, self.out, self.error)
command.execute() command.execute()
......
...@@ -26,21 +26,35 @@ class TagCommand(Command): ...@@ -26,21 +26,35 @@ class TagCommand(Command):
p_prompt=lambda a: None): p_prompt=lambda a: None):
super(TagCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt) super(TagCommand, self).__init__(p_args, p_todolist, p_out, p_err, p_prompt)
self.subsubcommand = None self.force = False
self.todo = None self.todo = None
self.tag = None self.tag = None
self.value = None self.value = None
self.values = [] self.values = []
def _process_flags(self):
flags, args = self.getopt("f")
for flag, value in flags:
if flag == "-f":
self.force = True
self.args = args
def _process_args(self):
self._process_flags()
try: try:
self.subsubcommand = self.argument(0) number = convert_todo_number(self.argument(0))
number = convert_todo_number(self.argument(1))
self.todo = self.todolist.todo(number) self.todo = self.todolist.todo(number)
self.tag = self.argument(2) self.tag = self.argument(1)
self.current_values = self.todo.tag_values(self.tag) self.current_values = self.todo.tag_values(self.tag)
self.value = self.argument(3)
except (InvalidCommandArgument, InvalidTodoNumberException, TodoList.InvalidTodoException): except (InvalidCommandArgument, InvalidTodoNumberException, TodoList.InvalidTodoException):
pass self.error("Invalid todo number.")
try:
self.value = self.argument(2)
except InvalidCommandArgument:
self.value = ""
def _print(self): def _print(self):
self.out(pretty_print(self.todo, [self.todolist.pp_number()])) self.out(pretty_print(self.todo, [self.todolist.pp_number()]))
...@@ -65,63 +79,41 @@ class TagCommand(Command): ...@@ -65,63 +79,41 @@ class TagCommand(Command):
return answer return answer
def _add(self): def _set_helper(self, p_old_value=""):
self._set(True)
def _set_helper(self, p_force, p_old_value=""):
old_src = self.todo.source() old_src = self.todo.source()
self.todo.set_tag(self.tag, self.value, p_force, p_old_value) self.todo.set_tag(self.tag, self.value, self.force, p_old_value)
if old_src != self.todo.source(): if old_src != self.todo.source():
self.todolist.set_dirty() self.todolist.set_dirty()
def _set(self, p_force_add=False): def _set(self):
if self.value == None:
self.error("Missing value for tag.")
self.error(self.usage())
else:
if len(self.current_values) > 1: if len(self.current_values) > 1:
answer = self._choose() answer = self._choose()
if answer == "all": if answer == "all":
for value in self.current_values: for value in self.current_values:
self._set_helper(False, value) self._set_helper(value)
elif answer != None and self.value != self.current_values[answer]: elif answer != None and self.value != self.current_values[answer]:
self._set_helper(False, self.current_values[answer]) self._set_helper(self.current_values[answer])
else: # if not self.todo.has_tag(self.tag, self.value): else:
self._set_helper(p_force_add) self._set_helper()
self._print() self._print()
def _rm(self):
self.value = ""
self._set()
def execute(self): def execute(self):
if not super(TagCommand, self).execute(): if not super(TagCommand, self).execute():
return False return False
dispatch = { self._process_args()
"add": self._add,
"set": self._set, if self.todo and self.tag:
"del": self._rm, self._set()
"rm": self._rm,
}
if self.subsubcommand in dispatch and self.todo and self.tag:
dispatch[self.subsubcommand]()
elif self.subsubcommand not in dispatch:
self.error(self.usage())
elif not self.todo:
self.error("Invalid todo number.")
def usage(self): def usage(self):
return """Synopsis: return """Synopsis: tag <NUMBER> <tag> [<value>]"""
tag (add|set) <NUMBER> <tag> <value>
tag rm <NUMBER> <tag> [value]"""
def help(self): def help(self):
return """* add: Add a tag to the given todo. return """Sets the given tag to the given todo number with the given value. If
* set: Changes a tag of the given todo. the value is omitted, the tag is removed from the todo item.
* rm: Removes a tag from the given todo.""" """
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