Skip to content

Commit 8108d18

Browse files
committed
Resolved all reviews and change request.
Added tests for `CanExecute`.
1 parent 6903773 commit 8108d18

File tree

5 files changed

+54
-9
lines changed

5 files changed

+54
-9
lines changed

Rubberduck.Core/UI/CodeExplorer/Commands/CodeExplorerExtractInterfaceCommand.cs

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Rubberduck.Navigation.CodeExplorer;
22
using Rubberduck.Parsing.VBA;
3+
using Rubberduck.Refactorings.Exceptions;
34
using Rubberduck.Refactorings.ExtractInterface;
45
using Rubberduck.UI.Command.Refactorings.Notifiers;
56
using Rubberduck.VBEditor.Events;
@@ -30,6 +31,7 @@ public CodeExplorerExtractInterfaceCommand(
3031
_refactoring = refactoring;
3132
_failureNotifier = failureNotifier;
3233
AddToCanExecuteEvaluation(SpecialEvaluateCanExecute);
34+
AddToOnExecuteEvaluation(FurtherCanExecuteEvaluation);
3335
}
3436

3537
public sealed override IEnumerable<Type> ApplicableNodeTypes => ApplicableNodes;
@@ -38,19 +40,28 @@ private bool SpecialEvaluateCanExecute(object parameter)
3840
{
3941
return _state.Status == ParserState.Ready &&
4042
parameter is CodeExplorerComponentViewModel node &&
41-
ExtractInterfaceRefactoring.CanExecute((RubberduckParserState)_state, node.QualifiedSelection.Value.QualifiedName);
43+
_refactoring.CanExecute((RubberduckParserState)_state, node.QualifiedSelection.Value.QualifiedName);
44+
}
45+
46+
private bool FurtherCanExecuteEvaluation(object parameter)
47+
{
48+
return _state.Status == ParserState.Ready &&
49+
parameter is CodeExplorerItemViewModel node &&
50+
node.Declaration != null;
4251
}
4352

4453
protected override void OnExecute(object parameter)
4554
{
46-
if (_state.Status != ParserState.Ready ||
47-
!(parameter is CodeExplorerItemViewModel node) ||
48-
node.Declaration == null)
55+
try
4956
{
50-
return;
57+
_refactoring.Refactor(((CodeExplorerItemViewModel)parameter).Declaration);
58+
}
59+
catch (RefactoringAbortedException)
60+
{ }
61+
catch (RefactoringException exception)
62+
{
63+
_failureNotifier.Notify(exception);
5164
}
52-
53-
_refactoring.Refactor(node.Declaration);
5465
}
5566
}
5667
}

Rubberduck.Core/UI/Command/Refactorings/RefactorExtractInterfaceCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ private bool SpecializedEvaluateCanExecute(object parameter)
3232
{
3333
return false;
3434
}
35-
return ExtractInterfaceRefactoring.CanExecute(_state, activeSelection.Value.QualifiedName);
35+
return ((ExtractInterfaceRefactoring)Refactoring).CanExecute(_state, activeSelection.Value.QualifiedName);
3636
}
3737

3838
private static readonly IReadOnlyList<DeclarationType> ModuleTypes = new[]

Rubberduck.Refactorings/ExtractInterface/ExtractInterfaceRefactoring.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ private string GetInterfaceModuleBody(ExtractInterfaceModel model)
137137
DeclarationType.UserForm
138138
};
139139

140-
public static bool CanExecute(RubberduckParserState state, QualifiedModuleName qualifiedName)
140+
public bool CanExecute(RubberduckParserState state, QualifiedModuleName qualifiedName)
141141
{
142142
var interfaceClass = state.AllUserDeclarations.SingleOrDefault(item =>
143143
item.QualifiedName.QualifiedModuleName.Equals(qualifiedName)

RubberduckTests/CodeExplorer/CodeExplorerViewModelTests.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,31 @@ public bool AddUserDocument_CanExecuteBasedOnProjectType(ProjectType projectType
245245
}
246246
}
247247

248+
[Category("Code Explorer")]
249+
[Test]
250+
[TestCase(ComponentType.ActiveXDesigner, ExpectedResult = true)]
251+
[TestCase(ComponentType.ClassModule, ExpectedResult = true)]
252+
[TestCase(ComponentType.ComComponent, ExpectedResult = true)]
253+
[TestCase(ComponentType.DocObject, ExpectedResult = true)]
254+
[TestCase(ComponentType.Document, ExpectedResult = true)]
255+
[TestCase(ComponentType.MDIForm, ExpectedResult = true)]
256+
[TestCase(ComponentType.PropPage, ExpectedResult = true)]
257+
[TestCase(ComponentType.RelatedDocument, ExpectedResult = false, Ignore = "Module doesn't contain members")]
258+
[TestCase(ComponentType.ResFile, ExpectedResult = false, Ignore = "Module doesn't contain members")]
259+
[TestCase(ComponentType.StandardModule, ExpectedResult = false)]
260+
[TestCase(ComponentType.Undefined, ExpectedResult = true)]
261+
[TestCase(ComponentType.UserControl, ExpectedResult = true)]
262+
[TestCase(ComponentType.UserForm, ExpectedResult = true)]
263+
[TestCase(ComponentType.VBForm, ExpectedResult = true)]
264+
public bool RefactorExtractInterface_CanExecuteBasedOnComponentType(ComponentType componentType)
265+
{
266+
using (var explorer = new MockedCodeExplorer(ProjectType.HostProject, componentType, @"Public Sub Foo(): MsgBox """":End Sub ")
267+
.ImplementExtractIntercaceCommand().SelectFirstModule())
268+
{
269+
return explorer.ViewModel.CodeExplorerExtractInterfaceCommand.CanExecute(explorer.ViewModel.SelectedItem);
270+
}
271+
}
272+
248273
[Category("Code Explorer")]
249274
[Test]
250275
public void AddTestModule()

RubberduckTests/CodeExplorer/MockedCodeExplorer.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,15 @@ public MockedCodeExplorer ImplementIndenterCommand()
403403
return this;
404404
}
405405

406+
public MockedCodeExplorer ImplementExtractIntercaceCommand()
407+
{
408+
ViewModel.CodeExplorerExtractInterfaceCommand = new CodeExplorerExtractInterfaceCommand(
409+
new Rubberduck.Refactorings.ExtractInterface.ExtractInterfaceRefactoring(
410+
State, State, null, null, null),
411+
State, null, VbeEvents.Object);
412+
return this;
413+
}
414+
406415
public MockedCodeExplorer ConfigureSaveDialog(string path, DialogResult result)
407416
{
408417
SaveDialog.Setup(o => o.FileName).Returns(path);

0 commit comments

Comments
 (0)