Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 19cf43b

Browse files
committed
Add tests for EnvDTE.CodeInterface.AddFunction()
1 parent f69ce55 commit 19cf43b

5 files changed

Lines changed: 146 additions & 16 deletions

File tree

src/AddIns/Misc/PackageManagement/Project/Src/EnvDTE/CodeInterface.cs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,7 @@
1818

1919
using System;
2020
using System.Linq;
21-
using System.Text;
2221
using ICSharpCode.NRefactory.TypeSystem;
23-
using ICSharpCode.NRefactory.TypeSystem.Implementation;
24-
using ICSharpCode.SharpDevelop.Dom;
2522

2623
namespace ICSharpCode.PackageManagement.EnvDTE
2724
{
@@ -54,13 +51,7 @@ public CodeInterface(CodeModelContext context, ITypeDefinition typeDefinition, p
5451
IType GetMethodReturnType(string typeName)
5552
{
5653
var fullTypeName = new FullTypeName(typeName);
57-
58-
IType type = typeDefinition.Compilation.FindType(fullTypeName);
59-
if (type != null) {
60-
return type;
61-
}
62-
63-
return new UnknownType(fullTypeName);
54+
return typeDefinition.Compilation.FindType(fullTypeName);
6455
}
6556

6657
void ReloadTypeDefinition()

src/AddIns/Misc/PackageManagement/Project/Src/PackageManagementViewModels.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ public class PackageManagementViewModels
2727
{
2828
ManagePackagesViewModel managePackagesViewModel;
2929
RegisteredPackageSourcesViewModel registeredPackageSourcesViewModel;
30-
RegisteredPackageSourcesViewModel registeredProjectTemplatePackageSourcesViewModel;
3130
PackageManagementOptionsViewModel packageManagementOptionsViewModel;
3231
PackageManagementConsoleViewModel packageManagementConsoleViewModel;
3332
IPackageManagementSolution solution;

src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeInterfaceTests.cs

Lines changed: 126 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,49 @@
2121
using ICSharpCode.NRefactory.TypeSystem;
2222
using ICSharpCode.PackageManagement.EnvDTE;
2323
using NUnit.Framework;
24+
using Rhino.Mocks;
2425

2526
namespace PackageManagement.Tests.EnvDTE
2627
{
2728
[TestFixture]
2829
public class CodeInterfaceTests : CodeModelTestBase
2930
{
3031
CodeInterface codeInterface;
32+
ITypeDefinition interfaceTypeDefinition;
3133

32-
void CreateInterface(string code)
34+
ITypeDefinition addMethodAtStartTypeDef;
35+
Accessibility addMethodAtStartAccess;
36+
IType addMethodAtStartReturnType;
37+
string addMethodAtStartName;
38+
39+
void UpdateCode(string code, string fileName = @"c:\projects\MyProject\interface.cs")
40+
{
41+
CreateCompilationForUpdatedCodeFile(fileName, code);
42+
}
43+
44+
void CreateInterface(string code, string fileName = @"c:\projects\MyProject\interface.cs")
45+
{
46+
CreateCodeModel();
47+
AddCodeFile(fileName, code);
48+
interfaceTypeDefinition = assemblyModel.TopLevelTypeDefinitions.First().Resolve();
49+
codeInterface = new CodeInterface(codeModelContext, interfaceTypeDefinition);
50+
}
51+
52+
void CaptureCodeGeneratorAddMethodAtStartParameters()
3353
{
34-
AddCodeFile("interface.cs", code);
35-
ITypeDefinition typeDefinition = assemblyModel.TopLevelTypeDefinitions.First().Resolve();
36-
codeInterface = new CodeInterface(codeModelContext, typeDefinition);
54+
codeGenerator
55+
.Stub(generator => generator.AddMethodAtStart(
56+
Arg<ITypeDefinition>.Is.Anything,
57+
Arg<Accessibility>.Is.Anything,
58+
Arg<IType>.Is.Anything,
59+
Arg<string>.Is.Anything))
60+
.Callback<ITypeDefinition, Accessibility, IType, string>((typeDef, access, returnType, name) => {
61+
addMethodAtStartTypeDef = typeDef;
62+
addMethodAtStartAccess = access;
63+
addMethodAtStartReturnType = returnType;
64+
addMethodAtStartName = name;
65+
return true;
66+
});
3767
}
3868

3969
[Test]
@@ -45,5 +75,97 @@ public void Kind_Interface_ReturnsInterface()
4575

4676
Assert.AreEqual(global::EnvDTE.vsCMElement.vsCMElementInterface, kind);
4777
}
78+
79+
[Test]
80+
public void AddFunction_PublicFunctionReturningSystemInt32_AddsPublicFunctionWithCodeConverter()
81+
{
82+
CreateInterface ("interface MyInterface {}");
83+
var kind = global::EnvDTE.vsCMFunction.vsCMFunctionFunction;
84+
var access = global::EnvDTE.vsCMAccess.vsCMAccessPublic;
85+
CaptureCodeGeneratorAddMethodAtStartParameters();
86+
string newCode =
87+
"interface MyInterface {\r\n" +
88+
" System.Int32 MyMethod();\r\n" +
89+
"}";
90+
UpdateCode(newCode);
91+
92+
codeInterface.AddFunction("MyMethod", kind, "System.Int32", null, access);
93+
94+
Assert.AreEqual(Accessibility.Public, addMethodAtStartAccess);
95+
Assert.AreEqual("MyMethod", addMethodAtStartName);
96+
Assert.AreEqual(interfaceTypeDefinition, addMethodAtStartTypeDef);
97+
Assert.AreEqual("System.Int32", addMethodAtStartReturnType.FullName);
98+
Assert.IsTrue(addMethodAtStartReturnType.IsKnownType (KnownTypeCode.Int32));
99+
}
100+
101+
[Test]
102+
public void AddFunction_PrivateFunctionReturningUnknownType_AddsPrivateFunctionWithCodeConverter()
103+
{
104+
CreateInterface ("interface MyInterface {}");
105+
var kind = global::EnvDTE.vsCMFunction.vsCMFunctionFunction;
106+
var access = global::EnvDTE.vsCMAccess.vsCMAccessPrivate;
107+
CaptureCodeGeneratorAddMethodAtStartParameters();
108+
string newCode =
109+
"interface MyInterface {\r\n" +
110+
" Unknown.MyUnknownType MyMethod();\r\n" +
111+
"}";
112+
UpdateCode(newCode);
113+
114+
codeInterface.AddFunction("MyMethod", kind, "Unknown.MyUnknownType", null, access);
115+
116+
Assert.AreEqual(Accessibility.Private, addMethodAtStartAccess);
117+
Assert.AreEqual("MyMethod", addMethodAtStartName);
118+
Assert.AreEqual(interfaceTypeDefinition, addMethodAtStartTypeDef);
119+
Assert.AreEqual("Unknown.MyUnknownType", addMethodAtStartReturnType.FullName);
120+
}
121+
122+
[Test]
123+
public void AddFunction_PublicFunctionReturningSystemInt32_ReturnsCodeFunctionForNewMethod()
124+
{
125+
string fileName = @"c:\projects\MyProject\interface.cs";
126+
CreateInterface("interface MyInterface {}", fileName);
127+
var kind = global::EnvDTE.vsCMFunction.vsCMFunctionFunction;
128+
var access = global::EnvDTE.vsCMAccess.vsCMAccessPublic;
129+
string newCode =
130+
"interface MyInterface {\r\n" +
131+
" int MyMethod();\r\n" +
132+
"}";
133+
UpdateCode(newCode, fileName);
134+
135+
var codefunction = codeInterface.AddFunction("MyMethod", kind, "System.Int32", null, access) as CodeFunction;
136+
137+
Assert.AreEqual("MyMethod", codefunction.Name);
138+
}
139+
140+
[Test]
141+
public void AddFunction_MethodNotFoundAfterReloadingTypeDefinition_ReturnsNull()
142+
{
143+
string fileName = @"c:\projects\MyProject\interface.cs";
144+
CreateInterface("interface MyInterface {}", fileName);
145+
var kind = global::EnvDTE.vsCMFunction.vsCMFunctionFunction;
146+
var access = global::EnvDTE.vsCMAccess.vsCMAccessPublic;
147+
string newCode = "interface MyInterface {}";
148+
UpdateCode(newCode, fileName);
149+
150+
var codefunction = codeInterface.AddFunction("MyMethod", kind, "System.Int32", null, access) as CodeFunction;
151+
152+
Assert.IsNull(codefunction);
153+
}
154+
155+
[Test]
156+
public void AddFunction_UnableToFindTypeDefinitionAfterUpdate_ReturnsNull()
157+
{
158+
string fileName = @"c:\projects\MyProject\interface.cs";
159+
CreateInterface("interface MyInterface {}", fileName);
160+
var kind = global::EnvDTE.vsCMFunction.vsCMFunctionFunction;
161+
var access = global::EnvDTE.vsCMAccess.vsCMAccessPublic;
162+
string newCode = "interface SomeOtherInterface {}";
163+
UpdateCode(newCode, fileName);
164+
165+
var codefunction = codeInterface.AddFunction("MyMethod", kind, "System.Int32", null, access) as CodeFunction;
166+
167+
Assert.IsNull(codefunction);
168+
Assert.AreEqual("MyInterface", codeInterface.Name);
169+
}
48170
}
49171
}

src/AddIns/Misc/PackageManagement/Test/Src/EnvDTE/CodeModelTestBase.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
using ICSharpCode.PackageManagement.EnvDTE;
2424
using ICSharpCode.SharpDevelop;
2525
using ICSharpCode.SharpDevelop.Dom;
26+
using ICSharpCode.SharpDevelop.Project;
2627
using ICSharpCode.SharpDevelop.Refactoring;
2728
using PackageManagement.Tests.Helpers;
2829
using Rhino.Mocks;
@@ -59,7 +60,8 @@ public override void SetUp()
5960

6061
protected void CreateCodeModel()
6162
{
62-
msbuildProject = ProjectHelper.CreateTestProject();
63+
ISolution solution = ProjectHelper.CreateSolutionWithoutInitializingServicesForUnitTests();
64+
msbuildProject = ProjectHelper.CreateTestProject(solution, "MyProject");
6365

6466
projectService = MockRepository.GenerateStub<IPackageManagementProjectService>();
6567
fileService = MockRepository.GenerateStub<IPackageManagementFileService>();
@@ -84,5 +86,16 @@ ICompilation CreateCompilation()
8486
solutionSnapshot.AddCompilation(projectContent, compilation);
8587
return compilation;
8688
}
89+
90+
protected void CreateCompilationForUpdatedCodeFile(string fileName, string code)
91+
{
92+
fileService
93+
.Stub(fs => fs.GetCompilationUnit(fileName))
94+
.WhenCalled(compilation => {
95+
UpdateCodeFile(fileName, code);
96+
compilation.ReturnValue = CreateCompilation();
97+
})
98+
.Return(null); // HACK: Not returning null here but Rhino Mocks fails to work otherwise.
99+
}
87100
}
88101
}

src/AddIns/Misc/PackageManagement/Test/Src/Helpers/ProjectHelper.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public static class ProjectHelper
3131
public static ISolution CreateSolution()
3232
{
3333
SD.InitializeForUnitTests();
34+
return CreateSolutionWithoutInitializingServicesForUnitTests();
35+
}
36+
37+
public static ISolution CreateSolutionWithoutInitializingServicesForUnitTests()
38+
{
3439
ISolution solution = MockRepository.GenerateStub<ISolution>();
3540
solution.Stub(s => s.MSBuildProjectCollection).Return(new Microsoft.Build.Evaluation.ProjectCollection());
3641
solution.Stub(s => s.Projects).Return(new NullSafeSimpleModelCollection<IProject>());

0 commit comments

Comments
 (0)