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

/src/NUnit/interfaces/Filters/CategoryFilter.cs

#
C# | 102 lines | 54 code | 11 blank | 37 comment | 11 complexity | 5d46b7ecf1403e6cb490f8edea78c826 MD5 | raw file
  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.
  5// ****************************************************************
  6
  7using System;
  8using System.Text;
  9using System.Collections;
 10
 11namespace NUnit.Core.Filters
 12{
 13	/// <summary>
 14	/// CategoryFilter is able to select or exclude tests
 15	/// based on their categories.
 16	/// </summary>
 17	/// 
 18	[Serializable]
 19	public class CategoryFilter : TestFilter
 20	{
 21		ArrayList categories;
 22
 23		/// <summary>
 24		/// Construct an empty CategoryFilter
 25		/// </summary>
 26		public CategoryFilter()
 27		{
 28			categories = new ArrayList();
 29		}
 30
 31		/// <summary>
 32		/// Construct a CategoryFilter using a single category name
 33		/// </summary>
 34		/// <param name="name">A category name</param>
 35		public CategoryFilter( string name )
 36		{
 37			categories = new ArrayList();
 38			if ( name != null && name != string.Empty )
 39				categories.Add( name );
 40		}
 41
 42		/// <summary>
 43		/// Construct a CategoryFilter using an array of category names
 44		/// </summary>
 45		/// <param name="names">An array of category names</param>
 46		public CategoryFilter( string[] names )
 47		{
 48			categories = new ArrayList();
 49			if ( names != null )
 50				categories.AddRange( names );
 51		}
 52
 53		/// <summary>
 54		/// Add a category name to the filter
 55		/// </summary>
 56		/// <param name="name">A category name</param>
 57		public void AddCategory(string name) 
 58		{
 59			categories.Add( name );
 60		}
 61
 62		/// <summary>
 63		/// Check whether the filter matches a test
 64		/// </summary>
 65		/// <param name="test">The test to be matched</param>
 66		/// <returns></returns>
 67        public override bool Match(ITest test)
 68        {
 69			if ( test.Categories == null )
 70				return false;
 71
 72			foreach( string cat in categories )
 73				if ( test.Categories.Contains( cat ) )
 74					return true;
 75
 76			return false;
 77        }
 78		
 79		/// <summary>
 80		/// Return the string representation of a category filter
 81		/// </summary>
 82		/// <returns></returns>
 83		public override string ToString()
 84		{
 85			StringBuilder sb = new StringBuilder();
 86			for( int i = 0; i < categories.Count; i++ )
 87			{
 88				if ( i > 0 ) sb.Append( ',' );
 89				sb.Append( categories[i] );
 90			}
 91			return sb.ToString();
 92		}
 93
 94		/// <summary>
 95		/// Gets the list of categories from this filter
 96		/// </summary>
 97		public IList Categories
 98		{
 99			get { return categories; }
100		}
101	}
102}