Skip to content

Commit 20edeb7

Browse files
committed
Cover Reorder presenter and factory
1 parent 3e10f87 commit 20edeb7

File tree

4 files changed

+184
-13
lines changed

4 files changed

+184
-13
lines changed

RetailCoder.VBE/Refactorings/ReorderParameters/ReorderParametersPresenter.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ public class ReorderParametersPresenter : IReorderParametersPresenter
1212
{
1313
private readonly IReorderParametersView _view;
1414
private readonly ReorderParametersModel _model;
15+
private readonly IMessageBox _messageBox;
1516

16-
public ReorderParametersPresenter(IReorderParametersView view, ReorderParametersModel model)
17+
public ReorderParametersPresenter(IReorderParametersView view, ReorderParametersModel model, IMessageBox messageBox)
1718
{
1819
_view = view;
1920
_model = model;
21+
_messageBox = messageBox;
2022
}
2123

2224
public ReorderParametersModel Show()
@@ -26,7 +28,7 @@ public ReorderParametersModel Show()
2628
if (_model.Parameters.Count < 2)
2729
{
2830
var message = string.Format(RubberduckUI.ReorderPresenter_LessThanTwoParametersError, _model.TargetDeclaration.IdentifierName);
29-
MessageBox.Show(message, RubberduckUI.ReorderParamsDialog_TitleText, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
31+
_messageBox.Show(message, RubberduckUI.ReorderParamsDialog_TitleText, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
3032
return null;
3133
}
3234

RetailCoder.VBE/Refactorings/ReorderParameters/ReorderParametersPresenterFactory.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ public class ReorderParametersPresenterFactory : IRefactoringPresenterFactory<IR
99
private readonly IActiveCodePaneEditor _editor;
1010
private readonly IReorderParametersView _view;
1111
private readonly VBProjectParseResult _parseResult;
12+
private readonly IMessageBox _messageBox;
1213

1314
public ReorderParametersPresenterFactory(IActiveCodePaneEditor editor, IReorderParametersView view,
14-
VBProjectParseResult parseResult)
15+
VBProjectParseResult parseResult, IMessageBox messageBox)
1516
{
1617
_editor = editor;
1718
_view = view;
1819
_parseResult = parseResult;
20+
_messageBox = messageBox;
1921
}
2022

2123
public IReorderParametersPresenter Create()
@@ -26,8 +28,8 @@ public IReorderParametersPresenter Create()
2628
return null;
2729
}
2830

29-
var model = new ReorderParametersModel(_parseResult, selection.Value, new RubberduckMessageBox());
30-
return new ReorderParametersPresenter(_view, model);
31+
var model = new ReorderParametersModel(_parseResult, selection.Value, _messageBox);
32+
return new ReorderParametersPresenter(_view, model, _messageBox);
3133
}
3234
}
3335
}

RetailCoder.VBE/UI/RefactorMenu.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -460,7 +460,7 @@ private void ReorderParameters(QualifiedSelection selection)
460460

461461
using (var view = new ReorderParametersDialog())
462462
{
463-
var factory = new ReorderParametersPresenterFactory(_editor, view, result);
463+
var factory = new ReorderParametersPresenterFactory(_editor, view, result, new RubberduckMessageBox());
464464
var refactoring = new ReorderParametersRefactoring(factory, new RubberduckMessageBox());
465465
refactoring.Refactor(selection);
466466
}

RubberduckTests/Refactoring/ReorderParametersTests.cs

Lines changed: 174 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1256,13 +1256,7 @@ Private Sub abc_Foo(ByVal arg2 As String, ByVal arg1 As Integer)
12561256

12571257
//Specify Params to remove
12581258
var model = new ReorderParametersModel(parseResult, qualifiedSelection, null);
1259-
var reorderedParams = new List<Parameter>()
1260-
{
1261-
model.Parameters[1],
1262-
model.Parameters[0]
1263-
};
1264-
1265-
model.Parameters = reorderedParams;
1259+
model.Parameters.Reverse();
12661260

12671261
//SetupFactory
12681262
var factory = SetupFactory(model);
@@ -1276,6 +1270,179 @@ Private Sub abc_Foo(ByVal arg2 As String, ByVal arg1 As Integer)
12761270
Assert.AreEqual(expectedCode2, module2.Lines());
12771271
}
12781272

1273+
1274+
1275+
[TestMethod]
1276+
public void Presenter_Accept_ReturnsModelWithParametersChanged()
1277+
{
1278+
//Input
1279+
const string inputCode =
1280+
@"Private Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
1281+
End Sub";
1282+
var selection = new Selection(1, 15, 1, 15); //startLine, startCol, endLine, endCol
1283+
1284+
//Arrange
1285+
var project = SetupMockProject(inputCode);
1286+
var parseResult = new RubberduckParser().Parse(project.Object);
1287+
1288+
var qualifiedSelection = GetQualifiedSelection(selection);
1289+
1290+
var editor = new Mock<IActiveCodePaneEditor>();
1291+
editor.Setup(e => e.GetSelection()).Returns(qualifiedSelection);
1292+
1293+
var model = new ReorderParametersModel(parseResult, qualifiedSelection, new RubberduckMessageBox());
1294+
model.Parameters.Reverse();
1295+
1296+
var view = new Mock<IReorderParametersView>();
1297+
view.Setup(v => v.ShowDialog()).Returns(DialogResult.OK);
1298+
view.Setup(v => v.Parameters).Returns(model.Parameters);
1299+
1300+
var factory = new ReorderParametersPresenterFactory(editor.Object, view.Object,
1301+
parseResult, null);
1302+
1303+
var presenter = factory.Create();
1304+
1305+
Assert.AreEqual(model.Parameters, presenter.Show().Parameters);
1306+
}
1307+
1308+
[TestMethod]
1309+
public void Presenter_Reject_ReturnsNull()
1310+
{
1311+
//Input
1312+
const string inputCode =
1313+
@"Private Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
1314+
End Sub";
1315+
var selection = new Selection(1, 15, 1, 15); //startLine, startCol, endLine, endCol
1316+
1317+
//Arrange
1318+
var project = SetupMockProject(inputCode);
1319+
var parseResult = new RubberduckParser().Parse(project.Object);
1320+
1321+
var qualifiedSelection = GetQualifiedSelection(selection);
1322+
1323+
var editor = new Mock<IActiveCodePaneEditor>();
1324+
editor.Setup(e => e.GetSelection()).Returns(qualifiedSelection);
1325+
1326+
var model = new ReorderParametersModel(parseResult, qualifiedSelection, new RubberduckMessageBox());
1327+
1328+
var view = new Mock<IReorderParametersView>();
1329+
view.Setup(v => v.ShowDialog()).Returns(DialogResult.Cancel);
1330+
view.Setup(v => v.Parameters).Returns(model.Parameters);
1331+
1332+
var factory = new ReorderParametersPresenterFactory(editor.Object, view.Object,
1333+
parseResult, null);
1334+
1335+
var presenter = factory.Create();
1336+
1337+
Assert.AreEqual(null, presenter.Show());
1338+
}
1339+
1340+
[TestMethod]
1341+
public void Presenter_NoParams()
1342+
{
1343+
//Input
1344+
const string inputCode =
1345+
@"Private Sub Foo()
1346+
End Sub";
1347+
var selection = new Selection(1, 15, 1, 15); //startLine, startCol, endLine, endCol
1348+
1349+
//Arrange
1350+
var project = SetupMockProject(inputCode);
1351+
var parseResult = new RubberduckParser().Parse(project.Object);
1352+
1353+
var qualifiedSelection = GetQualifiedSelection(selection);
1354+
1355+
var editor = new Mock<IActiveCodePaneEditor>();
1356+
editor.Setup(e => e.GetSelection()).Returns(qualifiedSelection);
1357+
1358+
var messageBox = new Mock<IMessageBox>();
1359+
messageBox.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxButtons>(), It.IsAny<MessageBoxIcon>())).Returns(DialogResult.OK);
1360+
1361+
var factory = new ReorderParametersPresenterFactory(editor.Object, null,
1362+
parseResult, messageBox.Object);
1363+
1364+
var presenter = factory.Create();
1365+
1366+
Assert.AreEqual(null, presenter.Show());
1367+
}
1368+
1369+
[TestMethod]
1370+
public void Presenter_OneParam()
1371+
{
1372+
//Input
1373+
const string inputCode =
1374+
@"Private Sub Foo(ByVal arg1 As Integer)
1375+
End Sub";
1376+
var selection = new Selection(1, 15, 1, 15); //startLine, startCol, endLine, endCol
1377+
1378+
//Arrange
1379+
var project = SetupMockProject(inputCode);
1380+
var parseResult = new RubberduckParser().Parse(project.Object);
1381+
1382+
var qualifiedSelection = GetQualifiedSelection(selection);
1383+
1384+
var editor = new Mock<IActiveCodePaneEditor>();
1385+
editor.Setup(e => e.GetSelection()).Returns(qualifiedSelection);
1386+
1387+
var messageBox = new Mock<IMessageBox>();
1388+
messageBox.Setup(m => m.Show(It.IsAny<string>(), It.IsAny<string>(), It.IsAny<MessageBoxButtons>(), It.IsAny<MessageBoxIcon>())).Returns(DialogResult.OK);
1389+
1390+
var factory = new ReorderParametersPresenterFactory(editor.Object, null,
1391+
parseResult, messageBox.Object);
1392+
1393+
var presenter = factory.Create();
1394+
1395+
Assert.AreEqual(null, presenter.Show());
1396+
}
1397+
1398+
[TestMethod]
1399+
public void Presenter_TargetIsNull()
1400+
{
1401+
//Input
1402+
const string inputCode =
1403+
@"
1404+
Private Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
1405+
End Sub";
1406+
var selection = new Selection(1, 1, 1, 1); //startLine, startCol, endLine, endCol
1407+
1408+
//Arrange
1409+
var project = SetupMockProject(inputCode);
1410+
var parseResult = new RubberduckParser().Parse(project.Object);
1411+
1412+
var qualifiedSelection = GetQualifiedSelection(selection);
1413+
1414+
var editor = new Mock<IActiveCodePaneEditor>();
1415+
editor.Setup(e => e.GetSelection()).Returns(qualifiedSelection);
1416+
1417+
var factory = new ReorderParametersPresenterFactory(editor.Object, null,
1418+
parseResult, null);
1419+
1420+
var presenter = factory.Create();
1421+
1422+
Assert.AreEqual(null, presenter.Show());
1423+
}
1424+
1425+
[TestMethod]
1426+
public void Factory_SelectionIsNull()
1427+
{
1428+
//Input
1429+
const string inputCode =
1430+
@"Private Sub Foo()
1431+
End Sub";
1432+
1433+
//Arrange
1434+
var project = SetupMockProject(inputCode);
1435+
var parseResult = new RubberduckParser().Parse(project.Object);
1436+
1437+
var editor = new Mock<IActiveCodePaneEditor>();
1438+
editor.Setup(e => e.GetSelection()).Returns((QualifiedSelection?)null);
1439+
1440+
var factory = new ReorderParametersPresenterFactory(editor.Object, null,
1441+
parseResult, null);
1442+
1443+
Assert.AreEqual(null, factory.Create());
1444+
}
1445+
12791446
#region setup
12801447
private static Mock<IRefactoringPresenterFactory<IReorderParametersPresenter>> SetupFactory(ReorderParametersModel model)
12811448
{

0 commit comments

Comments
 (0)