From bb4814b71981661a4a50d21c0964893357ee8dfa Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Mon, 18 Dec 2006 17:59:41 +0000 Subject: [PATCH] Fixed SD2-1219. New namespace, class and method tree nodes added to the Unit Tests tree are now sorted. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@2170 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Misc/UnitTesting/Src/TestClassTreeNode.cs | 1 + .../UnitTesting/Src/TestNamespaceTreeNode.cs | 9 + .../UnitTesting/Src/TestProjectTreeNode.cs | 5 + .../Misc/UnitTesting/Src/TestTreeNode.cs | 10 + .../Test/Tree/TreeNodeSortingTestFixture.cs | 256 ++++++++++++++++++ .../UnitTesting/Test/UnitTesting.Tests.csproj | 1 + 6 files changed, 282 insertions(+) create mode 100644 src/AddIns/Misc/UnitTesting/Test/Tree/TreeNodeSortingTestFixture.cs diff --git a/src/AddIns/Misc/UnitTesting/Src/TestClassTreeNode.cs b/src/AddIns/Misc/UnitTesting/Src/TestClassTreeNode.cs index a2d000b0e4..b6f12aeee8 100644 --- a/src/AddIns/Misc/UnitTesting/Src/TestClassTreeNode.cs +++ b/src/AddIns/Misc/UnitTesting/Src/TestClassTreeNode.cs @@ -83,6 +83,7 @@ namespace ICSharpCode.UnitTesting void TestMethodAdded(object source, TestMethodEventArgs e) { AddTestMethodTreeNode(e.TestMethod); + SortChildNodes(); } /// diff --git a/src/AddIns/Misc/UnitTesting/Src/TestNamespaceTreeNode.cs b/src/AddIns/Misc/UnitTesting/Src/TestNamespaceTreeNode.cs index b67952cb84..b173367faf 100644 --- a/src/AddIns/Misc/UnitTesting/Src/TestNamespaceTreeNode.cs +++ b/src/AddIns/Misc/UnitTesting/Src/TestNamespaceTreeNode.cs @@ -142,6 +142,9 @@ namespace ICSharpCode.UnitTesting TestClassTreeNode classNode = new TestClassTreeNode(TestProject, c); classNode.AddTo(this); } + + // Sort the nodes. + SortChildNodes(); } /// @@ -265,6 +268,9 @@ namespace ICSharpCode.UnitTesting // Add a new tree node. TestClassTreeNode classNode = new TestClassTreeNode(TestProject, e.TestClass); classNode.AddTo(this); + + // Sort the nodes. + SortChildNodes(); } else if (isInitialized && NamespaceStartsWith(e.TestClass.Namespace)) { // Check if there is a child namespace node for the class. string childNamespace = TestClass.GetChildNamespace(e.TestClass.Namespace, fullNamespace); @@ -272,6 +278,9 @@ namespace ICSharpCode.UnitTesting // Add a new namespace node. TestNamespaceTreeNode node = new TestNamespaceTreeNode(TestProject, fullNamespace, childNamespace); node.AddTo(this); + + // Sort the nodes. + SortChildNodes(); } } } diff --git a/src/AddIns/Misc/UnitTesting/Src/TestProjectTreeNode.cs b/src/AddIns/Misc/UnitTesting/Src/TestProjectTreeNode.cs index 41b1a11e8f..1011464d63 100644 --- a/src/AddIns/Misc/UnitTesting/Src/TestProjectTreeNode.cs +++ b/src/AddIns/Misc/UnitTesting/Src/TestProjectTreeNode.cs @@ -53,6 +53,9 @@ namespace ICSharpCode.UnitTesting foreach (TestClass c in TestProject.GetTestClasses(String.Empty)) { AddClassNode(c); } + + // Sort the nodes. + SortChildNodes(); } /// @@ -72,12 +75,14 @@ namespace ICSharpCode.UnitTesting { if (e.TestClass.Namespace == String.Empty) { AddClassNode(e.TestClass); + SortChildNodes(); } else if (isInitialized) { // Check that we have a namespace node for this class. if (!NamespaceNodeExists(e.TestClass.RootNamespace)) { // Add a new namespace node. TestNamespaceTreeNode node = new TestNamespaceTreeNode(TestProject, e.TestClass.RootNamespace); node.AddTo(this); + SortChildNodes(); } } } diff --git a/src/AddIns/Misc/UnitTesting/Src/TestTreeNode.cs b/src/AddIns/Misc/UnitTesting/Src/TestTreeNode.cs index 506e406587..2da88c6b84 100644 --- a/src/AddIns/Misc/UnitTesting/Src/TestTreeNode.cs +++ b/src/AddIns/Misc/UnitTesting/Src/TestTreeNode.cs @@ -92,5 +92,15 @@ namespace ICSharpCode.UnitTesting } return false; } + + /// + /// Sorts the immediate child nodes of this node. The sort + /// is not done recursively. + /// + protected void SortChildNodes() + { + ExtTreeView treeView = (ExtTreeView)TreeView; + treeView.SortNodes(Nodes, false); + } } } diff --git a/src/AddIns/Misc/UnitTesting/Test/Tree/TreeNodeSortingTestFixture.cs b/src/AddIns/Misc/UnitTesting/Test/Tree/TreeNodeSortingTestFixture.cs new file mode 100644 index 0000000000..2c25712b89 --- /dev/null +++ b/src/AddIns/Misc/UnitTesting/Test/Tree/TreeNodeSortingTestFixture.cs @@ -0,0 +1,256 @@ +// +// +// +// +// $Revision$ +// + +using System; +using System.Collections.Generic; +using System.Windows.Forms; + +using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.SharpDevelop.Gui; +using ICSharpCode.SharpDevelop.Project; +using ICSharpCode.UnitTesting; +using NUnit.Framework; +using UnitTesting.Tests.Utils; + +namespace UnitTesting.Tests.Tree +{ + /// + /// Test that when a new class is added to the set of test classes + /// currently being displayed in the tree the nodes are sorted + /// correctly afterwards. We also test that the order of the + /// tree nodes is correct after adding new methods. + /// + [TestFixture] + public class TreeNodeSortingTestFixture + { + DummyParserServiceTestTreeView treeView; + TestProjectTreeNode projectNode; + TestNamespaceTreeNode myTestsNamespaceNode; + TestClassTreeNode testFixtureNode; + MockProjectContent projectContent; + TestProject testProject; + + [SetUp] + public void Init() + { + // Create a project to display in the test tree view. + MockCSharpProject project = new MockCSharpProject(); + project.Name = "TestProject"; + ReferenceProjectItem nunitFrameworkReferenceItem = new ReferenceProjectItem(project); + nunitFrameworkReferenceItem.Include = "NUnit.Framework"; + ProjectService.AddProjectItem(project, nunitFrameworkReferenceItem); + List projects = new List(); + projects.Add(project); + + // Add a test class with a TestFixture attributes. + projectContent = new MockProjectContent(); + projectContent.Language = LanguageProperties.None; + TestClass testClass = CreateTestClass("MyTests.MyTestFixture"); + projectContent.Classes.Add(testClass.Class); + + // Add two methods to the test class only + // one of which has test attributes. + MockMethod testMethod = new MockMethod("NameExists"); + testMethod.Attributes.Add(new MockAttribute("Test")); + testMethod.DeclaringType = testClass.Class; + testClass.Class.Methods.Add(testMethod); + + // Init mock project content to be returned. + treeView = new DummyParserServiceTestTreeView(); + treeView.ProjectContentForProject = projectContent; + + // Load the projects into the test tree view. + treeView.AddProjects(projects); + projectNode = (TestProjectTreeNode)treeView.Nodes[0]; + testProject = projectNode.TestProject; + + // Initialise the root node so the child nodes are created. + projectNode.PerformInitialization(); + myTestsNamespaceNode = (TestNamespaceTreeNode)projectNode.FirstNode; + + // Initialise the first namespace node. + myTestsNamespaceNode.PerformInitialization(); + testFixtureNode = (TestClassTreeNode)myTestsNamespaceNode.FirstNode; + + // Initialise the test method tree nodes. + testFixtureNode.PerformInitialization(); + } + + [TearDown] + public void TearDown() + { + if (treeView != null) { + treeView.Dispose(); + } + } + + /// + /// Add a class that exists in a namespace that + /// should be inserted into the tree before the current + /// namespace node. + /// + [Test] + public void AddClassInNewNamespace() + { + // Create the new test class. + TestClass testClass = CreateTestClass("Apple.MyTestFixture"); + + // Add the class to the existing classes. + testProject.TestClasses.Add(testClass); + + // Check the namespace node is inserted before the + // existing namespace node. + TestNamespaceTreeNode namespaceTreeNode = (TestNamespaceTreeNode)projectNode.FirstNode; + + Assert.AreEqual("Apple", namespaceTreeNode.Text); + } + + /// + /// Here we add a class with no root namespace but with a name + /// that alphabetically would occur before the existing + /// namespace tree node. NUnit does not treat namespace + /// nodes any different to test class nodes so have the + /// same behaviour. This means a class node could appear + /// before the namespace node in the tree. + /// + [Test] + public void AddClassWithNoRootNamespace() + { + TestClass testClass = CreateTestClass("AppleTest"); + testProject.TestClasses.Add(testClass); + + TestClassTreeNode classNode = projectNode.FirstNode as TestClassTreeNode; + + Assert.AreEqual(classNode.Text, "AppleTest"); + } + + /// + /// Make sure that if we have a class with no root namespace + /// and it is alphabetically before an existing namespace + /// then it is added in the tree before that namespace. + /// We also test that the same thing occurs with a namespace + /// tree node. + /// + [Test] + public void ClassOccursBeforeNamespaceOnInitialLoad() + { + MockCSharpProject project = new MockCSharpProject(); + project.Name = "TestProject"; + ReferenceProjectItem nunitFrameworkReferenceItem = new ReferenceProjectItem(project); + nunitFrameworkReferenceItem.Include = "NUnit.Framework"; + ProjectService.AddProjectItem(project, nunitFrameworkReferenceItem); + List projects = new List(); + projects.Add(project); + + // Add a test class with a TestFixture attributes. + TestClass testClass = CreateTestClass("MyTests.MyTestFixture"); + projectContent.Classes.Add(testClass.Class); + + // Add a second class with no namespace. + testClass = CreateTestClass("AppleTestFixture"); + projectContent.Classes.Add(testClass.Class); + + // Add another class that exists in a namespace inside + // MyTests. + testClass = CreateTestClass("MyTests.ZebraTests.AddZebra"); + projectContent.Classes.Add(testClass.Class); + + // Load the project into the tree. + treeView.Clear(); + treeView.AddProject(project); + projectNode = (TestProjectTreeNode)treeView.Nodes[0]; + projectNode.PerformInitialization(); + + ExtTreeNode treeNode = (ExtTreeNode)projectNode.LastNode; + treeNode.PerformInitialization(); + + // Get the class node without a root namespace and + // the my tests namespace node. + TestClassTreeNode appleTestFixtureNode = projectNode.FirstNode as TestClassTreeNode; + TestNamespaceTreeNode myTestsNamespaceNode = projectNode.LastNode as TestNamespaceTreeNode; + + // Get the zebra namespace tree node. + TestNamespaceTreeNode zebraTestsNamespaceNode = treeNode.LastNode as TestNamespaceTreeNode; + + Assert.IsNotNull(appleTestFixtureNode); + Assert.AreEqual(appleTestFixtureNode.Text, "AppleTestFixture"); + Assert.IsNotNull(myTestsNamespaceNode); + Assert.AreEqual(myTestsNamespaceNode.Text, "MyTests"); + Assert.IsNotNull(zebraTestsNamespaceNode); + Assert.AreEqual(zebraTestsNamespaceNode.Text, "ZebraTests"); + } + + /// + /// Here we add a new class that exists in a namespace that + /// did not previously exist and we make sure the tree node is + /// inserted before any other tree node. + /// + [Test] + public void NewNamespaceNode() + { + // Add a class in a new namespace. + TestClass testClass = CreateTestClass("MyTests.AppleTests.IsEqual"); + testProject.TestClasses.Add(testClass); + + TestTreeNode insertedNode = myTestsNamespaceNode.FirstNode as TestTreeNode; + Assert.AreEqual("AppleTests", insertedNode.Text); + } + + /// + /// Here we add a new class that exists in a namespace that + /// already exists and we make sure the new tree node is + /// inserted before any other tree node. + /// + [Test] + public void AddNewClassToExistingNamespace() + { + // Add a class in the tests namespace. + TestClass testClass = CreateTestClass("MyTests.AppleTestFixture"); + testProject.TestClasses.Add(testClass); + + TestTreeNode insertedNode = myTestsNamespaceNode.FirstNode as TestTreeNode; + Assert.AreEqual("AppleTestFixture", insertedNode.Text); + } + + /// + /// Tests that new methods are inserted into the tree in the + /// correct alphabetical order. + /// + [Test] + public void NewClassMethodAdded() + { + MockClass c = (MockClass)testFixtureNode.Class; + MockMethod method = new MockMethod("Apple"); + method.Attributes.Add(new MockAttribute("Test")); + method.DeclaringType = c; + c.SetCompoundClass(c); + c.Methods.Add(method); + + // Add the test method to the class. + DefaultCompilationUnit newUnit = new DefaultCompilationUnit(projectContent); + newUnit.Classes.Add(c); + testProject.UpdateParseInfo(null, newUnit); + + // Check that the new tree node is inserted before the + // existing method node. + TestTreeNode insertedNode = testFixtureNode.FirstNode as TestTreeNode; + + Assert.AreEqual("Apple", insertedNode.Text); + } + + /// + /// Creates a new class with the fully qualified name. + /// + TestClass CreateTestClass(string name) + { + MockClass c = new MockClass(name); + c.Attributes.Add(new MockAttribute("TestFixture")); + c.ProjectContent = projectContent; + return new TestClass(c); + } + } +} diff --git a/src/AddIns/Misc/UnitTesting/Test/UnitTesting.Tests.csproj b/src/AddIns/Misc/UnitTesting/Test/UnitTesting.Tests.csproj index 22b2ebc4ea..72ed8a1afd 100644 --- a/src/AddIns/Misc/UnitTesting/Test/UnitTesting.Tests.csproj +++ b/src/AddIns/Misc/UnitTesting/Test/UnitTesting.Tests.csproj @@ -57,6 +57,7 @@ +