1+ using System . Collections . Generic ;
12using System . Linq ;
23using System . Threading ;
34using NUnit . Framework ;
@@ -13,6 +14,12 @@ namespace RubberduckTests.Inspections
1314 [ TestFixture ]
1415 public class ImplicitActiveSheetReferenceInspectionTests : InspectionTestsBase
1516 {
17+ private static readonly IDictionary < string , IEnumerable < string > > DefaultDocumentModuleSupertypeNames = new Dictionary < string , IEnumerable < string > >
18+ {
19+ [ "ThisWorkbook" ] = new [ ] { "Workbook" , "_Workbook" } ,
20+ [ "Sheet1" ] = new [ ] { "Worksheet" , "_Worksheet" }
21+ } ;
22+
1623 [ Test ]
1724 [ Category ( "Inspections" ) ]
1825 public void ImplicitActiveSheetReference_ReportsRange ( )
@@ -23,8 +30,13 @@ Dim arr1() As Variant
2330 arr1 = Range(""A1:B2"")
2431End Sub
2532" ;
26- var modules = new ( string , string , ComponentType ) [ ] { ( "Class1" , inputCode , ComponentType . ClassModule ) } ;
27- Assert . AreEqual ( 1 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel ) . Count ( ) ) ;
33+ var modules = new ( string , string , ComponentType ) [ ]
34+ {
35+ ( "ThisWorkbook" , string . Empty , ComponentType . Document ) ,
36+ ( "Sheet1" , string . Empty , ComponentType . Document ) ,
37+ ( "Class1" , inputCode , ComponentType . ClassModule )
38+ } ;
39+ Assert . AreEqual ( 1 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel , DefaultDocumentModuleSupertypeNames ) . Count ( ) ) ;
2840 }
2941
3042 [ Test ]
@@ -37,8 +49,13 @@ Dim arr1() As Variant
3749 arr1 = Cells(1,2)
3850End Sub
3951" ;
40- var modules = new ( string , string , ComponentType ) [ ] { ( "Class1" , inputCode , ComponentType . ClassModule ) } ;
41- Assert . AreEqual ( 1 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel ) . Count ( ) ) ;
52+ var modules = new ( string , string , ComponentType ) [ ]
53+ {
54+ ( "ThisWorkbook" , string . Empty , ComponentType . Document ) ,
55+ ( "Sheet1" , string . Empty , ComponentType . Document ) ,
56+ ( "Class1" , inputCode , ComponentType . ClassModule )
57+ } ;
58+ Assert . AreEqual ( 1 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel , DefaultDocumentModuleSupertypeNames ) . Count ( ) ) ;
4259 }
4360
4461 [ Test ]
@@ -51,8 +68,13 @@ Dim arr1() As Variant
5168 arr1 = Columns(3)
5269End Sub
5370" ;
54- var modules = new ( string , string , ComponentType ) [ ] { ( "Class1" , inputCode , ComponentType . ClassModule ) } ;
55- Assert . AreEqual ( 1 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel ) . Count ( ) ) ;
71+ var modules = new ( string , string , ComponentType ) [ ]
72+ {
73+ ( "ThisWorkbook" , string . Empty , ComponentType . Document ) ,
74+ ( "Sheet1" , string . Empty , ComponentType . Document ) ,
75+ ( "Class1" , inputCode , ComponentType . ClassModule )
76+ } ;
77+ Assert . AreEqual ( 1 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel , DefaultDocumentModuleSupertypeNames ) . Count ( ) ) ;
5678 }
5779
5880 [ Test ]
@@ -65,8 +87,13 @@ Dim arr1() As Variant
6587 arr1 = Rows(3)
6688End Sub
6789" ;
68- var modules = new ( string , string , ComponentType ) [ ] { ( "Class1" , inputCode , ComponentType . ClassModule ) } ;
69- Assert . AreEqual ( 1 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel ) . Count ( ) ) ;
90+ var modules = new ( string , string , ComponentType ) [ ]
91+ {
92+ ( "ThisWorkbook" , string . Empty , ComponentType . Document ) ,
93+ ( "Sheet1" , string . Empty , ComponentType . Document ) ,
94+ ( "Class1" , inputCode , ComponentType . ClassModule )
95+ } ;
96+ Assert . AreEqual ( 1 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel , DefaultDocumentModuleSupertypeNames ) . Count ( ) ) ;
7097 }
7198
7299 [ Test ]
@@ -79,21 +106,13 @@ Dim arr1() As Variant
79106 arr1 = Cells(1,2)
80107End Sub
81108" ;
82- var module = ( "Sheet1" , inputCode , ComponentType . Document ) ;
83- var vbe = MockVbeBuilder . BuildFromModules ( module , ReferenceLibrary . Excel ) . Object ;
84-
85- using ( var state = MockParser . CreateAndParse ( vbe ) )
109+ var modules = new ( string , string , ComponentType ) [ ]
86110 {
87- var documentModule = state . DeclarationFinder . UserDeclarations ( DeclarationType . Document )
88- . OfType < DocumentModuleDeclaration > ( )
89- . Single ( ) ;
90- documentModule . AddSupertypeName ( "Worksheet" ) ;
91-
92- var inspection = InspectionUnderTest ( state ) ;
93- var inspectionResults = inspection . GetInspectionResults ( CancellationToken . None ) ;
94-
95- Assert . AreEqual ( 0 , inspectionResults . Count ( ) ) ;
96- }
111+ ( "ThisWorkbook" , string . Empty , ComponentType . Document ) ,
112+ ( "Sheet1" , inputCode , ComponentType . Document ) ,
113+ ( "Class1" , string . Empty , ComponentType . ClassModule )
114+ } ;
115+ Assert . AreEqual ( 0 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel , DefaultDocumentModuleSupertypeNames ) . Count ( ) ) ;
97116 }
98117
99118 [ Test ]
@@ -106,21 +125,33 @@ Dim arr1() As Variant
106125 arr1 = Cells(1,2)
107126End Sub
108127" ;
109- var module = ( "Sheet1" , inputCode , ComponentType . Document ) ;
110- var vbe = MockVbeBuilder . BuildFromModules ( module , ReferenceLibrary . Excel ) . Object ;
111-
112- using ( var state = MockParser . CreateAndParse ( vbe ) )
128+ var modules = new ( string , string , ComponentType ) [ ]
113129 {
114- var documentModule = state . DeclarationFinder . UserDeclarations ( DeclarationType . Document )
115- . OfType < DocumentModuleDeclaration > ( )
116- . Single ( ) ;
117- documentModule . AddSupertypeName ( "Workbook" ) ;
118-
119- var inspection = InspectionUnderTest ( state ) ;
120- var inspectionResults = inspection . GetInspectionResults ( CancellationToken . None ) ;
130+ ( "ThisWorkbook" , inputCode , ComponentType . Document ) ,
131+ ( "Sheet1" , string . Empty , ComponentType . Document ) ,
132+ ( "Class1" , string . Empty , ComponentType . ClassModule )
133+ } ;
134+ Assert . AreEqual ( 1 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel , DefaultDocumentModuleSupertypeNames ) . Count ( ) ) ;
135+ }
121136
122- Assert . AreEqual ( 1 , inspectionResults . Count ( ) ) ;
123- }
137+ [ Test ]
138+ [ Category ( "Inspections" ) ]
139+ public void ImplicitActiveSheetReference_NoResultForWorksheetVariable ( )
140+ {
141+ const string inputCode =
142+ @"Sub foo()
143+ Dim sh As Worksheet
144+ Set sh = Sheet1
145+ Debug.Print sh.Cells(1, 1)
146+ End Sub
147+ " ;
148+ var modules = new ( string , string , ComponentType ) [ ]
149+ {
150+ ( "ThisWorkbook" , string . Empty , ComponentType . Document ) ,
151+ ( "Sheet1" , string . Empty , ComponentType . Document ) ,
152+ ( "Module1" , inputCode , ComponentType . StandardModule )
153+ } ;
154+ Assert . AreEqual ( 0 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel , DefaultDocumentModuleSupertypeNames ) . Count ( ) ) ;
124155 }
125156
126157 [ Test ]
@@ -135,9 +166,14 @@ Dim arr1() As Variant
135166 arr1 = Range(""A1:B2"")
136167End Sub
137168" ;
169+ var modules = new ( string , string , ComponentType ) [ ]
170+ {
171+ ( "ThisWorkbook" , string . Empty , ComponentType . Document ) ,
172+ ( "Sheet1" , string . Empty , ComponentType . Document ) ,
173+ ( "Class1" , inputCode , ComponentType . ClassModule )
174+ } ;
138175
139- var modules = new ( string , string , ComponentType ) [ ] { ( "Class1" , inputCode , ComponentType . ClassModule ) } ;
140- Assert . AreEqual ( 0 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel ) . Count ( ) ) ;
176+ Assert . AreEqual ( 0 , InspectionResultsForModules ( modules , ReferenceLibrary . Excel , DefaultDocumentModuleSupertypeNames ) . Count ( ) ) ;
141177 }
142178
143179 [ Test ]
0 commit comments