|
|
|
|
@ -21,6 +21,7 @@ using System.Linq;
@@ -21,6 +21,7 @@ using System.Linq;
|
|
|
|
|
using ICSharpCode.NRefactory.TypeSystem; |
|
|
|
|
using ICSharpCode.PackageManagement.EnvDTE; |
|
|
|
|
using NUnit.Framework; |
|
|
|
|
using Rhino.Mocks; |
|
|
|
|
|
|
|
|
|
namespace PackageManagement.Tests.EnvDTE |
|
|
|
|
{ |
|
|
|
|
@ -28,12 +29,41 @@ namespace PackageManagement.Tests.EnvDTE
@@ -28,12 +29,41 @@ namespace PackageManagement.Tests.EnvDTE
|
|
|
|
|
public class CodeInterfaceTests : CodeModelTestBase |
|
|
|
|
{ |
|
|
|
|
CodeInterface codeInterface; |
|
|
|
|
ITypeDefinition interfaceTypeDefinition; |
|
|
|
|
|
|
|
|
|
void CreateInterface(string code) |
|
|
|
|
ITypeDefinition addMethodAtStartTypeDef; |
|
|
|
|
Accessibility addMethodAtStartAccess; |
|
|
|
|
IType addMethodAtStartReturnType; |
|
|
|
|
string addMethodAtStartName; |
|
|
|
|
|
|
|
|
|
void UpdateCode(string code, string fileName = @"c:\projects\MyProject\interface.cs") |
|
|
|
|
{ |
|
|
|
|
CreateCompilationForUpdatedCodeFile(fileName, code); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CreateInterface(string code, string fileName = @"c:\projects\MyProject\interface.cs") |
|
|
|
|
{ |
|
|
|
|
CreateCodeModel(); |
|
|
|
|
AddCodeFile(fileName, code); |
|
|
|
|
interfaceTypeDefinition = assemblyModel.TopLevelTypeDefinitions.First().Resolve(); |
|
|
|
|
codeInterface = new CodeInterface(codeModelContext, interfaceTypeDefinition); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
void CaptureCodeGeneratorAddMethodAtStartParameters() |
|
|
|
|
{ |
|
|
|
|
AddCodeFile("interface.cs", code); |
|
|
|
|
ITypeDefinition typeDefinition = assemblyModel.TopLevelTypeDefinitions.First().Resolve(); |
|
|
|
|
codeInterface = new CodeInterface(codeModelContext, typeDefinition); |
|
|
|
|
codeGenerator |
|
|
|
|
.Stub(generator => generator.AddMethodAtStart( |
|
|
|
|
Arg<ITypeDefinition>.Is.Anything, |
|
|
|
|
Arg<Accessibility>.Is.Anything, |
|
|
|
|
Arg<IType>.Is.Anything, |
|
|
|
|
Arg<string>.Is.Anything)) |
|
|
|
|
.Callback<ITypeDefinition, Accessibility, IType, string>((typeDef, access, returnType, name) => { |
|
|
|
|
addMethodAtStartTypeDef = typeDef; |
|
|
|
|
addMethodAtStartAccess = access; |
|
|
|
|
addMethodAtStartReturnType = returnType; |
|
|
|
|
addMethodAtStartName = name; |
|
|
|
|
return true; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
@ -45,5 +75,97 @@ namespace PackageManagement.Tests.EnvDTE
@@ -45,5 +75,97 @@ namespace PackageManagement.Tests.EnvDTE
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(global::EnvDTE.vsCMElement.vsCMElementInterface, kind); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
public void AddFunction_PublicFunctionReturningSystemInt32_AddsPublicFunctionWithCodeConverter() |
|
|
|
|
{ |
|
|
|
|
CreateInterface ("interface MyInterface {}"); |
|
|
|
|
var kind = global::EnvDTE.vsCMFunction.vsCMFunctionFunction; |
|
|
|
|
var access = global::EnvDTE.vsCMAccess.vsCMAccessPublic; |
|
|
|
|
CaptureCodeGeneratorAddMethodAtStartParameters(); |
|
|
|
|
string newCode = |
|
|
|
|
"interface MyInterface {\r\n" + |
|
|
|
|
" System.Int32 MyMethod();\r\n" + |
|
|
|
|
"}"; |
|
|
|
|
UpdateCode(newCode); |
|
|
|
|
|
|
|
|
|
codeInterface.AddFunction("MyMethod", kind, "System.Int32", null, access); |
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Accessibility.Public, addMethodAtStartAccess); |
|
|
|
|
Assert.AreEqual("MyMethod", addMethodAtStartName); |
|
|
|
|
Assert.AreEqual(interfaceTypeDefinition, addMethodAtStartTypeDef); |
|
|
|
|
Assert.AreEqual("System.Int32", addMethodAtStartReturnType.FullName); |
|
|
|
|
Assert.IsTrue(addMethodAtStartReturnType.IsKnownType (KnownTypeCode.Int32)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
public void AddFunction_PrivateFunctionReturningUnknownType_AddsPrivateFunctionWithCodeConverter() |
|
|
|
|
{ |
|
|
|
|
CreateInterface ("interface MyInterface {}"); |
|
|
|
|
var kind = global::EnvDTE.vsCMFunction.vsCMFunctionFunction; |
|
|
|
|
var access = global::EnvDTE.vsCMAccess.vsCMAccessPrivate; |
|
|
|
|
CaptureCodeGeneratorAddMethodAtStartParameters(); |
|
|
|
|
string newCode = |
|
|
|
|
"interface MyInterface {\r\n" + |
|
|
|
|
" Unknown.MyUnknownType MyMethod();\r\n" + |
|
|
|
|
"}"; |
|
|
|
|
UpdateCode(newCode); |
|
|
|
|
|
|
|
|
|
codeInterface.AddFunction("MyMethod", kind, "Unknown.MyUnknownType", null, access); |
|
|
|
|
|
|
|
|
|
Assert.AreEqual(Accessibility.Private, addMethodAtStartAccess); |
|
|
|
|
Assert.AreEqual("MyMethod", addMethodAtStartName); |
|
|
|
|
Assert.AreEqual(interfaceTypeDefinition, addMethodAtStartTypeDef); |
|
|
|
|
Assert.AreEqual("Unknown.MyUnknownType", addMethodAtStartReturnType.FullName); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
public void AddFunction_PublicFunctionReturningSystemInt32_ReturnsCodeFunctionForNewMethod() |
|
|
|
|
{ |
|
|
|
|
string fileName = @"c:\projects\MyProject\interface.cs"; |
|
|
|
|
CreateInterface("interface MyInterface {}", fileName); |
|
|
|
|
var kind = global::EnvDTE.vsCMFunction.vsCMFunctionFunction; |
|
|
|
|
var access = global::EnvDTE.vsCMAccess.vsCMAccessPublic; |
|
|
|
|
string newCode = |
|
|
|
|
"interface MyInterface {\r\n" + |
|
|
|
|
" int MyMethod();\r\n" + |
|
|
|
|
"}"; |
|
|
|
|
UpdateCode(newCode, fileName); |
|
|
|
|
|
|
|
|
|
var codefunction = codeInterface.AddFunction("MyMethod", kind, "System.Int32", null, access) as CodeFunction; |
|
|
|
|
|
|
|
|
|
Assert.AreEqual("MyMethod", codefunction.Name); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
public void AddFunction_MethodNotFoundAfterReloadingTypeDefinition_ReturnsNull() |
|
|
|
|
{ |
|
|
|
|
string fileName = @"c:\projects\MyProject\interface.cs"; |
|
|
|
|
CreateInterface("interface MyInterface {}", fileName); |
|
|
|
|
var kind = global::EnvDTE.vsCMFunction.vsCMFunctionFunction; |
|
|
|
|
var access = global::EnvDTE.vsCMAccess.vsCMAccessPublic; |
|
|
|
|
string newCode = "interface MyInterface {}"; |
|
|
|
|
UpdateCode(newCode, fileName); |
|
|
|
|
|
|
|
|
|
var codefunction = codeInterface.AddFunction("MyMethod", kind, "System.Int32", null, access) as CodeFunction; |
|
|
|
|
|
|
|
|
|
Assert.IsNull(codefunction); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
[Test] |
|
|
|
|
public void AddFunction_UnableToFindTypeDefinitionAfterUpdate_ReturnsNull() |
|
|
|
|
{ |
|
|
|
|
string fileName = @"c:\projects\MyProject\interface.cs"; |
|
|
|
|
CreateInterface("interface MyInterface {}", fileName); |
|
|
|
|
var kind = global::EnvDTE.vsCMFunction.vsCMFunctionFunction; |
|
|
|
|
var access = global::EnvDTE.vsCMAccess.vsCMAccessPublic; |
|
|
|
|
string newCode = "interface SomeOtherInterface {}"; |
|
|
|
|
UpdateCode(newCode, fileName); |
|
|
|
|
|
|
|
|
|
var codefunction = codeInterface.AddFunction("MyMethod", kind, "System.Int32", null, access) as CodeFunction; |
|
|
|
|
|
|
|
|
|
Assert.IsNull(codefunction); |
|
|
|
|
Assert.AreEqual("MyInterface", codeInterface.Name); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|