Merge pull request #5714 from alexischr/update_bockbuild
[mono.git] / mcs / nunit24 / NUnitCore / interfaces / TestFilter.cs
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
6 \r
7 using System;\r
8 \r
9 namespace NUnit.Core\r
10 {\r
11         /// <summary>\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
15         /// </summary>\r
16         [Serializable]\r
17         public abstract class TestFilter : ITestFilter\r
18         {\r
19                 /// <summary>\r
20                 /// Unique Empty filter.\r
21                 /// </summary>\r
22                 public static TestFilter Empty = new EmptyFilter();\r
23 \r
24                 /// <summary>\r
25                 /// Indicates whether this is the EmptyFilter\r
26                 /// </summary>\r
27                 public bool IsEmpty\r
28                 {\r
29                         get { return this is TestFilter.EmptyFilter; }\r
30                 }\r
31 \r
32                 /// <summary>\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
35                 /// \r
36                 /// Derived classes may override this method or any of the Match methods\r
37                 /// to change the behavior of the filter.\r
38                 /// </summary>\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
42                 {\r
43                         return Match(test) || MatchParent(test) || MatchDescendant(test);\r
44                 }\r
45 \r
46                 /// <summary>\r
47                 /// Determine whether the test itself matches the filter criteria, without\r
48                 /// examining either parents or descendants.\r
49                 /// </summary>\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
53 \r
54                 /// <summary>\r
55                 /// Determine whether any ancestor of the test mateches the filter criteria\r
56                 /// </summary>\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
60                 {\r
61                         return (test.RunState != RunState.Explicit && test.Parent != null && \r
62                                 ( Match(test.Parent) || MatchParent(test.Parent)) );\r
63                 }\r
64 \r
65                 /// <summary>\r
66                 /// Determine whether any descendant of the test matches the filter criteria.\r
67                 /// </summary>\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
71                 {\r
72                         if (!test.IsSuite || test.Tests == null)\r
73                                 return false;\r
74 \r
75                         foreach (ITest child in test.Tests)\r
76                         {\r
77                                 if (Match(child) || MatchDescendant(child))\r
78                                         return true;\r
79                         }\r
80 \r
81                         return false;\r
82                 }\r
83                 \r
84                 /// <summary>\r
85                 /// Nested class provides an empty filter - one that always\r
86                 /// returns true when called, unless the test is marked explicit.\r
87                 /// </summary>\r
88                 [Serializable]\r
89                 private class EmptyFilter : TestFilter\r
90                 {\r
91                         public override bool Match( ITest test )\r
92                         {\r
93                                 return test.RunState != RunState.Explicit;\r
94                         }\r
95 \r
96                         public override bool Pass( ITest test )\r
97                         {\r
98                                 return test.RunState != RunState.Explicit;\r
99                         }\r
100                 }\r
101         }\r
102 }\r