/
ThunderCodeInspectionTests.cs
357 lines (338 loc) · 9.35 KB
/
ThunderCodeInspectionTests.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using Antlr4.Runtime.Tree;
using NUnit.Framework;
using Rubberduck.CodeAnalysis.Inspections;
using Rubberduck.CodeAnalysis.Inspections.Concrete.ThunderCode;
using Rubberduck.Parsing.VBA;
using Rubberduck.Parsing.VBA.Parsing;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.Extensions;
using RubberduckTests.Mocks;
namespace RubberduckTests.Inspections.ThunderCode
{
[TestFixture]
[Category("Inspections")]
[Category("ThunderCode")]
public class ThunderCodeInspectionTests
{
[Test]
[Category("Inspections")]
[TestCase(1, "Public Sub foo\u00A0bar()" + @"
End Sub")]
[TestCase(0, @"Public Sub foo()
End Sub")]
public void NonBreakingSpaceIdentifier_ReturnsResult(int expectedCount, string inputCode)
{
var func = new Func<RubberduckParserState, IInspection>(state =>
new NonBreakingSpaceIdentifierInspection(state));
ThunderCatsGo(func, inputCode, expectedCount);
}
[Test]
[Category("Inspections")]
[TestCase(0, @"Public Sub foo bar()
End Sub")]
public void NonBreakingSpaceIdentifier_IllegalInputCausesParserError(int expectedCount, string inputCode)
{
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
var parser = MockParser.Create(vbe.Object);
using (var state = parser.State)
{
parser.Parse(new CancellationTokenSource());
var actualStatus = state.Status;
Assert.AreEqual(ParserState.Error, actualStatus);
}
}
[Test]
[TestCase(2, @"Do")]
[TestCase(2, @"Loop")]
[TestCase(2, @"For")]
[TestCase(2, @"Next")]
[TestCase(0, @"Foo")]
[TestCase(0, @"Bar")]
[TestCase(0, @"ForNext")]
[TestCase(0, @"Fur")]
public void KeywordsUsedAsMember_ReturnsResult(int expectedCount, string inputVariable)
{
var inputCode = $@"Private Type HeeHaw
{inputVariable} As Variant
End Type
Private Enum HawHaw
[{inputVariable}] = 1
End Enum";
var func = new Func<RubberduckParserState, IInspection>(state =>
new KeywordsUsedAsMemberInspection(state));
ThunderCatsGo(func, inputCode, expectedCount);
}
[Test]
[TestCase( @"[Do]")]
[TestCase(@"Do ")]
[TestCase(@" Do")]
[TestCase(@"")]
[TestCase(@"[]")]
public void KeywordsUsedAsMember_CorrectlyDistinguishesBracketedIdentifiers(string inputVariable)
{
var inputCode = $@"
Private Enum HawHaw
[{inputVariable}] = 1
End Enum";
var func = new Func<RubberduckParserState, IInspection>(state =>
new KeywordsUsedAsMemberInspection(state));
ThunderCatsGo(func, inputCode, 0);
}
// NOTE: the inspection only covers trivial cases and is not exhaustive
// For that reason, some of test cases which the evil continuations exists
// may still pass without any results. To cover them all would likely be too
// expensive.
[Test]
[TestCase(1, @"Private Sub Foo()
End _
Sub")]
[TestCase(0, @"Private _
Sub Foo
End Sub")]
[TestCase(1, @"Private Function Foo() As Boolean
End _
_
Function")]
[TestCase(0, @"Private _
_
Function Foo() As Boolean
End Function")]
[TestCase(1, @"Private Property _
Get Foo() As String
End Property")]
[TestCase(0, @"Private Property Get Foo() As String
Foo = _
23
End Property")]
[TestCase(1, @"Private Property Get Foo() As String
Foo = 23
End _
Property")]
[TestCase(0, @"Private Property Let Foo(NewValue As String)
Bar = NewValue
End Property")]
[TestCase(1, @"Private Property Let Foo(NewValue As String)
Bar = NewValue
End _
Property")]
[TestCase(2, @"Private Property _
Let Foo(NewValue As String)
Bar = NewValue
End _
Property")]
[TestCase(1, @"Option _
Explicit")]
[TestCase(0, @"Option Explicit")]
[TestCase(1, @"Option Explicit
Option _
Base 1")]
[TestCase(0, @"Option Explicit
Option Base 1")]
[TestCase(1, @"Public Type foo
bar As Variant
End _
Type")]
[TestCase(0, @"Public Type foo
bar _
As _
Variant
End Type")]
[TestCase(1, @"Public Enum foo
bar = 1
End _
Enum", Ignore = "Enum does not allow line continuations")]
[TestCase(0, @"Public Enum bar
foo = 1
End Enum")]
[TestCase(1, @"Private Sub Foo()
On _
Error GoTo 0
End Sub")]
[TestCase(0, @"Private Sub Foo()
On Error GoTo 0
End Sub")]
[TestCase(0, @"Private Sub Foo()
On Local Error GoTo 0
End Sub")]
[TestCase(1, @"Private Sub Foo()
On _
Local Error GoTo 0
End Sub")]
[TestCase(0, @"Private Sub Foo()
For i = 0 to 3
Exit For
Next
End Sub")]
[TestCase(1, @"Private Sub Foo()
For i = 0 to 3
Exit _
For
Next
End Sub")]
public void EvilLineContinuation_ReturnResults(int expectedCount, string inputCode)
{
var func = new Func<RubberduckParserState, IInspection>(state =>
new LineContinuationBetweenKeywordsInspection(state));
ThunderCatsGo(func, inputCode, expectedCount);
}
[Test]
[TestCase(0,@"Public Sub Gogo()
GoTo 1
1:
End Sub")]
[TestCase(0, @"Public Sub Gogo()
GoTo 1
1
End Sub")]
[TestCase(1, @"Public Sub Gogo()
-1:
End Sub")]
[TestCase(1, @"Public Sub Gogo()
-1
End Sub")]
[TestCase(1, @"Public Sub Gogo()
GoTo 1
1
-1:
End Sub")]
[TestCase(2, @"Public Sub Gogo()
GoTo -1
1
-1:
End Sub")]
[TestCase(2, @"Public Sub Gogo()
GoTo -1
1:
-1
End Sub")]
[TestCase(2, @"Public Sub Gogo()
GoTo -5
1
-5:
End Sub")]
[TestCase(1, @"Public Sub Gogo()
On Error GoTo 1
1
-1:
End Sub")]
[TestCase(2, @"Public Sub Gogo()
On Error GoTo -1
1
-1:
End Sub")]
[TestCase(2, @"Public Sub Gogo()
On Error GoTo -1
1:
-1
End Sub")]
[TestCase(0, @"Public Sub Gogo()
On Error GoTo -1
1
End Sub")]
[TestCase(1, @"Public Sub Gogo()
On Error GoTo -2
1
End Sub")]
[TestCase(2, @"Public Sub Gogo()
On Error GoTo -5
1
-5:
End Sub")]
[TestCase(2, @"Public Sub Gogo()
On Error GoTo -5
1:
-5
End Sub")]
public void NegativeLineNumberLabel_ReturnResults(int expectedCount, string inputCode)
{
var func = new Func<RubberduckParserState, IInspection>(state =>
new NegativeLineNumberInspection(state));
ThunderCatsGo(func, inputCode, expectedCount);
}
[Test]
[TestCase(0, @"Public Sub Derp()
On Error GoTo 1
Exit Sub
1
Debug.Print ""derp""
End Sub")]
[TestCase(0, @"Public Sub Derp()
On Error GoTo 1
Exit Sub
1:
Debug.Print ""derp""
End Sub")]
[TestCase(0, @"Public Sub Derp()
On Error GoTo 0
Exit Sub
Debug.Print ""derp""
End Sub")]
[TestCase(1, @"Public Sub Derp()
On Error GoTo -1
Exit Sub
Debug.Print ""derp""
End Sub")]
[TestCase(0, @"Public Sub Derp()
On Error GoTo -2
Exit Sub
-2
Debug.Print ""derp""
End Sub")]
[TestCase(0, @"Public Sub Derp()
On Error GoTo -2
Exit Sub
-2:
Debug.Print ""derp""
End Sub")]
public void OnErrorGoToMinusOne_ReturnResults(int expectedCount, string inputCode)
{
var func = new Func<RubberduckParserState, IInspection>(state =>
new OnErrorGoToMinusOneInspection(state));
ThunderCatsGo(func, inputCode, expectedCount);
}
private static void ThunderCatsGo(Func<RubberduckParserState, IInspection> inspectionFunction, string inputCode, int expectedCount)
{
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
using (var state = MockParser.CreateAndParse(vbe.Object))
{
var inspection = inspectionFunction(state);
var actualResults = InspectionResults(inspection, state);
Assert.AreEqual(expectedCount, actualResults.Count());
}
}
private static IEnumerable<IInspectionResult> InspectionResults(IInspection inspection, RubberduckParserState state)
{
if (inspection is IParseTreeInspection parseTreeInspection)
{
WalkTrees(parseTreeInspection, state);
}
return inspection.GetInspectionResults(CancellationToken.None);
}
private static void WalkTrees(IParseTreeInspection inspection, RubberduckParserState state)
{
var codeKind = inspection.TargetKindOfCode;
var listener = inspection.Listener;
List<KeyValuePair<QualifiedModuleName, IParseTree>> trees;
switch (codeKind)
{
case CodeKind.AttributesCode:
trees = state.AttributeParseTrees;
break;
case CodeKind.CodePaneCode:
trees = state.ParseTrees;
break;
default:
throw new ArgumentOutOfRangeException(nameof(codeKind), codeKind, null);
}
foreach (var (module, tree) in trees)
{
listener.CurrentModuleName = module;
ParseTreeWalker.Default.Walk(listener, tree);
}
}
}
}