Skip to content

Commit 3f9eb30

Browse files
author
Andrin Meier
committed
Merge pull request #1 from retailcoder/rubberduck-vba-next
Rubberduck vba next
2 parents c7636d9 + d9fbaf0 commit 3f9eb30

File tree

6 files changed

+65
-11
lines changed

6 files changed

+65
-11
lines changed

Rubberduck.Parsing/Symbols/DeclarationSymbolsListener.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private void DeclareControlsAsMembers(VBComponent form)
144144
// using dynamic typing here, because not only MSForms could have a Controls collection (e.g. MS-Access forms are 'document' modules).
145145
foreach (var control in ((dynamic)designer).Controls)
146146
{
147-
var declaration = new Declaration(_qualifiedName.QualifyMemberName(control.Name), _parentDeclaration, _currentScopeDeclaration, "Control", true, true, Accessibility.Public, DeclarationType.Control, null, Selection.Home);
147+
var declaration = new Declaration(_qualifiedName.QualifyMemberName(control.Name), _parentDeclaration, _currentScopeDeclaration, "Control", true, true, Accessibility.Public, DeclarationType.Control, null, Selection.Home, false);
148148
OnNewDeclaration(declaration);
149149
}
150150
}

Rubberduck.sln

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio 14
4-
VisualStudioVersion = 14.0.25123.0
3+
# Visual Studio 2013
4+
VisualStudioVersion = 12.0.40629.0
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Rubberduck", "RetailCoder.VBE\Rubberduck.csproj", "{20589DE8-432E-4359-9232-69EB070B7185}"
77
ProjectSection(ProjectDependencies) = postProject
@@ -40,9 +40,6 @@ EndProject
4040
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RubberduckTests", "RubberduckTests\RubberduckTests.csproj", "{ADADE971-75E3-40C4-8C19-AB7409372F2E}"
4141
EndProject
4242
Global
43-
GlobalSection(Performance) = preSolution
44-
HasPerformanceSessions = true
45-
EndGlobalSection
4643
GlobalSection(SolutionConfigurationPlatforms) = preSolution
4744
Debug|Any CPU = Debug|Any CPU
4845
Debug|x64 = Debug|x64

RubberduckTests/Grammar/ResolverTests.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1445,5 +1445,42 @@ End Sub
14451445

14461446
Assert.AreEqual(1, usages.Count());
14471447
}
1448+
1449+
[TestMethod]
1450+
public void GivenControlDeclaration_ResolvesUsageInCodeBehind()
1451+
{
1452+
var code = @"
1453+
Public Sub DoSomething()
1454+
TextBox1.Text = ""Test""
1455+
End Sub
1456+
";
1457+
var builder = new MockVbeBuilder();
1458+
var project = builder.ProjectBuilder("TestProject1", vbext_ProjectProtection.vbext_pp_none);
1459+
var form = project.MockUserFormBuilder("Form1", code).AddControl("TextBox1").Build();
1460+
project.AddComponent(form);
1461+
builder.AddProject(project.Build());
1462+
var vbe = builder.Build();
1463+
1464+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState());
1465+
1466+
parser.Parse();
1467+
if (parser.State.Status == ParserState.ResolverError)
1468+
{
1469+
Assert.Fail("Parser state should be 'Ready', but returns '{0}'.", parser.State.Status);
1470+
}
1471+
if (parser.State.Status != ParserState.Ready)
1472+
{
1473+
Assert.Inconclusive("Parser state should be 'Ready', but returns '{0}'.", parser.State.Status);
1474+
}
1475+
1476+
var declaration = parser.State.AllUserDeclarations.Single(item =>
1477+
item.DeclarationType == DeclarationType.Control
1478+
&& item.IdentifierName == "TextBox1");
1479+
1480+
var usages = declaration.References.Where(item =>
1481+
item.ParentNonScoping.IdentifierName == "DoSomething");
1482+
1483+
Assert.AreEqual(1, usages.Count());
1484+
}
14481485
}
14491486
}

RubberduckTests/Grammar/VBAParserTests.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Rubberduck.Parsing.Symbols;
77
using System;
88
using System.Collections.Generic;
9+
using System.Text.RegularExpressions;
910

1011
namespace RubberduckTests.Grammar
1112
{
@@ -249,6 +250,22 @@ private Tuple<VBAParser, ParserRuleContext> Parse(string code)
249250
return Tuple.Create<VBAParser, ParserRuleContext>(parser, root);
250251
}
251252

253+
[TestMethod]
254+
public void GivenIfElseBlock_ParsesElseBlockAsElseStatement()
255+
{
256+
var code = @"
257+
Private Sub DoSomething()
258+
If Not True Then
259+
Debug.Print False
260+
Else
261+
Debug.Print True
262+
End If
263+
End Sub
264+
";
265+
var parser = Parse(code);
266+
AssertTree(parser.Item1, parser.Item2, "//ifElseBlockStmt", matches => matches.Count == 1);
267+
}
268+
252269
private void AssertTree(VBAParser parser, ParserRuleContext root, string xpath)
253270
{
254271
AssertTree(parser, root, xpath, matches => matches.Count >= 1);
@@ -257,7 +274,8 @@ private void AssertTree(VBAParser parser, ParserRuleContext root, string xpath)
257274
private void AssertTree(VBAParser parser, ParserRuleContext root, string xpath, Predicate<ICollection<IParseTree>> assertion)
258275
{
259276
var matches = new XPath(parser, xpath).Evaluate(root);
260-
Assert.IsTrue(assertion(matches));
277+
var actual = matches.Count;
278+
Assert.IsTrue(assertion(matches), string.Format("{0} matches found.", actual));
261279
}
262280
}
263281
}

RubberduckTests/Mocks/MockUserFormBuilder.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class MockUserFormBuilder
1616
private readonly Mock<VBComponent> _component;
1717
private readonly MockProjectBuilder _mockProjectBuilder;
1818
private readonly Mock<Controls> _vbControls;
19-
private readonly ICollection<Mock<Control>> _controls = new List<Mock<Control>>();
19+
private readonly ICollection<Control> _controls = new List<Control>();
2020

2121
public MockUserFormBuilder(Mock<VBComponent> component, MockProjectBuilder mockProjectBuilder)
2222
{
@@ -40,7 +40,7 @@ public MockUserFormBuilder AddControl(string name)
4040
var control = new Mock<Control>();
4141
control.SetupProperty(m => m.Name, name);
4242

43-
_controls.Add(control);
43+
_controls.Add(control.Object);
4444
return this;
4545
}
4646

@@ -63,14 +63,15 @@ public MockProjectBuilder MockProjectBuilder()
6363
public Mock<VBComponent> Build()
6464
{
6565
var designer = CreateMockDesigner();
66-
_component.SetupGet(m => m.Designer).Returns(() => designer);
66+
_component.SetupGet(m => m.Designer).Returns(() => designer.Object);
6767

6868
return _component;
6969
}
7070

7171
private Mock<UserForm> CreateMockDesigner()
7272
{
7373
var result = new Mock<UserForm>();
74+
7475
result.SetupGet(m => m.Controls).Returns(() => _vbControls.Object);
7576

7677
return result;
@@ -82,7 +83,7 @@ private Mock<Controls> CreateControlsMock()
8283
result.Setup(m => m.GetEnumerator()).Returns(() => _controls.GetEnumerator());
8384
result.As<IEnumerable>().Setup(m => m.GetEnumerator()).Returns(() => _controls.GetEnumerator());
8485

85-
result.Setup(m => m.Item(It.IsAny<int>())).Returns<int>(index => _controls.ElementAt(index).Object);
86+
result.Setup(m => m.Item(It.IsAny<int>())).Returns<int>(index => _controls.ElementAt(index));
8687
result.SetupGet(m => m.Count).Returns(_controls.Count);
8788
return result;
8889
}

RubberduckTests/RubberduckTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
<Reference Include="LibGit2Sharp">
4545
<HintPath>..\packages\LibGit2Sharp.0.22.0-pre20150516171636\lib\net40\LibGit2Sharp.dll</HintPath>
4646
</Reference>
47+
<Reference Include="Microsoft.CSharp" />
4748
<Reference Include="Microsoft.Vbe.Interop, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c, processorArchitecture=MSIL">
4849
<SpecificVersion>False</SpecificVersion>
4950
<EmbedInteropTypes>True</EmbedInteropTypes>

0 commit comments

Comments
 (0)