// **************************************************************** // This is free software licensed under the NUnit license. You // may obtain a copy of the license as well as information regarding // copyright ownership at http://nunit.org/?p=license&r=2.4. // **************************************************************** using System; using System.Text; using System.Collections; namespace NUnit.Core.Filters { /// /// CategoryFilter is able to select or exclude tests /// based on their categories. /// /// [Serializable] public class CategoryFilter : TestFilter { ArrayList categories; /// /// Construct an empty CategoryFilter /// public CategoryFilter() { categories = new ArrayList(); } /// /// Construct a CategoryFilter using a single category name /// /// A category name public CategoryFilter( string name ) { categories = new ArrayList(); if ( name != null && name != string.Empty ) categories.Add( name ); } /// /// Construct a CategoryFilter using an array of category names /// /// An array of category names public CategoryFilter( string[] names ) { categories = new ArrayList(); if ( names != null ) categories.AddRange( names ); } /// /// Add a category name to the filter /// /// A category name public void AddCategory(string name) { categories.Add( name ); } /// /// Check whether the filter matches a test /// /// The test to be matched /// public override bool Match(ITest test) { if ( test.Categories == null ) return false; foreach( string cat in categories ) if ( test.Categories.Contains( cat ) ) return true; return false; } /// /// Return the string representation of a category filter /// /// public override string ToString() { StringBuilder sb = new StringBuilder(); for( int i = 0; i < categories.Count; i++ ) { if ( i > 0 ) sb.Append( ',' ); sb.Append( categories[i] ); } return sb.ToString(); } /// /// Gets the list of categories from this filter /// public IList Categories { get { return categories; } } } }