Commit 99ca6057 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add AndFilter, OrFilter and NotFilter for composing more complex filters.

Still needs some mechanism to easily compose filter expressions
defined by the user.
parent b023d9fe
......@@ -27,6 +27,29 @@ class Filter(object):
""" Default match value. """
return True
class NegationFilter(Filter):
def __init__(self, p_filter):
self._filter = p_filter
def match(self, p_todo):
return not self._filter.match(p_todo)
class AndFilter(Filter):
def __init__(self, p_filter1, p_filter2):
self._filter1 = p_filter1
self._filter2 = p_filter2
def match(self, p_todo):
return self._filter1.match(p_todo) and self._filter2.match(p_todo)
class OrFilter(Filter):
def __init__(self, p_filter1, p_filter2):
self._filter1 = p_filter1
self._filter2 = p_filter2
def match(self, p_todo):
return self._filter1.match(p_todo) or self._filter2.match(p_todo)
class GrepFilter(Filter):
""" Matches when the todo text contains a text. """
......
......@@ -149,3 +149,37 @@ class FilterTest(unittest.TestCase):
filtered_todos = limit_filter.filter(todos)
self.assertEquals(len(filtered_todos), 4)
def test_filter16(self):
todos = load_file('data/FilterTest1.txt')
grep = Filter.NegationFilter(Filter.GrepFilter('+project'))
filtered_todos = grep.filter(todos)
reference = load_file('data/FilterTest3-result.txt')
self.assertEquals(todolist_to_string(filtered_todos), \
todolist_to_string(reference))
def test_filter17(self):
todos = load_file('data/FilterTest1.txt')
grep1 = Filter.GrepFilter('task')
grep2 = Filter.GrepFilter('project')
andfilter = Filter.AndFilter(grep1, grep2)
filtered_todos = andfilter.filter(todos)
reference = load_file('data/FilterTest4-result.txt')
self.assertEquals(todolist_to_string(filtered_todos), \
todolist_to_string(reference))
def test_filter18(self):
todos = load_file('data/FilterTest1.txt')
grep1 = Filter.GrepFilter('part')
grep2 = Filter.GrepFilter('important')
grep = Filter.OrFilter(grep1, grep2)
filtered_todos = grep.filter(todos)
reference = load_file('data/FilterTest5-result.txt')
self.assertEquals(todolist_to_string(filtered_todos), \
todolist_to_string(reference))
(D) Just an arbitrary task
(C) Another mildly important task for the +Project
(C) This is part of some +Project
(C) Another mildly important task for the +Project
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