3
3
using Rubberduck . Inspections . Concrete . UnreachableCaseInspection ;
4
4
using Rubberduck . Parsing ;
5
5
using Rubberduck . Parsing . Grammar ;
6
+ using Rubberduck . Parsing . Symbols ;
6
7
using Rubberduck . Resources . Inspections ;
7
8
using Rubberduck . VBEditor . SafeComWrappers ;
9
+ using Rubberduck . VBEditor . SafeComWrappers . Abstract ;
8
10
using RubberduckTests . Mocks ;
9
11
using System . Collections . Generic ;
10
12
using System . Linq ;
@@ -1357,10 +1359,10 @@ End Select
1357
1359
Assert . AreEqual ( expectedMsg , actualMsg ) ;
1358
1360
}
1359
1361
1360
- //# 4119
1362
+ //https://github.com/rubberduck-vba/Rubberduck/issues/ 4119
1361
1363
[ Test ]
1362
1364
[ Category ( "Inspections" ) ]
1363
- public void UnreachableCaseInspection_EnumerationIssue4119Scenario ( )
1365
+ public void UnreachableCaseInspection_Enumeration ( )
1364
1366
{
1365
1367
const string inputCode =
1366
1368
@"
@@ -1801,7 +1803,6 @@ End Select
1801
1803
Assert . AreEqual ( expectedMsg , actualMsg ) ;
1802
1804
}
1803
1805
1804
- //Issue# 3885
1805
1806
//this test only proves that the Select Statement is not inspected
1806
1807
[ Test ]
1807
1808
[ Category ( "Inspections" ) ]
@@ -1861,7 +1862,6 @@ End Select
1861
1862
Assert . AreEqual ( expectedMsg , actualMsg ) ;
1862
1863
}
1863
1864
1864
- //Issue# 3885 - replicates with UDT rather than a built-in
1865
1865
[ TestCase ( "Long" ) ]
1866
1866
[ TestCase ( "Variant" ) ]
1867
1867
[ Category ( "Inspections" ) ]
@@ -2258,7 +2258,7 @@ End Select
2258
2258
Assert . AreEqual ( expectedMsg , actualMsg ) ;
2259
2259
}
2260
2260
2261
- //From Issue # 3962
2261
+ //https://github.com/rubberduck-vba/Rubberduck/issues/ 3962
2262
2262
[ Test ]
2263
2263
[ Category ( "Inspections" ) ]
2264
2264
public void UnreachableCaseInspection_AdditionString ( )
@@ -2441,13 +2441,127 @@ End Sub
2441
2441
Assert . AreEqual ( expectedMsg , actualMsg ) ;
2442
2442
}
2443
2443
2444
+ [ TestCase ( "vbBack" , "\b " ) ]
2445
+ [ TestCase ( "vbCrLf" , "\r \n " ) ]
2446
+ [ TestCase ( "vbCr" , "\r " ) ]
2447
+ [ TestCase ( "vbLf" , "\n " ) ]
2448
+ [ TestCase ( "vbFormFeed" , "\f " ) ]
2449
+ [ TestCase ( "vbNewLine" , "\r \n " ) ]
2450
+ [ TestCase ( "vbNullChar" , "\0 " ) ]
2451
+ [ TestCase ( "vbNullString" , null ) ]
2452
+ [ TestCase ( "vbTab" , "\t " ) ]
2453
+ [ TestCase ( "vbVerticalTab" , "\v " ) ]
2454
+ [ Category ( "Inspections" ) ]
2455
+ public void UnreachableCaseInspection_VbStringConstantToLiteral_AreEqual ( string constToken , string expected )
2456
+ {
2457
+ var parseTreeValueVisitor = new ParseTreeValueVisitor ( null , new List < VBAParser . EnumerationStmtContext > ( ) , null ) as ITestParseTreeVisitor ;
2458
+ if ( parseTreeValueVisitor . IsVBStringConstantToLiteral ( constToken , out string literalValue ) )
2459
+ {
2460
+ Assert . AreEqual ( expected , literalValue ) ;
2461
+ return ;
2462
+ }
2463
+ Assert . Fail ( $ "TryConvertVBStringConstantToLiteral failed to convert { constToken } ") ;
2464
+ }
2465
+
2466
+ [ TestCase ( "\r " , true ) ]
2467
+ [ TestCase ( "\r \n " , true ) ]
2468
+ [ TestCase ( "\b " , true ) ]
2469
+ [ TestCase ( "\f " , true ) ]
2470
+ [ TestCase ( "\0 " , true ) ]
2471
+ [ TestCase ( "\t " , true ) ]
2472
+ [ TestCase ( "\v " , true ) ]
2473
+ [ TestCase ( null , false ) ]
2474
+ [ Category ( "Inspections" ) ]
2475
+ public void UnreachableCaseInspection_ControlCharToLiteral_AreEqual ( string constToken , bool expected )
2476
+ {
2477
+ var parseTreeValueVisitor = new ParseTreeValueVisitor ( null , new List < VBAParser . EnumerationStmtContext > ( ) , null ) as ITestParseTreeVisitor ;
2478
+ Assert . AreEqual ( expected , parseTreeValueVisitor . IsNonPrintingControlCharacter ( constToken ) ) ;
2479
+ }
2480
+
2481
+ //https://github.com/rubberduck-vba/Rubberduck/issues/4659
2482
+ [ Test ]
2483
+ [ Category ( "Inspections" ) ]
2484
+ public void UnreachableCaseInspection_VbObjectErrorConstant ( )
2485
+ {
2486
+ var expectedUnreachableCount = 2 ;
2487
+ string inputCode =
2488
+ @"
2489
+ Enum Fubar
2490
+ Foo = vbObjectError + 1
2491
+ Bar = vbObjectError + 2
2492
+ End Enum
2493
+
2494
+ Sub Example(value As Long)
2495
+ Select Case value
2496
+ Case Fubar.Foo
2497
+ Debug.Print ""Foo""
2498
+ Case Fubar.Bar
2499
+ Debug.Print ""Bar""
2500
+ Case vbObjectError + 1 'unreachable
2501
+ Debug.Print ""Unreachable""
2502
+ Case -2147221502 'unreachable
2503
+ Debug.Print ""Unreachable""
2504
+ End Select
2505
+ End Sub
2506
+ " ;
2507
+ ( bool IsType , string ExpressionValue , string TypeName ) TestGetValuedDeclaration ( Declaration declaration )
2508
+ {
2509
+ if ( declaration . IdentifierName . Equals ( "vbObjectError" ) )
2510
+ {
2511
+ return ( true , "-2147221504" , Tokens . Long ) ;
2512
+ }
2513
+ return ( false , null , null ) ;
2514
+ }
2515
+
2516
+ var vbe = CreateStandardModuleProject ( inputCode ) ;
2517
+
2518
+ IEnumerable < Rubberduck . Parsing . Inspections . Abstract . IInspectionResult > actualResults ;
2519
+ using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
2520
+ {
2521
+ var inspection = new UnreachableCaseInspection ( state ) ;
2522
+ var parseTreeValueVisitor = inspection . ParseTreeValueVisitor as ITestParseTreeVisitor ;
2523
+
2524
+ parseTreeValueVisitor . InjectValuedDeclarationEvaluator ( TestGetValuedDeclaration ) ;
2525
+
2526
+ var inspector = InspectionsHelper . GetInspector ( inspection ) ;
2527
+ actualResults = inspector . FindIssuesAsync ( state , CancellationToken . None ) . Result ;
2528
+ }
2529
+
2530
+ var actualUnreachable = actualResults . Where ( ar => ar . Description . Equals ( InspectionResults . UnreachableCaseInspection_Unreachable ) ) ;
2531
+
2532
+ Assert . AreEqual ( expectedUnreachableCount , actualUnreachable . Count ( ) ) ;
2533
+ }
2534
+
2535
+ //https://github.com/rubberduck-vba/Rubberduck/issues/4680
2536
+ [ Test ]
2537
+ [ Ignore ( "Issue 4680" ) ]
2538
+ [ Category ( "Inspections" ) ]
2539
+ public void UnreachableCaseInspection_VbStringConstant ( )
2540
+ {
2541
+ string inputCode =
2542
+ @"
2543
+ Sub Foo(value As String)
2544
+ Select Case value
2545
+ Case ""Hello"" + vbNewLine + ""World""
2546
+ MsgBox ""vbNewLine version""
2547
+ Case ""Hello"" + vbCr + vbLf + ""World"" 'unreachable
2548
+ MsgBox ""vbCr + vbLf version""
2549
+ Case ""Reachable""
2550
+ MsgBox ""Reachable""
2551
+ End Select
2552
+ End Sub
2553
+ " ;
2554
+ ( string expectedMsg , string actualMsg ) = CheckActualResultsEqualsExpected ( inputCode , unreachable : 1 ) ;
2555
+ Assert . AreEqual ( expectedMsg , actualMsg ) ;
2556
+ }
2557
+
2444
2558
private static ( string expectedMsg , string actualMsg ) CheckActualResultsEqualsExpected ( string inputCode , int unreachable = 0 , int mismatch = 0 , int caseElse = 0 , int inherentlyUnreachable = 0 , int overflow = 0 )
2445
2559
{
2446
2560
var components = new List < ( string moduleName , string inputCode ) > ( ) { ( "TestModule1" , inputCode ) } ;
2447
2561
return CheckActualResultsEqualsExpected ( components , unreachable , mismatch , caseElse , inherentlyUnreachable , overflow ) ;
2448
2562
}
2449
2563
2450
- private static ( string expectedMsg , string actualMsg ) CheckActualResultsEqualsExpected ( List < ( string moduleName , string inputBlock ) > inputCode , int unreachable = 0 , int mismatch = 0 , int caseElse = 0 , int inherentlyUnreachable = 0 , int overflow = 0 )
2564
+ private static ( string expectedMsg , string actualMsg ) CheckActualResultsEqualsExpected ( List < ( string moduleName , string inputCode ) > components , int unreachable = 0 , int mismatch = 0 , int caseElse = 0 , int inherentlyUnreachable = 0 , int overflow = 0 )
2451
2565
{
2452
2566
var expected = new Dictionary < string , int >
2453
2567
{
@@ -2458,10 +2572,7 @@ private static (string expectedMsg, string actualMsg) CheckActualResultsEqualsEx
2458
2572
{ InspectionResults . UnreachableCaseInspection_CaseElse , caseElse } ,
2459
2573
} ;
2460
2574
2461
- var builder = new MockVbeBuilder ( ) ;
2462
- var project = builder . ProjectBuilder ( "VBAProject" , ProjectProtection . Unprotected ) ;
2463
- inputCode . ForEach ( input => project . AddComponent ( input . moduleName , NameToComponentType ( input . moduleName ) , input . inputBlock ) ) ;
2464
- var vbe = builder . AddProject ( project . Build ( ) ) . Build ( ) ;
2575
+ var vbe = CreateStandardModuleProject ( components ) ;
2465
2576
2466
2577
IEnumerable < Rubberduck . Parsing . Inspections . Abstract . IInspectionResult > actualResults ;
2467
2578
using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
@@ -2486,6 +2597,17 @@ private static (string expectedMsg, string actualMsg) CheckActualResultsEqualsEx
2486
2597
return ( expectedMsg , actualMsg ) ;
2487
2598
}
2488
2599
2600
+ private Moq . Mock < IVBE > CreateStandardModuleProject ( string inputCode )
2601
+ => CreateStandardModuleProject ( new List < ( string moduleName , string inputCode ) > ( ) { ( "TestModule1" , inputCode ) } ) ;
2602
+
2603
+ private static Moq . Mock < IVBE > CreateStandardModuleProject ( List < ( string moduleName , string inputCode ) > components )
2604
+ {
2605
+ var builder = new MockVbeBuilder ( ) ;
2606
+ var project = builder . ProjectBuilder ( "VBAProject" , ProjectProtection . Unprotected ) ;
2607
+ components . ForEach ( input => project . AddComponent ( input . moduleName , NameToComponentType ( input . moduleName ) , input . inputCode ) ) ;
2608
+ return builder . AddProject ( project . Build ( ) ) . Build ( ) ;
2609
+ }
2610
+
2489
2611
private static ComponentType NameToComponentType ( string name )
2490
2612
=> name . StartsWith ( "Class" ) ? ComponentType . ClassModule : ComponentType . StandardModule ;
2491
2613
0 commit comments