2121using ICSharpCode . NRefactory . TypeSystem ;
2222using ICSharpCode . PackageManagement . EnvDTE ;
2323using NUnit . Framework ;
24+ using Rhino . Mocks ;
2425
2526namespace 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}
0 commit comments