1
- using System . Linq ;
1
+ using System . Collections . Generic ;
2
+ using System . Linq ;
2
3
using System . Threading ;
3
4
using Microsoft . Vbe . Interop ;
4
5
using Microsoft . VisualStudio . TestTools . UnitTesting ;
5
6
using Moq ;
6
7
using Rubberduck . Inspections ;
7
8
using Rubberduck . Parsing ;
9
+ using Rubberduck . Parsing . Annotations ;
8
10
using Rubberduck . Parsing . Symbols ;
9
11
using Rubberduck . Parsing . VBA ;
10
12
using Rubberduck . VBEditor ;
@@ -16,7 +18,7 @@ namespace RubberduckTests.Inspections
16
18
[ TestClass ]
17
19
public class ImplicitActiveSheetReferenceInspectionTests
18
20
{
19
- [ TestMethod , Ignore ] // doesn't pick up the reference to "Range".
21
+ [ TestMethod ] // doesn't pick up the reference to "Range".
20
22
[ TestCategory ( "Inspections" ) ]
21
23
public void ReportsRange ( )
22
24
{
@@ -29,12 +31,9 @@ End Sub
29
31
30
32
//Arrange
31
33
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 )
33
35
. 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 )
38
37
. Build ( ) ;
39
38
var vbe = builder . AddProject ( project ) . Build ( ) ;
40
39
@@ -43,33 +42,106 @@ End Sub
43
42
44
43
var parser = MockParser . Create ( vbe . Object , new RubberduckParserState ( vbe . Object , new Mock < ISinks > ( ) . Object ) ) ;
45
44
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
+ {
46
90
var excelDeclaration = new ProjectDeclaration ( new QualifiedMemberName ( new QualifiedModuleName ( "Excel" ,
47
91
"C:\\ Program Files\\ Microsoft Office\\ Root\\ Office 16\\ EXCEL.EXE" , "Excel" ) , "Excel" ) , "Excel" , true ) ;
48
92
49
- var listColumnDeclaration = new ClassModuleDeclaration ( new QualifiedMemberName (
93
+ var globalDeclaration = new ClassModuleDeclaration ( new QualifiedMemberName (
50
94
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 ) ;
53
97
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 ( ) ;
61
101
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 ) ;
65
106
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" ) ;
68
110
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 ( ) ) ;
71
115
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
+ } ;
73
145
}
74
146
}
75
147
}
0 commit comments