4
4
using Rubberduck . Parsing ;
5
5
using Rubberduck . Parsing . Grammar ;
6
6
using Rubberduck . Parsing . Inspections . Resources ;
7
+ using Rubberduck . VBEditor . SafeComWrappers ;
7
8
using RubberduckTests . Mocks ;
8
9
using System ;
9
10
using System . Collections . Generic ;
@@ -362,12 +363,6 @@ public void UciUnit_LogicBinaryConstants(string operands, string expected)
362
363
Assert . IsTrue ( result . ParsesToConstantValue ) ;
363
364
}
364
365
365
- // Dim A, B, C, D, MyCheck
366
- //A = 10: B = 8: C = 6: D = Null ' Initialize variables.
367
- //MyCheck = A > B Eqv B > C ' Returns True.
368
- //MyCheck = B > A Eqv B > C ' Returns False.
369
- //MyCheck = A > B Eqv B > D ' Returns Null.
370
- //MyCheck = A Eqv B ' Returns -3 (bitwise comparison).
371
366
[ TestCase ( "True_Eqv_True" , "True" ) ]
372
367
[ TestCase ( "False_Eqv_True" , "False" ) ]
373
368
[ TestCase ( "True_Eqv_False" , "False" ) ]
@@ -698,7 +693,7 @@ public void UciUnit_Extents(string typeName)
698
693
* stored as variable RelationalOp expressions.
699
694
*/
700
695
[ TestCase ( "Is < True" , "Single=False" ) ] //Always False
701
- [ TestCase ( "Is <= True" , "RelOp=Is <= True" ) ] //Result depends on Select Case value
696
+ [ TestCase ( "Is <= True" , "RelOp=Is <= True" ) ]
702
697
[ TestCase ( "Is > True" , "RelOp=Is > True" ) ]
703
698
[ TestCase ( "Is >= True" , "Single=True" ) ] //Always True
704
699
[ TestCase ( "Is = True" , "RelOp=Is = True" ) ]
@@ -2247,7 +2242,205 @@ End Select
2247
2242
CheckActualResultsEqualsExpected ( inputCode , unreachable : 1 ) ;
2248
2243
}
2249
2244
2245
+ //Issue# 3885
2246
+ //this test only proves that the Select Statement is not inspected
2247
+ [ Test ]
2248
+ [ Category ( "Inspections" ) ]
2249
+ public void UciFunctional_BuiltInMember ( )
2250
+ {
2251
+ string inputCode =
2252
+ @"
2253
+ Option Explicit
2254
+
2255
+ Sub FooCount(x As Long)
2256
+
2257
+ Select Case err.Number
2258
+ Case ""5903""
2259
+ 'OK
2260
+ Case 5900 + 3
2261
+ 'Unreachable - but undetected by unit tests,
2262
+ Case 5
2263
+ 'Unreachable - but undetected by unit tests,
2264
+ Case 4 + 1
2265
+ 'Unreachable - but undetected by unit tests,
2266
+ End Select
2267
+
2268
+ Select Case x
2269
+ Case ""5""
2270
+ MsgBox ""Foo""
2271
+ Case 2 + 3
2272
+ 'Unreachable - just to make sure the test finds something
2273
+ MsgBox ""Bar""
2274
+ End Select
2275
+ End Sub
2276
+ " ;
2277
+
2278
+ CheckActualResultsEqualsExpected ( inputCode , unreachable : 1 ) ;
2279
+ }
2280
+
2281
+ [ Test ]
2282
+ [ Category ( "Inspections" ) ]
2283
+ public void UciFunctional_BuiltInMemberInCaseClause ( )
2284
+ {
2285
+ string inputCode =
2286
+ @"
2287
+ Option Explicit
2288
+
2289
+ Sub FooCount(x As Long)
2290
+
2291
+ Select Case x
2292
+ Case 5900 + 3
2293
+ 'OK
2294
+ Case err.Number
2295
+ 'OK - not evaluated
2296
+ Case 5903
2297
+ 'Unreachable
2298
+ Case 5900 + 2 + 1
2299
+ 'Unreachable
2300
+ End Select
2301
+ End Sub
2302
+ " ;
2303
+
2304
+ CheckActualResultsEqualsExpected ( inputCode , unreachable : 2 ) ;
2305
+ }
2306
+
2307
+ //Issue# 3885 - replicates with UDT rather than a built-in
2308
+ [ TestCase ( "Long" ) ]
2309
+ [ TestCase ( "Variant" ) ]
2310
+ [ Category ( "Inspections" ) ]
2311
+ public void UciFunctional_MemberAccessor ( string propertyType )
2312
+ {
2313
+ string inputCode =
2314
+ @"
2315
+ Option Explicit
2316
+
2317
+ Sub AddVariable(testClass As Class1)
2318
+ Select Case testClass.AValue
2319
+ Case 5903
2320
+ 'OK
2321
+ Case 5900 + 3
2322
+ 'unreachable
2323
+ Case Else
2324
+ Exit Sub
2325
+ End Select
2326
+ End Sub" ;
2327
+
2328
+ string inputClassCode =
2329
+ @"
2330
+ Option Explicit
2331
+
2332
+ Private myVal As <propertyType>
2333
+
2334
+ Public Property Set AValue(val As <propertyType>)
2335
+ myVal = val
2336
+ End Property
2337
+
2338
+ Public Property Get AValue() As <propertyType>
2339
+ AValue = myVal
2340
+ End Property
2341
+ " ;
2342
+ inputClassCode = inputClassCode . Replace ( "<propertyType>" , propertyType ) ;
2343
+ var components = new List < Tuple < string , string > > ( )
2344
+ {
2345
+ new Tuple < string , string > ( "TestModule1" , inputCode ) ,
2346
+ new Tuple < string , string > ( "Class1" , inputClassCode )
2347
+ } ;
2348
+
2349
+ CheckActualResultsEqualsExpected ( components , unreachable : 1 ) ;
2350
+ }
2351
+
2352
+ [ TestCase ( "Long" ) ]
2353
+ [ TestCase ( "Variant" ) ]
2354
+ [ Category ( "Inspections" ) ]
2355
+ public void UciFunctional_MemberAccessorInCaseClause ( string propertyType )
2356
+ {
2357
+ string inputCode =
2358
+ @"
2359
+ Option Explicit
2360
+
2361
+ Sub AddVariable(x As Long)
2362
+ Select Case x
2363
+ Case 300
2364
+ 'OK
2365
+ Case testClass.AValue
2366
+ 'OK - variable, not value
2367
+ Case 150 + 150
2368
+ 'OK
2369
+ Case 3 * 100
2370
+ 'OK
2371
+ End Select
2372
+ End Sub" ;
2373
+
2374
+ string inputClassCode =
2375
+ @"
2376
+ Option Explicit
2377
+
2378
+ Private myVal As <propertyType>
2379
+
2380
+ Public Property Set AValue(val As <propertyType>)
2381
+ myVal = val
2382
+ End Property
2383
+
2384
+ Public Property Get AValue() As <propertyType>
2385
+ AValue = myVal
2386
+ End Property
2387
+ " ;
2388
+ inputClassCode = inputClassCode . Replace ( "<propertyType>" , propertyType ) ;
2389
+ var components = new List < Tuple < string , string > > ( )
2390
+ {
2391
+ new Tuple < string , string > ( "TestModule1" , inputCode ) ,
2392
+ new Tuple < string , string > ( "Class1" , inputClassCode )
2393
+ } ;
2394
+
2395
+ CheckActualResultsEqualsExpected ( components , unreachable : 2 ) ;
2396
+ }
2397
+
2398
+ [ TestCase ( "Long = 300" ) ]
2399
+ [ Category ( "Inspections" ) ]
2400
+ public void UciFunctional_ConstanInOtherModule ( string propertyType )
2401
+ {
2402
+ string inputCode =
2403
+ @"
2404
+ Option Explicit
2405
+
2406
+ Sub AddVariable(x As Variant)
2407
+ Select Case x
2408
+ Case TestModule2.My_CONSTANT
2409
+ 'OK
2410
+ Case 300
2411
+ 'unreachable
2412
+ Case Else
2413
+ Exit Sub
2414
+ End Select
2415
+ End Sub" ;
2416
+
2417
+ string inputModule2Code =
2418
+ @"
2419
+ Option Explicit
2420
+
2421
+ Public Const MY_CONSTANT As <propertyTypeAndAssignment>
2422
+ " ;
2423
+ inputModule2Code = inputModule2Code . Replace ( "<propertyTypeAndAssignment>" , propertyType ) ;
2424
+ var components = new List < Tuple < string , string > > ( )
2425
+ {
2426
+ new Tuple < string , string > ( "TestModule1" , inputCode ) ,
2427
+ new Tuple < string , string > ( "TestModule2" , inputModule2Code )
2428
+ } ;
2429
+
2430
+ CheckActualResultsEqualsExpected ( components , unreachable : 1 ) ;
2431
+ }
2432
+
2250
2433
private static void CheckActualResultsEqualsExpected ( string inputCode , int unreachable = 0 , int mismatch = 0 , int caseElse = 0 )
2434
+ {
2435
+ var components = new List < Tuple < string , string > > ( )
2436
+ {
2437
+ new Tuple < string , string > ( "TestModule1" , inputCode )
2438
+ } ;
2439
+
2440
+ CheckActualResultsEqualsExpected ( components , unreachable , mismatch , caseElse ) ;
2441
+ }
2442
+
2443
+ private static void CheckActualResultsEqualsExpected ( List < Tuple < string , string > > inputCode , int unreachable = 0 , int mismatch = 0 , int caseElse = 0 )
2251
2444
{
2252
2445
var expected = new Dictionary < string , int >
2253
2446
{
@@ -2256,7 +2449,12 @@ private static void CheckActualResultsEqualsExpected(string inputCode, int unrea
2256
2449
{ InspectionsUI . UnreachableCaseInspection_CaseElse , caseElse } ,
2257
2450
} ;
2258
2451
2259
- var vbe = MockVbeBuilder . BuildFromSingleStandardModule ( inputCode , out var _ ) ;
2452
+ var builder = new MockVbeBuilder ( ) ;
2453
+ var project = builder . ProjectBuilder ( "VBAProject" , ProjectProtection . Unprotected ) ;
2454
+ inputCode . ForEach ( input => project . AddComponent ( input . Item1 , NameToComponentType ( input . Item1 ) , input . Item2 ) ) ;
2455
+ builder = builder . AddProject ( project . Build ( ) ) ;
2456
+ var vbe = builder . Build ( ) ;
2457
+
2260
2458
IEnumerable < Rubberduck . Parsing . Inspections . Abstract . IInspectionResult > actualResults ;
2261
2459
using ( var state = MockParser . CreateAndParse ( vbe . Object ) )
2262
2460
{
@@ -2274,6 +2472,15 @@ private static void CheckActualResultsEqualsExpected(string inputCode, int unrea
2274
2472
Assert . AreEqual ( expectedMsg , actualMsg ) ;
2275
2473
}
2276
2474
2475
+ private static ComponentType NameToComponentType ( string name )
2476
+ {
2477
+ if ( name . StartsWith ( "Class" ) )
2478
+ {
2479
+ return ComponentType . ClassModule ;
2480
+ }
2481
+ return ComponentType . StandardModule ;
2482
+ }
2483
+
2277
2484
private static string BuildResultString ( int unreachableCount , int mismatchCount , int caseElseCount )
2278
2485
{
2279
2486
return $ "Unreachable={ unreachableCount } , Mismatch={ mismatchCount } , CaseElse={ caseElseCount } ";
0 commit comments