@@ -13,44 +13,36 @@ namespace RubberduckTests.Grammar
13
13
[ TestClass ]
14
14
public class SelectionExtensionsTests
15
15
{
16
- public class CollectorVBAParserBaseVisitor < Result > : VBAParserBaseVisitor < IList < Result > >
16
+ public class CollectorVBAParserBaseVisitor < Result > : VBAParserBaseVisitor < IEnumerable < Result > >
17
17
{
18
- private List < Result > defaultResult = new List < Result > ( ) ;
18
+ protected override IEnumerable < Result > DefaultResult => new List < Result > ( ) ;
19
19
20
- protected override IList < Result > DefaultResult => defaultResult ;
21
- /*
22
- protected override IList<Result> AggregateResult(IList<Result> firstResult, IList<Result> secondResult)
20
+ protected override IEnumerable < Result > AggregateResult ( IEnumerable < Result > firstResult , IEnumerable < Result > secondResult )
23
21
{
24
- if (firstResult != null && secondResult != null)
25
- return firstResult.Concat(secondResult) as IList<Result>;
26
-
27
- if (secondResult == null)
28
- return firstResult;
29
-
30
- return secondResult;
31
- }*/
22
+ return firstResult . Concat ( secondResult ) ;
23
+ }
32
24
}
33
25
34
26
public class SubStmtContextElementCollectorVisitor : CollectorVBAParserBaseVisitor < SubStmtContext >
35
27
{
36
- public override IList < SubStmtContext > VisitSubStmt ( [ NotNull ] SubStmtContext context )
28
+ public override IEnumerable < SubStmtContext > VisitSubStmt ( [ NotNull ] SubStmtContext context )
37
29
{
38
- DefaultResult . Add ( context ) ;
39
- return base . VisitSubStmt ( context ) ;
30
+ return new List < SubStmtContext > { context } ;
40
31
}
41
32
}
42
33
43
34
public class IfStmtContextElementCollectorVisitor : CollectorVBAParserBaseVisitor < IfStmtContext >
44
35
{
45
- public override IList < IfStmtContext > VisitIfStmt ( [ NotNull ] IfStmtContext context )
36
+ public override IEnumerable < IfStmtContext > VisitIfStmt ( [ NotNull ] IfStmtContext context )
46
37
{
47
- DefaultResult . Add ( context ) ;
48
- return base . VisitIfStmt ( context ) ;
38
+ base . VisitIfStmt ( context ) ;
39
+ return new List < IfStmtContext > { context } ;
49
40
}
50
41
}
51
42
52
43
[ TestMethod ]
53
44
[ TestCategory ( "Grammar" ) ]
45
+ [ TestCategory ( "Selection" ) ]
54
46
public void Context_Not_In_Selection_ZeroBased_EvilCode ( )
55
47
{
56
48
const string inputCode = @"
78
70
79
71
[ TestMethod ]
80
72
[ TestCategory ( "Grammar" ) ]
73
+ [ TestCategory ( "Selection" ) ]
81
74
public void Context_In_Selection_OneBased_EvilCode ( )
82
75
{
83
76
const string inputCode = @"
@@ -105,6 +98,63 @@ End _
105
98
106
99
[ TestMethod ]
107
100
[ TestCategory ( "Grammar" ) ]
101
+ [ TestCategory ( "Selection" ) ]
102
+ public void Context_Not_In_Selection_Start_OneBased_EvilCode ( )
103
+ {
104
+ const string inputCode = @"
105
+ Option Explicit
106
+
107
+ Public _
108
+ Sub _
109
+ foo()
110
+
111
+ Debug.Print ""foo""
112
+
113
+ End _
114
+ Sub : 'Lame comment!
115
+ " ;
116
+
117
+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out var component ) ;
118
+ var state = MockParser . CreateAndParse ( vbe . Object ) ;
119
+ var tree = state . GetParseTree ( new QualifiedModuleName ( component ) ) ;
120
+ var visitor = new SubStmtContextElementCollectorVisitor ( ) ;
121
+ var context = visitor . Visit ( tree ) . First ( ) ;
122
+ var selection = new Selection ( 5 , 1 , 11 , 8 ) ;
123
+
124
+ Assert . IsFalse ( context . Contains ( selection ) ) ;
125
+ }
126
+
127
+ [ TestMethod ]
128
+ [ TestCategory ( "Grammar" ) ]
129
+ [ TestCategory ( "Selection" ) ]
130
+ public void Context_Not_In_Selection_End_OneBased_EvilCode ( )
131
+ {
132
+ const string inputCode = @"
133
+ Option Explicit
134
+
135
+ Public _
136
+ Sub _
137
+ foo()
138
+
139
+ Debug.Print ""foo""
140
+
141
+ End _
142
+ Sub : 'Lame comment!
143
+ " ;
144
+
145
+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out var component ) ;
146
+ var state = MockParser . CreateAndParse ( vbe . Object ) ;
147
+ var tree = state . GetParseTree ( new QualifiedModuleName ( component ) ) ;
148
+ var visitor = new SubStmtContextElementCollectorVisitor ( ) ;
149
+ var context = visitor . Visit ( tree ) . First ( ) ;
150
+ var selection = new Selection ( 4 , 1 , 10 , 8 ) ;
151
+
152
+ Assert . IsFalse ( context . Contains ( selection ) ) ;
153
+ }
154
+
155
+ [ TestMethod ]
156
+ [ TestCategory ( "Grammar" ) ]
157
+ [ TestCategory ( "Selection" ) ]
108
158
public void Context_In_GetSelection_OneBased_EvilCode ( )
109
159
{
110
160
const string inputCode = @"
@@ -133,6 +183,7 @@ End _
133
183
134
184
[ TestMethod ]
135
185
[ TestCategory ( "Grammar" ) ]
186
+ [ TestCategory ( "Selection" ) ]
136
187
public void Context_Not_In_GetSelection_ZeroBased ( )
137
188
{
138
189
const string inputCode = @"
@@ -158,6 +209,7 @@ Public Sub foo()
158
209
159
210
[ TestMethod ]
160
211
[ TestCategory ( "Grammar" ) ]
212
+ [ TestCategory ( "Selection" ) ]
161
213
public void Context_In_GetSelection_OneBased ( )
162
214
{
163
215
const string inputCode = @"
@@ -183,6 +235,7 @@ Public Sub foo()
183
235
184
236
[ TestMethod ]
185
237
[ TestCategory ( "Grammar" ) ]
238
+ [ TestCategory ( "Selection" ) ]
186
239
public void Context_In_Selection_OneBased ( )
187
240
{
188
241
const string inputCode = @"
@@ -208,6 +261,7 @@ Public Sub foo()
208
261
209
262
[ TestMethod ]
210
263
[ TestCategory ( "Grammar" ) ]
264
+ [ TestCategory ( "Selection" ) ]
211
265
public void Context_NotIn_Selection_StartTooSoon_OneBased ( )
212
266
{
213
267
const string inputCode = @"
@@ -233,6 +287,7 @@ Public Sub foo()
233
287
234
288
[ TestMethod ]
235
289
[ TestCategory ( "Grammar" ) ]
290
+ [ TestCategory ( "Selection" ) ]
236
291
public void Context_NotIn_Selection_EndsTooSoon_OneBased ( )
237
292
{
238
293
const string inputCode = @"
@@ -293,6 +348,7 @@ End If
293
348
294
349
[ TestMethod ]
295
350
[ TestCategory ( "Grammar" ) ]
351
+ [ TestCategory ( "Selection" ) ]
296
352
public void Context_Not_In_Selection_SecondBlock_OneBased ( )
297
353
{
298
354
const string inputCode = @"
@@ -328,6 +384,7 @@ End If
328
384
329
385
[ TestMethod ]
330
386
[ TestCategory ( "Grammar" ) ]
387
+ [ TestCategory ( "Selection" ) ]
331
388
public void Context_In_Selection_SecondBlock_OneBased ( )
332
389
{
333
390
const string inputCode = @"
@@ -363,6 +420,7 @@ End If
363
420
364
421
[ TestMethod ]
365
422
[ TestCategory ( "Grammar" ) ]
423
+ [ TestCategory ( "Selection" ) ]
366
424
public void Selection_Contains_LastToken ( )
367
425
{
368
426
const string inputCode = @"
0 commit comments