Skip to content

Commit 480eefa

Browse files
committed
Add more tests around refactoring input validation
1 parent c6ec12b commit 480eefa

File tree

8 files changed

+358
-53
lines changed

8 files changed

+358
-53
lines changed

Rubberduck.Refactorings/ExtractInterface/ExtractInterfaceRefactoring.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public ExtractInterfaceRefactoring(IDeclarationFinderProvider declarationFinderP
3434
protected override Declaration FindTargetDeclaration(QualifiedSelection targetSelection)
3535
{
3636
var candidates = _declarationFinderProvider.DeclarationFinder
37-
.AllUserDeclarations
38-
.Where(item => ModuleTypes.Contains(item.DeclarationType)).ToList();
37+
.Members(targetSelection.QualifiedName)
38+
.Where(item => ModuleTypes.Contains(item.DeclarationType));
3939

4040
return candidates.SingleOrDefault(item =>
4141
item.QualifiedSelection.QualifiedName.Equals(targetSelection.QualifiedName));
@@ -50,7 +50,7 @@ protected override ExtractInterfaceModel InitializeModel(Declaration target)
5050

5151
if (!ModuleTypes.Contains(target.DeclarationType))
5252
{
53-
throw new InvalidTargetDeclarationException(target);
53+
throw new InvalidDeclarationTypeException(target);
5454
}
5555

5656
return new ExtractInterfaceModel(_declarationFinderProvider, target);

Rubberduck.Refactorings/MoveCloserToUsage/MoveCloserToUsageRefactoring.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ private void CheckThatTargetIsValid(Declaration target)
4444
throw new TargetDeclarationIsNullException();
4545
}
4646

47-
if (target.DeclarationType != DeclarationType.Variable)
47+
if (!target.IsUserDefined)
4848
{
49-
throw new InvalidDeclarationTypeException(target);
49+
throw new TargetDeclarationNotUserDefinedException(target);
5050
}
5151

52-
if (!target.IsUserDefined)
52+
if (target.DeclarationType != DeclarationType.Variable)
5353
{
54-
throw new TargetDeclarationNotUserDefinedException(target);
54+
throw new InvalidDeclarationTypeException(target);
5555
}
5656

5757
if (!target.References.Any())

RubberduckTests/Refactoring/EncapsulateFieldTests.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,36 @@ End Property
4646
Assert.AreEqual(expectedCode, actualCode);
4747
}
4848

49+
[Test]
50+
[Category("Refactorings")]
51+
[Category("Encapsulate Field")]
52+
public void EncapsulatePublicField_InvalidDeclarationType_Throws()
53+
{
54+
//Input
55+
const string inputCode =
56+
@"Public fizz As Integer";
57+
58+
var presenterAction = SetParameters("Name", implementLet: true);
59+
var actualCode = RefactoredCode(inputCode, "TestModule1", DeclarationType.ProceduralModule, presenterAction, typeof(InvalidDeclarationTypeException));
60+
Assert.AreEqual(inputCode, actualCode);
61+
}
62+
63+
[Test]
64+
[Category("Refactorings")]
65+
[Category("Encapsulate Field")]
66+
public void EncapsulatePublicField_InvalidIdentifierSelected_Throws()
67+
{
68+
//Input
69+
const string inputCode =
70+
@"Public Function fizz() As Integer
71+
End Function";
72+
var selection = new Selection(1, 19);
73+
74+
var presenterAction = SetParameters("Name", implementLet: true);
75+
var actualCode = RefactoredCode(inputCode, selection, presenterAction, typeof(NoDeclarationForSelectionException));
76+
Assert.AreEqual(inputCode, actualCode);
77+
}
78+
4979
[Test]
5080
[Category("Refactorings")]
5181
[Category("Encapsulate Field")]

RubberduckTests/Refactoring/ExtractInterfaceTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,66 @@ End Sub
6565
Assert.AreEqual(expectedInterfaceCode, actualInterfaceCode);
6666
}
6767

68+
[Test]
69+
[Category("Refactorings")]
70+
[Category("Extract Interface")]
71+
public void ExtractInterfaceRefactoring_InvalidTargetType_Throws()
72+
{
73+
//Input
74+
const string inputCode =
75+
@"Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
76+
End Sub";
77+
78+
Func<ExtractInterfaceModel, ExtractInterfaceModel> presenterAction = model =>
79+
{
80+
foreach (var interfaceMember in model.Members)
81+
{
82+
interfaceMember.IsSelected = true;
83+
}
84+
85+
return model;
86+
};
87+
88+
var actualCode = RefactoredCode(
89+
"Module",
90+
DeclarationType.ProceduralModule,
91+
presenterAction,
92+
typeof(InvalidDeclarationTypeException),
93+
("Module", inputCode, ComponentType.StandardModule));
94+
Assert.AreEqual(inputCode, actualCode["Module"]);
95+
}
96+
97+
[Test]
98+
[Category("Refactorings")]
99+
[Category("Extract Interface")]
100+
public void ExtractInterfaceRefactoring_NoValidTargetSelected_Throws()
101+
{
102+
//Input
103+
const string inputCode =
104+
@"Public Sub Foo(ByVal arg1 As Integer, ByVal arg2 As String)
105+
End Sub";
106+
var selection = new Selection(1, 23, 1, 27);
107+
108+
Func<ExtractInterfaceModel, ExtractInterfaceModel> presenterAction = model =>
109+
{
110+
foreach (var interfaceMember in model.Members)
111+
{
112+
interfaceMember.IsSelected = true;
113+
}
114+
115+
return model;
116+
};
117+
118+
var actualCode = RefactoredCode(
119+
"Module",
120+
selection,
121+
presenterAction,
122+
typeof(NoDeclarationForSelectionException),
123+
false,
124+
("Module", inputCode, ComponentType.StandardModule));
125+
Assert.AreEqual(inputCode, actualCode["Module"]);
126+
}
127+
68128
[Test]
69129
[Category("Refactorings")]
70130
[Category("Extract Interface")]

RubberduckTests/Refactoring/ImplementInterfaceTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Rubberduck.Parsing.Symbols;
66
using Rubberduck.Parsing.VBA;
77
using Rubberduck.Refactorings;
8+
using Rubberduck.Refactorings.Exceptions;
89
using Rubberduck.Refactorings.Exceptions.ImplementInterface;
910
using Rubberduck.Refactorings.ImplementInterface;
1011
using Rubberduck.VBEditor;

0 commit comments

Comments
 (0)