@@ -20,6 +20,60 @@ namespace RubberduckTests.Refactoring
20
20
[ TestClass ]
21
21
public class RenameTests : VbeTestBase
22
22
{
23
+ [ TestMethod ]
24
+ public void RenameRefactoring_RenameSub_Issue2727 ( )
25
+ {
26
+ //Input
27
+ string inputCode = GetIssue2727ExampleCode ( ) ;
28
+
29
+ Selection selection = Select2727Variable ( ) ;
30
+
31
+ //New name provided by the user - no conflicts
32
+ var userEnteredName = "Value" ;
33
+
34
+ //Expectation
35
+ //Expecation is that the messageBox.Show() is not invoked
36
+
37
+ //Arrange
38
+ var builder = new MockVbeBuilder ( ) ;
39
+ IVBComponent component ;
40
+ var vbe = builder . BuildFromSingleStandardModule ( inputCode , out component , selection ) ;
41
+ var project = vbe . Object . VBProjects [ 0 ] ;
42
+ var module = project . VBComponents [ 0 ] . CodeModule ;
43
+ var mockHost = new Mock < IHostApplication > ( ) ;
44
+ mockHost . SetupAllProperties ( ) ;
45
+ var parser = MockParser . Create ( vbe . Object , new RubberduckParserState ( vbe . Object ) ) ;
46
+
47
+ parser . Parse ( new CancellationTokenSource ( ) ) ;
48
+ if ( parser . State . Status >= ParserState . Error ) { Assert . Inconclusive ( "Parser Error" ) ; }
49
+
50
+ var qualifiedSelection = new QualifiedSelection ( new QualifiedModuleName ( component ) , selection ) ;
51
+
52
+ var msgbox = new Mock < IMessageBox > ( ) ;
53
+ msgbox . Setup ( m => m . Show ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , MessageBoxButtons . YesNo , It . IsAny < MessageBoxIcon > ( ) ) )
54
+ . Returns ( DialogResult . Yes ) ;
55
+
56
+ var vbeWrapper = vbe . Object ;
57
+ var model = new RenameModel ( vbeWrapper , parser . State , qualifiedSelection , msgbox . Object ) { NewName = userEnteredName } ;
58
+
59
+ //SetupFactory
60
+ var factory = SetupFactory ( model ) ;
61
+
62
+ //Act
63
+ var refactoring = new RenameRefactoring ( vbeWrapper , factory . Object , msgbox . Object , parser . State ) ;
64
+ refactoring . Refactor ( qualifiedSelection ) ;
65
+
66
+ //Assert
67
+ //#2727 bug describes a scenario where a declaration collision is detected where none exists.
68
+ //The result of detecting one or more collisions is that the messagebox is presented to the user
69
+ //To see if he wants to continue with the Renaming process.
70
+ //To pass this test, FindDeclarationForIdentifier() should find zero collisions and therefore
71
+ //skips the logic that presents the message box to the user.
72
+ string failMsg = "RenameRefactoring found a conflicting declaration where none exists." ;
73
+ msgbox . Verify ( m => m . Show ( It . IsAny < string > ( ) , It . IsAny < string > ( ) , MessageBoxButtons . YesNo , It . IsAny < MessageBoxIcon > ( ) ) , Times . Never , failMsg ) ;
74
+ }
75
+
76
+
23
77
[ TestMethod ]
24
78
public void RenameRefactoring_RenameSub ( )
25
79
{
@@ -1421,5 +1475,64 @@ private static Mock<IRefactoringPresenterFactory<IRenamePresenter>> SetupFactory
1421
1475
}
1422
1476
1423
1477
#endregion
1478
+
1479
+ //Module code taken directly from Issue #2727 - choosing to rename "ic" in 'Let Industry'
1480
+ //resulted in a false-positive name collision with parameter 'Value' in 'Let IndustryCode'.
1481
+ private string GetIssue2727ExampleCode ( )
1482
+ {
1483
+ return
1484
+ @"
1485
+ Option Explicit
1486
+ '@folder ""Data Objects""
1487
+
1488
+ Private pName As String
1489
+ Private pIndustryCode As Long
1490
+ Private pIndustry As String
1491
+ Private pLastYearAppts As Long
1492
+ Private pLastYearEmail As Long
1493
+
1494
+ Public Property Get IndustryCode() As String
1495
+ IndustryCode = pIndustryCode
1496
+ End Property
1497
+ Public Property Let IndustryCode(ByVal Value As String)
1498
+ pIndustryCode = Value
1499
+ End Property
1500
+
1501
+ Public Property Get Industry() As String
1502
+ Industry = pIndustry
1503
+ End Property
1504
+ Public Property Let Industry(ByVal ic As String)
1505
+ pIndustry = ic
1506
+ End Property
1507
+
1508
+ Public Property Get LastYearAppts() As Long
1509
+ LastYearAppts = pLastYearAppts
1510
+ End Property
1511
+ Public Property Let LastYearAppts(ByVal Value As Long)
1512
+ pLastYearAppts = Value
1513
+ End Property
1514
+ " ;
1515
+ }
1516
+ private Selection Select2727Variable ( )
1517
+ {
1518
+ var inputCode = GetIssue2727ExampleCode ( ) ;
1519
+ //Create the selection
1520
+ var splitToken = new string [ ] { "\r \n " } ;
1521
+ const string renameTarget = " ic " ;
1522
+
1523
+ var lines = inputCode . Split ( splitToken , System . StringSplitOptions . None ) ;
1524
+ int lineNumber = 0 ;
1525
+ for ( int idx = 0 ; idx < lines . Count ( ) & lineNumber < 1 ; idx ++ )
1526
+ {
1527
+ if ( lines [ idx ] . Contains ( renameTarget ) )
1528
+ {
1529
+ lineNumber = idx + 1 ;
1530
+ }
1531
+ }
1532
+ var column = lines [ lineNumber - 1 ] . IndexOf ( renameTarget ) + 3 ; /*places cursor between the 'i' and 'c'*/
1533
+ var selection = new Selection ( lineNumber , column , lineNumber , column ) ;
1534
+ return selection ;
1535
+ }
1536
+
1424
1537
}
1425
1538
}
0 commit comments