2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / nunit24 / NUnitCore / core / NamespaceTreeBuilder.cs
blob2e2546c7130edf8e3d129867c71523e60820a445
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/?p=license&r=2.4.
5 // ****************************************************************
7 using System.Collections;
9 namespace NUnit.Core
11 /// <summary>
12 /// Class that can build a tree of automatic namespace
13 /// suites from a group of fixtures.
14 /// </summary>
15 public class NamespaceTreeBuilder
17 #region Instance Variables
19 /// <summary>
20 /// Hashtable of all test suites we have created to represent namespaces.
21 /// Used to locate namespace parent suites for fixtures.
22 /// </summary>
23 Hashtable namespaceSuites = new Hashtable();
25 /// <summary>
26 /// The root of the test suite being created by this builder.
27 /// </summary>
28 TestSuite rootSuite;
30 #endregion
32 #region Constructor
34 public NamespaceTreeBuilder( TestSuite rootSuite )
36 this.rootSuite = rootSuite;
39 #endregion
41 #region Properties
43 public TestSuite RootSuite
45 get { return rootSuite; }
48 #endregion
50 #region Public Methods
52 public void Add( IList fixtures )
54 foreach (TestSuite fixture in fixtures)
55 //if (fixture is SetUpFixture)
56 // Add(fixture as SetUpFixture);
57 //else
58 Add( fixture );
61 public void Add( TestSuite fixture )
64 string ns = fixture.TestName.FullName;
65 int index = ns.LastIndexOf( '.' );
66 ns = index > 0 ? ns.Substring( 0, index ) : string.Empty;
67 TestSuite containingSuite = BuildFromNameSpace( ns );
69 if (fixture is SetUpFixture)
71 // The SetUpFixture must replace the namespace suite
72 // in which it is "contained".
74 // First, add the old suite's children
75 foreach (TestSuite child in containingSuite.Tests)
76 fixture.Add(child);
78 // Make the parent of the containing suite point to this
79 // fixture instead
80 // TODO: Get rid of this somehow?
81 TestSuite parent = (TestSuite)containingSuite.Parent;
82 if (parent == null)
84 fixture.TestName.Name = rootSuite.TestName.Name;
85 rootSuite = fixture;
87 else
89 parent.Tests.Remove(containingSuite);
90 parent.Add(fixture);
93 // Update the hashtable
94 namespaceSuites[ns] = fixture;
96 else
97 containingSuite.Add( fixture );
100 //public void Add( SetUpFixture fixture )
102 // string ns = fixture.FullName;
103 // int index = ns.LastIndexOf( '.' );
104 // ns = index > 0 ? ns.Substring( 0, index ) : string.Empty;
105 // TestSuite suite = BuildFromNameSpace( ns );
107 // // Make the parent point to this instead
108 // // TODO: Get rid of this somehow?
109 // TestSuite parent = suite.Parent;
110 // if ( parent != null )
111 // {
112 // parent.Tests.Remove( suite );
113 // parent.Add( fixture );
114 // }
116 // // Add the old suite's children
117 // foreach( TestSuite child in suite.Tests )
118 // fixture.Add( child );
120 // if (parent == null && fixture is SetUpFixture)
121 // {
122 // suite.Tests.Clear();
123 // suite.Add(fixture);
124 // }
125 // // Update the hashtable
126 // namespaceSuites[ns] = fixture;
129 #endregion
131 #region Helper Method
133 private TestSuite BuildFromNameSpace( string nameSpace )
135 if( nameSpace == null || nameSpace == "" ) return rootSuite;
136 TestSuite suite = (TestSuite)namespaceSuites[nameSpace];
137 if(suite!=null) return suite;
139 int index = nameSpace.LastIndexOf(".");
140 //string prefix = string.Format( "[{0}]" );
141 if( index == -1 )
143 suite = new TestSuite( nameSpace );
144 if ( rootSuite == null )
145 rootSuite = suite;
146 else
147 rootSuite.Add(suite);
148 namespaceSuites[nameSpace]=suite;
150 else
152 string parentNameSpace = nameSpace.Substring( 0,index );
153 TestSuite parent = BuildFromNameSpace( parentNameSpace );
154 string suiteName = nameSpace.Substring( index+1 );
155 suite = new TestSuite( parentNameSpace, suiteName );
156 parent.Add( suite );
157 namespaceSuites[nameSpace] = suite;
160 return suite;
163 #endregion