PageRenderTime 10ms CodeModel.GetById 2ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/src/NUnit/interfaces/Filters/NotFilter.cs

#
C# | 78 lines | 40 code | 9 blank | 29 comment | 11 complexity | 922b689b80e71bc2542f2d34633f3153 MD5 | raw file
 1// ****************************************************************
 2// Copyright 2007, Charlie Poole
 3// This is free software licensed under the NUnit license. You may
 4// obtain a copy of the license at http://nunit.org
 5// ****************************************************************
 6using System;
 7
 8namespace NUnit.Core.Filters
 9{
10	/// <summary>
11	/// NotFilter negates the operation of another filter
12	/// </summary>
13	[Serializable]
14	public class NotFilter : TestFilter
15	{
16		ITestFilter baseFilter;
17        bool topLevel = false;
18
19		/// <summary>
20		/// Construct a not filter on another filter
21		/// </summary>
22		/// <param name="baseFilter">The filter to be negated</param>
23		public NotFilter( ITestFilter baseFilter)
24		{
25			this.baseFilter = baseFilter;
26		}
27
28        /// <summary>
29        /// Indicates whether this is a top-level NotFilter,
30        /// requiring special handling of Explicit
31        /// </summary>
32        public bool TopLevel
33        {
34            get { return topLevel; }
35            set { topLevel = value; }
36        }
37
38		/// <summary>
39		/// Gets the base filter
40		/// </summary>
41		public ITestFilter BaseFilter
42		{
43			get { return baseFilter; }
44		}
45
46		/// <summary>
47		/// Check whether the filter matches a test
48		/// </summary>
49		/// <param name="test">The test to be matched</param>
50		/// <returns>True if it matches, otherwise false</returns>
51		public override bool Match( ITest test )
52		{
53            if (topLevel && test.RunState == RunState.Explicit)
54                return false;
55
56			return !baseFilter.Pass( test );
57		}
58
59		/// <summary>
60		/// Determine whether any descendant of the test matches the filter criteria.
61		/// </summary>
62		/// <param name="test">The test to be matched</param>
63		/// <returns>True if at least one descendant matches the filter criteria</returns>
64		protected override bool MatchDescendant(ITest test)
65		{
66			if (!test.IsSuite || test.Tests == null || topLevel && test.RunState == RunState.Explicit)
67				return false;
68
69			foreach (ITest child in test.Tests)
70			{
71				if (Match(child) || MatchDescendant(child))
72					return true;
73			}
74
75			return false;
76		}	
77	}
78}