Skip to content

Commit

Permalink
Improve test coverage of refactor rename regarding input validation
Browse files Browse the repository at this point in the history
  • Loading branch information
MDoerner committed Mar 26, 2019
1 parent c7cc8f0 commit c6ec12b
Show file tree
Hide file tree
Showing 5 changed files with 129 additions and 8 deletions.
5 changes: 0 additions & 5 deletions Rubberduck.Refactorings/Rename/RenameRefactoring.cs
Expand Up @@ -51,11 +51,6 @@ protected override Declaration FindTargetDeclaration(QualifiedSelection targetSe

protected override RenameModel InitializeModel(Declaration target)
{
if (target == null)
{
throw new TargetDeclarationIsNullException();
}

CheckWhetherValidTarget(target);

var model = DeriveTarget(new RenameModel(target));
Expand Down
33 changes: 33 additions & 0 deletions RubberduckTests/Refactoring/ImplementInterfaceTests.cs
@@ -1,18 +1,51 @@
using System;
using System.Linq;
using NUnit.Framework;
using Rubberduck.Parsing.Rewriter;
using Rubberduck.Parsing.Symbols;
using Rubberduck.Parsing.VBA;
using Rubberduck.Refactorings;
using Rubberduck.Refactorings.Exceptions.ImplementInterface;
using Rubberduck.Refactorings.ImplementInterface;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.SafeComWrappers;
using Rubberduck.VBEditor.Utility;
using RubberduckTests.Mocks;

namespace RubberduckTests.Refactoring
{
[TestFixture]
public class ImplementInterfaceTests : RefactoringTestBase
{
[Test]
[Category("Refactorings")]
[Category("Implement Interface")]
public override void TargetNull_Throws()
{
var testVbe = TestVbe(string.Empty, out _);
var (state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(testVbe);
using (state)
{
var refactoring = TestRefactoring(rewritingManager, state);
Assert.Throws<NotSupportedException>(() => refactoring.Refactor((Declaration)null));
}
}

[Test]
[Category("Refactorings")]
[Category("Implement Interface")]
public void DoesNotSupportCallingWithADeclaration()
{
var testVbe = TestVbe(("testClass", string.Empty, ComponentType.ClassModule));
var (state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(testVbe);
using (state)
{
var target = state.DeclarationFinder.UserDeclarations(DeclarationType.ClassModule).Single();
var refactoring = TestRefactoring(rewritingManager, state);
Assert.Throws<NotSupportedException>(() => refactoring.Refactor(target));
}
}

[Test]
[Category("Refactorings")]
[Category("Implement Interface")]
Expand Down
Expand Up @@ -97,7 +97,7 @@ protected string RefactoredCode(string code, string declarationName, Declaration
var (state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe);
using (state)
{
var target = state.DeclarationFinder.UserDeclarations(declarationType)
var target = state.DeclarationFinder.DeclarationsWithType(declarationType)
.Single(declaration => declaration.IdentifierName == declarationName);

var refactoring = TestRefactoring(rewritingManager, state, presenterAdjustment);
Expand Down
16 changes: 14 additions & 2 deletions RubberduckTests/Refactoring/RefactoringTestBase.cs
Expand Up @@ -21,7 +21,6 @@ public abstract class RefactoringTestBase
{
[Test]
[Category("Refactorings")]
[Category("Introduce Field")]
public void NoActiveSelection_Throws()
{
var rewritingManager = new Mock<IRewritingManager>().Object;
Expand All @@ -30,6 +29,19 @@ public void NoActiveSelection_Throws()
Assert.Throws<NoActiveSelectionException>(() => refactoring.Refactor());
}

[Test]
[Category("Refactorings")]
public virtual void TargetNull_Throws()
{
var testVbe = TestVbe(string.Empty, out _);
var (state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(testVbe);
using (state)
{
var refactoring = TestRefactoring(rewritingManager, state);
Assert.Throws<TargetDeclarationIsNullException>(() => refactoring.Refactor((Declaration)null));
}
}

protected string RefactoredCode(string code, Selection selection, Type expectedException = null, bool executeViaActiveSelection = false)
{
var vbe = TestVbe(code, out _);
Expand All @@ -49,7 +61,7 @@ protected string RefactoredCode(string code, Selection selection, Type expectedE
var (state, rewritingManager) = MockParser.CreateAndParseWithRewritingManager(vbe);
using (state)
{
var module = state.DeclarationFinder.UserDeclarations(DeclarationType.Module)
var module = state.DeclarationFinder.DeclarationsWithType(DeclarationType.Module)
.Single(declaration => declaration.IdentifierName == selectedComponentName)
.QualifiedModuleName;
var qualifiedSelection = new QualifiedSelection(module, selection);
Expand Down
81 changes: 81 additions & 0 deletions RubberduckTests/Refactoring/Rename/RenameTests.cs
Expand Up @@ -16,6 +16,7 @@
using Rubberduck.Parsing.VBA;
using Rubberduck.Refactorings;
using Rubberduck.Refactorings.Exceptions;
using Rubberduck.Refactorings.Exceptions.Rename;
using Rubberduck.UI.Refactorings;
using Rubberduck.VBEditor.Utility;

Expand Down Expand Up @@ -2646,6 +2647,86 @@ public void Rename_PresenterIsNull()
Assert.AreEqual(inputCode, actualCode);
}
}

[Category("Refactorings")]
[Category("Rename")]
[TestCase("Class_Initialize")]
[TestCase("Class_Terminate")]
public void Rename_StandardEventHandler(string handlerName)
{
var inputCode =
$@"Private Sub {handlerName}()
End Sub";

var presenterAction = AdjustName("test");

var actualCode = RefactoredCode(
handlerName,
DeclarationType.Procedure,
presenterAction,
typeof(TargetDeclarationIsStandardEventHandlerException),
("TestClass", inputCode, ComponentType.ClassModule));
Assert.AreEqual(inputCode, actualCode["TestClass"]);
}

[Test]
[Category("Refactorings")]
[Category("Rename")]
public void Rename_NotUserDefined()
{
const string inputCode =
@"Private Sub Foo()
Dim bar As Color|ScaleCriteria
End Sub";

var tdo = new RenameTestsDataObject(declarationName: "ColorScaleCriteria", newName: "Goo", declarationType: DeclarationType.ClassModule);
var inputOutput1 = new RenameTestModuleDefinition("Class1")
{
Input = inputCode,
Expected = inputCode.Replace("|", string.Empty)
};
tdo.UseLibraries = true;
tdo.ExpectedException = typeof(TargetDeclarationNotUserDefinedException);

PerformExpectedVersusActualRenameTests(tdo, inputOutput1);
}

[Test]
[Category("Refactorings")]
[Category("Rename")]
[Ignore("Something is off with the project id of the implemented class: it does not agree with the project id of the exposing library.")]
public void Rename_ImplementedInterfaceNotUserDefined()
{
var inputCode =
@"Implements PivotFields
Private Property Get Pivo|tFields_Count() As Long
End Property
Private Function PivotFields_Item(Index) As Object
End Function
Private Property Get PivotFields_Application() As Application
End Property
Private Property Get PivotFields_Creator() As XlCreator
End Property
Private Property Get PivotFields_Parent() As PivotTable
End Property
";

var tdo = new RenameTestsDataObject(declarationName: "PivotFields_Count", newName: "Goo", declarationType: DeclarationType.PropertyGet);
var inputOutput1 = new RenameTestModuleDefinition("Class1")
{
Input = inputCode,
Expected = inputCode.Replace("|", string.Empty)
};
tdo.UseLibraries = true;
tdo.ExpectedException = typeof(TargetDeclarationNotUserDefinedException);

PerformExpectedVersusActualRenameTests(tdo, inputOutput1);
}

[Test]
[Category("Refactorings")]
Expand Down

0 comments on commit c6ec12b

Please sign in to comment.