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