From 82c8dd1a3faede7d4d1d55d61caa9f25146ed4a4 Mon Sep 17 00:00:00 2001 From: Matt Ward Date: Sun, 24 Jun 2007 12:08:16 +0000 Subject: [PATCH] Fixed SD2-1359. Incorrect test fixture tested if the base class has unit test methods when running an individual test from the Unit Test tree. git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/2.1@2580 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../Misc/UnitTesting/Src/BaseTestMethod.cs | 46 ++++++++++ src/AddIns/Misc/UnitTesting/Src/TestClass.cs | 3 +- .../Misc/UnitTesting/Src/UnitTestCommands.cs | 4 + .../Test/Project/BaseTestMethodTestFixture.cs | 91 +++++++++++++++++++ .../TestMethodsInBaseClassTestFixture.cs | 16 +++- .../UnitTesting/Test/UnitTesting.Tests.csproj | 1 + .../Misc/UnitTesting/Test/Utils/MockMethod.cs | 15 ++- .../Misc/UnitTesting/UnitTesting.csproj | 1 + 8 files changed, 171 insertions(+), 6 deletions(-) create mode 100644 src/AddIns/Misc/UnitTesting/Src/BaseTestMethod.cs create mode 100644 src/AddIns/Misc/UnitTesting/Test/Project/BaseTestMethodTestFixture.cs diff --git a/src/AddIns/Misc/UnitTesting/Src/BaseTestMethod.cs b/src/AddIns/Misc/UnitTesting/Src/BaseTestMethod.cs new file mode 100644 index 0000000000..0d402f7f3e --- /dev/null +++ b/src/AddIns/Misc/UnitTesting/Src/BaseTestMethod.cs @@ -0,0 +1,46 @@ +// +// +// +// +// $Revision$ +// + +using System; +using ICSharpCode.SharpDevelop.Dom; + +namespace ICSharpCode.UnitTesting +{ + /// + /// Represents a test method that exists in a base class. + /// + /// + /// In order to have the Unit Test tree run the correct + /// test when we have a class that has a base class with + /// test methods is to return the derived class from the + /// DeclaringType's property. Otherwise the base class + /// method is tested and the derived class is not used. + /// + public class BaseTestMethod : DefaultMethod + { + IMethod method; + + /// + /// Creates a new instance of the BaseTestMethod. + /// + /// The derived class and not + /// the class where the method is actually defined. + /// The base class's test method. + public BaseTestMethod(IClass derivedClass, IMethod method) + : base(method.Name, method.ReturnType, method.Modifiers, method.Region, method.BodyRegion, derivedClass) + { + this.method = method; + } + + /// + /// Gets the actual method used to create this object. + /// + public IMethod Method { + get { return method; } + } + } +} diff --git a/src/AddIns/Misc/UnitTesting/Src/TestClass.cs b/src/AddIns/Misc/UnitTesting/Src/TestClass.cs index 74913b0c48..95a27a31d2 100644 --- a/src/AddIns/Misc/UnitTesting/Src/TestClass.cs +++ b/src/AddIns/Misc/UnitTesting/Src/TestClass.cs @@ -328,7 +328,8 @@ namespace ICSharpCode.UnitTesting if (c.BaseClass != null) { foreach (IMethod method in c.BaseClass.Methods) { if (TestMethod.IsTestMethod(method)) { - TestMethod testMethod = new TestMethod(c.BaseClass.Name, method); + BaseTestMethod baseTestMethod = new BaseTestMethod(c, method); + TestMethod testMethod = new TestMethod(c.BaseClass.Name, baseTestMethod); if (!testMethods.Contains(testMethod.Name)) { testMethods.Add(testMethod); } diff --git a/src/AddIns/Misc/UnitTesting/Src/UnitTestCommands.cs b/src/AddIns/Misc/UnitTesting/Src/UnitTestCommands.cs index 675c87e961..11750e9b2f 100644 --- a/src/AddIns/Misc/UnitTesting/Src/UnitTestCommands.cs +++ b/src/AddIns/Misc/UnitTesting/Src/UnitTestCommands.cs @@ -45,6 +45,10 @@ namespace ICSharpCode.UnitTesting IMember member = treeView.SelectedMethod; IClass c = treeView.SelectedClass; if (member != null) { + BaseTestMethod baseTestMethod = member as BaseTestMethod; + if (baseTestMethod != null) { + member = baseTestMethod.Method; + } GotoMember(member); } else if (c != null) { GotoClass(c); diff --git a/src/AddIns/Misc/UnitTesting/Test/Project/BaseTestMethodTestFixture.cs b/src/AddIns/Misc/UnitTesting/Test/Project/BaseTestMethodTestFixture.cs new file mode 100644 index 0000000000..5afa3d3be6 --- /dev/null +++ b/src/AddIns/Misc/UnitTesting/Test/Project/BaseTestMethodTestFixture.cs @@ -0,0 +1,91 @@ +// +// +// +// +// $Revision$ +// + +using System; +using ICSharpCode.SharpDevelop.Dom; +using ICSharpCode.UnitTesting; +using NUnit.Framework; +using UnitTesting.Tests.Utils; + +namespace UnitTesting.Tests.Project +{ + /// + /// Tests that the BaseTestMethod populates the various + /// properties of the DefaultMethod class in its constructor. + /// + [TestFixture] + public class BaseTestMethodTestFixture + { + MockClass mockClass; + MockMethod mockMethod; + BaseTestMethod baseTestMethod; + DomRegion mockMethodRegion; + DomRegion mockMethodBodyRegion; + DefaultReturnType returnType; + + [TestFixtureSetUp] + public void SetUpFixture() + { + mockClass = new MockClass("Tests.MyTestFixture"); + mockMethod = new MockMethod("MyMethod"); + + mockMethodRegion = new DomRegion(0, 0, 0, 10); + mockMethod.Region = mockMethodRegion; + mockMethodBodyRegion = new DomRegion(1, 0, 2, 5); + mockMethod.BodyRegion = mockMethodBodyRegion; + mockMethod.Modifiers = ModifierEnum.Public; + + MockClass returnTypeClass = new MockClass("Tests.ReturnType"); + returnType = new DefaultReturnType(returnTypeClass); + mockMethod.ReturnType = returnType; + + baseTestMethod = new BaseTestMethod(mockClass, mockMethod); + } + + [Test] + public void MethodName() + { + Assert.AreEqual("MyMethod", baseTestMethod.Name); + } + + [Test] + public void DeclaringType() + { + Assert.AreEqual(mockClass, baseTestMethod.DeclaringType); + } + + [Test] + public void ActualMethod() + { + Assert.AreEqual(mockMethod, baseTestMethod.Method); + } + + [Test] + public void MethodRegion() + { + Assert.AreEqual(mockMethodRegion, baseTestMethod.Region); + } + + [Test] + public void MethodBodyRegion() + { + Assert.AreEqual(mockMethodBodyRegion, baseTestMethod.BodyRegion); + } + + [Test] + public void Modifiers() + { + Assert.AreEqual(ModifierEnum.Public, baseTestMethod.Modifiers); + } + + [Test] + public void ReturnType() + { + Assert.IsTrue(Object.ReferenceEquals(returnType, baseTestMethod.ReturnType)); + } + } +} diff --git a/src/AddIns/Misc/UnitTesting/Test/Project/TestMethodsInBaseClassTestFixture.cs b/src/AddIns/Misc/UnitTesting/Test/Project/TestMethodsInBaseClassTestFixture.cs index 7633224b0d..28fbe985e3 100644 --- a/src/AddIns/Misc/UnitTesting/Test/Project/TestMethodsInBaseClassTestFixture.cs +++ b/src/AddIns/Misc/UnitTesting/Test/Project/TestMethodsInBaseClassTestFixture.cs @@ -38,6 +38,7 @@ namespace UnitTesting.Tests.Project public class TestMethodsInBaseClassTestFixture { TestClass testClass; + MockClass c; [SetUp] public void SetUp() @@ -55,7 +56,7 @@ namespace UnitTesting.Tests.Project baseClass.Methods.Add(baseMethod); // Create the derived test class. - MockClass c = new MockClass("RootNamespace.MyTestFixture"); + c = new MockClass("RootNamespace.MyTestFixture"); c.Attributes.Add(new MockAttribute("TestFixture")); c.ProjectContent = projectContent; MockMethod method = new MockMethod("DerivedMethod"); @@ -89,6 +90,19 @@ namespace UnitTesting.Tests.Project Assert.IsTrue(testClass.TestMethods.Contains("TestFixtureBase.BaseMethod")); } + /// + /// The TestMethod.Method property should return an IMethod + /// that returns the derived class from the DeclaringType property + /// and not the base class. This ensures that the correct + /// test is run when selected in the unit test tree. + /// + [Test] + public void BaseMethodDeclaringTypeIsDerivedClass() + { + TestMethod method = testClass.TestMethods["TestFixtureBase.BaseMethod"]; + Assert.AreEqual(c, method.Method.DeclaringType); + } + [Test] public void UpdateTestResultUsingPrefixBaseClassName() { diff --git a/src/AddIns/Misc/UnitTesting/Test/UnitTesting.Tests.csproj b/src/AddIns/Misc/UnitTesting/Test/UnitTesting.Tests.csproj index 90db24a44d..67ca8cf534 100644 --- a/src/AddIns/Misc/UnitTesting/Test/UnitTesting.Tests.csproj +++ b/src/AddIns/Misc/UnitTesting/Test/UnitTesting.Tests.csproj @@ -60,6 +60,7 @@ + diff --git a/src/AddIns/Misc/UnitTesting/Test/Utils/MockMethod.cs b/src/AddIns/Misc/UnitTesting/Test/Utils/MockMethod.cs index 69981326be..76edbef09e 100644 --- a/src/AddIns/Misc/UnitTesting/Test/Utils/MockMethod.cs +++ b/src/AddIns/Misc/UnitTesting/Test/Utils/MockMethod.cs @@ -14,10 +14,13 @@ namespace UnitTesting.Tests.Utils public class MockMethod : IMethod { IClass declaringType; + ModifierEnum modifiers; DomRegion region = DomRegion.Empty; + DomRegion bodyRegion = DomRegion.Empty; IList attributes = new List(); string name = String.Empty; IList parameters = new List(); + IReturnType returnType; public MockMethod() : this(String.Empty) { @@ -42,7 +45,10 @@ namespace UnitTesting.Tests.Utils public DomRegion BodyRegion { get { - throw new NotImplementedException(); + return bodyRegion; + } + set { + bodyRegion = value; } } @@ -96,10 +102,10 @@ namespace UnitTesting.Tests.Utils public IReturnType ReturnType { get { - return null; + return returnType; } set { - throw new NotImplementedException(); + returnType = value; } } @@ -114,9 +120,10 @@ namespace UnitTesting.Tests.Utils public ModifierEnum Modifiers { get { - return ModifierEnum.None; + return modifiers; } set { + modifiers = value; } } diff --git a/src/AddIns/Misc/UnitTesting/UnitTesting.csproj b/src/AddIns/Misc/UnitTesting/UnitTesting.csproj index d6e5dd6d95..8186242baa 100644 --- a/src/AddIns/Misc/UnitTesting/UnitTesting.csproj +++ b/src/AddIns/Misc/UnitTesting/UnitTesting.csproj @@ -58,6 +58,7 @@ Always +