1- using System . Linq ;
1+ using System . Collections . Generic ;
2+ using System . Linq ;
23using System . Threading ;
34using Microsoft . Vbe . Interop ;
45using Microsoft . VisualStudio . TestTools . UnitTesting ;
56using Moq ;
67using Rubberduck . Inspections ;
78using Rubberduck . Parsing ;
9+ using Rubberduck . Parsing . Annotations ;
810using Rubberduck . Parsing . Symbols ;
911using Rubberduck . Parsing . VBA ;
1012using Rubberduck . VBEditor ;
@@ -16,7 +18,7 @@ namespace RubberduckTests.Inspections
1618 [ TestClass ]
1719 public class ImplicitActiveSheetReferenceInspectionTests
1820 {
19- [ TestMethod , Ignore ] // doesn't pick up the reference to "Range".
21+ [ TestMethod ] // doesn't pick up the reference to "Range".
2022 [ TestCategory ( "Inspections" ) ]
2123 public void ReportsRange ( )
2224 {
@@ -29,12 +31,9 @@ End Sub
2931
3032 //Arrange
3133 var builder = new MockVbeBuilder ( ) ;
32- var project = builder . ProjectBuilder ( "TestProject1" , vbext_ProjectProtection . vbext_pp_none )
34+ var project = builder . ProjectBuilder ( "TestProject1" , "TestProject1" , vbext_ProjectProtection . vbext_pp_none )
3335 . AddComponent ( "Class1" , vbext_ComponentType . vbext_ct_ClassModule , inputCode )
34- . AddReference ( "Excel" , string . Empty , true )
35-
36- // Apparently, the COM loader can't find it when it isn't actually loaded...
37- //.AddReference("VBA", "C:\\Program Files\\Common Files\\Microsoft Shared\\VBA\\VBA7.1\\VBE7.DLL", true)
36+ . AddReference ( "Excel" , "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , true )
3837 . Build ( ) ;
3938 var vbe = builder . AddProject ( project ) . Build ( ) ;
4039
@@ -43,33 +42,106 @@ End Sub
4342
4443 var parser = MockParser . Create ( vbe . Object , new RubberduckParserState ( vbe . Object , new Mock < ISinks > ( ) . Object ) ) ;
4544
45+ GetExcelRangeDeclarations ( ) . ForEach ( d => parser . State . AddDeclaration ( d ) ) ;
46+
47+ parser . Parse ( new CancellationTokenSource ( ) ) ;
48+ if ( parser . State . Status >= ParserState . Error ) { Assert . Inconclusive ( "Parser Error" ) ; }
49+
50+ var inspection = new ImplicitActiveSheetReferenceInspection ( vbe . Object , parser . State ) ;
51+ var inspectionResults = inspection . GetInspectionResults ( ) ;
52+
53+ Assert . AreEqual ( 1 , inspectionResults . Count ( ) ) ;
54+ }
55+
56+ [ TestMethod ]
57+ [ TestCategory ( "Inspections" ) ]
58+ public void InspectionType ( )
59+ {
60+ var builder = new MockVbeBuilder ( ) ;
61+ var project = builder . ProjectBuilder ( "TestProject1" , "TestProject1" , vbext_ProjectProtection . vbext_pp_none )
62+ . AddComponent ( "Class1" , vbext_ComponentType . vbext_ct_ClassModule , string . Empty )
63+ . AddReference ( "Excel" , "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , true )
64+ . Build ( ) ;
65+ var vbe = builder . AddProject ( project ) . Build ( ) ;
66+
67+ var inspection = new ImplicitActiveSheetReferenceInspection ( vbe . Object , null ) ;
68+ Assert . AreEqual ( CodeInspectionType . MaintainabilityAndReadabilityIssues , inspection . InspectionType ) ;
69+ }
70+
71+ [ TestMethod ]
72+ [ TestCategory ( "Inspections" ) ]
73+ public void InspectionName ( )
74+ {
75+ var builder = new MockVbeBuilder ( ) ;
76+ var project = builder . ProjectBuilder ( "TestProject1" , "TestProject1" , vbext_ProjectProtection . vbext_pp_none )
77+ . AddComponent ( "Class1" , vbext_ComponentType . vbext_ct_ClassModule , string . Empty )
78+ . AddReference ( "Excel" , "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , true )
79+ . Build ( ) ;
80+ var vbe = builder . AddProject ( project ) . Build ( ) ;
81+
82+ const string inspectionName = "ImplicitActiveSheetReferenceInspection" ;
83+ var inspection = new ImplicitActiveSheetReferenceInspection ( vbe . Object , null ) ;
84+
85+ Assert . AreEqual ( inspectionName , inspection . Name ) ;
86+ }
87+
88+ private List < Declaration > GetExcelRangeDeclarations ( )
89+ {
4690 var excelDeclaration = new ProjectDeclaration ( new QualifiedMemberName ( new QualifiedModuleName ( "Excel" ,
4791 "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , "Excel" ) , "Excel" ) , "Excel" , true ) ;
4892
49- var listColumnDeclaration = new ClassModuleDeclaration ( new QualifiedMemberName (
93+ var globalDeclaration = new ClassModuleDeclaration ( new QualifiedMemberName (
5094 new QualifiedModuleName ( "Excel" ,
51- "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , "ListColumn " ) ,
52- "ListColumn " ) , excelDeclaration , "ListColumn " , true , null , null ) ;
95+ "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , "_Global " ) ,
96+ "_Global " ) , excelDeclaration , "_Global " , true , null , null ) ;
5397
54- var rangeDeclaration =
55- new Declaration (
56- new QualifiedMemberName (
57- new QualifiedModuleName ( "Excel" ,
58- "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , "ListColumn" ) , "Range" ) ,
59- listColumnDeclaration , "EXCEL.EXE;Excel.ListColumn" , "Range" , null , false , false , Accessibility . Global ,
60- ( DeclarationType ) 3712 , false , null , true , null , new Attributes ( ) ) ;
98+ var globalCoClassDeclarationAttributes = new Attributes ( ) ;
99+ globalCoClassDeclarationAttributes . AddPredeclaredIdTypeAttribute ( ) ;
100+ globalCoClassDeclarationAttributes . AddGlobalClassAttribute ( ) ;
61101
62- parser . State . AddDeclaration ( excelDeclaration ) ;
63- parser . State . AddDeclaration ( listColumnDeclaration ) ;
64- parser . State . AddDeclaration ( rangeDeclaration ) ;
102+ var globalCoClassDeclaration = new ClassModuleDeclaration ( new QualifiedMemberName (
103+ new QualifiedModuleName ( "Excel" ,
104+ "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , "Global" ) ,
105+ "Global" ) , excelDeclaration , "Global" , true , null , globalCoClassDeclarationAttributes ) ;
65106
66- parser . Parse ( new CancellationTokenSource ( ) ) ;
67- if ( parser . State . Status >= ParserState . Error ) { Assert . Inconclusive ( "Parser Error" ) ; }
107+ globalDeclaration . AddSubtype ( globalCoClassDeclaration ) ;
108+ globalCoClassDeclaration . AddSupertype ( globalDeclaration ) ;
109+ globalCoClassDeclaration . AddSupertype ( "_Global" ) ;
68110
69- var inspection = new ImplicitActiveSheetReferenceInspection ( vbe . Object , parser . State ) ;
70- var inspectionResults = inspection . GetInspectionResults ( ) ;
111+ var rangeClassModuleDeclaration = new ClassModuleDeclaration ( new QualifiedMemberName (
112+ new QualifiedModuleName ( "Excel" ,
113+ "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , "Range" ) ,
114+ "Range" ) , excelDeclaration , "Range" , true , new List < IAnnotation > ( ) , new Attributes ( ) ) ;
71115
72- Assert . AreEqual ( 1 , inspectionResults . Count ( ) ) ;
116+ var rangeDeclaration = new PropertyGetDeclaration ( new QualifiedMemberName (
117+ new QualifiedModuleName ( "Excel" ,
118+ "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , "_Global" ) , "Range" ) ,
119+ globalDeclaration , globalDeclaration , "Range" , null , null , Accessibility . Global , null , Selection . Home ,
120+ false , true , new List < IAnnotation > ( ) , new Attributes ( ) ) ;
121+
122+ var firstParamDeclaration = new ParameterDeclaration ( new QualifiedMemberName (
123+ new QualifiedModuleName ( "Excel" ,
124+ "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , "_Global" ) ,
125+ "Cell1" ) , rangeDeclaration , "Variant" , null , null , false , false ) ;
126+
127+ var secondParamDeclaration = new ParameterDeclaration ( new QualifiedMemberName (
128+ new QualifiedModuleName ( "Excel" ,
129+ "C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , "_Global" ) ,
130+ "Cell2" ) , rangeDeclaration , "Variant" , null , null , true , false ) ;
131+
132+ rangeDeclaration . AddParameter ( firstParamDeclaration ) ;
133+ rangeDeclaration . AddParameter ( secondParamDeclaration ) ;
134+
135+ return new List < Declaration >
136+ {
137+ excelDeclaration ,
138+ globalDeclaration ,
139+ globalCoClassDeclaration ,
140+ rangeClassModuleDeclaration ,
141+ rangeDeclaration ,
142+ firstParamDeclaration ,
143+ secondParamDeclaration ,
144+ } ;
73145 }
74146 }
75147}
0 commit comments