1
1
using Microsoft . VisualStudio . TestTools . UnitTesting ;
2
- using Moq ;
3
2
using Rubberduck . Parsing . Symbols ;
4
3
using Rubberduck . Parsing . VBA ;
5
4
using Rubberduck . VBEditor . SafeComWrappers ;
6
5
using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
7
6
using RubberduckTests . Mocks ;
8
- using System ;
9
7
using System . Collections . Generic ;
10
8
using System . Linq ;
11
- using System . Text ;
12
9
using System . Threading ;
13
- using System . Threading . Tasks ;
14
10
using Rubberduck . Parsing . Grammar ;
15
11
using Rubberduck . VBEditor ;
16
12
using Antlr4 . Runtime ;
13
+ using Rubberduck . Inspections ;
17
14
18
15
namespace RubberduckTests . Symbols
19
16
{
@@ -29,67 +26,76 @@ public class DeclarationFinderTests
29
26
private List < string > _accessibilityTests_GlobalScopeNames ;
30
27
31
28
[ TestMethod ]
32
- [ TestCategory ( "Inspections " ) ]
29
+ [ TestCategory ( "Resolver " ) ]
33
30
public void DeclarationFinder_FindsAccessibleDeclarations_InProcedure ( )
34
31
{
35
32
SetupSUT ( "TestProject" ) ;
36
- RespectsDeclarationAccessibilityRules ( _accessibilityTests_ProcedureScopeNames , "ProcedureScope" , true , false ) ;
33
+ TestAccessibleDeclarations ( _accessibilityTests_ProcedureScopeNames , "ProcedureScope" , true , false ) ;
37
34
38
35
}
39
36
40
37
[ TestMethod ]
41
- [ TestCategory ( "Inspections " ) ]
38
+ [ TestCategory ( "Resolver " ) ]
42
39
public void DeclarationFinder_FindsAccessibleDeclarations_ModuleScope ( )
43
40
{
44
41
SetupSUT ( "TestProject" ) ;
45
- RespectsDeclarationAccessibilityRules ( _accessibilityTests_ModuleScopeNames , "ModuleScope" , true , false ) ;
42
+ TestAccessibleDeclarations ( _accessibilityTests_ModuleScopeNames , "ModuleScope" , true , false ) ;
46
43
}
47
44
48
45
[ TestMethod ]
49
- [ TestCategory ( "Inspections " ) ]
46
+ [ TestCategory ( "Resolver " ) ]
50
47
public void DeclarationFinder_FindsAccessibleDeclarations_GlobalScope ( )
51
48
{
52
49
SetupSUT ( "TestProject" ) ;
53
- RespectsDeclarationAccessibilityRules ( _accessibilityTests_GlobalScopeNames , "GlobalScope" , true , true ) ;
50
+ TestAccessibleDeclarations ( _accessibilityTests_GlobalScopeNames , "GlobalScope" , true , true ) ;
54
51
}
55
52
56
53
[ TestMethod ]
57
54
58
- [ TestCategory ( "Inspections " ) ]
55
+ [ TestCategory ( "Resolver " ) ]
59
56
public void DeclarationFinder_FindsAccessibleDeclarations_Inaccessible ( )
60
57
{
61
58
SetupSUT ( "TestProject" ) ;
62
59
string [ ] inaccessible = { "result" , "mySecondEggo" , "localVar" , "FooFighters" , "filename" , "implicitVar" } ;
63
60
64
- RespectsDeclarationAccessibilityRules ( inaccessible . ToList ( ) , "GlobalScope" , false , false ) ;
61
+ TestAccessibleDeclarations ( inaccessible . ToList ( ) , "GlobalScope" , false , false ) ;
65
62
}
66
63
67
64
[ TestMethod ]
68
- [ TestCategory ( "Inspections " ) ]
65
+ [ TestCategory ( "Resolver " ) ]
69
66
public void DeclarationFinder_FindsAccessibleDeclarations_All ( )
70
67
{
71
-
68
+ //Tests that the DeclarationFinder does not return any unexpected declarations
69
+ //excluding Reserved Identifiers
72
70
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 ) ) ;
77
75
78
76
var target = GetTargetForAccessibilityTests ( ) ;
77
+
79
78
var declarationFinderResults =
80
79
_accessibilityTests_parser . State . DeclarationFinder . GetDeclarationsAccessibleToScope ( target , _accessibilityTests_declarations ) ;
81
80
var accessibleNames = declarationFinderResults . Select ( d => d . IdentifierName ) ;
82
81
83
- var namesOutsideOfChecks = accessibleNames . Except ( allNames ) . ToList ( ) ;
82
+ var unexpectedDeclarations = accessibleNames . Except ( allNamesUsedInTests ) . ToList ( ) . Where ( n => ! VariableNameValidator . IsReservedIdentifier ( n ) ) ;
84
83
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 )
87
86
{
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 ) ;
89
93
}
94
+
95
+ Assert . AreEqual ( 0 , unexpectedDeclarations . Count ( ) , failureMessage ) ;
90
96
}
91
97
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 )
93
99
{
94
100
95
101
var allIdentifiersToCheck = namesToTest . ToList ( ) ;
@@ -99,14 +105,16 @@ private void RespectsDeclarationAccessibilityRules(IEnumerable<string> namesToTe
99
105
}
100
106
101
107
var target = GetTargetForAccessibilityTests ( ) ;
108
+
102
109
var declarationFinderResults =
103
110
_accessibilityTests_parser . State . DeclarationFinder . GetDeclarationsAccessibleToScope ( target , _accessibilityTests_declarations ) ;
111
+
104
112
var accessibleNames = declarationFinderResults . Select ( d => d . IdentifierName ) ;
105
113
106
114
var messagePreface = "Test failed for " + scope + " identifier: " ;
107
115
foreach ( var identifier in allIdentifiersToCheck )
108
116
{
109
- if ( containsTestNames )
117
+ if ( containsIdentifiers )
110
118
{
111
119
Assert . IsTrue ( accessibleNames . Contains ( identifier ) , messagePreface + identifier ) ;
112
120
}
@@ -122,7 +130,7 @@ private Declaration GetTargetForAccessibilityTests()
122
130
var targets = _accessibilityTests_declarations . Where ( dec => dec . IdentifierName == targetIdentifier ) ;
123
131
if ( targets . Count ( ) > 1 )
124
132
{
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" ) ;
126
134
}
127
135
return targets . FirstOrDefault ( ) ;
128
136
}
@@ -187,14 +195,15 @@ public TestComponentSpecification(string componentName, string componentContent,
187
195
public string Content { get { return _content ; } }
188
196
public ComponentType ModuleType { get { return _componentType ; } }
189
197
}
198
+
190
199
#region AccessibilityTestsModuleContent
191
200
private string GetRespectsDeclarationsAccessibilityRules_FirstClassBody ( )
192
201
{
193
202
return
194
203
@"
195
204
Private memberString As String
196
205
Private memberLong As Long
197
- Private myEggo as String
206
+ Private myEggo As String
198
207
199
208
Public Sub Foo(ByVal arg1 As String)
200
209
Dim localVar as Long
@@ -231,7 +240,7 @@ private string GetRespectsDeclarationsAccessibilityRules_SecondClassBody()
231
240
@"
232
241
Private memberString As String
233
242
Private memberLong As Long
234
- Public mySecondEggo as String
243
+ Public mySecondEggo As String
235
244
236
245
237
246
Public Sub Foo2( arg1 As String, theSecondArg As Long)
@@ -301,7 +310,8 @@ Public Sub DoSomething(filename As String)
301
310
End Sub
302
311
" ;
303
312
}
304
- #endregion
313
+ #endregion
314
+
305
315
[ TestCategory ( "Resolver" ) ]
306
316
[ TestMethod ]
307
317
public void DeclarationFinderCanCopeWithMultipleModulesImplementingTheSameInterface ( )
0 commit comments