Skip to content

Commit e75e9b0

Browse files
committed
Added test to ensure references to removed params are removed
1 parent 2f36a33 commit e75e9b0

File tree

2 files changed

+56
-4
lines changed

2 files changed

+56
-4
lines changed

RubberduckTests/Mocks/MockFactory.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using System;
22
using System.Collections;
33
using System.Collections.Generic;
4+
using System.Linq;
5+
using System.Windows.Forms.VisualStyles;
46
using Microsoft.Vbe.Interop;
57
using Moq;
68

@@ -81,14 +83,16 @@ internal static Mock<VBE> CreateVbeMock(Windows windows, VBProjects projects)
8183
/// <returns></returns>
8284
internal static Mock<CodeModule> CreateCodeModuleMock(string code)
8385
{
84-
var lineCount = code.Split(new [] { Environment.NewLine }, StringSplitOptions.None).Length;
86+
var lines = code.Split(new [] { Environment.NewLine }, StringSplitOptions.None);
8587

8688
var codeModule = new Mock<CodeModule>();
87-
codeModule.SetupGet(c => c.CountOfLines).Returns(lineCount);
89+
codeModule.SetupGet(c => c.CountOfLines).Returns(lines.Length);
8890

8991
// ReSharper disable once UseIndexedProperty
9092
// No R#, the indexed property breaks the expression. I tried that first.
91-
codeModule.SetupGet(c => c.get_Lines(1, lineCount)).Returns(code);
93+
codeModule.Setup(m => m.get_Lines(It.IsAny<int>(), It.IsAny<int>()))
94+
.Returns((int start, int count) => String.Join(Environment.NewLine, lines.Skip(start - 1).Take(count)));
95+
9296
return codeModule;
9397
}
9498

RubberduckTests/Refactoring/RemoveParametersTests.cs

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Linq;
34
using Microsoft.Vbe.Interop;
45
using Microsoft.VisualStudio.TestTools.UnitTesting;
56
using Moq;
@@ -166,6 +167,51 @@ public void RemoveParamatersRefactoring_RemoveSecondParam()
166167
Assert.AreEqual(expectedCode, _module.Object.Lines());
167168
}
168169

170+
[TestMethod]
171+
public void RemoveParamatersRefactoring_ClientReferencesAreUpdated()
172+
{
173+
//Input
174+
const string inputCode =
175+
@"Private Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
176+
End Sub
177+
178+
Private Sub Bar()
179+
Foo 10, ""Hello""
180+
End Sub
181+
";
182+
var selection = new Selection(1, 23, 1, 27); //startLine, startCol, endLine, endCol
183+
184+
//Expectation
185+
const string expectedCode =
186+
@"Private Sub Foo(ByVal arg1 As Integer )
187+
End Sub
188+
189+
Private Sub Bar()
190+
Foo 10
191+
End Sub
192+
"; //note: The IDE strips out the extra whitespace, you can't see it but there's a space after "Foo 10 "
193+
194+
//Arrange
195+
SetupProject(inputCode);
196+
var parseResult = new RubberduckParser().Parse(_project.Object);
197+
198+
var qualifiedSelection = GetQualifiedSelection(selection);
199+
200+
//Specify Param(s) to remove
201+
var model = new RemoveParametersModel(parseResult, qualifiedSelection);
202+
model.Parameters[1].IsRemoved = true;
203+
204+
//SetupFactory
205+
var factory = SetupFactory(model);
206+
207+
//Act
208+
var refactoring = new RemoveParametersRefactoring(factory.Object);
209+
refactoring.Refactor(qualifiedSelection);
210+
211+
//Assert
212+
Assert.AreEqual(expectedCode, _module.Object.Lines());
213+
}
214+
169215
#region setup
170216
private QualifiedSelection GetQualifiedSelection(Selection selection)
171217
{
@@ -201,6 +247,7 @@ private void SetupProject(string inputCode)
201247

202248
_module = Mocks.MockFactory.CreateCodeModuleMock(inputCode);
203249
_module.SetupGet(m => m.CodePane).Returns(codePane.Object);
250+
204251
_module.Setup(m => m.ReplaceLine(It.IsAny<int>(), It.IsAny<string>()))
205252
.Callback<int, string>((i, s) => ReplaceModuleLine(_module, i, s));
206253

@@ -217,14 +264,15 @@ private void SetupProject(string inputCode)
217264

218265
private static void ReplaceModuleLine(Mock<CodeModule> module, int lineNumber, string newLine)
219266
{
220-
var lines = module.Object.Lines().Split(new[] {Environment.NewLine}, StringSplitOptions.None);
267+
var lines = module.Object.Lines().Split(new[] { Environment.NewLine }, StringSplitOptions.None);
221268

222269
lines[lineNumber - 1] = newLine;
223270

224271
var newCode = String.Join(Environment.NewLine, lines);
225272

226273
module.SetupGet(c => c.get_Lines(1, lines.Length)).Returns(newCode);
227274
}
275+
228276
#endregion
229277
}
230278
}

0 commit comments

Comments
 (0)