@@ -15,7 +15,8 @@ namespace nanoFramework.Tools.MetadataProcessor.Tests.Core
15
15
[ TestClass ]
16
16
public class StubsGenerationTests
17
17
{
18
- private const string NativeMethodGenerationDeclaration = @"void NativeMethodGeneration::NativeMethodWithReferenceParameters( uint8_t& param0, uint16_t& param1, HRESULT &hr )
18
+ private const string NativeMethodGenerationDeclaration =
19
+ @"void NativeMethodGeneration::NativeMethodWithReferenceParameters( uint8_t& param0, uint16_t& param1, HRESULT &hr )
19
20
{
20
21
21
22
(void)param0;
@@ -33,8 +34,171 @@ public class StubsGenerationTests
33
34
34
35
}" ;
35
36
37
+ private const string NativeMarshallingMethodGenerationDeclaration =
38
+ @"HRESULT Library_StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration::NativeMethodWithReferenceParameters___VOID__BYREF_U1__BYREF_U2( CLR_RT_StackFrame& stack )
39
+ {
40
+ NANOCLR_HEADER(); hr = S_OK;
41
+ {
42
+
43
+ uint8_t *param0;
44
+ uint8_t heapblock0[CLR_RT_HEAP_BLOCK_SIZE];
45
+ NANOCLR_CHECK_HRESULT( Interop_Marshal_UINT8_ByRef( stack, heapblock0, 1, param0 ) );
46
+
47
+ uint16_t *param1;
48
+ uint8_t heapblock1[CLR_RT_HEAP_BLOCK_SIZE];
49
+ NANOCLR_CHECK_HRESULT( Interop_Marshal_UINT16_ByRef( stack, heapblock1, 2, param1 ) );
50
+
51
+ NativeMethodGeneration::NativeMethodWithReferenceParameters( *param0, *param1, hr );
52
+ NANOCLR_CHECK_HRESULT( hr );
53
+
54
+ }
55
+ NANOCLR_NOCLEANUP();
56
+ }" ;
57
+
58
+ private const string NativeHeaderMethodGenerationDeclaration =
59
+ "static void NativeMethodWithReferenceParameters( uint8_t& param0, uint16_t& param1, HRESULT &hr );" ;
60
+
61
+ private string stubPath ;
62
+
36
63
[ TestMethod ]
37
64
public void GeneratingStubsFromNFAppTest ( )
65
+ {
66
+ // read generated stub file and look for the function declaration
67
+ var generatedFile =
68
+ File . ReadAllText (
69
+ $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.cpp") ;
70
+
71
+ Assert . IsTrue ( generatedFile . Contains ( NativeMethodGenerationDeclaration ) ) ;
72
+ }
73
+
74
+ [ TestMethod ]
75
+ public void GeneratingMarshallingStubsFromNFAppTest ( )
76
+ {
77
+ var generatedFile =
78
+ File . ReadAllText (
79
+ $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration_mshl.cpp") ;
80
+
81
+ Assert . IsTrue ( generatedFile . Contains ( NativeMarshallingMethodGenerationDeclaration ) ) ;
82
+ }
83
+
84
+ [ TestMethod ]
85
+ public void GeneratingHeaderStubsFromNFAppTest ( )
86
+ {
87
+ var generatedFile =
88
+ File . ReadAllText (
89
+ $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.h") ;
90
+
91
+ Assert . IsTrue ( generatedFile . Contains ( NativeHeaderMethodGenerationDeclaration ) ) ;
92
+ }
93
+
94
+ private const string StaticMethodWithoutParameterHeaderGeneration =
95
+ @"static void NativeStaticMethod( HRESULT &hr );" ;
96
+ private const string StaticMethodWithoutParameterMarshallGeneration =
97
+ @"HRESULT Library_StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration::NativeStaticMethod___STATIC__VOID( CLR_RT_StackFrame& stack )
98
+ {
99
+ NANOCLR_HEADER(); hr = S_OK;
100
+ {
101
+
102
+ NativeMethodGeneration::NativeStaticMethod( hr );
103
+ NANOCLR_CHECK_HRESULT( hr );
104
+
105
+ }
106
+ NANOCLR_NOCLEANUP();
107
+ }" ;
108
+ private const string StaticMethodWithoutParameterImplementationGeneration =
109
+ @"void NativeMethodGeneration::NativeStaticMethod( HRESULT &hr )
110
+ {
111
+
112
+ (void)hr;
113
+
114
+
115
+ ////////////////////////////////
116
+ // implementation starts here //
117
+
118
+
119
+ // implementation ends here //
120
+ ////////////////////////////////
121
+
122
+
123
+ }" ;
124
+
125
+ [ TestMethod ]
126
+ public void GeneratingStaticMethodWithoutParams ( )
127
+ {
128
+ var generatedHeaderFile =
129
+ File . ReadAllText (
130
+ $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.h") ;
131
+
132
+ var generatedMarshallFile =
133
+ File . ReadAllText (
134
+ $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration_mshl.cpp") ;
135
+
136
+ var generatedImplementationFile =
137
+ File . ReadAllText (
138
+ $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.cpp") ;
139
+
140
+ Assert . IsTrue ( generatedHeaderFile . Contains ( StaticMethodWithoutParameterHeaderGeneration ) ) ;
141
+ Assert . IsTrue ( generatedMarshallFile . Contains ( StaticMethodWithoutParameterMarshallGeneration ) ) ;
142
+ Assert . IsTrue ( generatedImplementationFile . Contains ( StaticMethodWithoutParameterImplementationGeneration ) ) ;
143
+ }
144
+
145
+ private const string StaticMethodHeaderGeneration =
146
+ @"static uint8_t NativeStaticMethodReturningByte( char param0, HRESULT &hr );" ;
147
+ private const string StaticMethodMarshallGeneration =
148
+ @"HRESULT Library_StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration::NativeStaticMethodReturningByte___STATIC__U1__CHAR( CLR_RT_StackFrame& stack )
149
+ {
150
+ NANOCLR_HEADER(); hr = S_OK;
151
+ {
152
+
153
+ char param0;
154
+ NANOCLR_CHECK_HRESULT( Interop_Marshal_CHAR( stack, 0, param0 ) );
155
+
156
+ uint8_t retValue = NativeMethodGeneration::NativeStaticMethodReturningByte( param0, hr );
157
+ NANOCLR_CHECK_HRESULT( hr );
158
+ SetResult_UINT8( stack, retValue );
159
+ }
160
+ NANOCLR_NOCLEANUP();
161
+ }" ;
162
+ private const string StaticMethodImplementationGeneration =
163
+ @"uint8_t NativeMethodGeneration::NativeStaticMethodReturningByte( char param0, HRESULT &hr )
164
+ {
165
+
166
+ (void)param0;
167
+ (void)hr;
168
+ uint8_t retValue = 0;
169
+
170
+ ////////////////////////////////
171
+ // implementation starts here //
172
+
173
+
174
+ // implementation ends here //
175
+ ////////////////////////////////
176
+
177
+ return retValue;
178
+ }" ;
179
+
180
+ [ TestMethod ]
181
+ public void GeneratingStaticMethod ( )
182
+ {
183
+ var generatedHeaderFile =
184
+ File . ReadAllText (
185
+ $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.h") ;
186
+
187
+ var generatedMarshallFile =
188
+ File . ReadAllText (
189
+ $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration_mshl.cpp") ;
190
+
191
+ var generatedImplementationFile =
192
+ File . ReadAllText (
193
+ $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.cpp") ;
194
+
195
+ Assert . IsTrue ( generatedHeaderFile . Contains ( StaticMethodHeaderGeneration ) ) ;
196
+ Assert . IsTrue ( generatedMarshallFile . Contains ( StaticMethodMarshallGeneration ) ) ;
197
+ Assert . IsTrue ( generatedImplementationFile . Contains ( StaticMethodImplementationGeneration ) ) ;
198
+ }
199
+
200
+ [ TestInitialize ]
201
+ public void GenerateStubs ( )
38
202
{
39
203
var loadHints = new Dictionary < string , string > ( StringComparer . Ordinal )
40
204
{
@@ -50,22 +214,22 @@ public void GeneratingStubsFromNFAppTest()
50
214
51
215
var fileToParse = TestObjectHelper . GenerationNFAppFullPath ;
52
216
var fileToCompile = Path . ChangeExtension ( fileToParse , "pe" ) ;
53
-
217
+
54
218
// get path where stubs will be generated
55
- var stubPath = Path . Combine (
219
+ stubPath = Path . Combine (
56
220
TestObjectHelper . TestExecutionLocation ,
57
221
"Stubs" ) ;
58
-
222
+
59
223
var assemblyDefinition = AssemblyDefinition . ReadAssembly (
60
224
fileToParse ,
61
225
new ReaderParameters { AssemblyResolver = new LoadHintsAssemblyResolver ( loadHints ) } ) ;
62
226
63
- var assemblyBuilder = new nanoAssemblyBuilder ( assemblyDefinition , classNamesToExclude , false , false ) ;
227
+ var assemblyBuilder = new nanoAssemblyBuilder ( assemblyDefinition , classNamesToExclude , false ) ;
64
228
65
229
using ( var stream = File . Open (
66
- Path . ChangeExtension ( fileToCompile , "tmp" ) ,
67
- FileMode . Create ,
68
- FileAccess . ReadWrite ) )
230
+ Path . ChangeExtension ( fileToCompile , "tmp" ) ,
231
+ FileMode . Create ,
232
+ FileAccess . ReadWrite ) )
69
233
using ( var writer = new BinaryWriter ( stream ) )
70
234
{
71
235
assemblyBuilder . Write ( GetBinaryWriter ( writer ) ) ;
@@ -87,12 +251,11 @@ public void GeneratingStubsFromNFAppTest()
87
251
false ) ;
88
252
89
253
skeletonGenerator . GenerateSkeleton ( ) ;
254
+ }
90
255
91
- // read generated stub file and look for the function declaration
92
- string generatedFile = File . ReadAllText ( $ "{ stubPath } \\ StubsGenerationTestNFApp_StubsGenerationTestNFApp_NativeMethodGeneration.cpp") ;
93
-
94
- Assert . IsTrue ( generatedFile . Contains ( NativeMethodGenerationDeclaration ) ) ;
95
-
256
+ [ TestCleanup ]
257
+ public void DeleteStubs ( )
258
+ {
96
259
Directory . Delete ( stubPath , true ) ;
97
260
}
98
261
0 commit comments