PageRenderTime 57ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/src/AddIns/Analysis/UnitTesting/Test/Tree/TwoTestClassesInDifferentNamespacesTestFixture.cs

https://github.com/eusebiu/SharpDevelop
C# | 282 lines | 200 code | 47 blank | 35 comment | 14 complexity | 40edd525337684b566658077b121e7e4 MD5 | raw file
  1. // Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt)
  2. // This code is distributed under the GNU LGPL (for details please see \doc\license.txt)
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Windows.Forms;
  6. using ICSharpCode.SharpDevelop.Dom;
  7. using ICSharpCode.SharpDevelop.Gui;
  8. using ICSharpCode.SharpDevelop.Project;
  9. using ICSharpCode.UnitTesting;
  10. using NUnit.Framework;
  11. using UnitTesting.Tests.Utils;
  12. namespace UnitTesting.Tests.Tree
  13. {
  14. /// <summary>
  15. /// Tests that when a namespace tree node has a class and another
  16. /// namespace as a child the image index is updated correctly.
  17. /// </summary>
  18. [TestFixture]
  19. public class TwoTestClassesInDifferentNamesTestFixture
  20. {
  21. Solution solution;
  22. ExtTreeNode rootNode;
  23. TreeNodeCollection nodes;
  24. DummyParserServiceTestTreeView dummyTreeView;
  25. TestTreeView treeView;
  26. MSBuildBasedProject project;
  27. MockClass testClass1;
  28. MockClass testClass2;
  29. ExtTreeNode projectNamespaceNode;
  30. TestProject testProject;
  31. MockProjectContent projectContent;
  32. MockTestFrameworksWithNUnitFrameworkSupport testFrameworks;
  33. [SetUp]
  34. public void SetUp()
  35. {
  36. solution = new Solution(new MockProjectChangeWatcher());
  37. // Create a project to display in the test tree view.
  38. project = new MockCSharpProject(solution, "TestProject");
  39. ReferenceProjectItem nunitFrameworkReferenceItem = new ReferenceProjectItem(project);
  40. nunitFrameworkReferenceItem.Include = "NUnit.Framework";
  41. ProjectService.AddProjectItem(project, nunitFrameworkReferenceItem);
  42. // Add a test class with a TestFixture attributes.
  43. projectContent = new MockProjectContent();
  44. projectContent.Language = LanguageProperties.None;
  45. testClass1 = new MockClass(projectContent, "Project.Tests.MyTestFixture");
  46. testClass1.Attributes.Add(new MockAttribute("TestFixture"));
  47. projectContent.Classes.Add(testClass1);
  48. testClass2 = new MockClass(projectContent, "Project.MyTestFixture");
  49. testClass2.Attributes.Add(new MockAttribute("TestFixture"));
  50. projectContent.Classes.Add(testClass2);
  51. testFrameworks = new MockTestFrameworksWithNUnitFrameworkSupport();
  52. dummyTreeView = new DummyParserServiceTestTreeView(testFrameworks);
  53. dummyTreeView.ProjectContentForProject = projectContent;
  54. // Load the projects into the test tree view.
  55. treeView = dummyTreeView as TestTreeView;
  56. solution.Folders.Add(project);
  57. treeView.AddSolution(solution);
  58. nodes = treeView.Nodes;
  59. rootNode = (ExtTreeNode)treeView.Nodes[0];
  60. treeView.SelectedNode = rootNode;
  61. testProject = treeView.SelectedTestProject;
  62. }
  63. [TearDown]
  64. public void TearDown()
  65. {
  66. if (treeView != null) {
  67. treeView.Dispose();
  68. }
  69. }
  70. public void ExpandRootNode()
  71. {
  72. // Expand the root node so any child nodes are lazily created.
  73. rootNode.Expanding();
  74. projectNamespaceNode = (ExtTreeNode)rootNode.Nodes[0];
  75. }
  76. [Test]
  77. public void TestClass2PassedAfterTestClass1Failed()
  78. {
  79. ExpandRootNode();
  80. TestClass testClass1 = testProject.TestClasses["Project.Tests.MyTestFixture"];
  81. testClass1.Result = TestResultType.Failure;
  82. TestClass testClass2 = testProject.TestClasses["Project.MyTestFixture"];
  83. testClass2.Result = TestResultType.Success;
  84. Assert.AreEqual(TestTreeViewImageListIndex.TestFailed, (TestTreeViewImageListIndex)projectNamespaceNode.ImageIndex);
  85. }
  86. [Test]
  87. public void TestClass2PassedAfterTestClass1Ignored()
  88. {
  89. ExpandRootNode();
  90. TestClass testClass1 = testProject.TestClasses["Project.Tests.MyTestFixture"];
  91. testClass1.Result = TestResultType.Ignored;
  92. TestClass testClass2 = testProject.TestClasses["Project.MyTestFixture"];
  93. testClass2.Result = TestResultType.Success;
  94. Assert.AreEqual(TestTreeViewImageListIndex.TestIgnored, (TestTreeViewImageListIndex)projectNamespaceNode.ImageIndex);
  95. }
  96. [Test]
  97. public void ExpandProjectNodeAfterOnePassOneFail()
  98. {
  99. TestClass testClass1 = testProject.TestClasses["Project.Tests.MyTestFixture"];
  100. testClass1.Result = TestResultType.Failure;
  101. TestClass testClass2 = testProject.TestClasses["Project.MyTestFixture"];
  102. testClass2.Result = TestResultType.Success;
  103. ExpandRootNode();
  104. Assert.AreEqual(TestTreeViewImageListIndex.TestFailed, (TestTreeViewImageListIndex)projectNamespaceNode.ImageIndex);
  105. }
  106. [Test]
  107. public void AddNewClass()
  108. {
  109. ExtTreeNode projectNamespaceNode = ExpandProjectNamespaceNode();
  110. ExtTreeNode testsNamespaceNode = ExpandTestsNamespaceNode();
  111. MockClass mockClass = new MockClass(projectContent, "Project.Tests.MyNewTestFixture");
  112. mockClass.Attributes.Add(new MockAttribute("TestFixture"));
  113. TestClass newTestClass = new TestClass(mockClass, testFrameworks);
  114. testProject.TestClasses.Add(newTestClass);
  115. ExtTreeNode newTestClassNode = null;
  116. foreach (ExtTreeNode node in testsNamespaceNode.Nodes) {
  117. if (node.Text == "MyNewTestFixture") {
  118. newTestClassNode = node;
  119. break;
  120. }
  121. }
  122. newTestClass.Result = TestResultType.Failure;
  123. // New test class node should be added to the test namespace node.
  124. Assert.AreEqual(2, testsNamespaceNode.Nodes.Count);
  125. Assert.IsNotNull(newTestClassNode);
  126. // Make sure the namespace node image index is affected by the
  127. // new test class added.
  128. Assert.AreEqual(TestTreeViewImageListIndex.TestFailed, (TestTreeViewImageListIndex)testsNamespaceNode.SelectedImageIndex);
  129. // Project namespace node should have two child nodes, one for the
  130. // Tests namespace and one test class node.
  131. Assert.AreEqual(2, projectNamespaceNode.Nodes.Count);
  132. }
  133. /// <summary>
  134. /// Tests that the test class is removed from the tree.
  135. /// </summary>
  136. [Test]
  137. public void RemoveClass()
  138. {
  139. AddNewClass();
  140. ExtTreeNode projectNamespaceNode = ExpandProjectNamespaceNode();
  141. ExtTreeNode testsNamespaceNode = ExpandTestsNamespaceNode();
  142. // Reset the new TestClass result after it was modified
  143. // in the AddNewClass call.
  144. TestClass newTestClass = testProject.TestClasses["Project.Tests.MyNewTestFixture"];
  145. newTestClass.Result = TestResultType.None;
  146. // Locate the class we are going to remove.
  147. TestClass testClass = testProject.TestClasses["Project.Tests.MyTestFixture"];
  148. testProject.TestClasses.Remove(testClass);
  149. ExtTreeNode testClassNode = null;
  150. foreach (ExtTreeNode node in testsNamespaceNode.Nodes) {
  151. if (node.Text == "MyTestFixture") {
  152. testClassNode = node;
  153. break;
  154. }
  155. }
  156. testClass.Result = TestResultType.Failure;
  157. Assert.AreEqual(1, testsNamespaceNode.Nodes.Count);
  158. Assert.IsNull(testClassNode);
  159. // Make sure the namespace node image index is NOT affected by the
  160. // test class just removed.
  161. Assert.AreEqual(TestTreeViewImageListIndex.TestNotRun, (TestTreeViewImageListIndex)testsNamespaceNode.SelectedImageIndex);
  162. // Check that the test class does not affect the project namespace node
  163. // image index either.
  164. Assert.AreEqual(TestTreeViewImageListIndex.TestNotRun, (TestTreeViewImageListIndex)projectNamespaceNode.ImageIndex);
  165. }
  166. /// <summary>
  167. /// Tests that after all the child nodes from a namespace node are removed
  168. /// the namespace node removes itself.
  169. /// </summary>
  170. [Test]
  171. public void RemoveNamespaceNode()
  172. {
  173. ExtTreeNode projectNamespaceNode = ExpandProjectNamespaceNode();
  174. ExtTreeNode testsNamespaceNode = ExpandTestsNamespaceNode();
  175. // Locate the class we are going to remove.
  176. TestClass testClass = testProject.TestClasses["Project.Tests.MyTestFixture"];
  177. testProject.TestClasses.Remove(testClass);
  178. ExtTreeNode testClassNode = null;
  179. foreach (ExtTreeNode node in testsNamespaceNode.Nodes) {
  180. if (node.Text == "MyTestFixture") {
  181. testClassNode = node;
  182. break;
  183. }
  184. }
  185. Assert.IsNull(testClassNode);
  186. // Project namespace node should only have one child node.
  187. Assert.AreEqual(1, projectNamespaceNode.Nodes.Count);
  188. Assert.AreEqual("MyTestFixture", projectNamespaceNode.Nodes[0].Text);
  189. }
  190. [Test]
  191. public void RemoveClassFromNamespaceNodeWithNonClassNodeChildren()
  192. {
  193. ExtTreeNode projectNamespaceNode = ExpandProjectNamespaceNode();
  194. ExtTreeNode testsNamespaceNode = ExpandTestsNamespaceNode();
  195. // Add a dummy tree node to the tests namespace node to make
  196. // sure the TestNamespaceTreeNode handles non-TestClassTreeNode
  197. // children when removing a class.
  198. testsNamespaceNode.Nodes.Insert(0, new ExtTreeNode());
  199. // Locate the class we are going to remove.
  200. TestClass testClass = testProject.TestClasses["Project.Tests.MyTestFixture"];
  201. testProject.TestClasses.Remove(testClass);
  202. ExtTreeNode testClassNode = null;
  203. foreach (ExtTreeNode node in testsNamespaceNode.Nodes) {
  204. if (node.Text == "MyTestFixture") {
  205. testClassNode = node;
  206. break;
  207. }
  208. }
  209. Assert.IsNull(testClassNode);
  210. Assert.AreEqual(1, testsNamespaceNode.Nodes.Count);
  211. }
  212. ExtTreeNode ExpandProjectNamespaceNode()
  213. {
  214. ExpandRootNode();
  215. foreach (ExtTreeNode node in rootNode.Nodes) {
  216. if (node.Text == "Project") {
  217. node.Expanding();
  218. return node;
  219. }
  220. }
  221. return null;
  222. }
  223. ExtTreeNode ExpandTestsNamespaceNode()
  224. {
  225. ExtTreeNode projectNamespaceNode = ExpandProjectNamespaceNode();
  226. foreach (ExtTreeNode childNode in projectNamespaceNode.Nodes) {
  227. if (childNode.Text == "Tests") {
  228. childNode.Expanding();
  229. return childNode;
  230. }
  231. }
  232. return null;
  233. }
  234. }
  235. }