From d3acf59322f5be3d7f9d94b62833093c7b396ada Mon Sep 17 00:00:00 2001 From: Tomasz Tretkowski Date: Sun, 16 Oct 2011 20:43:22 +0200 Subject: [PATCH] Showing any classes nested in test fixtures as test classes (as NUnit Gui does). --- .../UnitTesting/Src/InnerClassEnumerator.cs | 47 +++++++++++ .../UnitTesting/Src/NUnitTestFramework.cs | 2 + .../Analysis/UnitTesting/Src/TestProject.cs | 2 +- .../Project/InnerClassInTestFixtureTests.cs | 81 +++++++++++++++++++ .../Test/Project/InnerClassTestFixtureBase.cs | 3 +- .../UnitTesting/Test/UnitTesting.Tests.csproj | 18 +++-- .../Analysis/UnitTesting/UnitTesting.csproj | 11 ++- 7 files changed, 150 insertions(+), 14 deletions(-) create mode 100644 src/AddIns/Analysis/UnitTesting/Src/InnerClassEnumerator.cs create mode 100644 src/AddIns/Analysis/UnitTesting/Test/Project/InnerClassInTestFixtureTests.cs diff --git a/src/AddIns/Analysis/UnitTesting/Src/InnerClassEnumerator.cs b/src/AddIns/Analysis/UnitTesting/Src/InnerClassEnumerator.cs new file mode 100644 index 0000000000..6442c3f479 --- /dev/null +++ b/src/AddIns/Analysis/UnitTesting/Src/InnerClassEnumerator.cs @@ -0,0 +1,47 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ICSharpCode.SharpDevelop.Dom; + +namespace ICSharpCode.UnitTesting +{ + public class InnerClassEnumerator : IEnumerable + { + readonly IClass _c; + + public InnerClassEnumerator(IClass c) + { + _c = c; + } + + #region IEnumerable Members + + public IEnumerator GetEnumerator() + { + var result = new List(); + Visit(_c, result); + return result.GetEnumerator(); + } + + #endregion + + #region IEnumerable Members + + System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() + { + return GetEnumerator(); + } + + #endregion + + private void Visit(IClass c, List resultList) + { + foreach (var innerClass in c.InnerClasses) + { + resultList.Add(innerClass); + Visit(innerClass, resultList); + } + } + } +} diff --git a/src/AddIns/Analysis/UnitTesting/Src/NUnitTestFramework.cs b/src/AddIns/Analysis/UnitTesting/Src/NUnitTestFramework.cs index f341063fb1..1e24067b66 100644 --- a/src/AddIns/Analysis/UnitTesting/Src/NUnitTestFramework.cs +++ b/src/AddIns/Analysis/UnitTesting/Src/NUnitTestFramework.cs @@ -50,6 +50,8 @@ namespace ICSharpCode.UnitTesting return true; } } + if (c.DeclaringType != null) + return IsTestClass(c.DeclaringType); } return false; } diff --git a/src/AddIns/Analysis/UnitTesting/Src/TestProject.cs b/src/AddIns/Analysis/UnitTesting/Src/TestProject.cs index 1b54609048..cf88ec28d9 100644 --- a/src/AddIns/Analysis/UnitTesting/Src/TestProject.cs +++ b/src/AddIns/Analysis/UnitTesting/Src/TestProject.cs @@ -215,7 +215,7 @@ namespace ICSharpCode.UnitTesting testClasses.Add(CreateTestClass(c)); } } - foreach (IClass innerClass in c.InnerClasses) { + foreach (IClass innerClass in new InnerClassEnumerator(c)) { if (IsTestClass(innerClass)) { if (!testClasses.Contains(innerClass.DotNetName)) { testClasses.Add(CreateTestClass(innerClass)); diff --git a/src/AddIns/Analysis/UnitTesting/Test/Project/InnerClassInTestFixtureTests.cs b/src/AddIns/Analysis/UnitTesting/Test/Project/InnerClassInTestFixtureTests.cs new file mode 100644 index 0000000000..5a3b2e621d --- /dev/null +++ b/src/AddIns/Analysis/UnitTesting/Test/Project/InnerClassInTestFixtureTests.cs @@ -0,0 +1,81 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.SharpDevelop.Project; +using ICSharpCode.UnitTesting; +using NUnit.Framework; +using UnitTesting.Tests.Utils; +using System.Linq; + +namespace UnitTesting.Tests.Project +{ + /// + /// Tests that a class nested inside test fixture is recognized, e.g. + /// + /// [TestFixture] + /// public class A + /// { + /// public class InnerATest + /// { + /// [Test] + /// public void FooBar() + /// { + /// } + /// } + /// } + /// + /// In this case the FooBar test is identified via: "A+InnerATest.FooBar". + /// + [TestFixture] + public class InnerClassInTestFixtureTests : InnerClassTestFixtureBase + { + MockClass classNestedInInnerClass; + + [SetUp] + public void Init() + { + base.InitBase(); + + //Add TestFixture attribute to outer class + outerClass.Attributes.Add(new MockAttribute("TestFixture")); + testProject = new TestProject(null, projectContent, testFrameworks); + + //Add inner class nested in test class + classNestedInInnerClass = new MockClass(projectContent, "MyTests.A.InnerATest.InnerTestLevel2", "MyTests.A+InnerATest+InnerTestLevel2", innerClass); + innerClass.InnerClasses.Add(classNestedInInnerClass); + } + + [Test] + public void OuterTestClassFound() + { + AssertTestResultContainsClass(outerClass); + } + + [Test] + public void InnerTestClassWithTestFixtureAttributeFound() + { + AssertTestResultContainsClass(innerClass); + } + + [Test] + public void InnerTestClassWithoutTestFixtureAttributeFound() + { + AssertTestResultContainsClass(nonTestInnerClass); + } + + [Test] + public void InnerClassInInnerClassFound() + { + AssertTestResultContainsClass(classNestedInInnerClass); + } + + void AssertTestResultContainsClass(IClass clazz) + { + var count = testProject.TestClasses.Count(c => c.Class == clazz); + if (count != 1) + throw new AssertionException(string.Format("Test result should contain class {0}.", clazz.FullyQualifiedName)); + } + } +} diff --git a/src/AddIns/Analysis/UnitTesting/Test/Project/InnerClassTestFixtureBase.cs b/src/AddIns/Analysis/UnitTesting/Test/Project/InnerClassTestFixtureBase.cs index d87a94f6a3..b053a8f423 100644 --- a/src/AddIns/Analysis/UnitTesting/Test/Project/InnerClassTestFixtureBase.cs +++ b/src/AddIns/Analysis/UnitTesting/Test/Project/InnerClassTestFixtureBase.cs @@ -16,6 +16,7 @@ namespace UnitTesting.Tests.Project { protected TestClass testClass; protected MockClass innerClass; + protected MockClass nonTestInnerClass; protected TestProject testProject; protected MockProjectContent projectContent; protected MockClass outerClass; @@ -41,7 +42,7 @@ namespace UnitTesting.Tests.Project outerClass.InnerClasses.Add(innerClass); // Add another inner class that is not a test class. - MockClass nonTestInnerClass = new MockClass(projectContent, "MyTests.A.InnerBClass", outerClass); + nonTestInnerClass = new MockClass(projectContent, "MyTests.A.InnerBClass", outerClass); outerClass.InnerClasses.Add(nonTestInnerClass); // Add another inner class with the same name as the InnerATest. diff --git a/src/AddIns/Analysis/UnitTesting/Test/UnitTesting.Tests.csproj b/src/AddIns/Analysis/UnitTesting/Test/UnitTesting.Tests.csproj index 0f235a3bf8..edeed04a23 100644 --- a/src/AddIns/Analysis/UnitTesting/Test/UnitTesting.Tests.csproj +++ b/src/AddIns/Analysis/UnitTesting/Test/UnitTesting.Tests.csproj @@ -84,6 +84,7 @@ + @@ -144,8 +145,12 @@ - - + + Component + + + UserControl + @@ -225,7 +230,9 @@ - + + Component + @@ -250,7 +257,6 @@ - {1F261725-6318-4434-A1B1-6C70CE4CD324} UnitTesting @@ -267,14 +273,10 @@ {924EE450-603D-49C1-A8E5-4AFAA31CE6F3} ICSharpCode.SharpDevelop.Dom - - {3A9AE6AA-BC07-4A2F-972C-581E3AE2F195} NRefactory - - \ No newline at end of file diff --git a/src/AddIns/Analysis/UnitTesting/UnitTesting.csproj b/src/AddIns/Analysis/UnitTesting/UnitTesting.csproj index 96abcafe1b..fec9546f74 100644 --- a/src/AddIns/Analysis/UnitTesting/UnitTesting.csproj +++ b/src/AddIns/Analysis/UnitTesting/UnitTesting.csproj @@ -60,6 +60,7 @@ + @@ -121,7 +122,9 @@ - + + Component + @@ -140,7 +143,9 @@ - + + UserControl + @@ -171,7 +176,6 @@ ICSharpCode.Core False - {857CA1A3-FC88-4BE0-AB6A-D1EE772AB288} ICSharpCode.Core.WinForms @@ -182,7 +186,6 @@ ICSharpCode.SharpDevelop.Dom False -