PageRenderTime 46ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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