Skip to content

Commit

Permalink
Changes following PR comments, fixed tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mansellan committed Jul 23, 2019
1 parent 5a424ee commit d421173
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 27 deletions.
8 changes: 4 additions & 4 deletions Rubberduck.Core/UI/CodeExplorer/Commands/DeleteCommand.cs
Expand Up @@ -13,14 +13,14 @@ namespace Rubberduck.UI.CodeExplorer.Commands
public class DeleteCommand : CodeExplorerCommandBase
{
private readonly RemoveCommand _removeCommand;
private readonly IProjectsRepository _projectsRepository;
private readonly IProjectsProvider _projectsProvider;
private readonly IMessageBox _messageBox;
private readonly IVBE _vbe;

public DeleteCommand(RemoveCommand removeCommand, IProjectsRepository projectsRepository, IMessageBox messageBox, IVBE vbe)
public DeleteCommand(RemoveCommand removeCommand, IProjectsProvider projectsProvider, IMessageBox messageBox, IVBE vbe)
{
_removeCommand = removeCommand;
_projectsRepository = projectsRepository;
_projectsProvider = projectsProvider;
_messageBox = messageBox;
_vbe = vbe;

Expand All @@ -41,7 +41,7 @@ protected override void OnExecute(object parameter)
}

var qualifiedModuleName = node.Declaration.QualifiedName.QualifiedModuleName;
var component = _projectsRepository.Component(qualifiedModuleName);
var component = _projectsProvider.Component(qualifiedModuleName);
if (component is null)
{
return;
Expand Down
1 change: 0 additions & 1 deletion Rubberduck.VBEEditor/ComManagement/IProjectsProvider.cs
Expand Up @@ -14,6 +14,5 @@ public interface IProjectsProvider : IDisposable
IEnumerable<(QualifiedModuleName QualifiedModuleName, IVBComponent Component)> Components();
IEnumerable<(QualifiedModuleName QualifiedModuleName, IVBComponent Component)> Components(string projectId);
IVBComponent Component(QualifiedModuleName qualifiedModuleName);
void RemoveComponent(QualifiedModuleName qualifiedModuleName);
}
}
1 change: 1 addition & 0 deletions Rubberduck.VBEEditor/ComManagement/IProjectsRepository.cs
Expand Up @@ -4,5 +4,6 @@ public interface IProjectsRepository : IProjectsProvider
{
void Refresh();
void Refresh(string projectId);
void RemoveComponent(QualifiedModuleName qualifiedModuleName);
}
}
27 changes: 9 additions & 18 deletions Rubberduck.VBEEditor/ComManagement/ProjectsRepository.cs
Expand Up @@ -223,15 +223,15 @@ public IEnumerable<(string ProjectId, IVBProject Project)> LockedProjects()
var readLockTaken = false;
try
{
_refreshProtectionLock.EnterUpgradeableReadLock();
_refreshProtectionLock.EnterReadLock();
readLockTaken = true;
return function.Invoke();
}
finally
{
if (readLockTaken)
{
_refreshProtectionLock.ExitUpgradeableReadLock();
_refreshProtectionLock.ExitReadLock();
}
}
}
Expand Down Expand Up @@ -276,25 +276,16 @@ public IVBComponent Component(QualifiedModuleName qualifiedModuleName)

public void RemoveComponent(QualifiedModuleName qualifiedModuleName)
{
EvaluateWithinReadLock(() =>
ExecuteWithinWriteLock(() =>
{
if (_components.TryGetValue(qualifiedModuleName, out var component))
if (!_components.TryGetValue(qualifiedModuleName, out var component) ||
!_componentsCollections.TryGetValue(qualifiedModuleName.ProjectId, out var components))
{
ExecuteWithinWriteLock(() =>
{
if (_projects.TryGetValue(qualifiedModuleName.ProjectId, out var project))
{
using (var components = project.VBComponents)
{
// Remove the actual component...
components.Remove(component);
}
}
// ...and our cached copy of it
_components.Remove(qualifiedModuleName);
});
return;
}
return new { };
_components.Remove(qualifiedModuleName);
components.Remove(component);
});
}

Expand Down
5 changes: 3 additions & 2 deletions RubberduckTests/CodeExplorer/CodeExplorerViewModelTests.cs
Expand Up @@ -467,7 +467,7 @@ public void RemoveCommand_RemovesModuleWhenPromptOk()
var component = explorer.VbComponent.Object;

explorer.ViewModel.RemoveCommand.Execute(removing);
explorer.VbComponents.Verify(c => c.Remove(component), Times.Once);
explorer.ProjectsRepository.Verify(c => c.RemoveComponent(component.QualifiedModuleName), Times.Once);
}
}

Expand Down Expand Up @@ -503,8 +503,9 @@ public void RemoveCommand_GivenMsgBoxNo_RemovesModuleNoExport()
var removing = explorer.ViewModel.SelectedItem;
var component = explorer.VbComponent.Object;


explorer.ViewModel.RemoveCommand.Execute(removing);
explorer.VbComponents.Verify(c => c.Remove(component), Times.Once);
explorer.ProjectsRepository.Verify(c => c.RemoveComponent(component.QualifiedModuleName), Times.Once);
}
}

Expand Down
16 changes: 14 additions & 2 deletions RubberduckTests/CodeExplorer/MockedCodeExplorer.cs
Expand Up @@ -24,6 +24,8 @@
using Rubberduck.UnitTesting;
using Rubberduck.UnitTesting.CodeGeneration;
using Rubberduck.UnitTesting.Settings;
using Rubberduck.VBEditor;
using Rubberduck.VBEditor.ComManagement;
using Rubberduck.VBEditor.SourceCodeHandling;
using Rubberduck.VBEditor.Utility;
using RubberduckTests.Settings;
Expand Down Expand Up @@ -87,6 +89,10 @@ public MockedCodeExplorer(ProjectType projectType, ComponentType componentType =
VbProject = project.Build();
Vbe = builder.AddProject(VbProject).Build();

ProjectsRepository = new Mock<IProjectsRepository>();
ProjectsRepository.Setup(x => x.Project(It.IsAny<string>())).Returns(VbProject.Object);
ProjectsRepository.Setup(x => x.Component(It.IsAny<QualifiedModuleName>())).Returns(VbComponent.Object);

SetupViewModelAndParse();
}

Expand Down Expand Up @@ -120,6 +126,10 @@ public MockedCodeExplorer(ProjectType projectType, ComponentType componentType =
VbProject = project.Build();
Vbe = builder.AddProject(VbProject).Build();

ProjectsRepository = new Mock<IProjectsRepository>();
ProjectsRepository.Setup(x => x.Project(It.IsAny<string>())).Returns(VbProject.Object);
ProjectsRepository.Setup(x => x.Component(It.IsAny<QualifiedModuleName>())).Returns(VbComponent.Object);

SetupViewModelAndParse();

VbProject.SetupGet(m => m.VBComponents.Count).Returns(componentTypes.Count);
Expand All @@ -130,7 +140,8 @@ private void SetupViewModelAndParse()
var parser = MockParser.Create(Vbe.Object, null, MockVbeEvents.CreateMockVbeEvents(Vbe));
State = parser.State;

var removeCommand = new RemoveCommand(BrowserFactory.Object, MessageBox.Object, State.ProjectsProvider);
var exportCommand = new ExportCommand(BrowserFactory.Object, MessageBox.Object, State.ProjectsProvider, Vbe.Object);
var removeCommand = new RemoveCommand(exportCommand, ProjectsRepository.Object, MessageBox.Object, Vbe.Object);

ViewModel = new CodeExplorerViewModel(State, removeCommand,
_generalSettingsProvider.Object,
Expand All @@ -157,6 +168,7 @@ private void SetupViewModelAndParse()
public Mock<IOpenFileDialog> OpenDialog { get; }
public Mock<IFolderBrowser> FolderBrowser { get; }
public Mock<IMessageBox> MessageBox { get; } = new Mock<IMessageBox>();
public Mock<IProjectsRepository> ProjectsRepository { get; }

public WindowSettings WindowSettings { get; } = new WindowSettings();

Expand Down Expand Up @@ -354,7 +366,7 @@ public void ExecuteExportCommand()

public MockedCodeExplorer ImplementExportCommand()
{
ViewModel.ExportCommand = new ExportCommand(BrowserFactory.Object, MessageBox.Object, State.ProjectsProvider);
ViewModel.ExportCommand = new ExportCommand(BrowserFactory.Object, MessageBox.Object, State.ProjectsProvider, Vbe.Object);
return this;
}

Expand Down

0 comments on commit d421173

Please sign in to comment.