NamespaceTreeBuilder
Class that can build a tree of automatic namespace
suites from a group of fixtures.
using System.Collections;
using System.Collections.Generic;
namespace NUnit.Framework.Internal.Builders
{
public class NamespaceTreeBuilder
{
private Dictionary<string, TestSuite> namespaceSuites = new Dictionary<string, TestSuite>();
private TestSuite rootSuite;
public TestSuite RootSuite => rootSuite;
public NamespaceTreeBuilder(TestSuite rootSuite)
{
this.rootSuite = rootSuite;
}
public void Add(IList fixtures)
{
foreach (TestSuite fixture in fixtures) {
Add(fixture);
}
}
public void Add(TestSuite fixture)
{
string text = fixture.FullName;
int num = text.IndexOf("[");
if (num >= 0)
text = text.Substring(0, num);
num = text.LastIndexOf('.');
text = ((num > 0) ? text.Substring(0, num) : string.Empty);
TestSuite testSuite = BuildFromNameSpace(text);
if (fixture is SetUpFixture) {
foreach (TestSuite test in testSuite.Tests) {
fixture.Add(test);
}
TestSuite testSuite2 = (TestSuite)testSuite.Parent;
if (testSuite2 == null) {
fixture.Name = rootSuite.Name;
rootSuite = fixture;
} else {
testSuite2.Tests.Remove(testSuite);
testSuite2.Add(fixture);
}
namespaceSuites[text] = fixture;
} else
testSuite.Add(fixture);
}
private TestSuite BuildFromNameSpace(string ns)
{
if (ns == null || ns == "")
return rootSuite;
TestSuite testSuite = namespaceSuites.ContainsKey(ns) ? namespaceSuites[ns] : null;
if (testSuite != null)
return testSuite;
int num = ns.LastIndexOf(".");
if (num == -1) {
testSuite = new TestSuite(ns);
if (rootSuite == null)
rootSuite = testSuite;
else
rootSuite.Add(testSuite);
} else {
string text = ns.Substring(0, num);
TestSuite testSuite2 = BuildFromNameSpace(text);
string name = ns.Substring(num + 1);
testSuite = new TestSuite(text, name);
testSuite2.Add(testSuite);
}
namespaceSuites[ns] = testSuite;
return testSuite;
}
}
}