Commit dbaf20b4 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add possibility to specify which value to replace.

parent d8e76bc0
......@@ -75,7 +75,7 @@ class TodoBase(object):
""" Adds a tag to the todo. """
self.set_tag(p_key, p_value, True)
def set_tag(self, p_key, p_value="", p_force_add=False):
def set_tag(self, p_key, p_value="", p_force_add=False, p_old_value=None):
"""
Sets a occurrence of the tag identified by p_key. Sets an arbitrary
instance of the tag when the todo contains multiple tags with this key.
......@@ -85,13 +85,17 @@ class TodoBase(object):
When p_force_add is true, a tag will always be added to the todo, in
case there is already a tag with the given key.
When p_old_value is set, all tags having this value will be set to the
new value.
"""
if p_value == "":
self.remove_tag(p_key)
return
value = self.tag_value(p_key)
value = p_old_value if p_old_value else self.tag_value(p_key)
if not p_force_add and value:
# remove old value from the tags
self.fields['tags'] = [t for t in self.fields['tags'] \
......
......@@ -49,7 +49,7 @@ class TodoBaseTester(unittest.TestCase):
self.assertEquals(todo.source(), '(C) Foo id:1 id:2')
def test_set_tag(self):
def test_set_tag1(self):
todo = TodoBase.TodoBase("(C) Foo foo:bar")
todo.set_tag('foo', 'baz')
......@@ -60,6 +60,28 @@ class TodoBaseTester(unittest.TestCase):
self.assertTrue(re.search(r'\bfoo:baz\b', todo.src))
self.assertFalse(re.search(r'\bfoo:bar\b', todo.src))
def test_set_tag2(self):
todo = TodoBase.TodoBase("(C) Foo foo:bar foo:baz")
todo.set_tag('foo', 'bang', False, 'baz')
self.assertTrue(todo.has_tag('foo'))
self.assertTrue(todo.has_tag('foo', 'bar'))
self.assertTrue(todo.has_tag('foo', 'bang'))
self.assertFalse(todo.has_tag('foo', 'baz'))
self.assertTrue(re.search(r'\bfoo:bar\b', todo.src))
self.assertFalse(re.search(r'\bfoo:baz\b', todo.src))
def test_set_tag3(self):
todo = TodoBase.TodoBase("(C) Foo foo:bar foo:bar")
todo.set_tag('foo', 'bang', False, 'bar')
self.assertTrue(todo.has_tag('foo'))
self.assertTrue(todo.has_tag('foo', 'bang'))
self.assertFalse(todo.has_tag('foo', 'bar'))
self.assertTrue(re.search(r'\bfoo:bang foo:bang\b', todo.src))
def test_set_tag_double_value(self):
todo = TodoBase.TodoBase("(C) Foo foo:bar baz:bar")
todo.set_tag('foo', 'blah');
......
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