Browse Source

Fix tests for MSTest sample.

pull/297/head
Matt Ward 12 years ago
parent
commit
c178ac1e82
  1. 6
      samples/MSTest/MSTest.SharpDevelop.Tests/MSTest.SharpDevelop.Tests.csproj
  2. 61
      samples/MSTest/MSTest.SharpDevelop.Tests/MSTestBaseTests.cs
  3. 77
      samples/MSTest/MSTest.SharpDevelop.Tests/MSTestClassTests.cs
  4. 145
      samples/MSTest/MSTest.SharpDevelop.Tests/MSTestFrameworkTests.cs
  5. 141
      samples/MSTest/MSTest.SharpDevelop.Tests/MSTestProjectTests.cs
  6. 28
      samples/MSTest/MSTest.SharpDevelop/MSTestClass.cs
  7. 25
      samples/MSTest/MSTest.SharpDevelop/MSTestProject.cs

6
samples/MSTest/MSTest.SharpDevelop.Tests/MSTest.SharpDevelop.Tests.csproj

@ -37,6 +37,9 @@ @@ -37,6 +37,9 @@
<Reference Include="ICSharpCode.NRefactory">
<HintPath>..\..\..\bin\ICSharpCode.NRefactory.dll</HintPath>
</Reference>
<Reference Include="ICSharpCode.NRefactory.CSharp">
<HintPath>..\..\..\bin\ICSharpCode.NRefactory.CSharp.dll</HintPath>
</Reference>
<Reference Include="ICSharpCode.SharpDevelop">
<HintPath>..\..\..\bin\ICSharpCode.SharpDevelop.dll</HintPath>
</Reference>
@ -56,7 +59,10 @@ @@ -56,7 +59,10 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="MSTestBaseTests.cs" />
<Compile Include="MSTestFrameworkTests.cs" />
<Compile Include="MSTestClassTests.cs" />
<Compile Include="MSTestProjectTests.cs" />
<Compile Include="MSTestResultsTests.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>

61
samples/MSTest/MSTest.SharpDevelop.Tests/MSTestBaseTests.cs

@ -0,0 +1,61 @@ @@ -0,0 +1,61 @@
// 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 System.Linq;
using ICSharpCode.NRefactory.CSharp;
using ICSharpCode.NRefactory.TypeSystem;
using ICSharpCode.SharpDevelop;
using ICSharpCode.SharpDevelop.Parser;
using ICSharpCode.SharpDevelop.Project;
using NUnit.Framework;
using Rhino.Mocks;
namespace MSTest.SharpDevelop.Tests
{
public class MSTestBaseTests
{
protected IProject project;
protected IProjectContent projectContent;
[SetUp]
public void SetUp()
{
SD.InitializeForUnitTests();
SD.Services.AddService(typeof(IParserService), MockRepository.GenerateStrictMock<IParserService>());
project = MockRepository.GenerateStrictMock<IProject>();
projectContent = new CSharpProjectContent();
SD.ParserService
.Stub(p => p.GetCompilation(project))
.WhenCalled(c => c.ReturnValue = projectContent.CreateCompilation());
}
[TearDown]
public void TearDown()
{
SD.TearDownForUnitTests();
}
public void AddCodeFile(string fileName, string code)
{
IUnresolvedFile oldFile = projectContent.GetFile(fileName);
IUnresolvedFile newFile = Parse(fileName, code);
projectContent = projectContent.AddOrUpdateFiles(newFile);
}
IUnresolvedFile Parse(string fileName, string code)
{
var parser = new CSharpParser();
SyntaxTree syntaxTree = parser.Parse(code, fileName);
Assert.IsFalse(parser.HasErrors);
return syntaxTree.ToTypeSystem();
}
protected ITypeDefinition GetFirstTypeDefinition()
{
ICompilation compilation = projectContent.CreateCompilation();
return compilation.MainAssembly.TopLevelTypeDefinitions.First();
}
}
}

77
samples/MSTest/MSTest.SharpDevelop.Tests/MSTestClassTests.cs

@ -0,0 +1,77 @@ @@ -0,0 +1,77 @@
// 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 System.Linq;
using ICSharpCode.MSTest;
using NUnit.Framework;
namespace MSTest.SharpDevelop.Tests
{
[TestFixture]
public class MSTestClassTests : MSTestBaseTests
{
bool IsTestClass()
{
return MSTestClass.IsTestClass(GetFirstTypeDefinition());
}
[Test]
public void IsTestClass_ClassHasNoAttributes_ReturnsFalse()
{
AddCodeFile("myclass.cs", "class MyTest {}");
bool result = IsTestClass();
Assert.IsFalse(result);
}
[Test]
public void IsTestClass_ClassHasTestFixtureAttributeMissingAttributePart_ReturnsTrue()
{
AddCodeFile("myclass.cs", "[TestClass]class MyTest {}");
bool result = IsTestClass();
Assert.IsTrue(result);
}
[Test]
public void IsTestClass_ClassHasTestClassAttributeAndIsAbstract_ReturnsFalse()
{
AddCodeFile("myclass.cs", "[TestClass] abstract class MyTest {}");
bool result = IsTestClass();
Assert.IsFalse(result);
}
[Test]
public void IsTestClass_ClassHasTestClassAttributeIncludingAttributePart_ReturnsTrue()
{
AddCodeFile("myclass.cs", "[TestClassAttribute]class MyTest {}");
bool result = IsTestClass();
Assert.IsTrue(result);
}
[Test]
public void IsTestClass_ClassHasFullyQualifiedMSTestClassAttribute_ReturnsTrue()
{
AddCodeFile("myclass.cs", "[Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute]class MyTest {}");
bool result = IsTestClass();
Assert.IsTrue(result);
}
[Test]
public void IsTestClass_ClassIsNull_ReturnsFalse()
{
bool result = MSTestClass.IsTestClass(null);
Assert.IsFalse(result);
}
}
}

145
samples/MSTest/MSTest.SharpDevelop.Tests/MSTestFrameworkTests.cs

@ -189,150 +189,5 @@ namespace MSTest.SharpDevelop.Tests @@ -189,150 +189,5 @@ namespace MSTest.SharpDevelop.Tests
Assert.IsTrue(result);
}
//
// [Test]
// public void IsTestClass_ClassHasNoAttributes_ReturnsFalse()
// {
// IClass fakeClass = CreateClassWithoutAnyAttributes();
//
// bool result = testFramework.IsTestClass(fakeClass);
//
// Assert.IsFalse(result);
// }
//
// [Test]
// public void IsTestClass_ClassHasTestFixtureAttributeMissingAttributePart_ReturnsTrue()
// {
// IClass fakeClass = CreateClassWithAttributes("TestClass");
//
// bool result = testFramework.IsTestClass(fakeClass);
//
// Assert.IsTrue(result);
// }
//
// [Test]
// public void IsTestClass_ClassHasTestClassAttributeAndIsAbstract_ReturnsFalse()
// {
// IClass fakeClass = CreateClassWithAttributes("TestClass");
// MakeClassAbstract(fakeClass);
//
// bool result = testFramework.IsTestClass(fakeClass);
//
// Assert.IsFalse(result);
// }
//
// [Test]
// public void IsTestClass_ClassHasTestClassAttributeIncludingAttributePart_ReturnsTrue()
// {
// IClass fakeClass = CreateClassWithAttributes("TestClassAttribute");
//
// bool result = testFramework.IsTestClass(fakeClass);
//
// Assert.IsTrue(result);
// }
//
// [Test]
// public void IsTestClass_ClassHasFullyQualifiedMSTestClassAttribute_ReturnsTrue()
// {
// IClass fakeClass = CreateClassWithAttributes("Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute");
//
// bool result = testFramework.IsTestClass(fakeClass);
//
// Assert.IsTrue(result);
// }
//
// [Test]
// public void IsTestClass_ClassIsNull_ReturnsFalse()
// {
// bool result = testFramework.IsTestClass(null);
//
// Assert.IsFalse(result);
// }
//
// [Test]
// public void IsTestMember_MethodHasNoAttributes_ReturnsFalse()
// {
// IMethod method = CreateMethodWithoutAnyAttributes();
//
// bool result = testFramework.IsTestMember(method);
//
// Assert.IsFalse(result);
// }
//
// [Test]
// public void IsTestMember_MethodHasTestMethodAttributeWithoutAttributePart_ReturnsTrue()
// {
// IMethod method = CreateMethodWithAttributes("TestMethod");
//
// bool result = testFramework.IsTestMember(method);
//
// Assert.IsTrue(result);
// }
//
// [Test]
// public void IsTestMember_MethodHasTestMethodAttributeAttribute_ReturnsTrue()
// {
// IMethod method = CreateMethodWithAttributes("TestMethodAttribute");
//
// bool result = testFramework.IsTestMember(method);
//
// Assert.IsTrue(result);
// }
//
// [Test]
// public void IsTestMember_MethodHasFullyQualifiedMSTestTestMethodAttribute_ReturnsTrue()
// {
// IMethod method = CreateMethodWithAttributes("Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute");
//
// bool result = testFramework.IsTestMember(method);
//
// Assert.IsTrue(result);
// }
//
// [Test]
// public void IsTestMember_MethodIsNull_ReturnsFalse()
// {
// bool result = testFramework.IsTestMember(null);
//
// Assert.IsFalse(result);
// }
//
// [Test]
// public void IsTestMember_MemberNotMethod_ReturnsFalse()
// {
// IMember member = MockRepository.GenerateStub<IMember>();
//
// bool result = testFramework.IsTestMember(member);
//
// Assert.IsFalse(result);
// }
//
// [Test]
// public void GetTestMembersFor_ClassHasNoMethods_ReturnsFalse()
// {
// IClass fakeClass = CreateClassWithAttributes("TestClass");
// AddMethodsToClass(fakeClass, new List<IMethod>());
//
// List<TestMember> testMembers = GetTestMembersFor(fakeClass);
//
// Assert.AreEqual(0, testMembers.Count);
// }
//
// [Test]
// public void GetTestMembersFor_ClassHasTwoMethodsAndSecondOneIsTestMethod_ReturnsSecondTestMethodOnly()
// {
// IClass fakeClass = CreateClassWithAttributes("TestClass");
//
// var methods = new List<IMethod>();
// methods.Add(CreateMethodWithoutAnyAttributes());
// IMethod testMethod = CreateMethodWithAttributes("TestMethod");
// methods.Add(testMethod);
// AddMethodsToClass(fakeClass, methods);
//
// List<TestMember> testMembers = GetTestMembersFor(fakeClass);
//
// Assert.AreEqual(1, testMembers.Count);
// Assert.AreEqual(testMethod, testMembers[0].Member);
// }
}
}

141
samples/MSTest/MSTest.SharpDevelop.Tests/MSTestProjectTests.cs

@ -0,0 +1,141 @@ @@ -0,0 +1,141 @@
// 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 System.Collections.Generic;
using System.Linq;
using ICSharpCode.MSTest;
using NUnit.Framework;
namespace MSTest.SharpDevelop.Tests
{
[TestFixture]
public class MSTestProjectTests : MSTestBaseTests
{
MSTestProject testProject;
void CreateTestProject()
{
testProject = new MSTestProject(project);
}
List<MSTestMember> GetTestMembersFor()
{
return testProject.GetTestMembersFor(GetFirstTypeDefinition()).ToList();
}
[Test]
public void GetTestMembersFor_OneMethodHasNoAttributes_ReturnsOneMember()
{
string code =
"[TestClass]" +
"class MyTest {" +
" public void MyMethod() {}" +
"}";
AddCodeFile("myclass.cs", code);
CreateTestProject();
List<MSTestMember> members = GetTestMembersFor();
Assert.AreEqual(0, members.Count);
}
[Test]
public void GetTestMembersFor_MethodHasTestMethodAttributeWithoutAttributePart_ReturnsOneMember()
{
string code =
"[TestClass]" +
"class MyTest {" +
" [TestMethod]" +
" public void MyMethod() {}" +
"}";
AddCodeFile("myclass.cs", code);
CreateTestProject();
List<MSTestMember> members = GetTestMembersFor();
Assert.AreEqual(1, members.Count);
Assert.AreEqual("MyMethod", members[0].DisplayName);
}
[Test]
public void GetTestMembersFor_MethodHasTestMethodAttributeAttribute_ReturnsOneMember()
{
string code =
"[TestClass]" +
"class MyTest {" +
" [TestMethodAttribute]" +
" public void MyMethod() {}" +
"}";
AddCodeFile("myclass.cs", code);
CreateTestProject();
List<MSTestMember> members = GetTestMembersFor();
Assert.AreEqual("MyMethod", members[0].DisplayName);
}
[Test]
public void GetTestMembersFor_MethodHasFullyQualifiedMSTestTestMethodAttribute_ReturnsOneMember()
{
string code =
"[TestClass]" +
"class MyTest {" +
" [Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute]" +
" public void MyMethod() {}" +
"}";
AddCodeFile("myclass.cs", code);
CreateTestProject();
List<MSTestMember> members = GetTestMembersFor();
Assert.AreEqual("MyMethod", members[0].DisplayName);
}
[Test]
public void GetTestMembersFor_MemberNotMethod_ReturnsNoItems()
{
string code =
"[TestClass]" +
"class MyTest {" +
" public int MyProperty { get; set; }" +
"}";
AddCodeFile("myclass.cs", code);
CreateTestProject();
List<MSTestMember> members = GetTestMembersFor();
Assert.AreEqual(0, members.Count);
}
[Test]
public void GetTestMembersFor_ClassHasNoMethods_ReturnsNoItems()
{
AddCodeFile("myclass.cs", "[TestClass]class MyTest {}");
CreateTestProject();
List<MSTestMember> testMembers = GetTestMembersFor();
Assert.AreEqual(0, testMembers.Count);
}
[Test]
public void GetTestMembersFor_ClassHasTwoMethodsAndSecondOneIsTestMethod_ReturnsSecondTestMethodOnly()
{
string code =
"[TestClass]" +
"class MyTest {" +
" public void MyFirstMethod() {}" +
"" +
" [TestMethod]" +
" public void MySecondMethod() {}" +
"}";
AddCodeFile("myclass.cs", code);
CreateTestProject();
List<MSTestMember> members = GetTestMembersFor();
Assert.AreEqual("MySecondMethod", members[0].DisplayName);
}
}
}

28
samples/MSTest/MSTest.SharpDevelop/MSTestClass.cs

@ -24,6 +24,34 @@ namespace ICSharpCode.MSTest @@ -24,6 +24,34 @@ namespace ICSharpCode.MSTest
BindResultToCompositeResultOfNestedTests();
}
public static bool IsTestClass(ITypeDefinition typeDefinition)
{
if ((typeDefinition == null) || (typeDefinition.IsAbstract)) {
return false;
}
foreach (IAttribute attribute in typeDefinition.Attributes) {
if (IsMSTestClassAttribute(attribute)) {
return true;
}
}
return false;
}
static bool IsMSTestClassAttribute(IAttribute attribute)
{
return IsMSTestClassAttribute(attribute.AttributeType.FullName);
}
static bool IsMSTestClassAttribute(string name)
{
return
name == "TestClass" ||
name == "TestClassAttribute" ||
name == "Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute";
}
public string GetTypeName()
{
return fullTypeName.ReflectionName;

25
samples/MSTest/MSTest.SharpDevelop/MSTestProject.cs

@ -27,30 +27,7 @@ namespace ICSharpCode.MSTest @@ -27,30 +27,7 @@ namespace ICSharpCode.MSTest
protected override bool IsTestClass(ITypeDefinition typeDefinition)
{
if ((typeDefinition == null) || (typeDefinition.IsAbstract)) {
return false;
}
foreach (IAttribute attribute in typeDefinition.Attributes) {
if (IsMSTestClassAttribute(attribute)) {
return true;
}
}
return false;
}
bool IsMSTestClassAttribute(IAttribute attribute)
{
return IsMSTestClassAttribute(attribute.AttributeType.FullName);
}
bool IsMSTestClassAttribute(string name)
{
return
name == "TestClass" ||
name == "TestClassAttribute" ||
name == "Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute";
return MSTestClass.IsTestClass(typeDefinition);
}
protected override ITest CreateTestClass(ITypeDefinition typeDefinition)

Loading…
Cancel
Save