1 // ****************************************************************
2 // This is free software licensed under the NUnit license. You
3 // may obtain a copy of the license as well as information regarding
4 // copyright ownership at http://nunit.org/?p=license&r=2.4.
5 // ****************************************************************
12 /// Interface to be implemented by filters applied to tests.
13 /// The filter applies when running the test, after it has been
14 /// loaded, since this is the only time an ITest exists.
17 public abstract class TestFilter
: ITestFilter
20 /// Unique Empty filter.
22 public static TestFilter Empty
= new EmptyFilter();
25 /// Indicates whether this is the EmptyFilter
29 get { return this is TestFilter.EmptyFilter; }
33 /// Determine if a particular test passes the filter criteria. The default
34 /// implementation checks the test itself, its parents and any descendants.
36 /// Derived classes may override this method or any of the Match methods
37 /// to change the behavior of the filter.
39 /// <param name="test">The test to which the filter is applied</param>
40 /// <returns>True if the test passes the filter, otherwise false</returns>
41 public virtual bool Pass( ITest test
)
43 return Match(test
) || MatchParent(test
) || MatchDescendant(test
);
47 /// Determine whether the test itself matches the filter criteria, without
48 /// examining either parents or descendants.
50 /// <param name="test">The test to which the filter is applied</param>
51 /// <returns>True if the filter matches the any parent of the test</returns>
52 public abstract bool Match(ITest test
);
55 /// Determine whether any ancestor of the test mateches the filter criteria
57 /// <param name="test">The test to which the filter is applied</param>
58 /// <returns>True if the filter matches the an ancestor of the test</returns>
59 protected virtual bool MatchParent(ITest test
)
61 return (test
.RunState
!= RunState
.Explicit
&& test
.Parent
!= null &&
62 ( Match(test
.Parent
) || MatchParent(test
.Parent
)) );
66 /// Determine whether any descendant of the test matches the filter criteria.
68 /// <param name="test">The test to be matched</param>
69 /// <returns>True if at least one descendant matches the filter criteria</returns>
70 protected virtual bool MatchDescendant(ITest test
)
72 if (!test
.IsSuite
|| test
.Tests
== null)
75 foreach (ITest child
in test
.Tests
)
77 if (Match(child
) || MatchDescendant(child
))
85 /// Nested class provides an empty filter - one that always
86 /// returns true when called, unless the test is marked explicit.
89 private class EmptyFilter
: TestFilter
91 public override bool Match( ITest test
)
93 return test
.RunState
!= RunState
.Explicit
;
96 public override bool Pass( ITest test
)
98 return test
.RunState
!= RunState
.Explicit
;