7 changed files with 314 additions and 169 deletions
@ -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(); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -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); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -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); |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue