Skip to content

Commit 04b1775

Browse files
committed
Overhaul ITypeLibWrapper handling in TestEngine
1 parent 9ca187c commit 04b1775

File tree

2 files changed

+64
-70
lines changed

2 files changed

+64
-70
lines changed

Rubberduck.UnitTesting/UnitTesting/TestEngine.cs

Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -143,88 +143,91 @@ private void RunWhileSuspended(IEnumerable<TestMethod> tests)
143143
testResults.Clear();
144144
try
145145
{
146-
var modules = testMethods.GroupBy(test => test.Declaration.QualifiedName.QualifiedModuleName);
147-
foreach (var module in modules)
146+
var modules = testMethods.GroupBy(test => test.Declaration.QualifiedName.QualifiedModuleName)
147+
.Select(grouping => grouping.Key);
148+
foreach (var qmn in modules)
148149
{
149-
var testInitialize = TestDiscovery.FindTestInitializeMethods(module.Key, _state).ToList();
150-
var testCleanup = TestDiscovery.FindTestCleanupMethods(module.Key, _state).ToList();
150+
var testInitialize = TestDiscovery.FindTestInitializeMethods(qmn, _state).ToList();
151+
var testCleanup = TestDiscovery.FindTestCleanupMethods(qmn, _state).ToList();
151152

152-
var capturedModule = module;
153153
var moduleTestMethods = testMethods
154154
.Where(test =>
155155
{
156-
var qmn = test.Declaration.QualifiedName.QualifiedModuleName;
156+
var testModuleName = test.Declaration.QualifiedName.QualifiedModuleName;
157157

158-
return qmn.ProjectId == capturedModule.Key.ProjectId
159-
&& qmn.ComponentName == capturedModule.Key.ComponentName;
158+
return testModuleName.ProjectId == qmn.ProjectId
159+
&& testModuleName.ComponentName == qmn.ComponentName;
160160
});
161161

162162
var fakes = _fakesFactory.Create();
163-
var initializeMethods = TestDiscovery.FindModuleInitializeMethods(module.Key, _state);
164-
try
163+
var initializeMethods = TestDiscovery.FindModuleInitializeMethods(qmn, _state);
164+
using (var typeLibWrapper = _wrapperProvider.TypeLibWrapperFromProject(qmn.ProjectId))
165165
{
166-
RunInternal(initializeMethods);
167-
}
168-
catch (COMException ex)
169-
{
170-
Logger.Error(ex,
171-
"Unexpected COM exception while initializing tests for module {0}. The module will be skipped.",
172-
module.Key.Name);
173-
foreach (var method in moduleTestMethods)
166+
try
174167
{
175-
OnTestCompleted(method, new TestResult(TestOutcome.Unknown, AssertMessages.Assert_ComException));
168+
RunInternal(typeLibWrapper, initializeMethods);
176169
}
177-
continue;
178-
}
179-
foreach (var test in moduleTestMethods)
180-
{
181-
// no need to run setup/teardown for ignored tests
182-
if (test.Declaration.Annotations.Any(a => a.AnnotationType == AnnotationType.IgnoreTest))
170+
catch (COMException ex)
183171
{
184-
OnTestCompleted(test, new TestResult(TestOutcome.Ignored));
172+
Logger.Error(ex,
173+
"Unexpected COM exception while initializing tests for module {0}. The module will be skipped.",
174+
qmn.Name);
175+
foreach (var method in moduleTestMethods)
176+
{
177+
OnTestCompleted(method, new TestResult(TestOutcome.Unknown, AssertMessages.Assert_ComException));
178+
}
185179
continue;
186180
}
187-
188-
try
181+
foreach (var test in moduleTestMethods)
189182
{
190-
fakes.StartTest();
183+
// no need to run setup/teardown for ignored tests
184+
if (test.Declaration.Annotations.Any(a => a.AnnotationType == AnnotationType.IgnoreTest))
185+
{
186+
OnTestCompleted(test, new TestResult(TestOutcome.Ignored));
187+
continue;
188+
}
189+
191190
try
192191
{
193-
RunInternal(testInitialize);
192+
fakes.StartTest();
193+
try
194+
{
195+
RunInternal(typeLibWrapper, testInitialize);
196+
}
197+
catch (Exception trace)
198+
{
199+
OnTestCompleted(test, new TestResult(TestOutcome.Inconclusive, AssertMessages.Assert_TestInitializeFailure));
200+
Logger.Trace(trace, "Unexpected Exception when running TestInitialize");
201+
continue;
202+
}
203+
var result = RunTestMethod(typeLibWrapper, test);
204+
// we can trigger this event, because cleanup can fail without affecting the result
205+
OnTestCompleted(test, result);
206+
RunInternal(typeLibWrapper, testCleanup);
194207
}
195-
catch (Exception trace)
208+
catch (COMException ex)
196209
{
197-
OnTestCompleted(test, new TestResult(TestOutcome.Inconclusive, AssertMessages.Assert_TestInitializeFailure));
198-
Logger.Trace(trace, "Unexpected Exception when running TestInitialize");
199-
continue;
210+
Logger.Error(ex, "Unexpected COM exception while running tests.");
211+
OnTestCompleted(test, new TestResult(TestOutcome.Inconclusive, AssertMessages.Assert_ComException));
212+
}
213+
finally
214+
{
215+
fakes.StopTest();
200216
}
201-
var result = RunTestMethod(test);
202-
// we can trigger this event, because cleanup can fail without affecting the result
203-
OnTestCompleted(test, result);
204-
RunInternal(testCleanup);
205217
}
206-
catch (COMException ex)
218+
var cleanupMethods = TestDiscovery.FindModuleCleanupMethods(qmn, _state);
219+
try
207220
{
208-
Logger.Error(ex, "Unexpected COM exception while running tests.");
209-
OnTestCompleted(test, new TestResult(TestOutcome.Inconclusive, AssertMessages.Assert_ComException));
221+
RunInternal(typeLibWrapper, cleanupMethods);
210222
}
211-
finally
223+
catch (COMException ex)
212224
{
213-
fakes.StopTest();
225+
Logger.Error(ex,
226+
"Unexpected COM exception while cleaning up tests for module {0}. Aborting any further unit tests",
227+
qmn.Name);
228+
break;
214229
}
215230
}
216-
var cleanupMethods = TestDiscovery.FindModuleCleanupMethods(module.Key, _state);
217-
try
218-
{
219-
RunInternal(cleanupMethods);
220-
}
221-
catch (COMException ex)
222-
{
223-
Logger.Error(ex,
224-
"Unexpected COM exception while cleaning up tests for module {0}. Aborting any further unit tests",
225-
module.Key.Name);
226-
break;
227-
}
228231
}
229232
}
230233
catch (Exception ex)
@@ -233,7 +236,7 @@ private void RunWhileSuspended(IEnumerable<TestMethod> tests)
233236
}
234237
}
235238

236-
private TestResult RunTestMethod(TestMethod test)
239+
private TestResult RunTestMethod(ITypeLibWrapper typeLib, TestMethod test)
237240
{
238241
var assertResults = new List<AssertCompletedEventArgs>();
239242

@@ -243,7 +246,6 @@ private TestResult RunTestMethod(TestMethod test)
243246
{
244247
AssertHandler.OnAssertCompleted += (s, e) => assertResults.Add(e);
245248
var testDeclaration = test.Declaration;
246-
var typeLib = _wrapperProvider.TypeLibWrapperFromProject(testDeclaration.Project);
247249
duration.Start();
248250

249251
_typeLibApi.ExecuteCode(typeLib, testDeclaration.ComponentName, testDeclaration.QualifiedName.MemberName);
@@ -271,19 +273,12 @@ private AssertCompletedEventArgs EvaluateResults(IEnumerable<AssertCompletedEven
271273
return result;
272274
}
273275

274-
private void RunInternal(IEnumerable<Declaration> members)
276+
private void RunInternal(ITypeLibWrapper typeLib, IEnumerable<Declaration> members)
275277
{
276-
var groupedMembers = members.GroupBy(m => m.ProjectId);
277-
foreach (var group in groupedMembers)
278+
foreach (var member in members)
278279
{
279-
using (var typeLib = _wrapperProvider.TypeLibWrapperFromProject(group.Key))
280-
{
281-
foreach (var member in group)
282-
{
283-
_typeLibApi.ExecuteCode(typeLib, member.QualifiedModuleName.ComponentName,
284-
member.QualifiedName.MemberName);
285-
}
286-
}
280+
_typeLibApi.ExecuteCode(typeLib, member.QualifiedModuleName.ComponentName,
281+
member.QualifiedName.MemberName);
287282
}
288283
}
289284

RubberduckTests/UnitTesting/EngineTests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
using Moq;
22
using NUnit.Framework;
33
using Rubberduck.Parsing.UIContext;
4-
using Rubberduck.Parsing.VBA;
4+
using Rubberduck.Resources.UnitTesting;
55
using Rubberduck.UnitTesting;
6-
using Rubberduck.VBEditor;
76
using Rubberduck.VBEditor.ComManagement.TypeLibs;
87
using Rubberduck.VBEditor.ComManagement.TypeLibsAPI;
98
using Rubberduck.VBEditor.SafeComWrappers;

0 commit comments

Comments
 (0)