Skip to content

Commit 40c2a7f

Browse files
committed
Finishing updates to DeclarationFinderTests after merge.
1 parent 5f45a47 commit 40c2a7f

File tree

1 file changed

+38
-28
lines changed

1 file changed

+38
-28
lines changed

RubberduckTests/Symbols/DeclarationFinderTests.cs

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,16 @@
11
using Microsoft.VisualStudio.TestTools.UnitTesting;
2-
using Moq;
32
using Rubberduck.Parsing.Symbols;
43
using Rubberduck.Parsing.VBA;
54
using Rubberduck.VBEditor.SafeComWrappers;
65
using Rubberduck.VBEditor.SafeComWrappers.Abstract;
76
using RubberduckTests.Mocks;
8-
using System;
97
using System.Collections.Generic;
108
using System.Linq;
11-
using System.Text;
129
using System.Threading;
13-
using System.Threading.Tasks;
1410
using Rubberduck.Parsing.Grammar;
1511
using Rubberduck.VBEditor;
1612
using Antlr4.Runtime;
13+
using Rubberduck.Inspections;
1714

1815
namespace RubberduckTests.Symbols
1916
{
@@ -29,67 +26,76 @@ public class DeclarationFinderTests
2926
private List<string> _accessibilityTests_GlobalScopeNames;
3027

3128
[TestMethod]
32-
[TestCategory("Inspections")]
29+
[TestCategory("Resolver")]
3330
public void DeclarationFinder_FindsAccessibleDeclarations_InProcedure()
3431
{
3532
SetupSUT("TestProject");
36-
RespectsDeclarationAccessibilityRules(_accessibilityTests_ProcedureScopeNames, "ProcedureScope", true, false);
33+
TestAccessibleDeclarations(_accessibilityTests_ProcedureScopeNames, "ProcedureScope", true, false);
3734

3835
}
3936

4037
[TestMethod]
41-
[TestCategory("Inspections")]
38+
[TestCategory("Resolver")]
4239
public void DeclarationFinder_FindsAccessibleDeclarations_ModuleScope()
4340
{
4441
SetupSUT("TestProject");
45-
RespectsDeclarationAccessibilityRules(_accessibilityTests_ModuleScopeNames, "ModuleScope", true, false);
42+
TestAccessibleDeclarations(_accessibilityTests_ModuleScopeNames, "ModuleScope", true, false);
4643
}
4744

4845
[TestMethod]
49-
[TestCategory("Inspections")]
46+
[TestCategory("Resolver")]
5047
public void DeclarationFinder_FindsAccessibleDeclarations_GlobalScope()
5148
{
5249
SetupSUT("TestProject");
53-
RespectsDeclarationAccessibilityRules(_accessibilityTests_GlobalScopeNames, "GlobalScope", true, true);
50+
TestAccessibleDeclarations(_accessibilityTests_GlobalScopeNames, "GlobalScope", true, true);
5451
}
5552

5653
[TestMethod]
5754

58-
[TestCategory("Inspections")]
55+
[TestCategory("Resolver")]
5956
public void DeclarationFinder_FindsAccessibleDeclarations_Inaccessible()
6057
{
6158
SetupSUT("TestProject");
6259
string[] inaccessible = {"result", "mySecondEggo","localVar" , "FooFighters" , "filename", "implicitVar"};
6360

64-
RespectsDeclarationAccessibilityRules(inaccessible.ToList(), "GlobalScope", false, false);
61+
TestAccessibleDeclarations(inaccessible.ToList(), "GlobalScope", false, false);
6562
}
6663

6764
[TestMethod]
68-
[TestCategory("Inspections")]
65+
[TestCategory("Resolver")]
6966
public void DeclarationFinder_FindsAccessibleDeclarations_All()
7067
{
71-
68+
//Tests that the DeclarationFinder does not return any unexpected declarations
69+
//excluding Reserved Identifiers
7270
SetupSUT("TestProject");
73-
var allNames = _accessibilityTests_ProcedureScopeNames;
74-
allNames.AddRange(_accessibilityTests_ModuleScopeNames);
75-
allNames.AddRange(_accessibilityTests_GlobalScopeNames);
76-
allNames.AddRange(_accessibilityTests_Components.Select(n => n.Name));
71+
var allNamesUsedInTests = _accessibilityTests_ProcedureScopeNames;
72+
allNamesUsedInTests.AddRange(_accessibilityTests_ModuleScopeNames);
73+
allNamesUsedInTests.AddRange(_accessibilityTests_GlobalScopeNames);
74+
allNamesUsedInTests.AddRange(_accessibilityTests_Components.Select(n => n.Name));
7775

7876
var target = GetTargetForAccessibilityTests();
77+
7978
var declarationFinderResults =
8079
_accessibilityTests_parser.State.DeclarationFinder.GetDeclarationsAccessibleToScope(target, _accessibilityTests_declarations);
8180
var accessibleNames = declarationFinderResults.Select(d => d.IdentifierName);
8281

83-
var namesOutsideOfChecks = accessibleNames.Except(allNames).ToList();
82+
var unexpectedDeclarations = accessibleNames.Except(allNamesUsedInTests).ToList().Where(n => !VariableNameValidator.IsReservedIdentifier(n));
8483

85-
var messagePreface = "Test failed for All Names identifier: ";
86-
foreach (var identifier in allNames)
84+
string failureMessage = string.Empty;
85+
if( unexpectedDeclarations.Count() > 0)
8786
{
88-
Assert.IsTrue(accessibleNames.Contains(identifier), messagePreface + identifier + " Left out of accessibility tests.");
87+
failureMessage = unexpectedDeclarations.Count().ToString() + " unexpected declaration(s) found:";
88+
foreach(string identifier in unexpectedDeclarations)
89+
{
90+
failureMessage = failureMessage + " '" + identifier + "', ";
91+
}
92+
failureMessage = failureMessage.Substring(0, failureMessage.Length - 2);
8993
}
94+
95+
Assert.AreEqual(0, unexpectedDeclarations.Count(), failureMessage);
9096
}
9197

92-
private void RespectsDeclarationAccessibilityRules(IEnumerable<string> namesToTest, string scope, bool containsTestNames, bool includeModuleNames)
98+
private void TestAccessibleDeclarations(IEnumerable<string> namesToTest, string scope, bool containsIdentifiers, bool includeModuleNames)
9399
{
94100

95101
var allIdentifiersToCheck = namesToTest.ToList();
@@ -99,14 +105,16 @@ private void RespectsDeclarationAccessibilityRules(IEnumerable<string> namesToTe
99105
}
100106

101107
var target = GetTargetForAccessibilityTests();
108+
102109
var declarationFinderResults =
103110
_accessibilityTests_parser.State.DeclarationFinder.GetDeclarationsAccessibleToScope( target, _accessibilityTests_declarations);
111+
104112
var accessibleNames = declarationFinderResults.Select(d => d.IdentifierName);
105113

106114
var messagePreface = "Test failed for " + scope + " identifier: ";
107115
foreach (var identifier in allIdentifiersToCheck)
108116
{
109-
if (containsTestNames)
117+
if (containsIdentifiers)
110118
{
111119
Assert.IsTrue(accessibleNames.Contains(identifier), messagePreface + identifier);
112120
}
@@ -122,7 +130,7 @@ private Declaration GetTargetForAccessibilityTests()
122130
var targets = _accessibilityTests_declarations.Where(dec => dec.IdentifierName == targetIdentifier);
123131
if (targets.Count() > 1)
124132
{
125-
Assert.Inconclusive("Multiple targets found with identifier: " + targetIdentifier + ". Test requires a globally a unique identifierName");
133+
Assert.Inconclusive("Multiple targets found with identifier: " + targetIdentifier + ". Test requires a unique identifierName");
126134
}
127135
return targets.FirstOrDefault();
128136
}
@@ -187,14 +195,15 @@ public TestComponentSpecification(string componentName, string componentContent,
187195
public string Content { get { return _content; } }
188196
public ComponentType ModuleType { get { return _componentType; } }
189197
}
198+
190199
#region AccessibilityTestsModuleContent
191200
private string GetRespectsDeclarationsAccessibilityRules_FirstClassBody()
192201
{
193202
return
194203
@"
195204
Private memberString As String
196205
Private memberLong As Long
197-
Private myEggo as String
206+
Private myEggo As String
198207
199208
Public Sub Foo(ByVal arg1 As String)
200209
Dim localVar as Long
@@ -231,7 +240,7 @@ private string GetRespectsDeclarationsAccessibilityRules_SecondClassBody()
231240
@"
232241
Private memberString As String
233242
Private memberLong As Long
234-
Public mySecondEggo as String
243+
Public mySecondEggo As String
235244
236245
237246
Public Sub Foo2( arg1 As String, theSecondArg As Long)
@@ -301,7 +310,8 @@ Public Sub DoSomething(filename As String)
301310
End Sub
302311
";
303312
}
304-
#endregion
313+
#endregion
314+
305315
[TestCategory("Resolver")]
306316
[TestMethod]
307317
public void DeclarationFinderCanCopeWithMultipleModulesImplementingTheSameInterface()

0 commit comments

Comments
 (0)