@@ -103,54 +103,63 @@ public void DiscoverTests(IEnumerable<string> sources, IDiscoveryContext discove
103
103
/// <returns>A list of test cases</returns>
104
104
public static List < TestCase > FindTestCases ( string source )
105
105
{
106
- List < TestCase > testCases = new List < TestCase > ( ) ;
106
+ List < TestCase > collectionOfTestCases = new List < TestCase > ( ) ;
107
107
108
108
var nfprojSources = FindNfprojSources ( source ) ;
109
109
if ( nfprojSources . Length == 0 )
110
110
{
111
- return testCases ;
111
+ return collectionOfTestCases ;
112
112
}
113
113
114
- var allCsFils = GetAllCsFileNames ( nfprojSources ) ;
114
+ var allCsFiles = GetAllCsFileNames ( nfprojSources ) ;
115
115
116
+ // developer note: we have to use LoadFile() and not Load() which loads the assembly into the caller domain
116
117
Assembly test = Assembly . LoadFile ( source ) ;
117
118
AppDomain . CurrentDomain . AssemblyResolve += App_AssemblyResolve ;
118
119
AppDomain . CurrentDomain . Load ( test . GetName ( ) ) ;
119
120
120
- var allTypes = test . GetTypes ( ) . Where ( x=> x . IsClass ) ;
121
- foreach ( var type in allTypes )
121
+ var typeCandidatesForTests = test . GetTypes ( )
122
+ . Where ( x => x . IsClass ) ;
123
+
124
+ foreach ( var typeCandidate in typeCandidatesForTests )
122
125
{
123
- if ( ! type . IsClass )
124
- {
125
- continue ;
126
- }
126
+ var testClasses = typeCandidate . GetCustomAttributes ( true )
127
+ . Where ( x => x . GetType ( ) . FullName == typeof ( TestClassAttribute ) . FullName ) ;
127
128
128
- var typeAttribs = type . GetCustomAttributes ( true )
129
- . Where ( x => x . GetType ( ) . FullName == typeof ( TestClassAttribute ) . GetType ( ) . FullName ) ;
130
- foreach ( var typeAttrib in typeAttribs )
129
+ foreach ( var testClassAttrib in testClasses )
131
130
{
132
- var methods = type . GetMethods ( ) ;
131
+ var methods = typeCandidate . GetMethods ( ) ;
132
+
133
133
// First we look at Setup
134
134
foreach ( var method in methods )
135
135
{
136
- var attribs = method . GetCustomAttributes ( true ) ;
137
- attribs = Helper . RemoveTestMethodIfDataRowExists ( attribs ) ;
138
- var attribsToItterate = attribs . Where ( x => IsTestMethod ( x ) ) . ToArray ( ) ;
139
- for ( int i = 0 ; i < attribsToItterate . Length ; i ++ )
136
+ var methodAttribs = method . GetCustomAttributes ( true ) ;
137
+ methodAttribs = Helper . RemoveTestMethodIfDataRowExists ( methodAttribs ) ;
138
+
139
+ var testMethodsToItterate = methodAttribs . Where ( x => IsTestMethod ( x ) ) . ToArray ( ) ;
140
+
141
+ for ( int i = 0 ; i < testMethodsToItterate . Length ; i ++ )
140
142
{
141
- var attrib = attribsToItterate [ i ] ;
142
- var testCase = GetFileNameAndLineNumber ( allCsFils , type , method , attrib , i ) ;
143
+ var testMethodAttrib = testMethodsToItterate [ i ] ;
144
+ var testCase = GetFileNameAndLineNumber (
145
+ allCsFiles ,
146
+ typeCandidate ,
147
+ method ,
148
+ testMethodAttrib ,
149
+ i ) ;
150
+
143
151
testCase . Source = source ;
144
152
testCase . ExecutorUri = new Uri ( TestsConstants . NanoExecutor ) ;
145
- testCase . FullyQualifiedName = $ "{ type . FullName } .{ testCase . DisplayName } ";
146
- testCase . Traits . Add ( new Trait ( "Type" , attrib . GetType ( ) . Name . Replace ( "Attribute" , "" ) ) ) ;
147
- testCases . Add ( testCase ) ;
153
+ testCase . FullyQualifiedName = $ "{ typeCandidate . FullName } .{ testCase . DisplayName } ";
154
+ testCase . Traits . Add ( new Trait ( "Type" , testMethodAttrib . GetType ( ) . Name . Replace ( "Attribute" , "" ) ) ) ;
155
+
156
+ collectionOfTestCases . Add ( testCase ) ;
148
157
}
149
158
}
150
159
}
151
160
}
152
161
153
- return testCases ;
162
+ return collectionOfTestCases ;
154
163
}
155
164
156
165
private static bool IsTestMethod ( object attrib )
@@ -244,42 +253,54 @@ private static FileInfo[] FindNfprojSources(string source)
244
253
}
245
254
}
246
255
247
- private static TestCase GetFileNameAndLineNumber ( string [ ] csFiles , Type className , MethodInfo method , object attribute , int attributeIndex )
256
+ private static TestCase GetFileNameAndLineNumber (
257
+ string [ ] csFiles ,
258
+ Type className ,
259
+ MethodInfo method ,
260
+ object attribute ,
261
+ int attributeIndex )
248
262
{
249
- var clName = className . Name ;
250
- var methodName = method . Name ;
251
- TestCase flret = new TestCase ( ) ;
263
+ TestCase testCase = new TestCase ( ) ;
264
+
252
265
foreach ( var csFile in csFiles )
253
266
{
254
- StreamReader sr = new StreamReader ( csFile ) ;
255
- var allFile = sr . ReadToEnd ( ) ;
256
- if ( ! allFile . Contains ( $ "class { clName } ") )
267
+ using ( StreamReader sr = new StreamReader ( csFile ) )
257
268
{
258
- continue ;
259
- }
269
+ var fileContent = sr . ReadToEnd ( ) ;
260
270
261
- if ( ! allFile . Contains ( $ " { methodName } ( ") )
262
- {
263
- continue ;
264
- }
271
+ if ( ! fileContent . Contains ( $ "class { className . Name } ") )
272
+ {
273
+ continue ;
274
+ }
265
275
266
- // We found it!
267
- int lineNum = 1 ;
268
- foreach ( var line in allFile . Split ( '\r ' ) )
269
- {
270
- if ( line . Contains ( $ " { methodName } (") )
276
+ if ( ! fileContent . Contains ( $ " { method . Name } (") )
271
277
{
272
- flret . CodeFilePath = csFile ;
273
- flret . LineNumber = lineNum ;
274
- flret . DisplayName = Helper . GetTestDisplayName ( method , attribute , attributeIndex ) ;
275
- return flret ;
278
+ continue ;
276
279
}
277
280
278
- lineNum ++ ;
281
+ // We found it!
282
+ int lineNumber = 1 ;
283
+
284
+ foreach ( var line in fileContent . Split ( '\r ' ) )
285
+ {
286
+ if ( line . Contains ( $ " { method . Name } (") )
287
+ {
288
+ testCase . CodeFilePath = csFile ;
289
+ testCase . LineNumber = lineNumber ;
290
+ testCase . DisplayName = Helper . GetTestDisplayName (
291
+ method ,
292
+ attribute ,
293
+ attributeIndex ) ;
294
+
295
+ return testCase ;
296
+ }
297
+
298
+ lineNumber ++ ;
299
+ }
279
300
}
280
301
}
281
302
282
- return flret ;
303
+ return testCase ;
283
304
}
284
305
}
285
306
}
0 commit comments