Skip to content

Commit 4adf94f

Browse files
committed
Merge branch 'next'
2 parents f3c0609 + e12dbd8 commit 4adf94f

File tree

3 files changed

+275
-10
lines changed

3 files changed

+275
-10
lines changed

Rubberduck.Parsing/Rubberduck.Parsing.csproj

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,6 @@
4242
<Reference Include="Antlr4.Runtime.net45">
4343
<HintPath>..\packages\Antlr4.Runtime.4.3.0\lib\net45\Antlr4.Runtime.net45.dll</HintPath>
4444
</Reference>
45-
<Reference Include="Microsoft.Vbe.Interop, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
46-
<EmbedInteropTypes>True</EmbedInteropTypes>
47-
</Reference>
4845
<Reference Include="System" />
4946
<Reference Include="System.Core" />
5047
<Reference Include="System.Windows.Forms" />
@@ -102,7 +99,26 @@
10299
<CustomToolNamespace>Rubberduck.Parsing.Grammar</CustomToolNamespace>
103100
</EmbeddedResource>
104101
</ItemGroup>
105-
<ItemGroup />
102+
<ItemGroup>
103+
<COMReference Include="Microsoft.Office.Core">
104+
<Guid>{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}</Guid>
105+
<VersionMajor>2</VersionMajor>
106+
<VersionMinor>4</VersionMinor>
107+
<Lcid>0</Lcid>
108+
<WrapperTool>primary</WrapperTool>
109+
<Isolated>False</Isolated>
110+
<EmbedInteropTypes>True</EmbedInteropTypes>
111+
</COMReference>
112+
<COMReference Include="VBIDE">
113+
<Guid>{0002E157-0000-0000-C000-000000000046}</Guid>
114+
<VersionMajor>5</VersionMajor>
115+
<VersionMinor>3</VersionMinor>
116+
<Lcid>0</Lcid>
117+
<WrapperTool>primary</WrapperTool>
118+
<Isolated>False</Isolated>
119+
<EmbedInteropTypes>True</EmbedInteropTypes>
120+
</COMReference>
121+
</ItemGroup>
106122
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
107123
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
108124
<PropertyGroup>

RubberduckTests/RubberduckTests.csproj

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,14 @@
133133
<Reference Include="Moq">
134134
<HintPath>..\packages\Moq.4.2.1409.1722\lib\net40\Moq.dll</HintPath>
135135
</Reference>
136+
<Reference Include="Rubberduck">
137+
<HintPath>..\RetailCoder.VBE\bin\Debug\Rubberduck.dll</HintPath>
138+
</Reference>
139+
<Reference Include="Rubberduck.Parsing">
140+
<HintPath>..\Rubberduck.Parsing\bin\Debug\Rubberduck.Parsing.dll</HintPath>
141+
</Reference>
136142
<Reference Include="System" />
143+
<Reference Include="System.Windows.Forms" />
137144
</ItemGroup>
138145
<Choose>
139146
<When Condition="('$(VisualStudioVersion)' == '10.0' or '$(VisualStudioVersion)' == '') and '$(TargetFrameworkVersion)' == 'v3.5'">
@@ -157,12 +164,7 @@
157164
<Compile Include="TodoControllerTests.cs" />
158165
<Compile Include="ComponentTypeExtensionTests.cs" />
159166
<Compile Include="SourceControlTests.cs" />
160-
</ItemGroup>
161-
<ItemGroup>
162-
<ProjectReference Include="..\RetailCoder.VBE\Rubberduck.csproj">
163-
<Project>{20589de8-432e-4359-9232-69eb070b7185}</Project>
164-
<Name>Rubberduck</Name>
165-
</ProjectReference>
167+
<Compile Include="UI\Refactorings\Rename\RenamePresenterTests.cs" />
166168
</ItemGroup>
167169
<ItemGroup>
168170
<COMReference Include="Microsoft.Office.Core">
Lines changed: 247 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,247 @@
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using System.Windows.Forms;
4+
using Antlr4.Runtime;
5+
using Microsoft.Vbe.Interop;
6+
using Microsoft.VisualStudio.TestTools.UnitTesting;
7+
using Moq;
8+
using Rubberduck.Parsing.Symbols;
9+
using Rubberduck.UI.Refactorings.Rename;
10+
using Parsing = Rubberduck.Parsing;
11+
12+
namespace RubberduckTests.UI.Refactorings.Rename
13+
{
14+
[TestClass]
15+
public class RenamePresenterTests
16+
{
17+
private static Mock<VBE> _vbe;
18+
private static Mock<VBProject> _vbProject;
19+
private static Declarations _declarations;
20+
private static Parsing.QualifiedModuleName _module;
21+
private static Mock<IRenameView> _view;
22+
private List<Declaration> _listDeclarations;
23+
24+
[TestInitialize]
25+
public void TestInitialization()
26+
{
27+
_vbe = new Mock<VBE>();
28+
_vbProject = new Mock<VBProject>();
29+
_declarations = new Declarations();
30+
_module = new Parsing.QualifiedModuleName();
31+
_view = new Mock<IRenameView>();
32+
}
33+
34+
/// <summary>Common method for adding declaration with some default values</summary>
35+
private void AddDeclarationItem(IMock<ParserRuleContext> context,
36+
Parsing.Selection selection,
37+
Parsing.QualifiedMemberName? qualifiedName = null,
38+
DeclarationType declarationType = DeclarationType.Project,
39+
string identifierName = "identifierName")
40+
{
41+
Declaration declarationItem = null;
42+
var qualName = qualifiedName ?? new Parsing.QualifiedMemberName(_module, "fakeModule");
43+
44+
declarationItem = new Declaration(
45+
qualifiedName:qualName,
46+
parentScope:"module.proc",
47+
identifierName: identifierName,
48+
asTypeName: "asTypeName",
49+
isSelfAssigned: false,
50+
isWithEvents: false,
51+
accessibility: Accessibility.Public,
52+
declarationType: declarationType,
53+
context: context.Object,
54+
selection: selection
55+
);
56+
57+
_declarations.Add(declarationItem);
58+
if (_listDeclarations == null) _listDeclarations = new List<Declaration>();
59+
_listDeclarations.Add(declarationItem);
60+
}
61+
62+
/// <summary>Common method for adding a reference to given declaration item</summary>
63+
private static void AddReference(Declaration itemToAdd, IdentifierReference reference)
64+
{
65+
var declaration = _declarations.Items.ToList().FirstOrDefault(x => x.Equals(itemToAdd));
66+
if (declaration == null) return;
67+
68+
declaration.AddReference(reference);
69+
}
70+
71+
[TestMethod]
72+
public void ConstructorWorks_IsNotNull()
73+
{
74+
// arange
75+
var symbolSelection = new Parsing.Selection(1, 1, 2, 2);
76+
var qualifiedSelection = new Parsing.QualifiedSelection(_module, symbolSelection);
77+
78+
//act
79+
var presenter = new RenamePresenter(_vbe.Object, _view.Object, _declarations, qualifiedSelection);
80+
81+
//assert
82+
Assert.IsNotNull(presenter, "Successfully initialized");
83+
}
84+
85+
[TestMethod]
86+
public void NoTargetFound()
87+
{
88+
// arange
89+
var symbolSelection = new Parsing.Selection(1, 1, 2, 2);
90+
var qualifiedSelection = new Parsing.QualifiedSelection(_module, symbolSelection);
91+
92+
var context = new Mock<ParserRuleContext>();
93+
AddDeclarationItem(context, symbolSelection);
94+
_view.Setup(form => form.ShowDialog()).Returns(DialogResult.Cancel);
95+
96+
//act
97+
var presenter = new RenamePresenter(_vbe.Object, _view.Object, _declarations, qualifiedSelection);
98+
presenter.Show();
99+
100+
//assert
101+
Assert.IsNull(_view.Object.Target, "No Target was found");
102+
}
103+
104+
[TestMethod]
105+
public void AcquireTarget_ProcedureRenaming_TargetIsNotNull()
106+
{
107+
// arange
108+
var symbolSelection = new Parsing.Selection(1, 1, 2, 4);
109+
var qualifiedSelection = new Parsing.QualifiedSelection(_module, symbolSelection);
110+
111+
// just for passing null reference exception
112+
var context = new Mock<ParserRuleContext>();
113+
context.SetupGet(c => c.Start.Line).Returns(1);
114+
context.SetupGet(c => c.Stop.Line).Returns(2);
115+
context.SetupGet(c => c.Stop.Text).Returns("Four");
116+
117+
// setting a declaration item as a procedure that will be renamed
118+
const string identifierName = "AProcedure";
119+
AddDeclarationItem(context, symbolSelection, null, DeclarationType.Procedure, identifierName);
120+
121+
// allow Moq to set the Target property
122+
_view.Setup(view => view.ShowDialog()).Returns(DialogResult.Cancel);
123+
_view.SetupProperty(view => view.Target);
124+
125+
//act
126+
var presenter = new RenamePresenter(_vbe.Object, _view.Object, _declarations, qualifiedSelection);
127+
presenter.Show();
128+
129+
//assert
130+
Assert.IsNotNull(_view.Object.Target, "A target was found");
131+
Assert.AreEqual(identifierName, _view.Object.Target.IdentifierName);
132+
}
133+
134+
[TestMethod]
135+
public void AcquireTarget_ModuleRenaming_TargetIsNotNull()
136+
{
137+
// arange
138+
var symbolSelection = new Parsing.Selection(1, 1, 2, 4);
139+
var qualifiedSelection = new Parsing.QualifiedSelection(_module, symbolSelection);
140+
141+
// just for passing null reference exception
142+
var context = new Mock<ParserRuleContext>();
143+
context.SetupGet(c => c.Start.Line).Returns(1);
144+
context.SetupGet(c => c.Stop.Line).Returns(2);
145+
context.SetupGet(c => c.Stop.Text).Returns("Four");
146+
147+
// setting a declaration item as a module that will be renamed
148+
const string identifierName = "FakeModule";
149+
AddDeclarationItem(context, symbolSelection, null, DeclarationType.Module, identifierName);
150+
151+
// allow Moq to set the Target property
152+
_view.Setup(view => view.ShowDialog()).Returns(DialogResult.Cancel);
153+
_view.SetupProperty(view => view.Target);
154+
155+
//act
156+
var presenter = new RenamePresenter(_vbe.Object, _view.Object, _declarations, qualifiedSelection);
157+
presenter.Show();
158+
159+
//assert
160+
Assert.IsNotNull(_view.Object.Target, "A target was found");
161+
Assert.AreEqual(identifierName, _view.Object.Target.IdentifierName);
162+
}
163+
164+
[TestMethod]
165+
public void AcquireTarget_MethodRenamingAtSameComponent_CorrectTargetChosen()
166+
{
167+
// arange
168+
var symbolSelection = new Parsing.Selection(8, 1, 8, 16);
169+
var selectedComponent = new Parsing.QualifiedModuleName("TestProject", "TestModule", _vbProject.Object, 1);
170+
var qualifiedSelection = new Parsing.QualifiedSelection(selectedComponent, symbolSelection);
171+
172+
// just for passing null reference exception
173+
var context = new Mock<ParserRuleContext>();
174+
context.SetupGet(c => c.Start.Line).Returns(1);
175+
context.SetupGet(c => c.Stop.Line).Returns(2);
176+
context.SetupGet(c => c.Stop.Text).Returns("Four");
177+
178+
// simulate all the components and symbols
179+
var member = new Parsing.QualifiedMemberName(selectedComponent, "fakeModule");
180+
const string identifierName = "Foo";
181+
AddDeclarationItem(context, symbolSelection, member, DeclarationType.Procedure, identifierName);
182+
AddDeclarationItem(context, new Parsing.Selection(1, 1, 1, 16), member, DeclarationType.Procedure);
183+
AddDeclarationItem(context, new Parsing.Selection(1, 1, 1, 1), member, DeclarationType.Module);
184+
185+
// allow Moq to set the Target property
186+
_view.Setup(view => view.ShowDialog()).Returns(DialogResult.Cancel);
187+
_view.SetupProperty(view => view.Target);
188+
189+
//act
190+
var presenter = new RenamePresenter(_vbe.Object, _view.Object, _declarations, qualifiedSelection);
191+
presenter.Show();
192+
193+
//assert
194+
var retVal = _view.Object.Target;
195+
Assert.AreEqual(symbolSelection, retVal.Selection, "Returns only the declaration on the desired selection");
196+
Assert.AreEqual(identifierName, retVal.IdentifierName);
197+
}
198+
199+
[TestMethod]
200+
public void AcquireTarget_MethodRenamingMoreComponents_CorrectTargetChosen()
201+
{
202+
// arange
203+
// initial selection
204+
var symbolSelection = new Parsing.Selection(4, 5, 4, 8);
205+
var selectedComponent = new Parsing.QualifiedModuleName("TestProject", "Module1", _vbProject.Object, 1);
206+
var qualifiedSelection = new Parsing.QualifiedSelection(selectedComponent, symbolSelection);
207+
208+
// just for passing null reference exception
209+
var context = new Mock<ParserRuleContext>();
210+
context.SetupGet(c => c.Start.Line).Returns(-1);
211+
context.SetupGet(c => c.Stop.Line).Returns(-1);
212+
context.SetupGet(c => c.Stop.Text).Returns("Fake");
213+
214+
// simulate all the components and symbols
215+
IdentifierReference reference;
216+
var differentComponent = new Parsing.QualifiedModuleName("TestProject", "Module2", _vbProject.Object, 1);
217+
var differentMember = new Parsing.QualifiedMemberName(differentComponent, "Module2");
218+
AddDeclarationItem(context, new Parsing.Selection(4, 9, 4, 16), differentMember, DeclarationType.Variable,"FooTest");
219+
220+
// add references to the Foo declaration item to simulate prod usage
221+
AddDeclarationItem(context, new Parsing.Selection(3, 5, 3, 8), differentMember, DeclarationType.Procedure, "Foo");
222+
var declarationItem = _listDeclarations[_listDeclarations.Count - 1];
223+
reference = new IdentifierReference(selectedComponent, "Foo", new Parsing.Selection(7, 5, 7, 11), false,context.Object, declarationItem);
224+
AddReference(declarationItem, reference);
225+
reference = new IdentifierReference(selectedComponent, "Foo", symbolSelection, false, context.Object,declarationItem);
226+
AddReference(declarationItem, reference);
227+
228+
AddDeclarationItem(context, new Parsing.Selection(1, 1, 1, 1), differentMember, DeclarationType.Module, "Module2");
229+
var member = new Parsing.QualifiedMemberName(selectedComponent, "fakeModule");
230+
AddDeclarationItem(context, new Parsing.Selection(7, 5, 7, 11), member, DeclarationType.Procedure, "RunFoo");
231+
AddDeclarationItem(context, new Parsing.Selection(3, 5, 3, 9), member, DeclarationType.Procedure, "Main");
232+
AddDeclarationItem(context, new Parsing.Selection(1, 1, 1, 1), member, DeclarationType.Module, "Module1");
233+
234+
_view.Setup(view => view.ShowDialog()).Returns(DialogResult.Cancel);
235+
_view.SetupProperty(view => view.Target);
236+
237+
//act
238+
var presenter = new RenamePresenter(_vbe.Object, _view.Object, _declarations, qualifiedSelection);
239+
presenter.Show();
240+
241+
//assert
242+
var retVal = _view.Object.Target;
243+
Assert.AreEqual("Foo", retVal.IdentifierName, "Selected the correct symbol name");
244+
Assert.AreEqual(declarationItem.References.Count(), retVal.References.Count());
245+
}
246+
}
247+
}

0 commit comments

Comments
 (0)