Skip to content

Commit 335d3ff

Browse files
Hosch250retailcoder
authored andcommitted
Unit test tests (#1659)
* A few tests. * A bunch of unit test UI tests.
1 parent 8bb1230 commit 335d3ff

File tree

7 files changed

+441
-27
lines changed

7 files changed

+441
-27
lines changed

RetailCoder.VBE/UI/Command/MenuItems/UiDispatcher.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ public static class UiDispatcher
88
// thanks to Pellared on http://stackoverflow.com/a/12909070/1188513
99

1010
private static SynchronizationContext UiContext { get; set; }
11-
11+
1212
public static void Initialize()
1313
{
1414
if (UiContext == null)

RetailCoder.VBE/UI/UnitTesting/TestExplorerModel.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.ObjectModel;
33
using System.Linq;
44
using System.Windows.Media;
5+
using System.Windows.Threading;
56
using Microsoft.Vbe.Interop;
67
using Rubberduck.Parsing.VBA;
78
using Rubberduck.UI.Command.MenuItems;
@@ -13,12 +14,15 @@ public class TestExplorerModel : ViewModelBase
1314
{
1415
private readonly VBE _vbe;
1516
private readonly RubberduckParserState _state;
17+
private readonly Dispatcher _dispatcher;
1618

1719
public TestExplorerModel(VBE vbe, RubberduckParserState state)
1820
{
1921
_vbe = vbe;
2022
_state = state;
2123
_state.StateChanged += State_StateChanged;
24+
25+
_dispatcher = Dispatcher.CurrentDispatcher;
2226
}
2327

2428
private void State_StateChanged(object sender, ParserStateEventArgs e)
@@ -33,34 +37,38 @@ private void State_StateChanged(object sender, ParserStateEventArgs e)
3337
t.Declaration.IdentifierName == test.Declaration.IdentifierName &&
3438
t.Declaration.ProjectId == test.Declaration.ProjectId)).ToList();
3539

36-
// remove old tests
37-
foreach (var test in removedTests)
40+
_dispatcher.Invoke(() =>
3841
{
39-
UiDispatcher.Invoke(() => { Tests.Remove(test); });
40-
}
4142

42-
// update declarations for existing tests--declarations are immutable
43-
foreach (var test in Tests.Except(removedTests))
44-
{
45-
var declaration = tests.First(t =>
46-
t.Declaration.ComponentName == test.Declaration.ComponentName &&
47-
t.Declaration.IdentifierName == test.Declaration.IdentifierName &&
48-
t.Declaration.ProjectId == test.Declaration.ProjectId).Declaration;
43+
// remove old tests
44+
foreach (var test in removedTests)
45+
{
46+
Tests.Remove(test);
47+
}
4948

50-
test.SetDeclaration(declaration);
51-
}
49+
// update declarations for existing tests--declarations are immutable
50+
foreach (var test in Tests.Except(removedTests))
51+
{
52+
var declaration = tests.First(t =>
53+
t.Declaration.ComponentName == test.Declaration.ComponentName &&
54+
t.Declaration.IdentifierName == test.Declaration.IdentifierName &&
55+
t.Declaration.ProjectId == test.Declaration.ProjectId).Declaration;
5256

53-
// add new tests
54-
foreach (var test in tests)
55-
{
56-
if (!Tests.Any(t =>
57-
t.Declaration.ComponentName == test.Declaration.ComponentName &&
58-
t.Declaration.IdentifierName == test.Declaration.IdentifierName &&
59-
t.Declaration.ProjectId == test.Declaration.ProjectId))
57+
test.SetDeclaration(declaration);
58+
}
59+
60+
// add new tests
61+
foreach (var test in tests)
6062
{
61-
UiDispatcher.Invoke(() => { Tests.Add(test); });
63+
if (!Tests.Any(t =>
64+
t.Declaration.ComponentName == test.Declaration.ComponentName &&
65+
t.Declaration.IdentifierName == test.Declaration.IdentifierName &&
66+
t.Declaration.ProjectId == test.Declaration.ProjectId))
67+
{
68+
Tests.Add(test);
69+
}
6270
}
63-
}
71+
});
6472
}
6573

6674
private readonly ObservableCollection<TestMethod> _tests = new ObservableCollection<TestMethod>();

Rubberduck.VBEEditor/QualifiedModuleName.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,15 @@ private static string GetDisplayName(VBProject project)
3030
//Eg. A Workbook's parent is the application, so read the workbook's name
3131
try
3232
{
33-
var nameProperty = project.VBComponents.Cast<VBComponent>()
33+
var component = project.VBComponents.Cast<VBComponent>()
3434
.FirstOrDefault(comp => comp.Type == vbext_ComponentType.vbext_ct_Document
3535
&& comp.Properties.Item("Name").Value != null
3636
&& comp.Properties.Item("Parent")
37-
.Object.Equals(comp.Properties.Item("Application").Object))
38-
.Properties.Cast<Property>().FirstOrDefault(property => property.Name == "Name");
37+
.Object.Equals(comp.Properties.Item("Application").Object));
38+
39+
if (component == null) { return null; }
40+
41+
var nameProperty = component.Properties.Cast<Property>().FirstOrDefault(property => property.Name == "Name");
3942
return nameProperty == null
4043
? null
4144
: nameProperty.Value.ToString();

RubberduckTests/Mocks/MockProjectBuilder.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ public class MockProjectBuilder
2121
private readonly Mock<References> _vbReferences;
2222

2323
private readonly List<Mock<VBComponent>> _componentsMock = new List<Mock<VBComponent>>();
24-
//private readonly List<VBComponent> _components = new List<VBComponent>();
2524
private readonly List<Reference> _references = new List<Reference>();
2625

2726
public Mock<VBComponents> MockVBComponents
@@ -39,6 +38,11 @@ private List<VBComponent> Components
3938
get { return _componentsMock.Select(m => m.Object).ToList(); }
4039
}
4140

41+
public void RemoveComponent(Mock<VBComponent> component)
42+
{
43+
_componentsMock.Remove(component);
44+
}
45+
4246
public MockProjectBuilder(string name, string filename, vbext_ProjectProtection protection, Func<VBE> getVbe, MockVbeBuilder mockVbeBuilder)
4347
{
4448
_getVbe = getVbe;

RubberduckTests/RubberduckTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@
172172
<Compile Include="UnitTesting\AssertTests.cs" />
173173
<Compile Include="UnitTesting\EngineTests.cs" />
174174
<Compile Include="UnitTesting\DiscoveryTests.cs" />
175+
<Compile Include="UnitTesting\ViewModelTests.cs" />
175176
<Compile Include="VbeTestBase.cs" />
176177
</ItemGroup>
177178
<ItemGroup>

RubberduckTests/UnitTesting/DiscoveryTests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
using System.Linq;
2+
using System.Threading;
23
using Microsoft.Vbe.Interop;
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
45
using Moq;
56
using Rubberduck.Parsing.VBA;
7+
using Rubberduck.UI.Command.MenuItems;
8+
using Rubberduck.UI.UnitTesting;
69
using Rubberduck.UnitTesting;
710
using Rubberduck.VBEditor;
811
using Rubberduck.VBEditor.VBEHost;

0 commit comments

Comments
 (0)