Commit 1ed16cd0 authored by Bram Schoenmakers's avatar Bram Schoenmakers

Add instance filter.

parent c1674443
......@@ -73,3 +73,25 @@ class DependencyFilter(Filter):
uncompleted = [todo for todo in children if not todo.is_completed()]
return not uncompleted
class InstanceFilter(Filter):
def __init__(self, p_todos):
"""
Constructor.
A filter which selects a number of Todo instances from a TodoList
instance.
This is handy for constructing a view given a plain list of Todo items.
"""
self.todos = p_todos
def match(self, p_todo):
"""
Returns True when p_todo appears in the list of given todos.
"""
try:
self.todos.index(p_todo)
return True
except ValueError:
return False
......@@ -83,3 +83,30 @@ class FilterTest(unittest.TestCase):
self.assertEquals(todolist_to_string(filtered_todos), \
todolist_to_string(reference))
def test_filter9(self):
""" Test instance filter """
todos = load_file('data/FilterTest1.txt')
instance_filter = Filter.InstanceFilter(todos[2:])
filtered_todos = instance_filter.filter(todos)
self.assertEquals(todos[2:], filtered_todos)
def test_filter10(self):
""" Test instance filter """
todos = load_file('data/FilterTest1.txt')
instance_filter = Filter.InstanceFilter([])
filtered_todos = instance_filter.filter(todos)
self.assertEquals([], filtered_todos)
def test_filter11(self):
""" Test instance filter """
todos = load_file('data/FilterTest1.txt')
instance_filter = Filter.InstanceFilter(todos[2:])
filtered_todos = instance_filter.filter([])
self.assertEquals([], filtered_todos)
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