Skip to content

Commit 1e84e4f

Browse files
committed
Use a user defined precompiler argument in dual binding test template and add associated test to see if reference needs to be added.
1 parent 7308be1 commit 1e84e4f

File tree

10 files changed

+101
-29
lines changed

10 files changed

+101
-29
lines changed

Rubberduck.Resources/UnitTesting/TestExplorer.Designer.cs

Lines changed: 20 additions & 10 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Rubberduck.Resources/UnitTesting/TestExplorer.cs.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
<data name="UnitTest_NewModule_RunOnce" xml:space="preserve">
226226
<value>tato metoda běží jedenkrát v modulu</value>
227227
</data>
228-
<data name="UnitTest_TotalDuration" xml:space="preserve">
228+
<data name="TestExplorer_TotalDuration" xml:space="preserve">
229229
<value>Celkové Trvání</value>
230230
</data>
231231
<data name="TestExplorer_CategoryName" xml:space="preserve">

Rubberduck.Resources/UnitTesting/TestExplorer.de.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@
216216
<data name="TestOutcome_Ignored" xml:space="preserve">
217217
<value>Ingoriert</value>
218218
</data>
219-
<data name="UnitTest_TotalDuration" xml:space="preserve">
219+
<data name="TestExplorer_TotalDuration" xml:space="preserve">
220220
<value>Gesamtdauer</value>
221221
</data>
222222
<data name="TestExplorer_RunMenuInconclusiveTests" xml:space="preserve">

Rubberduck.Resources/UnitTesting/TestExplorer.fr.resx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
<data name="Command_AddTestModule_Error" xml:space="preserve">
226226
<value>Impossible de créer le module de test. L'application hôte peut ne pas permettre la création de nouveaux modules, ou le projet peut être verrouillé. Vérifiez les journaux pour les détails.</value>
227227
</data>
228-
<data name="UnitTest_TotalDuration" xml:space="preserve">
228+
<data name="TestExplorer_TotalDuration" xml:space="preserve">
229229
<value>Durée totale</value>
230230
</data>
231231
<data name="TestExplorer_CategoryName" xml:space="preserve">

Rubberduck.Resources/UnitTesting/TestExplorer.resx

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,29 +197,36 @@
197197
</data>
198198
<data name="UnitTest_NewModule_RunOnce" xml:space="preserve">
199199
<value>this method runs once per module</value>
200+
<comment>Comment text.</comment>
200201
</data>
201202
<data name="UnitTest_NewModule_RunBeforeTest" xml:space="preserve">
202203
<value>this method runs before every test in the module</value>
204+
<comment>Comment text.</comment>
203205
</data>
204206
<data name="UnitTest_NewModule_RunAfterTest" xml:space="preserve">
205207
<value>this method runs after every test in the module</value>
208+
<comment>Comment text.</comment>
206209
</data>
207210
<data name="UnitTest_NewMethod_Rename" xml:space="preserve">
208211
<value>Rename test</value>
212+
<comment>TODO comment text.</comment>
209213
</data>
210214
<data name="UnitTest_NewMethod_RaisedTestError" xml:space="preserve">
211215
<value>Test raised an error</value>
216+
<comment>Comment text.</comment>
212217
</data>
213218
<data name="UnitTest_NewMethod_ChangeErrorNo" xml:space="preserve">
214219
<value>Change to expected error number</value>
220+
<comment>Comment text.</comment>
215221
</data>
216222
<data name="UnitTest_NewMethod_ErrorNotRaised" xml:space="preserve">
217223
<value>Expected error was not raised</value>
224+
<comment>Comment text.</comment>
218225
</data>
219226
<data name="TestOutcome_Ignored" xml:space="preserve">
220227
<value>Ignored</value>
221228
</data>
222-
<data name="UnitTest_TotalDuration" xml:space="preserve">
229+
<data name="TestExplorer_TotalDuration" xml:space="preserve">
223230
<value>Total Duration</value>
224231
</data>
225232
<data name="TestExplorer_RunMenuInconclusiveTests" xml:space="preserve">
@@ -251,44 +258,62 @@
251258
</data>
252259
<data name="UnitTest_NewMethod_Act" xml:space="preserve">
253260
<value>Act</value>
261+
<comment>"Act" text inside the comments for the "Act, Arrange, Assert" pattern.</comment>
254262
</data>
255263
<data name="UnitTest_NewMethod_Arrange" xml:space="preserve">
256264
<value>Arrange</value>
265+
<comment>"Arrange" text inside the comments for the "Act, Arrange, Assert" pattern.</comment>
257266
</data>
258267
<data name="UnitTest_NewMethod_Assert" xml:space="preserve">
259268
<value>Assert</value>
269+
<comment>"Assert" text inside the comments for the "Act, Arrange, Assert" pattern. Also a label, so localization must follow allowable identifier rules.</comment>
260270
</data>
261271
<data name="UnitTest_NewMethod_BaseName" xml:space="preserve">
262272
<value>TestMethod</value>
273+
<comment>As it "Sub TestMethod1()". Also appended to the end of Stub tests - i.e "Sub FooTestMethod()". Localize as an indentifier.</comment>
263274
</data>
264275
<data name="UnitTest_NewMethod_ExpectedError" xml:space="preserve">
265276
<value>ExpectedError</value>
277+
<comment>Constant identifier - "Const ExpectedError As Long = 0". Localize as an indentifier.</comment>
266278
</data>
267279
<data name="UnitTest_NewMethod_ModuleCleanupMethod" xml:space="preserve">
268280
<value>ModuleCleanup</value>
281+
<comment>Method name for the module cleanup method. Localize as an indentifier.</comment>
269282
</data>
270283
<data name="UnitTest_NewMethod_ModuleInitializeMethod" xml:space="preserve">
271284
<value>ModuleInitialize</value>
285+
<comment>Method name for the module initialize method. Localize as an indentifier.</comment>
272286
</data>
273287
<data name="UnitTest_NewMethod_TestCleanupMethod" xml:space="preserve">
274288
<value>TestCleanup</value>
289+
<comment>Method name for the test cleanup method. Localize as an indentifier.</comment>
275290
</data>
276291
<data name="UnitTest_NewMethod_TestExitLabel" xml:space="preserve">
277292
<value>TestExit</value>
293+
<comment>Label for exiting tests - localize as an indentifier.</comment>
278294
</data>
279295
<data name="UnitTest_NewMethod_TestFailLabel" xml:space="preserve">
280296
<value>TestFail</value>
297+
<comment>Target label for "On Error GoTo TestFail" tests - localize as an indentifier.</comment>
281298
</data>
282299
<data name="UnitTest_NewMethod_TestInitializeMethod" xml:space="preserve">
283300
<value>TestInitialize</value>
301+
<comment>Method name for the test initialization method. Localize as an indentifier.</comment>
284302
</data>
285303
<data name="UnitTest_NewModule_BaseName" xml:space="preserve">
286304
<value>TestModule</value>
305+
<comment>Test module name, i.e. "TestModule1". Localize as an indentifier.</comment>
287306
</data>
288307
<data name="UnitTest_NewModule_DefaultFolder" xml:space="preserve">
289308
<value>Tests</value>
309+
<comment>Default folder name for new test modules.</comment>
290310
</data>
291311
<data name="UnitTest_NewModule_LateBindConstant" xml:space="preserve">
292312
<value>LateBind</value>
313+
<comment>Precompiler constant as in "#If LateBind Then". Localize as an indentifier.</comment>
314+
</data>
315+
<data name="UnitTest_NewModule_LateBindDirective" xml:space="preserve">
316+
<value>LateBindTests</value>
317+
<comment>Precompiler directive, as in "#Const LateBind = LateBindTests". Localize as an indentifier.</comment>
293318
</data>
294319
</root>

Rubberduck.UnitTesting/CodeGeneration/TestCodeGenerator.cs

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using NLog;
55
using Rubberduck.Interaction;
6+
using Rubberduck.Parsing.PreProcessing;
67
using Rubberduck.Parsing.Symbols;
78
using Rubberduck.Parsing.VBA;
89
using Rubberduck.Resources.UnitTesting;
@@ -24,15 +25,24 @@ public partial class TestCodeGenerator : ITestCodeGenerator
2425
private readonly IMessageBox _messageBox;
2526
private readonly IIndenter _indenter;
2627
private readonly IVBEInteraction _interaction;
27-
28-
public TestCodeGenerator(IVBE vbe, RubberduckParserState state, IMessageBox messageBox, IVBEInteraction interaction, IConfigProvider<UnitTestSettings> settings, IIndenter indenter)
28+
private readonly ICompilationArgumentsProvider _argumentsProvider;
29+
30+
public TestCodeGenerator(
31+
IVBE vbe,
32+
RubberduckParserState state,
33+
IMessageBox messageBox,
34+
IVBEInteraction interaction,
35+
IConfigProvider<UnitTestSettings> settings,
36+
IIndenter indenter,
37+
ICompilationArgumentsProvider argumentsProvider)
2938
{
3039
_isAccess = "AccessApp".Equals(vbe?.HostApplication()?.GetType().Name);
3140
_state = state;
3241
_messageBox = messageBox;
3342
_interaction = interaction;
3443
_settings = settings;
3544
_indenter = indenter;
45+
_argumentsProvider = argumentsProvider;
3646
}
3747

3848
public void AddTestModuleToProject(IVBProject project)
@@ -54,10 +64,7 @@ private void AddTestModule(IVBProject project, Declaration stubSource)
5464

5565
var settings = _settings.Create();
5666

57-
if (settings.BindingMode == BindingMode.EarlyBinding)
58-
{
59-
_interaction.EnsureProjectReferencesUnitTesting(project);
60-
}
67+
AddReferenceIfNeeded(project, settings);
6168

6269
try
6370
{
@@ -94,6 +101,36 @@ private void AddTestModule(IVBProject project, Declaration stubSource)
94101
}
95102
}
96103

104+
private void AddReferenceIfNeeded(IVBProject project, IUnitTestSettings settings)
105+
{
106+
switch (settings.BindingMode)
107+
{
108+
case BindingMode.EarlyBinding:
109+
_interaction.EnsureProjectReferencesUnitTesting(project);
110+
return;
111+
case BindingMode.LateBinding:
112+
return;
113+
case BindingMode.DualBinding:
114+
var precompile = _argumentsProvider.UserDefinedCompilationArguments(project.ProjectId);
115+
if (precompile is null)
116+
{
117+
return;
118+
}
119+
120+
var setting = precompile.FirstOrDefault(option =>
121+
option.Key.Equals(LateBindDirectiveName, StringComparison.CurrentCultureIgnoreCase));
122+
123+
if (!string.IsNullOrEmpty(setting.Key) && setting.Value != 0)
124+
{
125+
_interaction.EnsureProjectReferencesUnitTesting(project);
126+
}
127+
128+
return;
129+
default:
130+
throw new ArgumentOutOfRangeException();
131+
}
132+
}
133+
97134
private string GetNewTestModuleCode(IVBComponent component)
98135
{
99136
var settings = _settings.Create();

Rubberduck.UnitTesting/CodeGeneration/TestCodeGeneratorStatics.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public partial class TestCodeGenerator
2727

2828
private static string AccessCompareOption => $"Option Compare Database{Environment.NewLine}";
2929
private static string LateBindConstName => TestExplorer.UnitTest_NewModule_LateBindConstant;
30+
private static string LateBindDirectiveName => TestExplorer.UnitTest_NewModule_LateBindDirective;
3031
private static string DefaultTestFolder => TestExplorer.UnitTest_NewModule_DefaultFolder;
3132
private static string ModuleInitializeMethod => TestExplorer.UnitTest_NewMethod_ModuleInitializeMethod;
3233
private static string ModuleInitializeComment => $"'{TestExplorer.UnitTest_NewModule_RunOnce}.";
@@ -87,7 +88,7 @@ End If
8788
Private Fakes As Rubberduck.FakesProvider";
8889

8990
private static string DualBindingDeclarations =>
90-
$@"#Const {LateBindConstName} = 0
91+
$@"#Const {LateBindConstName} = {LateBindDirectiveName}
9192
9293
#If {LateBindConstName} Then
9394
{LateBindingDeclarations}

Rubberduck.VBEEditor/Extensions/DictionaryExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public static IReadOnlyDictionary<TKey, IReadOnlyList<TValue>> ToReadonlyDiction
4343
}
4444

4545
//See https://stackoverflow.com/a/3804852/5536802
46-
public static bool HasEqualContent<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IDictionary<TKey, TValue> otherDictionary)
46+
public static bool HasEqualContent<TKey, TValue>(this IReadOnlyDictionary<TKey, TValue> dictionary, IReadOnlyDictionary<TKey, TValue> otherDictionary)
4747
{
4848
return dictionary.Count == otherDictionary.Count && !dictionary.Except(otherDictionary).Any();
4949
}

RubberduckTests/CodeExplorer/MockedCodeExplorer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ public void ExecuteAddUserDocumentCommand()
278278
public MockedCodeExplorer ImplementAddTestModuleCommand()
279279
{
280280
var indenter = new Indenter(null, () => IndenterSettingsTests.GetMockIndenterSettings());
281-
var codeGenerator = new TestCodeGenerator(Vbe.Object, State, MessageBox.Object, _interaction.Object, _unitTestSettingsProvider.Object, indenter);
281+
var codeGenerator = new TestCodeGenerator(Vbe.Object, State, MessageBox.Object, _interaction.Object, _unitTestSettingsProvider.Object, indenter, null);
282282
ViewModel.AddTestModuleCommand = new AddTestComponentCommand(Vbe.Object, State, codeGenerator);
283283
return this;
284284
}

RubberduckTests/Commands/UnitTestCommandTests.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,27 @@
11
using System;
22
using System.Collections.Generic;
33
using System.Linq;
4-
using System.Reflection;
54
using System.Threading;
65
using System.Windows.Input;
76
using NUnit.Framework;
87
using Moq;
9-
using Rubberduck.Inspections.Inspections.Concrete;
108
using Rubberduck.Parsing.Annotations;
119
using Rubberduck.Parsing.VBA;
12-
using Rubberduck.Settings;
1310
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
1411
using RubberduckTests.Mocks;
1512
using Rubberduck.Interaction;
1613
using Rubberduck.Navigation.CodeExplorer;
14+
using Rubberduck.Parsing.PreProcessing;
1715
using Rubberduck.Parsing.Symbols;
1816
using Rubberduck.Resources.UnitTesting;
1917
using Rubberduck.SettingsProvider;
2018
using Rubberduck.SmartIndenter;
21-
using Rubberduck.UI.CodeExplorer.Commands;
2219
using Rubberduck.VBEditor.SafeComWrappers;
2320
using Rubberduck.UI.UnitTesting.Commands;
2421
using Rubberduck.UnitTesting;
2522
using Rubberduck.UnitTesting.CodeGeneration;
2623
using Rubberduck.UnitTesting.Settings;
2724
using RubberduckTests.Settings;
28-
using Rubberduck.VBEditor.Utility;
2925

3026
namespace RubberduckTests.Commands
3127
{
@@ -464,10 +460,13 @@ private ITestCodeGenerator ArrangeCodeGenerator(IVBE vbe, RubberduckParserState
464460
{
465461
var indenter = new Indenter(null, () => IndenterSettingsTests.GetMockIndenterSettings());
466462
var settings = new Mock<IConfigProvider<UnitTestSettings>>();
463+
var arguments = new Mock<ICompilationArgumentsProvider>();
467464

468465
settings.Setup(s => s.Create()).Returns(new UnitTestSettings(BindingMode.LateBinding, AssertMode.StrictAssert, true, true, false));
466+
arguments.Setup(m => m.UserDefinedCompilationArguments(It.IsAny<string>()))
467+
.Returns(new Dictionary<string, short>());
469468

470-
return new TestCodeGenerator(vbe, state, new Mock<IMessageBox>().Object, new Mock<IVBEInteraction>().Object, settings.Object, indenter);
469+
return new TestCodeGenerator(vbe, state, new Mock<IMessageBox>().Object, new Mock<IVBEInteraction>().Object, settings.Object, indenter, arguments.Object);
471470
}
472471
}
473472
}

0 commit comments

Comments
 (0)