Skip to content

Commit acf1c6d

Browse files
committed
all tests pass. missing mock designer.
1 parent 2139f2c commit acf1c6d

File tree

2 files changed

+55
-21
lines changed

2 files changed

+55
-21
lines changed

RubberduckTests/Mocks/MockFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ internal static Mock<VBProject> CreateProjectMock(string name, vbext_ProjectProt
233233
/// </summary>
234234
/// <param name="projectList">The collection to be iterated over.</param>
235235
/// <returns></returns>
236-
internal static Mock<VBProjects> CreateProjectsMock(List<VBProject> projectList)
236+
internal static Mock<VBProjects> CreateProjectsMock(ICollection<VBProject> projectList)
237237
{
238238
var projects = new Mock<VBProjects>();
239239
projects.Setup(p => p.GetEnumerator()).Returns(projectList.GetEnumerator());

RubberduckTests/Refactoring/RefactoringTestBase.cs

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,56 @@
11
using System.Collections.Generic;
2+
using System.Linq;
23
using Microsoft.Vbe.Interop;
34
using Microsoft.VisualStudio.TestTools.UnitTesting;
45
using Moq;
56
using Rubberduck.VBEditor;
6-
using RubberduckTests.Mocks;
77
using MockFactory = RubberduckTests.Mocks.MockFactory;
88

99
namespace RubberduckTests.Refactoring
1010
{
1111
public abstract class RefactoringTestBase
1212
{
13-
private readonly Mock<VBE> _ide;
13+
private Mock<VBE> _ide;
14+
private ICollection<VBProject> _projects;
1415

15-
protected RefactoringTestBase()
16+
[TestInitialize]
17+
public void Initialize()
1618
{
1719
_ide = MockFactory.CreateVbeMock();
20+
_ide.SetupProperty(m => m.ActiveCodePane);
21+
_ide.SetupProperty(m => m.ActiveVBProject);
22+
_ide.SetupGet(m => m.SelectedVBComponent).Returns(() => _ide.Object.ActiveCodePane.CodeModule.Parent);
23+
_ide.SetupGet(m => m.ActiveWindow).Returns(() => _ide.Object.ActiveCodePane.Window);
24+
25+
_projects = new List<VBProject>();
26+
var projects = MockFactory.CreateProjectsMock(_projects);
27+
projects.Setup(m => m.Item(It.IsAny<int>())).Returns<int>(i => _projects.ElementAt(i));
28+
29+
_ide.SetupGet(m => m.VBProjects).Returns(() => projects.Object);
30+
}
31+
32+
[TestCleanup]
33+
public void Cleanup()
34+
{
35+
_ide = null;
1836
}
1937

2038
protected QualifiedSelection GetQualifiedSelection(Selection selection)
2139
{
22-
return GetQualifiedSelection(selection, _ide.Object.ActiveVBProject.VBComponents.Item(0));
40+
if (_ide.Object.ActiveCodePane == null)
41+
{
42+
_ide.Object.ActiveVBProject = _ide.Object.VBProjects.Item(0);
43+
_ide.Object.ActiveCodePane = _ide.Object.ActiveVBProject.VBComponents.Item(0).CodeModule.CodePane;
44+
}
45+
return GetQualifiedSelection(selection, _ide.Object.ActiveCodePane.CodeModule.Parent);
2346
}
2447

2548
protected QualifiedSelection GetQualifiedSelection(Selection selection, VBComponent component)
2649
{
2750
return new QualifiedSelection(new QualifiedModuleName(component), selection);
2851
}
2952

30-
protected Mock<VBProject> SetupMockProject(string inputCode, string moduleName = null, vbext_ComponentType? componentType = null)
53+
protected Mock<VBProject> SetupMockProject(string inputCode, string projectName = null, string moduleName = null, vbext_ComponentType? componentType = null)
3154
{
3255
if (componentType == null)
3356
{
@@ -36,32 +59,40 @@ protected Mock<VBProject> SetupMockProject(string inputCode, string moduleName =
3659

3760
if (moduleName == null)
3861
{
39-
moduleName = "Module1";
62+
moduleName = componentType == vbext_ComponentType.vbext_ct_StdModule
63+
? "Module1"
64+
: componentType == vbext_ComponentType.vbext_ct_ClassModule
65+
? "Class1"
66+
: componentType == vbext_ComponentType.vbext_ct_MSForm
67+
? "Form1"
68+
: "Document1";
4069
}
4170

42-
var project = MockFactory.CreateProjectMock("VBAProject", vbext_ProjectProtection.vbext_pp_none);
43-
var component = CreateMockComponent(inputCode, moduleName, componentType.Value);
44-
var components = SetupMockComponents(new List<Mock<VBComponent>> {component}, project.Object);
71+
if (projectName == null)
72+
{
73+
projectName = "VBAProject";
74+
}
4575

46-
_ide.SetupGet(m => m.ActiveCodePane).Returns(component.Object.CodeModule.CodePane);
47-
_ide.SetupSet(vbe => vbe.ActiveCodePane = It.IsAny<CodePane>()); // todo: verify that this works as expected
48-
49-
_ide.SetupGet(m => m.ActiveVBProject).Returns(project.Object);
50-
_ide.SetupSet(vbe => vbe.ActiveVBProject = It.IsAny<VBProject>()); // todo: verify that this works as expected
76+
var component = CreateMockComponent(inputCode, moduleName, componentType.Value);
77+
var components = new List<Mock<VBComponent>> {component};
5178

52-
_ide.SetupGet(m => m.ActiveWindow).Returns(_ide.Object.ActiveCodePane.Window);
79+
var project = CreateMockProject(projectName, vbext_ProjectProtection.vbext_pp_none, components);
80+
return project;
81+
}
5382

54-
project.SetupGet(m => m.VBComponents).Returns(components.Object);
55-
components.Setup(m => m.Item(0)).Returns(component.Object);
56-
components.SetupGet(m => m.Parent).Returns(project.Object);
57-
component.SetupGet(m => m.Collection).Returns(components.Object);
83+
protected Mock<VBProject> CreateMockProject(string name, vbext_ProjectProtection protection, ICollection<Mock<VBComponent>> components)
84+
{
85+
var project = MockFactory.CreateProjectMock(name, protection);
86+
var projectComponents = SetupMockComponents(components, project.Object);
5887

5988
project.SetupGet(m => m.VBE).Returns(_ide.Object);
89+
project.SetupGet(m => m.VBComponents).Returns(projectComponents.Object);
6090

91+
_projects.Add(project.Object);
6192
return project;
6293
}
6394

64-
public Mock<VBComponent> CreateMockComponent(string content, string name, vbext_ComponentType type)
95+
protected Mock<VBComponent> CreateMockComponent(string content, string name, vbext_ComponentType type)
6596
{
6697
var module = SetupMockCodeModule(content, name);
6798
var component = MockFactory.CreateComponentMock(name, module.Object, type, _ide);
@@ -70,11 +101,14 @@ public Mock<VBComponent> CreateMockComponent(string content, string name, vbext_
70101
return component;
71102
}
72103

104+
73105
private Mock<VBComponents> SetupMockComponents(ICollection<Mock<VBComponent>> items, VBProject project)
74106
{
75107
var components = MockFactory.CreateComponentsMock(items, project);
76108
components.SetupGet(m => m.Parent).Returns(project);
77109
components.SetupGet(m => m.VBE).Returns(_ide.Object);
110+
components.Setup(m => m.Item(It.IsAny<int>())).Returns((int index) => items.ElementAt(index).Object);
111+
components.Setup(m => m.Item(It.IsAny<string>())).Returns((string name) => items.Single(e => e.Name == name).Object);
78112

79113
return components;
80114
}

0 commit comments

Comments
 (0)