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
;
12 /// Class that can build a tree of automatic namespace
13 /// suites from a group of fixtures.
15 public class NamespaceTreeBuilder
17 #region Instance Variables
20 /// Hashtable of all test suites we have created to represent namespaces.
21 /// Used to locate namespace parent suites for fixtures.
23 Hashtable namespaceSuites
= new Hashtable();
26 /// The root of the test suite being created by this builder.
34 public NamespaceTreeBuilder( TestSuite rootSuite
)
36 this.rootSuite
= rootSuite
;
43 public TestSuite RootSuite
45 get { return rootSuite; }
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);
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
)
78 // Make the parent of the containing suite point to this
80 // TODO: Get rid of this somehow?
81 TestSuite parent
= (TestSuite
)containingSuite
.Parent
;
84 fixture
.TestName
.Name
= rootSuite
.TestName
.Name
;
89 parent
.Tests
.Remove(containingSuite
);
93 // Update the hashtable
94 namespaceSuites
[ns
] = fixture
;
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 )
112 // parent.Tests.Remove( suite );
113 // parent.Add( fixture );
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)
122 // suite.Tests.Clear();
123 // suite.Add(fixture);
125 // // Update the hashtable
126 // namespaceSuites[ns] = fixture;
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}]" );
143 suite
= new TestSuite( nameSpace
);
144 if ( rootSuite
== null )
147 rootSuite
.Add(suite
);
148 namespaceSuites
[nameSpace
]=suite
;
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
);
157 namespaceSuites
[nameSpace
] = suite
;