Skip to content

Commit 1817e16

Browse files
authored
Merge pull request #3778 from bclothier/ImplementRunMethod
Unit testing should now work in every single VBA host application!
2 parents 96c152e + 7426491 commit 1817e16

File tree

6 files changed

+21
-121
lines changed

6 files changed

+21
-121
lines changed

RetailCoder.VBE/UnitTesting/TestEngine.cs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
using Rubberduck.UI;
1111
using Rubberduck.UI.UnitTesting;
1212
using Rubberduck.VBEditor.Application;
13+
using Rubberduck.VBEditor.ComManagement.TypeLibs;
14+
using Rubberduck.VBEditor.ComManagement.TypeLibsAPI;
1315
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
1416

1517
namespace Rubberduck.UnitTesting
@@ -20,8 +22,6 @@ public class TestEngine : ITestEngine
2022
private readonly RubberduckParserState _state;
2123
private readonly IFakesProviderFactory _fakesFactory;
2224

23-
// can't be assigned from constructor because ActiveVBProject is null at startup:
24-
private IHostApplication _hostApplication;
2525
private static readonly Logger Logger = LogManager.GetCurrentClassLogger();
2626

2727
public TestEngine(TestExplorerModel model, IVBE vbe, RubberduckParserState state, IFakesProviderFactory fakesFactory)
@@ -118,20 +118,17 @@ public void Run(IEnumerable<TestMethod> tests)
118118

119119
private void Run(IEnumerable<Declaration> members)
120120
{
121-
if (_hostApplication == null)
121+
var groupedMembers = members.GroupBy(m=> m.ProjectName);
122+
foreach (var group in groupedMembers)
122123
{
123-
_hostApplication = _vbe.HostApplication();
124-
}
125-
126-
foreach (var member in members)
127-
{
128-
try
129-
{
130-
_hostApplication.Run(member);
131-
}
132-
catch (COMException ex)
124+
using (var project = _vbe.VBProjects[group.Key])
125+
using (var typeLib = TypeLibWrapper.FromVBProject(project))
133126
{
134-
Logger.Error(ex, "Unexpected COM exception while running tests.", member?.QualifiedName);
127+
foreach (var member in group)
128+
{
129+
VBETypeLibsAPI.ExecuteCode(typeLib, member.QualifiedModuleName.ComponentName,
130+
member.QualifiedName.MemberName);
131+
}
135132
}
136133
}
137134
}

RetailCoder.VBE/UnitTesting/TestMethod.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Rubberduck.UI.Controls;
1010
using Rubberduck.VBEditor;
1111
using Rubberduck.VBEditor.Application;
12+
using Rubberduck.VBEditor.ComManagement.TypeLibsAPI;
1213
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
1314

1415
namespace Rubberduck.UnitTesting
@@ -17,12 +18,12 @@ namespace Rubberduck.UnitTesting
1718
public class TestMethod : ViewModelBase, IEquatable<TestMethod>, INavigateSource
1819
{
1920
private readonly ICollection<AssertCompletedEventArgs> _assertResults = new List<AssertCompletedEventArgs>();
20-
private readonly IHostApplication _hostApp;
21+
private readonly IVBE _vbe;
2122

2223
public TestMethod(Declaration declaration, IVBE vbe)
2324
{
2425
_declaration = declaration;
25-
_hostApp = vbe.HostApplication();
26+
_vbe = vbe;
2627
}
2728

2829
private Declaration _declaration;
@@ -44,7 +45,8 @@ public void Run()
4445
try
4546
{
4647
AssertHandler.OnAssertCompleted += HandleAssertCompleted;
47-
_hostApp.Run(Declaration);
48+
VBETypeLibsAPI.ExecuteCode(_vbe, Declaration.ProjectName, Declaration.QualifiedModuleName.ComponentName,
49+
Declaration.QualifiedName.MemberName);
4850
AssertHandler.OnAssertCompleted -= HandleAssertCompleted;
4951

5052
result = EvaluateResults();

Rubberduck.Parsing/Emitter.cs

Lines changed: 0 additions & 102 deletions
This file was deleted.

Rubberduck.Parsing/Rubberduck.Parsing.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,6 @@
149149
<Compile Include="ComReflection\ComStruct.cs" />
150150
<Compile Include="ComReflection\ComType.cs" />
151151
<Compile Include="ComReflection\ComVariant.cs" />
152-
<Compile Include="Emitter.cs" />
153152
<Compile Include="Grammar\Annotations.cs" />
154153
<Compile Include="Grammar\PartialExtensions\IIdentifierContext.cs" />
155154
<Compile Include="Grammar\PartialExtensions\IAnnotatedContext.cs" />

Rubberduck.Parsing/VBA/ReferenceResolveRunnerBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
using System.Diagnostics;
99
using NLog;
1010
using Rubberduck.VBEditor.SafeComWrappers;
11-
using System.Runtime.InteropServices;
1211

1312
namespace Rubberduck.Parsing.VBA
1413
{
@@ -145,6 +144,8 @@ private Declaration SupertypeForDocument(QualifiedModuleName module, RubberduckP
145144
}
146145

147146
Declaration superType = null;
147+
// TODO: Replace with TypeLibAPI call, require a solution regarding thread synchronization or caching
148+
/*
148149
using (var properties = component.Properties)
149150
{
150151
int documentPropertyCount = 0;
@@ -194,6 +195,7 @@ private Declaration SupertypeForDocument(QualifiedModuleName module, RubberduckP
194195
}
195196
}
196197
}
198+
*/
197199

198200
return superType;
199201
}

Rubberduck.VBEEditor/Application/IHostApplication.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public interface IHostApplication : IDisposable
99
/// This should ONLY be passed a Declaration object.
1010
/// </summary>
1111
/// <param name="declaration">The Declaration object for the method to be executed.</param>
12+
[Obsolete("Use ExecuteCode in TypeLibAPI instead", true)]
1213
void Run(dynamic declaration);
1314

1415
/// <summary>
@@ -20,6 +21,7 @@ public interface IHostApplication : IDisposable
2021
/// <remarks>
2122
/// May not be available in all host applications.
2223
/// </remarks>
24+
[Obsolete("Use ExecuteCode in TypeLibAPI instead", true)]
2325
object Run(string name, params object[] args);
2426

2527
/// <summary>

0 commit comments

Comments
 (0)