Skip to content

Commit 40be1a0

Browse files
committed
Merge pull request #636 from ckuhn203/unitTesting
Unit testing - Moved Reference Ensurance from TestEngine to Middle Tier
2 parents edbc1ca + e7a8451 commit 40be1a0

File tree

8 files changed

+96
-132
lines changed

8 files changed

+96
-132
lines changed

RetailCoder.VBE/UI/DockableWindowHost.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@ internal void AddUserControl(UserControl control)
4848

4949
if (control != null)
5050
{
51-
control.Dock = DockStyle.Fill;
52-
Controls.Add(control);
51+
control.Dock = DockStyle.Fill;
52+
Controls.Add(control);
5353
}
5454
AdjustSize();
5555
}

RetailCoder.VBE/UI/UnitTesting/TestExplorerDockablePresenter.cs

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,11 @@ namespace Rubberduck.UI.UnitTesting
1313
{
1414
public class TestExplorerDockablePresenter : DockablePresenterBase
1515
{
16-
private ITestExplorerWindow Control { get { return UserControl as ITestExplorerWindow; } }
17-
private GridViewSort<TestExplorerItem> _gridViewSort;
16+
private readonly GridViewSort<TestExplorerItem> _gridViewSort;
1817
private readonly ITestEngine _testEngine;
18+
private readonly ITestExplorerWindow _view;
1919

20-
public TestExplorerDockablePresenter(VBE vbe, AddIn addin, IDockableUserControl control, ITestEngine testEngine, GridViewSort<TestExplorerItem> gridViewSort)
20+
public TestExplorerDockablePresenter(VBE vbe, AddIn addin, ITestExplorerWindow control, ITestEngine testEngine, GridViewSort<TestExplorerItem> gridViewSort)
2121
: base(vbe, addin, control)
2222
{
2323
_testEngine = testEngine;
@@ -28,18 +28,19 @@ public TestExplorerDockablePresenter(VBE vbe, AddIn addin, IDockableUserControl
2828
_testEngine.MethodInitialize += TestEngineMethodInitialize;
2929
_testEngine.MethodCleanup += TestEngineMethodCleanup;
3030

31-
Control.SortColumn += SortColumn;
31+
_view = control;
32+
_view.SortColumn += SortColumn;
3233

3334
RegisterTestExplorerEvents();
3435
}
3536

3637
private void SortColumn(object sender, DataGridViewCellMouseEventArgs e)
3738
{
38-
var columnName = Control.GridView.Columns[e.ColumnIndex].Name;
39+
var columnName = _view.GridView.Columns[e.ColumnIndex].Name;
3940

4041
// type "Image" doesn't implement "IComparable", so we need to sort by the outcome instead
4142
if (columnName == RubberduckUI.Result) { columnName = RubberduckUI.Outcome; }
42-
Control.AllTests = new BindingList<TestExplorerItem>(_gridViewSort.Sort(Control.AllTests.AsEnumerable(), columnName).ToList());
43+
_view.AllTests = new BindingList<TestExplorerItem>(_gridViewSort.Sort(_view.AllTests.AsEnumerable(), columnName).ToList());
4344
}
4445

4546

@@ -70,7 +71,7 @@ private void _testEngine_ModuleInitialize(object sender, TestModuleEventArgs e)
7071
private void Synchronize()
7172
{
7273
FindAllTests();
73-
Control.Refresh(_testEngine.AllTests);
74+
_view.Refresh(_testEngine.AllTests);
7475
}
7576

7677
public override void Show()
@@ -105,15 +106,25 @@ public void RunTests()
105106

106107
public void RunTests(IEnumerable<TestMethod> tests)
107108
{
108-
Control.ClearResults();
109-
Control.SetPlayList(tests);
110-
Control.ClearProgress();
111-
_testEngine.Run(tests, VBE.ActiveVBProject);
109+
_view.ClearResults();
110+
111+
var testMethods = tests as IList<TestMethod> ?? tests.ToList(); //bypasses multiple enumeration
112+
_view.SetPlayList(testMethods);
113+
114+
_view.ClearProgress();
115+
116+
var projects = testMethods.Select(t => t.QualifiedMemberName.QualifiedModuleName.Project).Distinct();
117+
foreach (var project in projects)
118+
{
119+
project.EnsureReferenceToAddInLibrary();
120+
}
121+
122+
_testEngine.Run(testMethods);
112123
}
113124

114125
private void TestComplete(object sender, TestCompletedEventArgs e)
115126
{
116-
Control.WriteResult(e.Test, e.Result);
127+
_view.WriteResult(e.Test, e.Result);
117128
}
118129

119130
private void OnExplorerRefreshListButtonClick(object sender, EventArgs e)
@@ -219,20 +230,20 @@ private void OnExplorerAddTestModuleButtonClick(object sender, EventArgs e)
219230

220231
private void RegisterTestExplorerEvents()
221232
{
222-
Control.OnRefreshListButtonClick += OnExplorerRefreshListButtonClick;
233+
_view.OnRefreshListButtonClick += OnExplorerRefreshListButtonClick;
223234

224-
Control.OnRunAllTestsButtonClick += OnExplorerRunAllTestsButtonClick;
225-
Control.OnRunFailedTestsButtonClick += OnExplorerRunFailedTestsButtonClick;
226-
Control.OnRunLastRunTestsButtonClick += OnExplorerRunLastRunTestsButtonClick;
227-
Control.OnRunNotRunTestsButtonClick += OnExplorerRunNotRunTestsButtonClick;
228-
Control.OnRunPassedTestsButtonClick += OnExplorerRunPassedTestsButtonClick;
229-
Control.OnRunSelectedTestButtonClick += OnExplorerRunSelectedTestButtonClick;
235+
_view.OnRunAllTestsButtonClick += OnExplorerRunAllTestsButtonClick;
236+
_view.OnRunFailedTestsButtonClick += OnExplorerRunFailedTestsButtonClick;
237+
_view.OnRunLastRunTestsButtonClick += OnExplorerRunLastRunTestsButtonClick;
238+
_view.OnRunNotRunTestsButtonClick += OnExplorerRunNotRunTestsButtonClick;
239+
_view.OnRunPassedTestsButtonClick += OnExplorerRunPassedTestsButtonClick;
240+
_view.OnRunSelectedTestButtonClick += OnExplorerRunSelectedTestButtonClick;
230241

231-
Control.OnGoToSelectedTest += OnExplorerGoToSelectedTest;
242+
_view.OnGoToSelectedTest += OnExplorerGoToSelectedTest;
232243

233-
Control.OnAddExpectedErrorTestMethodButtonClick += OnExplorerAddExpectedErrorTestMethodButtonClick;
234-
Control.OnAddTestMethodButtonClick += OnExplorerAddTestMethodButtonClick;
235-
Control.OnAddTestModuleButtonClick += OnExplorerAddTestModuleButtonClick;
244+
_view.OnAddExpectedErrorTestMethodButtonClick += OnExplorerAddExpectedErrorTestMethodButtonClick;
245+
_view.OnAddTestMethodButtonClick += OnExplorerAddTestMethodButtonClick;
246+
_view.OnAddTestModuleButtonClick += OnExplorerAddTestModuleButtonClick;
236247

237248
_testEngine.TestComplete += TestComplete;
238249
}

RetailCoder.VBE/UnitTesting/ITestEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public interface ITestEngine
1717
event EventHandler<TestModuleEventArgs> ModuleCleanup;
1818
event EventHandler<TestModuleEventArgs> MethodInitialize;
1919
event EventHandler<TestModuleEventArgs> MethodCleanup;
20-
void Run(IEnumerable<TestMethod> tests, VBProject vbe);
20+
void Run(IEnumerable<TestMethod> tests);
2121

2222
event EventHandler<TestCompletedEventArgs> TestComplete;
2323
}

RetailCoder.VBE/UnitTesting/TestEngine.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,8 @@ private void RunMethodCleanup(QualifiedModuleName qualifiedModuleName)
8282
}
8383
}
8484

85-
public void Run(IEnumerable<TestMethod> tests, VBProject project)
85+
public void Run(IEnumerable<TestMethod> tests)
8686
{
87-
//todo: move this to the "UI" layer. This code doesn't have to run for COM clients.
88-
// COM clients will have to either already have a good reference, or be late bound.
89-
// This is problematic for late bound code, because now we've *forced* them into early binding.
90-
project.EnsureReferenceToAddInLibrary();
91-
9287
var testMethods = tests as IList<TestMethod> ?? tests.ToList();
9388
if (!testMethods.Any()) return;
9489

RetailCoder.VBE/UnitTesting/TestRunner.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ private void LoadAllTests(VBE vbe)
6464
public string RunAllTests(VBE vbe, string outputFilePath = null)
6565
{
6666
LoadAllTests(vbe);
67-
_engine.Run(_engine.AllTests.Keys, vbe.ActiveVBProject);
67+
_engine.Run(_engine.AllTests.Keys);
6868

6969
var results = OutputToString();
7070

RubberduckTests/Mocks/MockFactory.cs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ internal static Mock<VBComponent> CreateComponentMock(string name, CodeModule co
9999
}
100100

101101
/// <summary>
102-
/// Creates a new <see cref="Mock{VBComponets}"/> that can be iterated over as an <see cref="IEnumerable"/>.
102+
/// Creates a new <see cref="Mock{VBComponents}"/> that can be iterated over as an <see cref="IEnumerable"/>.
103103
/// </summary>
104104
/// <param name="componentList">The collection to be iterated over.</param>
105105
/// <returns></returns>
@@ -113,7 +113,7 @@ internal static Mock<VBComponents> CreateComponentsMock(List<VBComponent> compon
113113
}
114114

115115
/// <summary>
116-
/// Creates a new <see cref="Mock{VBComponets}"/> that can be iterated over as an <see cref="IEnumerable"/>.
116+
/// Creates a new <see cref="Mock{VBComponents}"/> that can be iterated over as an <see cref="IEnumerable"/>.
117117
/// </summary>
118118
/// <param name="componentList">The collection to be iterated over.</param>
119119
/// <param name="project">The <see cref="VBComponents.Parent"/> property.</param>
@@ -157,5 +157,54 @@ internal static Mock<VBProjects> CreateProjectsMock(List<VBProject> projectList)
157157

158158
return projects;
159159
}
160+
161+
//internal static Mock<VBProjects> CreateProjectsMock(List<VBProject> projectList, VBProject project, VBComponents components)
162+
//{
163+
// CreateProjectsMock(projectList, project);
164+
// project.SetupGet(p => p.VBComponents).Returns(components.Object);
165+
// return projects;
166+
//}
167+
168+
/// <summary>
169+
/// Creates a new <see cref="Mock{Reference}"/>.
170+
/// </summary>
171+
/// <param name="name">The see<see cref="Reference.Name"/>.</param>
172+
/// <param name="filePath">The <see cref="Reference.FullPath"/> filepath.</param>
173+
/// <returns></returns>
174+
internal static Mock<Reference> CreateMockReference(string name, string filePath)
175+
{
176+
var reference = new Mock<Reference>();
177+
reference.SetupGet(r => r.Name).Returns(name);
178+
reference.SetupGet(r => r.FullPath).Returns(filePath);
179+
180+
return reference;
181+
}
182+
183+
/// <summary>
184+
/// Creates a new <see cref="Mock{References}"/> collection that can be iterated over as an <see cref="IEnumerable"/>.
185+
/// </summary>
186+
/// <param name="referenceList">The collection to be iterated over.</param>
187+
/// <returns></returns>
188+
internal static Mock<References> CreateReferencesMock(List<Reference> referenceList)
189+
{
190+
var references = new Mock<References>();
191+
references.Setup(r => r.GetEnumerator()).Returns(referenceList.GetEnumerator());
192+
references.As<IEnumerable>().Setup(r => r.GetEnumerator()).Returns(referenceList.GetEnumerator());
193+
return references;
194+
}
195+
196+
/// <summary>
197+
/// Creates a new <see cref="Mock{Project}"/> that is set up with a <see cref="References"/> collection.
198+
/// </summary>
199+
/// <param name="name">The <see cref="VBProject"/> <see cref="VBProject.Name"/>.</param>
200+
/// <param name="references">The <see cref="References"/> collection.</param>
201+
/// <returns></returns>
202+
internal static Mock<VBProject> CreateProjectMock(string name, Mock<References> references)
203+
{
204+
var project = new Mock<VBProject>();
205+
project.SetupProperty(p => p.Name, name);
206+
project.SetupGet(p => p.References).Returns(references.Object);
207+
return project;
208+
}
160209
}
161210
}

RubberduckTests/RubberduckTests.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,6 @@
186186
<Compile Include="StringExtensionsTests.cs" />
187187
<Compile Include="Properties\AssemblyInfo.cs" />
188188
<Compile Include="ConfigurationTests.cs" />
189-
<Compile Include="TodoExplorerTests.cs" />
190189
<Compile Include="UnitTesting\AssertTests.cs" />
191190
<Compile Include="CodeInspectionTests.cs" />
192191
<Compile Include="TodoControllerTests.cs" />

0 commit comments

Comments
 (0)