Skip to content

Commit 8d46382

Browse files
committed
added DeclarationFinderTests (failing/ignored)
1 parent 3676cbd commit 8d46382

File tree

4 files changed

+74
-2
lines changed

4 files changed

+74
-2
lines changed

Rubberduck.Parsing/Symbols/DeclarationFinder.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,6 @@ public DeclarationFinder(IReadOnlyList<Declaration> declarations, IEnumerable<IA
157157
implementableMembers.ToDictionary(item => item.Context, item => item.Members)), true);
158158
}
159159

160-
private QualifiedSelection _lastSelection;
161160
public Declaration FindSelectedDeclaration(ICodePane activeCodePane)
162161
{
163162
if (activeCodePane == null || activeCodePane.IsWrappingNullReference)
@@ -171,7 +170,6 @@ public Declaration FindSelectedDeclaration(ICodePane activeCodePane)
171170
return null;
172171
}
173172

174-
_lastSelection = qualifiedSelection.Value;
175173
var selection = qualifiedSelection.Value.Selection;
176174

177175
// statistically we'll be on an IdentifierReference more often than on a Declaration:

Rubberduck.VBEEditor/Selection.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ namespace Rubberduck.VBEditor
44
{
55
public struct Selection : IEquatable<Selection>, IComparable<Selection>
66
{
7+
public Selection(int line, int column) : this(line, column, line, column) { }
8+
79
public Selection(int startLine, int startColumn, int endLine, int endColumn)
810
{
911
_startLine = startLine;

RubberduckTests/RubberduckTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
<Compile Include="SmartIndenter\VerticalSpacingTests.cs" />
103103
<Compile Include="Symbols\AccessibilityCheckTests.cs" />
104104
<Compile Include="Symbols\ClassModuleDeclarationTests.cs" />
105+
<Compile Include="Symbols\DeclarationFinderTests.cs" />
105106
<Compile Include="Symbols\ProjectDeclarationTests.cs" />
106107
<Compile Include="Symbols\ConstantDeclarationTests.cs" />
107108
<Compile Include="Symbols\ProceduralModuleDeclarationTests.cs" />
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System.Linq;
2+
using System.Threading;
3+
using Microsoft.VisualStudio.TestTools.UnitTesting;
4+
using Rubberduck.Parsing.Symbols;
5+
using Rubberduck.Parsing.VBA;
6+
using Rubberduck.VBEditor;
7+
using Rubberduck.VBEditor.SafeComWrappers;
8+
using RubberduckTests.Mocks;
9+
10+
namespace RubberduckTests.Symbols
11+
{
12+
[TestClass]
13+
public class DeclarationFinderTests
14+
{
15+
[TestMethod]
16+
[Ignore] // ref. https://github.com/rubberduck-vba/Rubberduck/issues/2330
17+
public void FiendishlyAmbiguousNameSelectsSmallestScopedDeclaration()
18+
{
19+
var code = @"
20+
Option Explicit
21+
22+
Public Sub foo()
23+
Dim foo As Long
24+
foo = 42
25+
Debug.Print foo
26+
End Sub
27+
";
28+
var vbe = new MockVbeBuilder()
29+
.ProjectBuilder("foo", ProjectProtection.Unprotected)
30+
.AddComponent("foo", ComponentType.StandardModule, code, new Selection(6, 6))
31+
.MockVbeBuilder()
32+
.Build();
33+
34+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object));
35+
parser.Parse(new CancellationTokenSource());
36+
37+
var expected = parser.State.AllDeclarations.Single(item => item.DeclarationType == DeclarationType.Variable);
38+
var actual = parser.State.DeclarationFinder.FindSelectedDeclaration(vbe.Object.ActiveCodePane);
39+
40+
Assert.AreEqual(expected, actual, "Expected {0}, resolved to {1}", expected.DeclarationType, actual.DeclarationType);
41+
}
42+
43+
[TestMethod]
44+
[Ignore] // bug: this test should pass... it's not all that evil
45+
public void AmbiguousNameSelectsSmallestScopedDeclaration()
46+
{
47+
var code = @"
48+
Option Explicit
49+
50+
Public Sub foo()
51+
Dim foo As Long
52+
foo = 42
53+
Debug.Print foo
54+
End Sub
55+
";
56+
var vbe = new MockVbeBuilder()
57+
.ProjectBuilder("TestProject", ProjectProtection.Unprotected)
58+
.AddComponent("TestModule", ComponentType.StandardModule, code, new Selection(6, 6))
59+
.MockVbeBuilder()
60+
.Build();
61+
62+
var parser = MockParser.Create(vbe.Object, new RubberduckParserState(vbe.Object));
63+
parser.Parse(new CancellationTokenSource());
64+
65+
var expected = parser.State.AllDeclarations.Single(item => item.DeclarationType == DeclarationType.Variable);
66+
var actual = parser.State.DeclarationFinder.FindSelectedDeclaration(vbe.Object.ActiveCodePane);
67+
68+
Assert.AreEqual(expected, actual, "Expected {0}, resolved to {1}", expected.DeclarationType, actual.DeclarationType);
69+
}
70+
}
71+
}

0 commit comments

Comments
 (0)