2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / nunit24 / NUnitCore / interfaces / TestFilter.cs
blob0500ec471fde9a4fc7f43ed105ce8c86706008e4
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 // ****************************************************************
7 using System;
9 namespace NUnit.Core
11 /// <summary>
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.
15 /// </summary>
16 [Serializable]
17 public abstract class TestFilter : ITestFilter
19 /// <summary>
20 /// Unique Empty filter.
21 /// </summary>
22 public static TestFilter Empty = new EmptyFilter();
24 /// <summary>
25 /// Indicates whether this is the EmptyFilter
26 /// </summary>
27 public bool IsEmpty
29 get { return this is TestFilter.EmptyFilter; }
32 /// <summary>
33 /// Determine if a particular test passes the filter criteria. The default
34 /// implementation checks the test itself, its parents and any descendants.
35 ///
36 /// Derived classes may override this method or any of the Match methods
37 /// to change the behavior of the filter.
38 /// </summary>
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);
46 /// <summary>
47 /// Determine whether the test itself matches the filter criteria, without
48 /// examining either parents or descendants.
49 /// </summary>
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);
54 /// <summary>
55 /// Determine whether any ancestor of the test mateches the filter criteria
56 /// </summary>
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)) );
65 /// <summary>
66 /// Determine whether any descendant of the test matches the filter criteria.
67 /// </summary>
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)
73 return false;
75 foreach (ITest child in test.Tests)
77 if (Match(child) || MatchDescendant(child))
78 return true;
81 return false;
84 /// <summary>
85 /// Nested class provides an empty filter - one that always
86 /// returns true when called, unless the test is marked explicit.
87 /// </summary>
88 [Serializable]
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;