Exemplo n.º 1
0
 public void EnsureNestedTestsInitialized()
 {
     if (nestedTests == null)
     {
         nestedTests = InitializeNestedTests();
         OnNestedTestsInitialized();
     }
 }
        void RemoveTestClass(TopLevelTypeName fullName, ITest test)
        {
            topLevelTestClasses.Remove(fullName);
            TestCollection testNamespace = FindNamespace(NestedTestCollection, project.RootNamespace, fullName.Namespace);

            if (testNamespace != null)
            {
                testNamespace.Remove(test);
                if (testNamespace.Count == 0)
                {
                    // Remove the namespace
                    RemoveTestNamespace(NestedTestCollection, project.RootNamespace, fullName.Namespace);
                }
            }
            OnTestClassRemoved(test);
        }
 static TestCollection FindNamespace(TestCollection collection, string parentNamespace, string @namespace)
 {
     if (parentNamespace == @namespace)
     {
         return(collection);
     }
     foreach (var node in collection.OfType <TestNamespace>())
     {
         if (@namespace == node.NamespaceName)
         {
             return(node.NestedTests);
         }
         if (@namespace.StartsWith(node.NamespaceName + ".", StringComparison.Ordinal))
         {
             return(FindNamespace(node.NestedTests, node.NamespaceName, @namespace));
         }
     }
     return(null);
 }
        TestCollection FindOrCreateNamespace(TestCollection collection, string parentNamespace, string @namespace)
        {
            if (parentNamespace == @namespace)
            {
                return(collection);
            }
            foreach (var node in collection.OfType <TestNamespace>())
            {
                if (@namespace == node.NamespaceName)
                {
                    return(node.NestedTests);
                }
                if (@namespace.StartsWith(node.NamespaceName + ".", StringComparison.Ordinal))
                {
                    return(FindOrCreateNamespace(node.NestedTests, node.NamespaceName, @namespace));
                }
            }
            // Create missing namespace node:

            // Figure out which part of the namespace we can remove due to the parent namespace:
            int startPos = 0;

            if (@namespace.StartsWith(parentNamespace + ".", StringComparison.Ordinal))
            {
                startPos = parentNamespace.Length + 1;
            }
            // Get the next dot
            int dotPos = @namespace.IndexOf('.', startPos);

            if (dotPos < 0)
            {
                var newNode = new TestNamespace(this, @namespace);
                collection.Add(newNode);
                return(newNode.NestedTests);
            }
            else
            {
                var newNode = new TestNamespace(this, @namespace.Substring(0, dotPos));
                collection.Add(newNode);
                return(FindOrCreateNamespace(newNode.NestedTests, newNode.NamespaceName, @namespace));
            }
        }
 /// <summary>
 /// Removes the target namespace and all parent namespaces that are empty after the removal.
 /// </summary>
 static void RemoveTestNamespace(TestCollection collection, string parentNamespace, string @namespace)
 {
     if (parentNamespace == @namespace)
     {
         return;
     }
     foreach (var node in collection.OfType <TestNamespace>())
     {
         if (@namespace == node.NamespaceName)
         {
             collection.Remove(node);
             return;
         }
         if (@namespace.StartsWith(node.NamespaceName + ".", StringComparison.Ordinal))
         {
             RemoveTestNamespace(node.NestedTests, node.NamespaceName, @namespace);
             if (node.NestedTests.Count == 0)
             {
                 collection.Remove(node);
             }
             return;
         }
     }
 }