@@ -263,14 +263,14 @@ public void GivenArrayParameter_ReturnsNoResult()
263
263
264
264
[ Test ]
265
265
[ Category ( "Inspections" ) ]
266
- public void ParameterCanBeByVal_ReturnsResult_PassedToByRefProc_NoAssignment ( )
266
+ public void ParameterCanBeByVal_DoesNotReturnResult_PassedToByRefProc_NoAssignment ( )
267
267
{
268
268
const string inputCode =
269
269
@"Sub DoSomething(foo As Integer)
270
270
DoSomethingElse foo
271
271
End Sub
272
272
273
- Sub DoSomethingElse(ByVal bar As Integer)
273
+ Sub DoSomethingElse(ByRef bar As Integer)
274
274
End Sub" ;
275
275
var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out _ ) ;
276
276
using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
@@ -279,7 +279,7 @@ Sub DoSomethingElse(ByVal bar As Integer)
279
279
var inspection = new ParameterCanBeByValInspection ( state ) ;
280
280
var inspectionResults = inspection . GetInspectionResults ( CancellationToken . None ) ;
281
281
282
- Assert . AreEqual ( 1 , inspectionResults . Count ( ) ) ;
282
+ Assert . IsFalse ( inspectionResults . Any ( result => result . Target . IdentifierName . Equals ( "foo" ) ) ) ;
283
283
}
284
284
}
285
285
@@ -308,14 +308,37 @@ Sub DoSomethingElse(ByRef bar As Integer)
308
308
309
309
[ Test ]
310
310
[ Category ( "Inspections" ) ]
311
- public void ParameterCanBeByVal_ReturnsResult_PassedToByValProc_WithAssignment ( )
311
+ public void ParameterCanBeByVal_ReturnsResult_PassedToByRefProc_ExplicitlyByVal ( )
312
312
{
313
313
const string inputCode =
314
314
@"Sub DoSomething(foo As Integer)
315
- DoSomethingElse foo
315
+ DoSomethingElse (foo)
316
+ End Sub
317
+
318
+ Sub DoSomethingElse(ByRef bar As Integer)
319
+ bar = 42
320
+ End Sub" ;
321
+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out _ ) ;
322
+ using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
323
+ {
324
+
325
+ var inspection = new ParameterCanBeByValInspection ( state ) ;
326
+ var inspectionResults = inspection . GetInspectionResults ( CancellationToken . None ) ;
327
+
328
+ Assert . AreEqual ( 1 , inspectionResults . Count ( ) ) ;
329
+ }
330
+ }
331
+
332
+ [ Test ]
333
+ [ Category ( "Inspections" ) ]
334
+ public void ParameterCanBeByVal_ReturnsResult_PassedToByRefProc_PartOfExpression ( )
335
+ {
336
+ const string inputCode =
337
+ @"Sub DoSomething(foo As Integer)
338
+ DoSomethingElse foo + 2
316
339
End Sub
317
340
318
- Sub DoSomethingElse(ByVal bar As Integer)
341
+ Sub DoSomethingElse(ByRef bar As Integer)
319
342
bar = 42
320
343
End Sub" ;
321
344
var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out _ ) ;
@@ -329,6 +352,94 @@ Sub DoSomethingElse(ByVal bar As Integer)
329
352
}
330
353
}
331
354
355
+ [ Test ]
356
+ [ Category ( "Inspections" ) ]
357
+ public void ParameterCanBeByVal_ReturnsResult_PassedToByValEvent ( )
358
+ {
359
+ const string inputCode =
360
+ @" Public Event Bar(ByVal baz As Integer)
361
+
362
+ Sub DoSomething(foo As Integer)
363
+ RaiseEvent Bar(foo)
364
+ End Sub" ;
365
+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out _ ) ;
366
+ using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
367
+ {
368
+
369
+ var inspection = new ParameterCanBeByValInspection ( state ) ;
370
+ var inspectionResults = inspection . GetInspectionResults ( CancellationToken . None ) ;
371
+
372
+ Assert . AreEqual ( 1 , inspectionResults . Count ( ) ) ;
373
+ }
374
+ }
375
+
376
+ [ Test ]
377
+ [ Category ( "Inspections" ) ]
378
+ public void ParameterCanBeByVal_DoesNotReturnResult_PassedToByRefEvent ( )
379
+ {
380
+ const string inputCode =
381
+ @" Public Event Bar(ByRef baz As Integer)
382
+
383
+ Sub DoSomething(foo As Integer)
384
+ RaiseEvent Bar(foo)
385
+ End Sub" ;
386
+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out _ ) ;
387
+ using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
388
+ {
389
+
390
+ var inspection = new ParameterCanBeByValInspection ( state ) ;
391
+ var inspectionResults = inspection . GetInspectionResults ( CancellationToken . None ) ;
392
+
393
+ Assert . IsFalse ( inspectionResults . Any ( result => result . Target . IdentifierName . Equals ( "foo" ) ) ) ;
394
+ }
395
+ }
396
+
397
+
398
+ [ Test ]
399
+ [ Category ( "Inspections" ) ]
400
+ public void ParameterCanBeByVal_ReturnsResult_PassedToByRefEvent_ExplicilyByVal ( )
401
+ {
402
+ const string inputCode =
403
+ @" Public Event Bar(ByRef baz As Integer)
404
+
405
+ Sub DoSomething(foo As Integer)
406
+ RaiseEvent Bar(BYVAL foo)
407
+ End Sub" ;
408
+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out _ ) ;
409
+ using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
410
+ {
411
+
412
+ var inspection = new ParameterCanBeByValInspection ( state ) ;
413
+ var inspectionResults = inspection . GetInspectionResults ( CancellationToken . None ) ;
414
+ var fooInspectionResults = inspectionResults . Where ( result => result . Target . IdentifierName . Equals ( "foo" ) ) ;
415
+
416
+ Assert . AreEqual ( 1 , fooInspectionResults . Count ( ) ) ;
417
+ }
418
+ }
419
+
420
+
421
+ [ Test ]
422
+ [ Category ( "Inspections" ) ]
423
+ public void ParameterCanBeByVal_ReturnsResult_PassedToByRefEvent_PartOfExpression ( )
424
+ {
425
+ const string inputCode =
426
+ @" Public Event Bar(ByRef baz As Integer)
427
+
428
+ Sub DoSomething(foo As Integer)
429
+ RaiseEvent Bar(foo + 2)
430
+ End Sub" ;
431
+ var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out _ ) ;
432
+ using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
433
+ {
434
+
435
+ var inspection = new ParameterCanBeByValInspection ( state ) ;
436
+ var inspectionResults = inspection . GetInspectionResults ( CancellationToken . None ) ;
437
+ var fooInspectionResults = inspectionResults . Where ( result => result . Target . IdentifierName . Equals ( "foo" ) ) ;
438
+
439
+ Assert . AreEqual ( 1 , fooInspectionResults . Count ( ) ) ;
440
+ }
441
+ }
442
+
332
443
[ Test ]
333
444
[ Category ( "Inspections" ) ]
334
445
public void ParameterCanBeByVal_Ignored_DoesNotReturnResult ( )
0 commit comments