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