2
2
using System . Threading ;
3
3
using NUnit . Framework ;
4
4
using Rubberduck . Inspections . Concrete ;
5
- using Rubberduck . Parsing . VBA ;
6
5
using Rubberduck . VBEditor . SafeComWrappers ;
7
6
using RubberduckTests . Mocks ;
8
7
@@ -22,20 +21,110 @@ Dim sheet As Worksheet
22
21
Set sheet = Worksheets(""Sheet1"")
23
22
End Sub" ;
24
23
25
- var builder = new MockVbeBuilder ( ) ;
26
- var project = builder . ProjectBuilder ( "TestProject1" , "TestProject1" , ProjectProtection . Unprotected )
27
- . AddComponent ( "Class1" , ComponentType . ClassModule , inputCode )
28
- . AddReference ( "Excel" , MockVbeBuilder . LibraryPathMsExcel , 1 , 8 , true )
29
- . Build ( ) ;
30
- var vbe = builder . AddProject ( project ) . Build ( ) ;
24
+ const int expected = 1 ;
25
+ var actual = ArrangeAndGetInspectionCount ( inputCode ) ;
31
26
32
- using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
33
- {
34
- var inspection = new ImplicitActiveWorkbookReferenceInspection ( state ) ;
35
- var inspectionResults = inspection . GetInspectionResults ( CancellationToken . None ) ;
27
+ Assert . AreEqual ( expected , actual ) ;
28
+ }
36
29
37
- Assert . AreEqual ( 1 , inspectionResults . Count ( ) ) ;
38
- }
30
+ [ Test ]
31
+ [ Category ( "Inspections" ) ]
32
+ public void ImplicitActiveWorkbookReference_ExplicitApplication ( )
33
+ {
34
+ const string inputCode =
35
+ @"
36
+ Sub foo()
37
+ Dim sheet As Worksheet
38
+ Set sheet = Application.Worksheets(""Sheet1"")
39
+ End Sub" ;
40
+
41
+ const int expected = 1 ;
42
+ var actual = ArrangeAndGetInspectionCount ( inputCode ) ;
43
+
44
+ Assert . AreEqual ( expected , actual ) ;
45
+ }
46
+
47
+ [ Test ]
48
+ [ Category ( "Inspections" ) ]
49
+ public void ImplicitActiveWorkbookReference_ReportsSheets ( )
50
+ {
51
+ const string inputCode =
52
+ @"
53
+ Sub foo()
54
+ Dim sheet As Worksheet
55
+ Set sheet = Sheets(""Sheet1"")
56
+ End Sub" ;
57
+
58
+ const int expected = 1 ;
59
+ var actual = ArrangeAndGetInspectionCount ( inputCode ) ;
60
+
61
+ Assert . AreEqual ( expected , actual ) ;
62
+ }
63
+
64
+ [ Test ]
65
+ [ Category ( "Inspections" ) ]
66
+ public void ImplicitActiveWorkbookReference_ReportsNames ( )
67
+ {
68
+ const string inputCode =
69
+ @"
70
+ Sub foo()
71
+ Names.Add ""foo"", Rows(1)
72
+ End Sub" ;
73
+
74
+ const int expected = 1 ;
75
+ var actual = ArrangeAndGetInspectionCount ( inputCode ) ;
76
+
77
+ Assert . AreEqual ( expected , actual ) ;
78
+ }
79
+
80
+ [ Test ]
81
+ [ Category ( "Inspections" ) ]
82
+ public void ImplicitActiveWorkbookReference_ExplicitReference_NotReported ( )
83
+ {
84
+ const string inputCode =
85
+ @"
86
+ Sub foo()
87
+ Dim book As Workbook
88
+ Dim sheet As Worksheet
89
+ Set sheet = book.Worksheets(1)
90
+ End Sub" ;
91
+
92
+ const int expected = 0 ;
93
+ var actual = ArrangeAndGetInspectionCount ( inputCode ) ;
94
+
95
+ Assert . AreEqual ( expected , actual ) ;
96
+ }
97
+
98
+ [ Test ]
99
+ [ Category ( "Inspections" ) ]
100
+ public void ImplicitActiveWorkbookReference_ExplicitParameterReference_NotReported ( )
101
+ {
102
+ const string inputCode =
103
+ @"
104
+ Sub foo(book As Workbook)
105
+ Debug.Print book.Worksheets.Count
106
+ End Sub" ;
107
+
108
+ const int expected = 0 ;
109
+ var actual = ArrangeAndGetInspectionCount ( inputCode ) ;
110
+
111
+ Assert . AreEqual ( expected , actual ) ;
112
+ }
113
+
114
+ [ Test ]
115
+ [ Category ( "Inspections" ) ]
116
+ public void ImplicitActiveWorkbookReference_DimAsTypeSheets_NotReported ( )
117
+ {
118
+ const string inputCode =
119
+ @"
120
+ Sub foo()
121
+ Dim allSheets As Sheets
122
+ End Sub" ;
123
+
124
+ const int expected = 0 ;
125
+ var actual = ArrangeAndGetInspectionCount ( inputCode ) ;
126
+
127
+ Assert . AreEqual ( expected , actual ) ;
39
128
}
40
129
41
130
[ Test ]
@@ -51,9 +140,17 @@ Dim sheet As Worksheet
51
140
Set sheet = Worksheets(""Sheet1"")
52
141
End Sub" ;
53
142
143
+ const int expected = 0 ;
144
+ var actual = ArrangeAndGetInspectionCount ( inputCode ) ;
145
+
146
+ Assert . AreEqual ( expected , actual ) ;
147
+ }
148
+
149
+ private int ArrangeAndGetInspectionCount ( string code )
150
+ {
54
151
var builder = new MockVbeBuilder ( ) ;
55
152
var project = builder . ProjectBuilder ( "TestProject1" , "TestProject1" , ProjectProtection . Unprotected )
56
- . AddComponent ( "Class1 " , ComponentType . ClassModule , inputCode )
153
+ . AddComponent ( "Module1 " , ComponentType . StandardModule , code )
57
154
. AddReference ( "Excel" , MockVbeBuilder . LibraryPathMsExcel , 1 , 8 , true )
58
155
. Build ( ) ;
59
156
var vbe = builder . AddProject ( project ) . Build ( ) ;
@@ -64,7 +161,7 @@ Dim sheet As Worksheet
64
161
var inspection = new ImplicitActiveWorkbookReferenceInspection ( state ) ;
65
162
var inspectionResults = inspection . GetInspectionResults ( CancellationToken . None ) ;
66
163
67
- Assert . AreEqual ( 0 , inspectionResults . Count ( ) ) ;
164
+ return inspectionResults . Count ( ) ;
68
165
}
69
166
}
70
167
0 commit comments