Skip to content

Commit e814efb

Browse files
committed
Added TodoDockablePresenter Test
1 parent 08ca1f4 commit e814efb

File tree

8 files changed

+210
-34
lines changed

8 files changed

+210
-34
lines changed

RetailCoder.VBE/UI/ToDoItems/ToDoExplorerDockablePresenter.cs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,20 @@ public class ToDoExplorerDockablePresenter : DockablePresenterBase
2020
{
2121
private readonly IRubberduckParser _parser;
2222
private readonly IEnumerable<ToDoMarker> _markers;
23-
private GridViewSort<ToDoItem> _gridViewSort;
24-
private IToDoExplorerWindow Control { get { return UserControl as IToDoExplorerWindow; } }
23+
private readonly GridViewSort<ToDoItem> _gridViewSort;
24+
private readonly IToDoExplorerWindow _view;
2525

2626
public ToDoExplorerDockablePresenter(IRubberduckParser parser, IEnumerable<ToDoMarker> markers, VBE vbe, AddIn addin, IToDoExplorerWindow window, GridViewSort<ToDoItem> gridViewSort)
2727
: base(vbe, addin, window)
2828
{
2929
_parser = parser;
3030
_markers = markers;
3131
_gridViewSort = gridViewSort;
32-
Control.NavigateToDoItem += NavigateToDoItem;
33-
Control.RefreshToDoItems += RefreshToDoList;
34-
Control.SortColumn += SortColumn;
32+
33+
_view = window;
34+
_view.NavigateToDoItem += NavigateToDoItem;
35+
_view.RefreshToDoItems += RefreshToDoList;
36+
_view.SortColumn += SortColumn;
3537
}
3638

3739
public override void Show()
@@ -45,8 +47,9 @@ public async void Refresh()
4547
try
4648
{
4749
Cursor.Current = Cursors.WaitCursor;
48-
Control.TodoItems = await GetItems();
50+
_view.TodoItems = await GetItems();
4951
}
52+
//wtf? why are we catching everything without even so much as leaving a comment as to why?
5053
catch { }
5154

5255
Cursor.Current = Cursors.Default;
@@ -59,9 +62,9 @@ private void RefreshToDoList(object sender, EventArgs e)
5962

6063
private void SortColumn(object sender, DataGridViewCellMouseEventArgs e)
6164
{
62-
var columnName = Control.GridView.Columns[e.ColumnIndex].Name;
65+
var columnName = _view.GridView.Columns[e.ColumnIndex].Name;
6366

64-
Control.TodoItems = _gridViewSort.Sort(Control.TodoItems, columnName);
67+
_view.TodoItems = _gridViewSort.Sort(_view.TodoItems, columnName);
6568
}
6669

6770
private async Task<IOrderedEnumerable<ToDoItem>> GetItems()

RetailCoder.VBE/UI/ToDoItems/ToDoExplorerWindow.cs

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
using System;
22
using System.Collections.Generic;
33
using System.ComponentModel;
4+
using System.Diagnostics.CodeAnalysis;
45
using System.Linq;
56
using System.Windows.Forms;
67
using Rubberduck.ToDoItems;
78

89
namespace Rubberduck.UI.ToDoItems
910
{
11+
[ExcludeFromCodeCoverage]
1012
public partial class ToDoExplorerWindow : UserControl, IToDoExplorerWindow
1113
{
1214
private const string ClassId = "8B071EDA-2C9C-4009-9A22-A1958BF98B28";
@@ -66,38 +68,32 @@ private void ToDoGridViewCellDoubleClicked(object sender, DataGridViewCellEventA
6668
}
6769

6870
var handler = NavigateToDoItem;
69-
if (handler == null)
71+
if (handler != null)
7072
{
71-
return;
73+
var item = (ToDoItem)todoItemsGridView[e.ColumnIndex, e.RowIndex].OwningRow.DataBoundItem;
74+
var args = new ToDoItemClickEventArgs(item);
75+
handler(this, args);
7276
}
73-
74-
var item = (ToDoItem)todoItemsGridView[e.ColumnIndex, e.RowIndex].OwningRow.DataBoundItem;
75-
var args = new ToDoItemClickEventArgs(item);
76-
handler(this, args);
7777
}
7878

7979
public event EventHandler RefreshToDoItems;
8080
private void RefreshButtonClicked(object sender, EventArgs e)
8181
{
8282
var handler = RefreshToDoItems;
83-
if (handler == null)
83+
if (handler != null)
8484
{
85-
return;
86-
}
87-
88-
handler(this, EventArgs.Empty);
85+
handler(this, EventArgs.Empty);
86+
}
8987
}
9088

9189
public event EventHandler<DataGridViewCellMouseEventArgs> SortColumn;
9290
private void ColumnHeaderMouseClicked(object sender, DataGridViewCellMouseEventArgs e)
9391
{
9492
var handler = SortColumn;
95-
if (handler == null)
93+
if (handler != null)
9694
{
97-
return;
98-
}
99-
100-
handler(this, e);
95+
handler(this, e);
96+
}
10197
}
10298
}
10399
}

Rubberduck.Parsing/VBA/RubberduckParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ private void parseResult_Progress(object sender, ResolutionProgressEventArgs e)
9494
OnResolveProgress(e.Component);
9595
}
9696

97-
private IParseTree Parse(string code, out ITokenStream outStream)
97+
public IParseTree Parse(string code, out ITokenStream outStream)
9898
{
9999
var input = new AntlrInputStream(code);
100100
var lexer = new VBALexer(input);

RubberduckTests/Mocks/MockFactory.cs

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
using System;
2+
using System.Collections;
3+
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.Vbe.Interop;
8+
using Moq;
9+
10+
namespace RubberduckTests.Mocks
11+
{
12+
static class MockFactory
13+
{
14+
internal static Mock<Window> CreateWindowMock()
15+
{
16+
var window = new Mock<Window>();
17+
window.SetupProperty(w => w.Visible, false);
18+
window.SetupGet(w => w.LinkedWindows).Returns((LinkedWindows) null);
19+
window.SetupProperty(w => w.Height);
20+
window.SetupProperty(w => w.Width);
21+
22+
return window;
23+
}
24+
25+
internal static Mock<VBE> CreateVbeMock(Windows windows)
26+
{
27+
var vbe = new Mock<VBE>();
28+
vbe.Setup(v => v.Windows).Returns(windows);
29+
30+
return vbe;
31+
}
32+
33+
internal static Mock<VBE> CreateVbeMock(Windows windows, VBProjects projects)
34+
{
35+
var vbe = CreateVbeMock(windows);
36+
vbe.SetupGet(v => v.VBProjects).Returns(projects);
37+
38+
return vbe;
39+
}
40+
41+
internal static Mock<CodeModule> CreateCodeModuleMock(string code)
42+
{
43+
var lineCount = code.Split(new string[] { Environment.NewLine }, StringSplitOptions.None).Length;
44+
45+
var codeModule = new Mock<CodeModule>();
46+
codeModule.SetupGet(c => c.CountOfLines).Returns(lineCount);
47+
codeModule.SetupGet(c => c.get_Lines(1, lineCount)).Returns(code);
48+
return codeModule;
49+
}
50+
51+
internal static Mock<VBComponent> CreateComponentMock(string name, CodeModule codeModule, vbext_ComponentType componentType)
52+
{
53+
var component = new Mock<VBComponent>();
54+
component.SetupProperty(c => c.Name, name);
55+
component.SetupGet(c => c.CodeModule).Returns(codeModule);
56+
component.SetupGet(c => c.Type).Returns(componentType);
57+
return component;
58+
}
59+
60+
internal static Mock<VBComponents> CreateComponentsMock(List<VBComponent> componentList, VBProject project)
61+
{
62+
var components = new Mock<VBComponents>();
63+
components.Setup(c => c.GetEnumerator()).Returns(componentList.GetEnumerator());
64+
components.As<IEnumerable>().Setup(c => c.GetEnumerator()).Returns(componentList.GetEnumerator());
65+
components.SetupGet(c => c.Parent).Returns(project);
66+
67+
return components;
68+
}
69+
70+
internal static Mock<VBProject> CreateProjectMock(string name, vbext_ProjectProtection protectionLevel)
71+
{
72+
var project = new Mock<VBProject>();
73+
project.SetupProperty(p => p.Name, name);
74+
project.SetupGet(p => p.Protection).Returns(protectionLevel);
75+
return project;
76+
}
77+
78+
internal static Mock<VBProjects> CreateProjectsMock(List<VBProject> projectList, VBProject project)
79+
{
80+
var projects = new Mock<VBProjects>();
81+
projects.Setup(p => p.GetEnumerator()).Returns(projectList.GetEnumerator());
82+
projects.As<IEnumerable>().Setup(p => p.GetEnumerator()).Returns(projectList.GetEnumerator());
83+
84+
return projects;
85+
}
86+
87+
//internal static Mock<VBProjects> CreateProjectsMock(List<VBProject> projectList, VBProject project, VBComponents components)
88+
//{
89+
// CreateProjectsMock(projectList, project);
90+
// project.SetupGet(p => p.VBComponents).Returns(components.Object);
91+
// return projects;
92+
//}
93+
}
94+
}

RubberduckTests/RubberduckTests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@
145145
<Reference Include="Moq">
146146
<HintPath>..\packages\Moq.4.2.1409.1722\lib\net40\Moq.dll</HintPath>
147147
</Reference>
148+
<Reference Include="NLog, Version=3.2.1.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
149+
<HintPath>..\packages\NLog.3.2.1\lib\net45\NLog.dll</HintPath>
150+
<Private>True</Private>
151+
</Reference>
148152
<Reference Include="Office, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c">
149153
<SpecificVersion>False</SpecificVersion>
150154
<EmbedInteropTypes>True</EmbedInteropTypes>
@@ -172,6 +176,7 @@
172176
</Otherwise>
173177
</Choose>
174178
<ItemGroup>
179+
<Compile Include="Mocks\MockFactory.cs" />
175180
<Compile Include="Mocks\MockTodoSettingsView.cs" />
176181
<Compile Include="Mocks\MockWindowsCollection.cs" />
177182
<Compile Include="SourceControl\ChangesPresenterTests.cs" />
@@ -181,6 +186,7 @@
181186
<Compile Include="StringExtensionsTests.cs" />
182187
<Compile Include="Properties\AssemblyInfo.cs" />
183188
<Compile Include="ConfigurationTests.cs" />
189+
<Compile Include="TodoExplorerTests.cs" />
184190
<Compile Include="UnitTesting\AssertTests.cs" />
185191
<Compile Include="CodeInspectionTests.cs" />
186192
<Compile Include="TodoControllerTests.cs" />

RubberduckTests/SourceControl/SCPresenterTests.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,9 @@ public class ScPresenterTests
4343
[TestInitialize]
4444
public void InitializeMocks()
4545
{
46-
_window = new Mock<Window>();
47-
_window.SetupProperty(w => w.Visible, false);
48-
_window.SetupGet(w => w.LinkedWindows).Returns((LinkedWindows)null);
49-
_window.SetupProperty(w => w.Height);
50-
_window.SetupProperty(w => w.Width);
51-
46+
_window = Mocks.MockFactory.CreateWindowMock();
5247
_windows = new MockWindowsCollection(_window.Object);
53-
54-
_vbe = new Mock<VBE>();
55-
_vbe.Setup(v => v.Windows).Returns(_windows);
48+
_vbe = Mocks.MockFactory.CreateVbeMock(_windows);
5649

5750
_addIn = new Mock<AddIn>();
5851

RubberduckTests/TodoExplorerTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Microsoft.Vbe.Interop;
4+
using Microsoft.VisualStudio.TestTools.UnitTesting;
5+
using Moq;
6+
using Rubberduck.Parsing.VBA;
7+
using Rubberduck.Settings;
8+
using Rubberduck.ToDoItems;
9+
using Rubberduck.UI;
10+
using Rubberduck.UI.ToDoItems;
11+
using RubberduckTests.Mocks;
12+
using RdMockFacotry = RubberduckTests.Mocks.MockFactory;
13+
14+
namespace RubberduckTests
15+
{
16+
[TestClass]
17+
public class TodoExplorerTests
18+
{
19+
private Mock<AddIn> _addin;
20+
private Mock<IToDoExplorerWindow> _view;
21+
private Mock<Window> _window;
22+
private MockWindowsCollection _windows;
23+
private Mock<VBE> _vbe;
24+
private ConfigurationLoader _loader;
25+
private ToDoMarker[] _markers;
26+
private GridViewSort<ToDoItem> _gridViewSorter;
27+
28+
[TestInitialize]
29+
public void Intialize()
30+
{
31+
_addin = new Mock<AddIn>();
32+
_view = new Mock<IToDoExplorerWindow>();
33+
34+
_window = RdMockFacotry.CreateWindowMock();
35+
_windows = new MockWindowsCollection(_window.Object);
36+
37+
_loader = new ConfigurationLoader();
38+
_markers = _loader.GetDefaultTodoMarkers();
39+
40+
_gridViewSorter = new GridViewSort<ToDoItem>("Priority", false);
41+
42+
}
43+
44+
[TestMethod]
45+
public void TodoPresenter_RefreshUpdatesViewItems()
46+
{
47+
var code = @"
48+
Public Sub Bazzer()
49+
'Todo: Fix the foobarred bazzer.
50+
End Sub";
51+
52+
var codeModule = RdMockFacotry.CreateCodeModuleMock(code);
53+
54+
var component = RdMockFacotry.CreateComponentMock("Module1", codeModule.Object, vbext_ComponentType.vbext_ct_StdModule);
55+
56+
var project = RdMockFacotry.CreateProjectMock("VBAProject", vbext_ProjectProtection.vbext_pp_none);
57+
58+
var componentList = new List<VBComponent>() { component.Object };
59+
var components = RdMockFacotry.CreateComponentsMock(componentList, project.Object);
60+
61+
component.SetupGet(c => c.Collection).Returns(components.Object);
62+
63+
var projectList = new List<VBProject>() {project.Object};
64+
65+
var projects = RdMockFacotry.CreateProjectsMock(projectList, project.Object);
66+
project.SetupGet(p => p.VBComponents).Returns(components.Object);
67+
68+
_vbe = RdMockFacotry.CreateVbeMock(_windows, projects.Object);
69+
70+
_view.SetupProperty(v => v.TodoItems);
71+
72+
var parser = new RubberduckParser();
73+
74+
var presenter = new ToDoExplorerDockablePresenter(parser, _markers, _vbe.Object, _addin.Object, _view.Object, _gridViewSorter);
75+
76+
//act
77+
presenter.Refresh();
78+
79+
//assert
80+
Assert.AreEqual("Todo: Fix the foobarred bazzer.", _view.Object.TodoItems.First().Description);
81+
}
82+
}
83+
}

RubberduckTests/packages.config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
<packages>
33
<package id="LibGit2Sharp" version="0.22.0-pre20150516171636" targetFramework="net45" />
44
<package id="Moq" version="4.2.1409.1722" targetFramework="net45" />
5+
<package id="NLog" version="3.2.1" targetFramework="net45" />
56
</packages>

0 commit comments

Comments
 (0)