Skip to content

Commit d36f47a

Browse files
committed
More tests, remove dead code
1 parent 54887ac commit d36f47a

File tree

3 files changed

+123
-10
lines changed

3 files changed

+123
-10
lines changed

RetailCoder.VBE/Refactorings/ReorderParameters/ReorderParametersRefactoring.cs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -122,14 +122,6 @@ private void RewriteCall(VBAParser.ArgsCallContext paramList, CodeModule module)
122122

123123
if (parameterStringIndex <= -1) { continue; }
124124

125-
if (_model.Parameters.ElementAt(parameterIndex).Index >= paramNames.Count)
126-
{
127-
newContent = newContent.Insert(parameterStringIndex, " , ");
128-
i--;
129-
parameterIndex++;
130-
continue;
131-
}
132-
133125
var oldParameterString = paramNames.ElementAt(i);
134126
var newParameterString = paramNames.ElementAt(_model.Parameters.ElementAt(parameterIndex).Index);
135127
var beginningSub = newContent.Substring(0, parameterStringIndex);

RubberduckTests/Refactoring/RemoveParametersTests.cs

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
24
using System.Windows.Forms;
35
using Microsoft.Vbe.Interop;
46
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -1057,7 +1059,7 @@ Private Sub IClass1_DoSomething(ByVal a As Integer )
10571059
}
10581060

10591061
[TestMethod]
1060-
public void ReorderParametersRefactoring_ParamsSwapped_RejectPrompt()
1062+
public void RemoveParametersRefactoring_LastParamRemoved_RejectPrompt()
10611063
{
10621064
//Input
10631065
const string inputCode1 =
@@ -1093,6 +1095,42 @@ Private Sub IClass1_DoSomething(ByVal a As Integer, ByVal b As String)
10931095
Assert.AreEqual(null, model.TargetDeclaration);
10941096
}
10951097

1098+
[TestMethod]
1099+
public void RemoveParams_RefactorDeclaration_FailsInvalidTarget()
1100+
{
1101+
//Input
1102+
const string inputCode =
1103+
@"Private Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
1104+
End Sub";
1105+
var selection = new Selection(1, 23, 1, 27); //startLine, startCol, endLine, endCol
1106+
1107+
//Arrange
1108+
var project = SetupMockProject(inputCode);
1109+
var parseResult = new RubberduckParser().Parse(project.Object);
1110+
1111+
var qualifiedSelection = GetQualifiedSelection(selection);
1112+
1113+
//set up model
1114+
var model = new RemoveParametersModel(parseResult, qualifiedSelection, null);
1115+
1116+
var factory = SetupFactory(model);
1117+
1118+
//act
1119+
var refactoring = new RemoveParametersRefactoring(factory.Object);
1120+
1121+
//assert
1122+
try
1123+
{
1124+
refactoring.Refactor(
1125+
model.Declarations.Items.FirstOrDefault(
1126+
i => i.DeclarationType == Rubberduck.Parsing.Symbols.DeclarationType.Class));
1127+
}
1128+
catch (ArgumentException e)
1129+
{
1130+
Assert.AreEqual("Invalid declaration type", e.Message);
1131+
}
1132+
}
1133+
10961134
#region setup
10971135
private static Mock<IRefactoringPresenterFactory<IRemoveParametersPresenter>> SetupFactory(RemoveParametersModel model)
10981136
{

RubberduckTests/Refactoring/ReorderParametersTests.cs

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,89 @@ Public Sub Goo(ByVal arg As Date, _
740740
ByVal arg6 As Integer)
741741
742742
Foo arg, ""test"", test1x, test2x, test3x, test4x, test5x, test6x
743+
End Sub
744+
";
745+
746+
//Arrange
747+
var project = SetupMockProject(inputCode);
748+
var module = project.Object.VBComponents.Item(0).CodeModule;
749+
var parseResult = new RubberduckParser().Parse(project.Object);
750+
751+
var qualifiedSelection = GetQualifiedSelection(selection);
752+
753+
//Specify Params to reorder
754+
var model = new ReorderParametersModel(parseResult, qualifiedSelection, null);
755+
var reorderedParams = new List<Parameter>()
756+
{
757+
model.Parameters[1],
758+
model.Parameters[0]
759+
};
760+
761+
model.Parameters = reorderedParams;
762+
763+
//SetupFactory
764+
var factory = SetupFactory(model);
765+
766+
var messageBox = new Mock<IMessageBox>();
767+
messageBox.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), MessageBoxButtons.OK, MessageBoxIcon.Warning)).Returns(DialogResult.OK);
768+
769+
//Act
770+
var refactoring = new ReorderParametersRefactoring(factory.Object, messageBox.Object);
771+
refactoring.Refactor(qualifiedSelection);
772+
773+
//Assert
774+
Assert.AreEqual(expectedCode, module.Lines());
775+
}
776+
777+
[TestMethod]
778+
public void ReorderParametersRefactoring_ClientReferencesAreUpdated_ParamArray_CallOnMultiplelines()
779+
{
780+
//Input
781+
const string inputCode =
782+
@"Sub Foo(ByVal arg1 As String, ByVal arg2 As Date, ParamArray arg3())
783+
End Sub
784+
785+
Public Sub Goo(ByVal arg As Date, _
786+
ByVal arg1 As Integer, _
787+
ByVal arg2 As Integer, _
788+
ByVal arg3 As Integer, _
789+
ByVal arg4 As Integer, _
790+
ByVal arg5 As Integer, _
791+
ByVal arg6 As Integer)
792+
793+
Foo ""test"", _
794+
arg, _
795+
test1x, _
796+
test2x, _
797+
test3x, _
798+
test4x, _
799+
test5x, _
800+
test6x
801+
End Sub
802+
";
803+
var selection = new Selection(1, 23, 1, 27); //startLine, startCol, endLine, endCol
804+
805+
//Expectation
806+
const string expectedCode =
807+
@"Sub Foo(ByVal arg2 As Date, ByVal arg1 As String, ParamArray arg3())
808+
End Sub
809+
810+
Public Sub Goo(ByVal arg As Date, _
811+
ByVal arg1 As Integer, _
812+
ByVal arg2 As Integer, _
813+
ByVal arg3 As Integer, _
814+
ByVal arg4 As Integer, _
815+
ByVal arg5 As Integer, _
816+
ByVal arg6 As Integer)
817+
818+
Foo arg, ""test"", test1x, test2x, test3x, test4x, test5x, test6x
819+
820+
821+
822+
823+
824+
825+
743826
End Sub
744827
";
745828

0 commit comments

Comments
 (0)