Exemplo n.º 1
0
        public override bool Visit(CodeElement element)
        {
            bool visitChildren;

            switch (element.Kind)
            {
            case vsCMElement.vsCMElementNamespace:
                visitChildren = true;
                currentSuite  = null;
                break;

            case vsCMElement.vsCMElementClass:
                /*
                 * Only visit a class's or struct's children if it inherits
                 * from the CxxTest suite class.
                 */
                visitChildren = IsClassTestSuite((VCCodeClass)element);

                if (visitChildren)
                {
                    TestCaseCollector collector = new TestCaseCollector();
                    collector.Process(element.Children);

                    currentSuite.CreateLineNumber  = collector.CreateLineNumber;
                    currentSuite.DestroyLineNumber = collector.DestroyLineNumber;

                    foreach (TestCase testCase in collector.TestCases)
                    {
                        currentSuite.AddTestCase(testCase);
                    }

                    visitChildren = false;
                }

                break;

            case vsCMElement.vsCMElementStruct:
                /*
                 * Only visit a class's or struct's children if it inherits
                 * from the CxxTest suite class.
                 */
                visitChildren = IsStructTestSuite((VCCodeStruct)element);

                if (visitChildren)
                {
                    TestCaseCollector collector = new TestCaseCollector();
                    collector.Process(element.Children);

                    currentSuite.CreateLineNumber  = collector.CreateLineNumber;
                    currentSuite.DestroyLineNumber = collector.DestroyLineNumber;

                    foreach (TestCase testCase in collector.TestCases)
                    {
                        currentSuite.AddTestCase(testCase);
                    }

                    visitChildren = false;
                }

                break;

            case vsCMElement.vsCMElementFunction:
                visitChildren = false;

                if (currentSuite == null)
                {
                    /*
                     * Check global functions to see if the user has created a
                     * main() function. If so, we need to generate the test
                     * runner as a static object instead.
                     */
                    CheckForMain((VCCodeFunction)element);
                }

                break;

            default:
                visitChildren = false;
                break;
            }

            return(visitChildren);
        }
        public void ChangeElement(CodeElement element, vsCMChangeKind changeKind)
        {
            if (testsTreeView.Nodes.Count == 0)
                return;

            if (element is VCCodeClass || element is VCCodeStruct)
            {
                testsTreeView.BeginUpdate();

                VCCodeModel model = ((VCCodeElement)element).CodeModel;

                TestSuiteCollector collector = new TestSuiteCollector();
                collector.Process(model);
                List<TestSuite> suitesInModel = new List<TestSuite>(collector.Suites);

                // Iterate through the current suites in the view and pull out any
                // that are no longer in the model.

                TreeNode solutionNode = testsTreeView.Nodes[0];

                for (int i = 0; i < solutionNode.Nodes.Count; )
                {
                    TreeNode viewNode = solutionNode.Nodes[i];
                    int index = suitesInModel.FindIndex(
                        new Predicate<TestSuite>(delegate(TestSuite suite)
                        {
                            return suite.Name == viewNode.Text;
                        }));

                    if (index == -1)
                        viewNode.Remove();
                    else
                    {
                        // The suites that are left over will be those in the
                        // model that aren't yet in the tree, so they need to
                        // be added.

                        suitesInModel.RemoveAt(index);
                        i++;
                    }
                }

                // Make sure a suite with the same name isn't already in the
                // view (this could happen if the name changes and then quickly
                // changes back before the event fires. If it's not there, add
                // it.

                foreach(TestSuite suite in suitesInModel)
                    AddTestSuiteToNode(suite, solutionNode, null).Expand();

                testsTreeView.Sort();
                testsTreeView.EndUpdate();
            }
            else if (element is VCCodeFunction)
            {
                testsTreeView.BeginUpdate();

                VCCodeElement parent = (VCCodeElement)((VCCodeElement)element).Parent;

                TestCaseCollector collector = new TestCaseCollector();
                collector.Process(parent.Children);
                List<TestCase> casesInModel = new List<TestCase>(collector.TestCases);

                // Iterate through the current test cases for this suite in the
                // view and pull out any that are no longer in the model.

                TreeNode suiteNode = FindNodeForTestSuite(parent);

                if (suiteNode == null)
                    return;

                for (int i = 0; i < suiteNode.Nodes.Count; )
                {
                    TreeNode viewNode = suiteNode.Nodes[i];
                    int index = casesInModel.FindIndex(
                        new Predicate<TestCase>(delegate(TestCase testCase)
                        {
                            return testCase.Name == viewNode.Text;
                        }));

                    if (index == -1)
                        viewNode.Remove();
                    else
                    {
                        // The test cases that are left over will be those in
                        // the model that aren't yet in the tree, so they need
                        // to be added.

                        casesInModel.RemoveAt(index);
                        i++;
                    }
                }

                foreach (TestCase testCase in casesInModel)
                    AddTestCaseToNode((TestSuite)suiteNode.Tag, testCase, suiteNode, null);

                testsTreeView.Sort();
                testsTreeView.EndUpdate();
            }
        }
        public override bool Visit(CodeElement element)
        {
            bool visitChildren;

            switch (element.Kind)
            {
                case vsCMElement.vsCMElementNamespace:
                    visitChildren = true;
                    currentSuite = null;
                    break;

                case vsCMElement.vsCMElementClass:
                    /*
                     * Only visit a class's or struct's children if it inherits
                     * from the CxxTest suite class.
                     */
                    visitChildren = IsClassTestSuite((VCCodeClass)element);

                    if (visitChildren)
                    {
                        TestCaseCollector collector = new TestCaseCollector();
                        collector.Process(element.Children);

                        currentSuite.CreateLineNumber = collector.CreateLineNumber;
                        currentSuite.DestroyLineNumber = collector.DestroyLineNumber;

                        foreach (TestCase testCase in collector.TestCases)
                            currentSuite.AddTestCase(testCase);

                        visitChildren = false;
                    }

                    break;

                case vsCMElement.vsCMElementStruct:
                    /*
                     * Only visit a class's or struct's children if it inherits
                     * from the CxxTest suite class.
                     */
                    visitChildren = IsStructTestSuite((VCCodeStruct)element);

                    if (visitChildren)
                    {
                        TestCaseCollector collector = new TestCaseCollector();
                        collector.Process(element.Children);

                        currentSuite.CreateLineNumber = collector.CreateLineNumber;
                        currentSuite.DestroyLineNumber = collector.DestroyLineNumber;

                        foreach (TestCase testCase in collector.TestCases)
                            currentSuite.AddTestCase(testCase);

                        visitChildren = false;
                    }

                    break;

                case vsCMElement.vsCMElementFunction:
                    visitChildren = false;

                    if (currentSuite == null)
                    {
                        /*
                         * Check global functions to see if the user has created a
                         * main() function. If so, we need to generate the test
                         * runner as a static object instead.
                         */
                        CheckForMain((VCCodeFunction)element);
                    }

                    break;

                default:
                    visitChildren = false;
                    break;
            }

            return visitChildren;
        }
        public void AddElement(CodeElement element)
        {
            if (testsTreeView.Nodes.Count == 0)
                return;

            if (element is VCCodeClass || element is VCCodeStruct)
            {
                TestSuiteCollector collector = new TestSuiteCollector();
                collector.Process(element);

                foreach (TestSuite suite in collector.Suites)
                    AddTestSuiteToNode(suite, testsTreeView.Nodes[0], null);

                testsTreeView.Sort();
            }
            else if (element is VCCodeFunction)
            {
                VCCodeFunction function = (VCCodeFunction)element;

                TreeNode suiteNode = FindNodeForTestSuite(function.Parent);

                TestCaseCollector collector = new TestCaseCollector();
                collector.Process((CodeElement)function);

                foreach (TestCase testCase in collector.TestCases)
                    AddTestCaseToNode((TestSuite)suiteNode.Tag, testCase, suiteNode, null);

                testsTreeView.Sort();
            }
        }