Skip to content

Commit 1b9dbdd

Browse files
committed
Add failing test reproducing #4355
1 parent 6cc9ec6 commit 1b9dbdd

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

RubberduckTests/Refactoring/MoveCloserToUsageTests.cs

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,59 @@ Private Sub Foo()
918918
}
919919
}
920920

921+
[Test]
922+
[Category("Move Closer")]
923+
[Category("Refactorings")]
924+
public void MoveCloser_RespectsWithBlockContexts()
925+
{
926+
const string inputCode =
927+
@"
928+
Public Sub foo()
929+
Dim count As Long
930+
Dim report As Worksheet
931+
Set report = ThisWorkbook.ActiveSheet
932+
With report
933+
For count = 1 To 10
934+
If .Cells(1, count) > count Then
935+
.Cells(2, count).Value2 = count
936+
End If
937+
Next
938+
End With
939+
End Sub";
940+
941+
const string expectedCode =
942+
@"
943+
Public Sub foo()
944+
Dim report As Worksheet
945+
Set report = ThisWorkbook.ActiveSheet
946+
With report
947+
Dim count As Long
948+
For count = 1 To 10
949+
If .Cells(1, count) > count Then
950+
.Cells(2, count).Value2 = count
951+
End If
952+
Next
953+
End With
954+
End Sub";
955+
956+
var vbe = MockVbeBuilder.BuildFromSingleStandardModule(inputCode, out var component);
957+
using (var state = MockParser.CreateAndParse(vbe.Object))
958+
{
959+
var qualifiedSelection = state.DeclarationFinder
960+
.UserDeclarations(DeclarationType.Variable)
961+
.Where(d => d.IdentifierName == "count")
962+
.Single().QualifiedSelection;
963+
964+
var refactoring = new MoveCloserToUsageRefactoring(vbe.Object, state, null);
965+
refactoring.Refactor(qualifiedSelection);
966+
967+
var rewriter = state.GetRewriter(component);
968+
Assert.NotNull(rewriter);
969+
Assert.AreEqual(expectedCode, rewriter.GetText());
970+
}
971+
}
972+
973+
921974
[Test]
922975
[Category("Move Closer")]
923976
[Category("Refactorings")]

0 commit comments

Comments
 (0)