-
Notifications
You must be signed in to change notification settings - Fork 300
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #694 from ckuhn203/next
ExtractMethodRefactoring test harness
- Loading branch information
Showing
10 changed files
with
179 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Rubberduck.Parsing.Symbols; | ||
using Rubberduck.Parsing.VBA; | ||
using Rubberduck.Refactorings; | ||
using Rubberduck.Refactorings.ExtractMethod; | ||
using Rubberduck.VBEditor; | ||
using Rubberduck.VBEditor.Extensions; | ||
|
||
namespace RubberduckTests.Refactoring | ||
{ | ||
[TestClass] | ||
public class ExtractMethodTests : RefactoringTestBase | ||
{ | ||
[TestMethod] | ||
public void ExtractMethod_PrivateFunction() | ||
{ | ||
const string inputCode = @" | ||
Private Sub Foo() | ||
Dim x As Integer | ||
x = 1 + 2 | ||
End Sub"; | ||
|
||
const string expectedCode = @" | ||
Private Sub Foo() | ||
x = Bar() | ||
End Sub | ||
Private Function Bar() As Integer | ||
Dim x As Integer | ||
x = 1 + 2 | ||
Bar = x | ||
End Function | ||
"; | ||
|
||
SetupProject(inputCode); | ||
|
||
var qualifiedSelection = GetQualifiedSelection(new Selection(4,1,4,20)); | ||
|
||
var parseResult = new RubberduckParser().Parse(Project.Object); | ||
|
||
var editor = new ActiveCodePaneEditor(IDE.Object); | ||
|
||
var model = new ExtractMethodModel(editor, parseResult.Declarations, qualifiedSelection); | ||
model.Method.Accessibility = Accessibility.Private; | ||
model.Method.MethodName = "Bar"; | ||
model.Method.ReturnValue = new ExtractedParameter("Integer", ExtractedParameter.PassedBy.ByVal, "x"); | ||
model.Method.Parameters = new List<ExtractedParameter>(); | ||
|
||
var factory = SetupFactory(model); | ||
|
||
//act | ||
var refactoring = new ExtractMethodRefactoring(factory.Object, editor); | ||
refactoring.Refactor(qualifiedSelection); | ||
|
||
//assert | ||
Assert.AreEqual(expectedCode, Module.Object.Lines()); | ||
} | ||
|
||
private static Mock<IRefactoringPresenterFactory<IExtractMethodPresenter>> SetupFactory(ExtractMethodModel model) | ||
{ | ||
var presenter = new Mock<IExtractMethodPresenter>(); | ||
presenter.Setup(p => p.Show()).Returns(model); | ||
|
||
var factory = new Mock<IRefactoringPresenterFactory<IExtractMethodPresenter>>(); | ||
factory.Setup(f => f.Create()).Returns(presenter.Object); | ||
return factory; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.Collections.Generic; | ||
using Microsoft.Vbe.Interop; | ||
using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
using Moq; | ||
using Rubberduck.VBEditor; | ||
using RubberduckTests.Mocks; | ||
using MockFactory = RubberduckTests.Mocks.MockFactory; | ||
|
||
namespace RubberduckTests.Refactoring | ||
{ | ||
public abstract class RefactoringTestBase | ||
{ | ||
protected Mock<VBProject> Project; | ||
protected Mock<VBComponent> Component; | ||
protected Mock<CodeModule> Module; | ||
protected Mock<VBE> IDE; | ||
|
||
[TestCleanup] | ||
public void CleanUp() | ||
{ | ||
Project = null; | ||
Component = null; | ||
Module = null; | ||
} | ||
|
||
protected QualifiedSelection GetQualifiedSelection(Selection selection) | ||
{ | ||
return new QualifiedSelection(new QualifiedModuleName(Component.Object), selection); | ||
} | ||
|
||
protected void SetupProject(string inputCode) | ||
{ | ||
var window = MockFactory.CreateWindowMock(string.Empty); | ||
var windows = new MockWindowsCollection(window.Object); | ||
|
||
IDE = MockFactory.CreateVbeMock(windows); | ||
|
||
var codePane = MockFactory.CreateCodePaneMock(IDE, window); | ||
|
||
IDE.SetupGet(vbe => vbe.ActiveCodePane).Returns(codePane.Object); | ||
|
||
Module = MockFactory.CreateCodeModuleMock(inputCode, codePane.Object); | ||
|
||
codePane.SetupGet(p => p.CodeModule).Returns(Module.Object); | ||
|
||
Project = MockFactory.CreateProjectMock("VBAProject", vbext_ProjectProtection.vbext_pp_none); | ||
|
||
Component = MockFactory.CreateComponentMock("Module1", Module.Object, vbext_ComponentType.vbext_ct_StdModule); | ||
|
||
var components = MockFactory.CreateComponentsMock(new List<VBComponent>() { Component.Object }); | ||
components.SetupGet(c => c.Parent).Returns(Project.Object); | ||
|
||
Project.SetupGet(p => p.VBComponents).Returns(components.Object); | ||
Component.SetupGet(c => c.Collection).Returns(components.Object); | ||
} | ||
} | ||
} |
Oops, something went wrong.