2004-06-10 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / nunit20 / core / CategoryFilter.cs
1 using System;
2 using System.Collections;
3
4 namespace NUnit.Core
5 {
6         /// <summary>
7         /// Summary description for CategoryFilter.
8         /// </summary>
9         /// 
10         [Serializable]
11         public class CategoryFilter : Filter
12         {
13                 ArrayList categories;
14
15                 public CategoryFilter() : this( false ) { }
16
17                 public CategoryFilter( bool exclude ) : base( exclude )
18                 {
19                         categories = new ArrayList();
20                 }
21
22                 public CategoryFilter( string name ) : this( name, false ) { }
23
24                 public CategoryFilter( string name, bool exclude ) : base( exclude )
25                 {
26                         categories = new ArrayList();
27                         categories.Add( name );
28                 }
29
30                 public CategoryFilter( string[] names ) : this( names, false ) { }
31
32                 public CategoryFilter( string[] names, bool exclude ) : base( exclude )
33                 {
34                         categories = new ArrayList();
35                         categories.AddRange( names );
36                 }
37
38                 public void AddCategory(string name) 
39                 {
40                         categories.Add( name );
41                 }
42
43                 #region IFilter Members
44
45                 public override bool Pass(TestSuite suite)
46                 {
47 //                      return CheckCategories( suite ) ? !Exclude : Exclude;
48
49                         if ( categories.Count == 0 ) return true;
50
51                         bool pass = Exclude;
52
53                         if (CheckCategories(suite))
54                                 return !Exclude;
55
56                         foreach (Test test in suite.Tests) 
57                         {
58                                 if ( test.Filter(this) == !Exclude )
59                                 {
60                                         pass=true;
61                                         break;
62                                 }
63                         }
64
65                         return pass;
66                 }
67
68                 public override bool Pass(TestCase test)
69                 {
70                         if ( categories.Count == 0 )
71                                 return true;
72                         return CheckCategories( test ) ? !Exclude : Exclude ;
73
74 //                      if (CheckCategories(test.Parent))
75 //                              return true;
76 //
77 //                      return CheckCategories(test);
78                 }
79
80                 #endregion
81
82                 /// <summary>
83                 /// Method returns true if the test has a particular
84                 /// category or if an ancestor test does. We don't
85                 /// worry about whether this is an include or an
86                 /// exclude filter at this point because only positive
87                 /// categories are inherited, not their absence.
88                 /// </summary>
89                 private bool CheckCategories(Test test) 
90                 {
91                         return test.HasCategory( categories )
92                                 || test.Parent != null 
93                                 && test.Parent.HasCategory( categories );
94
95 //                      if (test.Categories != null) 
96 //                      {
97 //                              foreach (string name in categories) 
98 //                              {
99 //                                      if (test.Categories.Contains(name))
100 //                                              return true;
101 //                              }
102 //                      }
103 //
104 //                      return false;
105                 }
106         }
107 }