/src/NUnit/core/NamespaceTreeBuilder.cs
C# | 169 lines | 89 code | 27 blank | 53 comment | 15 complexity | f891df54cd0d47dfefa987b2ce58ea2c 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.Collections; 8 9namespace NUnit.Core 10{ 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 16 { 17 #region Instance Variables 18 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(); 24 25 /// <summary> 26 /// The root of the test suite being created by this builder. 27 /// </summary> 28 TestSuite rootSuite; 29 30 #endregion 31 32 #region Constructor 33 34 public NamespaceTreeBuilder( TestSuite rootSuite ) 35 { 36 this.rootSuite = rootSuite; 37 } 38 39 #endregion 40 41 #region Properties 42 43 public TestSuite RootSuite 44 { 45 get { return rootSuite; } 46 } 47 48 #endregion 49 50 #region Public Methods 51 52 public void Add( IList fixtures ) 53 { 54 foreach (TestSuite fixture in fixtures) 55 //if (fixture is SetUpFixture) 56 // Add(fixture as SetUpFixture); 57 //else 58 Add( fixture ); 59 } 60 61 public void Add( TestSuite fixture ) 62 { 63 if (fixture != null) 64 { 65 string ns = fixture.TestName.FullName; 66 int index = ns.IndexOf("["); 67 if (index >= 0) ns = ns.Substring(0, index); 68 index = ns.LastIndexOf('.'); 69 ns = index > 0 ? ns.Substring(0, index) : string.Empty; 70 TestSuite containingSuite = BuildFromNameSpace(ns); 71 72 if (fixture is SetUpFixture) 73 { 74 // The SetUpFixture must replace the namespace suite 75 // in which it is "contained". 76 // 77 // First, add the old suite's children 78 foreach (TestSuite child in containingSuite.Tests) 79 fixture.Add(child); 80 81 // Make the parent of the containing suite point to this 82 // fixture instead 83 // TODO: Get rid of this somehow? 84 TestSuite parent = (TestSuite)containingSuite.Parent; 85 if (parent == null) 86 { 87 fixture.TestName.Name = rootSuite.TestName.Name; 88 rootSuite = fixture; 89 } 90 else 91 { 92 parent.Tests.Remove(containingSuite); 93 parent.Add(fixture); 94 } 95 96 // Update the hashtable 97 namespaceSuites[ns] = fixture; 98 } 99 else 100 containingSuite.Add(fixture); 101 } 102 } 103 104 //public void Add( SetUpFixture fixture ) 105 //{ 106 // string ns = fixture.FullName; 107 // int index = ns.LastIndexOf( '.' ); 108 // ns = index > 0 ? ns.Substring( 0, index ) : string.Empty; 109 // TestSuite suite = BuildFromNameSpace( ns ); 110 111 // // Make the parent point to this instead 112 // // TODO: Get rid of this somehow? 113 // TestSuite parent = suite.Parent; 114 // if ( parent != null ) 115 // { 116 // parent.Tests.Remove( suite ); 117 // parent.Add( fixture ); 118 // } 119 120 // // Add the old suite's children 121 // foreach( TestSuite child in suite.Tests ) 122 // fixture.Add( child ); 123 124 // if (parent == null && fixture is SetUpFixture) 125 // { 126 // suite.Tests.Clear(); 127 // suite.Add(fixture); 128 // } 129 // // Update the hashtable 130 // namespaceSuites[ns] = fixture; 131 //} 132 133 #endregion 134 135 #region Helper Method 136 137 private TestSuite BuildFromNameSpace( string nameSpace ) 138 { 139 if( nameSpace == null || nameSpace == "" ) return rootSuite; 140 TestSuite suite = (TestSuite)namespaceSuites[nameSpace]; 141 if(suite!=null) return suite; 142 143 int index = nameSpace.LastIndexOf("."); 144 //string prefix = string.Format( "[{0}]" ); 145 if( index == -1 ) 146 { 147 suite = new NamespaceSuite( nameSpace ); 148 if ( rootSuite == null ) 149 rootSuite = suite; 150 else 151 rootSuite.Add(suite); 152 namespaceSuites[nameSpace]=suite; 153 } 154 else 155 { 156 string parentNameSpace = nameSpace.Substring( 0,index ); 157 TestSuite parent = BuildFromNameSpace( parentNameSpace ); 158 string suiteName = nameSpace.Substring( index+1 ); 159 suite = new NamespaceSuite( parentNameSpace, suiteName ); 160 parent.Add( suite ); 161 namespaceSuites[nameSpace] = suite; 162 } 163 164 return suite; 165 } 166 167 #endregion 168 } 169}