6
6
using System ;
7
7
using System . Collections . Generic ;
8
8
using System . IO ;
9
+ using System . Text . RegularExpressions ;
9
10
using Microsoft . VisualStudio . TestTools . UnitTesting ;
10
11
using Mono . Cecil ;
11
12
using nanoFramework . Tools . MetadataProcessor . Core ;
@@ -58,15 +59,15 @@ public class StubsGenerationTests
58
59
private const string NativeHeaderMethodGenerationDeclaration =
59
60
"static void NativeMethodWithReferenceParameters( uint8_t& param0, uint16_t& param1, HRESULT &hr );" ;
60
61
61
- private string stubPath ;
62
+ private string _stubsPath ;
62
63
63
64
[ TestMethod ]
64
65
public void GeneratingStubsFromNFAppTest ( )
65
66
{
66
67
// read generated stub file and look for the function declaration
67
68
var generatedFile =
68
69
File . ReadAllText (
69
- $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.cpp") ;
70
+ $ "{ _stubsPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.cpp") ;
70
71
71
72
Assert . IsTrue ( generatedFile . Contains ( NativeMethodGenerationDeclaration ) ) ;
72
73
}
@@ -76,7 +77,7 @@ public void GeneratingMarshallingStubsFromNFAppTest()
76
77
{
77
78
var generatedFile =
78
79
File . ReadAllText (
79
- $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration_mshl.cpp") ;
80
+ $ "{ _stubsPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration_mshl.cpp") ;
80
81
81
82
Assert . IsTrue ( generatedFile . Contains ( NativeMarshallingMethodGenerationDeclaration ) ) ;
82
83
}
@@ -86,7 +87,7 @@ public void GeneratingHeaderStubsFromNFAppTest()
86
87
{
87
88
var generatedFile =
88
89
File . ReadAllText (
89
- $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.h") ;
90
+ $ "{ _stubsPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.h") ;
90
91
91
92
Assert . IsTrue ( generatedFile . Contains ( NativeHeaderMethodGenerationDeclaration ) ) ;
92
93
}
@@ -127,15 +128,15 @@ public void GeneratingStaticMethodWithoutParams()
127
128
{
128
129
var generatedHeaderFile =
129
130
File . ReadAllText (
130
- $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.h") ;
131
+ $ "{ _stubsPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.h") ;
131
132
132
133
var generatedMarshallFile =
133
134
File . ReadAllText (
134
- $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration_mshl.cpp") ;
135
+ $ "{ _stubsPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration_mshl.cpp") ;
135
136
136
137
var generatedImplementationFile =
137
138
File . ReadAllText (
138
- $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.cpp") ;
139
+ $ "{ _stubsPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.cpp") ;
139
140
140
141
Assert . IsTrue ( generatedHeaderFile . Contains ( StaticMethodWithoutParameterHeaderGeneration ) ) ;
141
142
Assert . IsTrue ( generatedMarshallFile . Contains ( StaticMethodWithoutParameterMarshallGeneration ) ) ;
@@ -182,27 +183,41 @@ public void GeneratingStaticMethod()
182
183
{
183
184
var generatedHeaderFile =
184
185
File . ReadAllText (
185
- $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.h") ;
186
+ $ "{ _stubsPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.h") ;
186
187
187
188
var generatedMarshallFile =
188
189
File . ReadAllText (
189
- $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration_mshl.cpp") ;
190
+ $ "{ _stubsPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration_mshl.cpp") ;
190
191
191
192
var generatedImplementationFile =
192
193
File . ReadAllText (
193
- $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.cpp") ;
194
+ $ "{ _stubsPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.cpp") ;
194
195
195
196
Assert . IsTrue ( generatedHeaderFile . Contains ( StaticMethodHeaderGeneration ) ) ;
196
197
Assert . IsTrue ( generatedMarshallFile . Contains ( StaticMethodMarshallGeneration ) ) ;
197
198
Assert . IsTrue ( generatedImplementationFile . Contains ( StaticMethodImplementationGeneration ) ) ;
198
199
}
199
200
201
+ [ TestMethod ]
202
+ public void BackingFieldsAbsentTests ( )
203
+ {
204
+ string generatedAssemblyHeaderFile =
205
+ File . ReadAllText (
206
+ $ "{ _stubsPath } \\ TestNFClassLibrary.h") ;
207
+
208
+ // check for property with backing field patter in the name
209
+ Assert . IsFalse ( generatedAssemblyHeaderFile . Contains ( "k__BackingField =" ) , "Found a name with BackingField pattern, when it shouldn't" ) ;
210
+
211
+ // deep check for backing field name pattern (except for entry patter in comments)
212
+ Assert . IsFalse ( Regex . IsMatch ( generatedAssemblyHeaderFile , @"(?<!')<\w+>k__BackingField(?!')" ) , "Found a name with BackingField pattern, when it shouldn't" ) ;
213
+ }
214
+
200
215
[ TestInitialize ]
201
216
public void GenerateStubs ( )
202
217
{
203
218
var loadHints = new Dictionary < string , string > ( StringComparer . Ordinal )
204
219
{
205
- [ "mscorlib" ] = Path . Combine ( Directory . GetParent ( TestObjectHelper . GenerationNFAppFullPath ) . FullName ,
220
+ [ "mscorlib" ] = Path . Combine ( Directory . GetParent ( TestObjectHelper . StubsGenerationNFAppFullPath ) . FullName ,
206
221
"mscorlib.dll" )
207
222
} ;
208
223
@@ -212,51 +227,92 @@ public void GenerateStubs()
212
227
"THIS_NAME_DOES_NOT_EXIST_IN_THE_PROJECT"
213
228
} ;
214
229
215
- var fileToParse = TestObjectHelper . GenerationNFAppFullPath ;
216
- var fileToCompile = Path . ChangeExtension ( fileToParse , "pe" ) ;
230
+ // Conpile StubsGenerationNFApp
231
+ string stubsGenerationFileToParse = TestObjectHelper . StubsGenerationNFAppFullPath ;
232
+ string stubsGenerationFileToCompile = Path . ChangeExtension ( stubsGenerationFileToParse , "pe" ) ;
217
233
218
234
// get path where stubs will be generated
219
- stubPath = Path . Combine (
235
+ _stubsPath = Path . Combine (
220
236
TestObjectHelper . TestExecutionLocation ,
221
237
"Stubs" ) ;
222
238
223
- var assemblyDefinition = AssemblyDefinition . ReadAssembly (
224
- fileToParse ,
239
+ AssemblyDefinition assemblyDefinition = AssemblyDefinition . ReadAssembly (
240
+ stubsGenerationFileToParse ,
225
241
new ReaderParameters { AssemblyResolver = new LoadHintsAssemblyResolver ( loadHints ) } ) ;
226
242
227
- var assemblyBuilder = new nanoAssemblyBuilder ( assemblyDefinition , classNamesToExclude , false ) ;
243
+ nanoAssemblyBuilder assemblyBuilder = new nanoAssemblyBuilder ( assemblyDefinition , classNamesToExclude , false ) ;
228
244
229
- using ( var stream = File . Open (
230
- Path . ChangeExtension ( fileToCompile , "tmp" ) ,
245
+ using ( FileStream stream = File . Open (
246
+ Path . ChangeExtension ( stubsGenerationFileToCompile , "tmp" ) ,
231
247
FileMode . Create ,
232
248
FileAccess . ReadWrite ) )
233
- using ( var writer = new BinaryWriter ( stream ) )
249
+ using ( BinaryWriter writer = new BinaryWriter ( stream ) )
234
250
{
235
251
assemblyBuilder . Write ( GetBinaryWriter ( writer ) ) ;
236
252
}
237
253
238
254
// OK to delete tmp PE file
239
- File . Delete ( Path . ChangeExtension ( fileToCompile , "tmp" ) ) ;
255
+ File . Delete ( Path . ChangeExtension ( stubsGenerationFileToCompile , "tmp" ) ) ;
240
256
241
257
assemblyBuilder . Minimize ( ) ;
242
258
243
- var tablesContext = assemblyBuilder . TablesContext ;
259
+ nanoTablesContext tablesContext = assemblyBuilder . TablesContext ;
244
260
245
261
var skeletonGenerator = new nanoSkeletonGenerator (
246
262
tablesContext ,
247
- stubPath ,
263
+ _stubsPath ,
248
264
"testStubs" ,
249
265
"StubsGenerationTestNFApp" ,
250
266
false ,
251
267
false ) ;
252
268
253
269
skeletonGenerator . GenerateSkeleton ( ) ;
270
+
271
+ // Compile the TestNFClassLibrary
272
+ string nfLibFileToParse = TestObjectHelper . TestNFClassLibFullPath ;
273
+ string nfLibFileToCompile = Path . ChangeExtension ( nfLibFileToParse , "pe" ) ;
274
+
275
+ assemblyDefinition = AssemblyDefinition . ReadAssembly (
276
+ nfLibFileToParse ,
277
+ new ReaderParameters { AssemblyResolver = new LoadHintsAssemblyResolver ( loadHints ) } ) ;
278
+
279
+ assemblyBuilder = new nanoAssemblyBuilder (
280
+ assemblyDefinition ,
281
+ new List < string > ( ) ,
282
+ false ) ;
283
+
284
+ using ( FileStream stream = File . Open (
285
+ Path . ChangeExtension ( nfLibFileToCompile , "tmp" ) ,
286
+ FileMode . Create ,
287
+ FileAccess . ReadWrite ) )
288
+
289
+ using ( BinaryWriter writer = new BinaryWriter ( stream ) )
290
+ {
291
+ assemblyBuilder . Write ( GetBinaryWriter ( writer ) ) ;
292
+ }
293
+
294
+ // OK to delete tmp PE file
295
+ File . Delete ( Path . ChangeExtension ( nfLibFileToCompile , "tmp" ) ) ;
296
+
297
+ assemblyBuilder . Minimize ( ) ;
298
+
299
+ tablesContext = assemblyBuilder . TablesContext ;
300
+
301
+ skeletonGenerator = new nanoSkeletonGenerator (
302
+ tablesContext ,
303
+ _stubsPath ,
304
+ "testStubs" ,
305
+ "TestNFClassLibrary" ,
306
+ true ,
307
+ true ) ;
308
+
309
+ skeletonGenerator . GenerateSkeleton ( ) ;
254
310
}
255
311
256
312
[ TestCleanup ]
257
313
public void DeleteStubs ( )
258
314
{
259
- Directory . Delete ( stubPath , true ) ;
315
+ Directory . Delete ( _stubsPath , true ) ;
260
316
}
261
317
262
318
private nanoBinaryWriter GetBinaryWriter (
@@ -265,4 +321,4 @@ private nanoBinaryWriter GetBinaryWriter(
265
321
return nanoBinaryWriter . CreateLittleEndianBinaryWriter ( writer ) ;
266
322
}
267
323
}
268
- }
324
+ }
0 commit comments