Skip to content

Commit

Permalink
Migrated slimmed down Surround With feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey M committed Jan 29, 2020
1 parent d74d968 commit 5a31f28
Show file tree
Hide file tree
Showing 21 changed files with 740 additions and 146 deletions.
1 change: 1 addition & 0 deletions DPackRx.Tests/DPackRx.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@
<Compile Include="Features\MiscellaneousFeatureTests.cs" />
<Compile Include="Features\BookmarksServiceTests.cs" />
<Compile Include="Features\FileBrowserFeatureTests.cs" />
<Compile Include="Features\SurroundWithFeatureTests.cs" />
<Compile Include="Features\SupportOptionsFirstTimeUseTests.cs" />
<Compile Include="Features\SupportOptionsFeatureTests.cs" />
<Compile Include="Language\LanguageServiceTests.cs" />
Expand Down
10 changes: 10 additions & 0 deletions DPackRx.Tests/Features/CodeBrowserFeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,16 @@ public void Execute(int commandId, CodeModelFilterFlags filter)
_messageServiceMock.Verify(m => m.ShowError(It.IsNotNull<string>(), false), Times.Never);
}

[Test]
public void Execute_InvalidCommand()
{
var feature = GetFeature();

var result = feature.Execute(0);

Assert.That(result, Is.False);
}

[Test]
public void Execute_NoActiveFile()
{
Expand Down
10 changes: 10 additions & 0 deletions DPackRx.Tests/Features/FileBrowserFeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,16 @@ public void Execute()
_modalDialogServiceMock.Verify(d => d.ShowDialog<FileBrowserWindow, FileBrowserViewModel>(It.IsNotNull<string>()));
}

[Test]
public void Execute_InvalidCommand()
{
var feature = GetFeature();

var result = feature.Execute(0);

Assert.That(result, Is.False);
}

#endregion
}
}
10 changes: 10 additions & 0 deletions DPackRx.Tests/Features/MiscellaneousFeatureTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,16 @@ public void Execute_CopyProjectFullPath(string path)
_shellHelperServiceMock.Verify(s => s.GetCurrentProjectPath());
}

[Test]
public void Execute_InvalidCommand()
{
var feature = GetFeature();

var result = feature.Execute(0);

Assert.That(result, Is.False);
}

#endregion
}
}
197 changes: 197 additions & 0 deletions DPackRx.Tests/Features/SurroundWithFeatureTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,197 @@
using System;
using System.Windows.Input;

using Moq;
using NUnit.Framework;

using DPackRx.CodeModel;
using DPackRx.Features;
using DPackRx.Features.SurroundWith;
using DPackRx.Language;
using DPackRx.Options;
using DPackRx.Package;
using DPackRx.Services;

namespace DPackRx.Tests.Features
{
/// <summary>
/// SurroundWithFeature tests.
/// </summary>
[TestFixture]
public class SurroundWithFeatureTests
{
#region Fields

private Mock<IServiceProvider> _serviceProviderMock;
private Mock<ILog> _logMock;
private Mock<IOptionsService> _optionsServiceMock;
private Mock<IShellHelperService> _shellHelperServiceMock;
private Mock<IShellSelectionService> _shellSelectionServiceMock;
private Mock<IFileTypeResolver> _fileTypeResolverMock;
private Mock<IKeyboardService> _keyboardServiceMock;

#endregion

#region Tests Setup

[SetUp]
public void Setup()
{
_serviceProviderMock = new Mock<IServiceProvider>();

_logMock = new Mock<ILog>();
_logMock.Setup(l => l.LogMessage(It.IsAny<string>(), It.IsAny<string>())).Verifiable();

_optionsServiceMock = new Mock<IOptionsService>();

_shellHelperServiceMock = new Mock<IShellHelperService>();
_shellHelperServiceMock.Setup(s => s.ExecuteCommand(It.IsNotNull<string>(), null)).Verifiable();

_shellSelectionServiceMock = new Mock<IShellSelectionService>();
_shellSelectionServiceMock.Setup(s => s.IsContextActive(It.IsAny<ContextType>())).Returns(true).Verifiable();
_shellSelectionServiceMock.Setup(s => s.GetActiveProject()).Returns(new object()).Verifiable();

var webProject = false;
_fileTypeResolverMock = new Mock<IFileTypeResolver>();
_fileTypeResolverMock.Setup(f => f.GetCurrentLanguage(It.IsAny<object>(), out webProject))
.Returns(new LanguageSettings("C#", "C#") { Type = LanguageType.CSharp, SurroundWith = true }).Verifiable();

_keyboardServiceMock = new Mock<IKeyboardService>();
_keyboardServiceMock.Setup(k => k.Type(It.IsAny<Key>())).Verifiable();
_keyboardServiceMock.Setup(k => k.Type(It.IsAny<string>())).Verifiable();
}

[TearDown]
public void TearDown()
{
_serviceProviderMock = null;
_logMock = null;
_optionsServiceMock = null;
_shellHelperServiceMock = null;
_shellSelectionServiceMock = null;
_fileTypeResolverMock = null;
_keyboardServiceMock = null;
}

#endregion

#region Private Methods

/// <summary>
/// Returns test feature instance.
/// </summary>
private IFeature GetFeature()
{
return new SurroundWithFeature(_serviceProviderMock.Object, _logMock.Object, _optionsServiceMock.Object,
_shellHelperServiceMock.Object, _shellSelectionServiceMock.Object, _fileTypeResolverMock.Object,
_keyboardServiceMock.Object);
}

#endregion

#region Tests

[Test]
public void GetCommandIds()
{
var feature = GetFeature();

var commands = feature.GetCommandIds();

Assert.That(commands, Is.Not.Null);
Assert.That(commands.Count, Is.EqualTo(5));
Assert.That(commands, Contains.Item(CommandIDs.SW_TRY_CATCH));
Assert.That(commands, Contains.Item(CommandIDs.SW_TRY_FINALLY));
Assert.That(commands, Contains.Item(CommandIDs.SW_FOR));
Assert.That(commands, Contains.Item(CommandIDs.SW_FOR_EACH));
Assert.That(commands, Contains.Item(CommandIDs.SW_REGION));
}

[TestCase(CommandIDs.SW_TRY_CATCH, true)]
[TestCase(CommandIDs.SW_TRY_FINALLY, true)]
[TestCase(CommandIDs.SW_FOR, true)]
[TestCase(CommandIDs.SW_FOR_EACH, true)]
[TestCase(CommandIDs.SW_REGION, true)]
[TestCase(0, false)]
public void IsValidContext(int commandId, bool expectedResult)
{
var feature = GetFeature();

var result = feature.IsValidContext(commandId);

Assert.That(result, Is.EqualTo(expectedResult));
if (expectedResult)
_shellSelectionServiceMock.Verify(s => s.IsContextActive(It.IsAny<ContextType>()), Times.AtLeast(2));
else
_shellSelectionServiceMock.Verify(s => s.IsContextActive(It.IsAny<ContextType>()), Times.Never);
}

[TestCase(CommandIDs.SW_TRY_CATCH, SurroundWithFeature.SNIPPET_TRY_CATCH)]
[TestCase(CommandIDs.SW_TRY_FINALLY, SurroundWithFeature.SNIPPET_TRY_FINALLY)]
[TestCase(CommandIDs.SW_FOR, SurroundWithFeature.SNIPPET_FOR)]
[TestCase(CommandIDs.SW_FOR_EACH, SurroundWithFeature.SNIPPET_FOR_EACH)]
[TestCase(CommandIDs.SW_REGION, SurroundWithFeature.SNIPPET_REGION)]
public void Execute(int commandId, string command)
{
var feature = GetFeature();
var webProject = false;

var result = feature.Execute(commandId);

Assert.That(result, Is.True);
_shellSelectionServiceMock.Verify(s => s.GetActiveProject());
_fileTypeResolverMock.Verify(f => f.GetCurrentLanguage(It.IsAny<object>(), out webProject));
_shellHelperServiceMock.Verify(s => s.ExecuteCommand(SurroundWithFeature.SURROUND_WITH_COMMAND, null));
_keyboardServiceMock.Verify(k => k.Type(command));
_keyboardServiceMock.Verify(k => k.Type(Key.Enter));
}

[Test]
public void Execute_InvalidCommand()
{
var feature = GetFeature();

var result = feature.Execute(0);

Assert.That(result, Is.False);
}

[Test]
public void Execute_UnknownLanguage()
{
var feature = GetFeature();
var webProject = false;
_fileTypeResolverMock.Setup(f => f.GetCurrentLanguage(It.IsAny<object>(), out webProject))
.Returns(LanguageSettings.UnknownLanguage).Verifiable();

var result = feature.Execute(CommandIDs.SW_REGION);

Assert.That(result, Is.True);
_shellSelectionServiceMock.Verify(s => s.GetActiveProject());
_fileTypeResolverMock.Verify(f => f.GetCurrentLanguage(It.IsAny<object>(), out webProject));
_shellHelperServiceMock.Verify(s => s.ExecuteCommand(SurroundWithFeature.SURROUND_WITH_COMMAND, null), Times.Never);
_keyboardServiceMock.Verify(k => k.Type(It.IsAny<string>()), Times.Never);
_keyboardServiceMock.Verify(k => k.Type(Key.Enter), Times.Never);
}

[Test]
public void Execute_UnsupportedLanguage()
{
var feature = GetFeature();
var webProject = false;
_fileTypeResolverMock.Setup(f => f.GetCurrentLanguage(It.IsAny<object>(), out webProject))
.Returns(new LanguageSettings("C#", "C#") { Type = LanguageType.CSharp, SurroundWith = false }).Verifiable();

var result = feature.Execute(CommandIDs.SW_REGION);

Assert.That(result, Is.True);
_shellSelectionServiceMock.Verify(s => s.GetActiveProject());
_fileTypeResolverMock.Verify(f => f.GetCurrentLanguage(It.IsAny<object>(), out webProject));
_shellHelperServiceMock.Verify(s => s.ExecuteCommand(SurroundWithFeature.SURROUND_WITH_COMMAND, null), Times.Never);
_keyboardServiceMock.Verify(k => k.Type(It.IsAny<string>()), Times.Never);
_keyboardServiceMock.Verify(k => k.Type(Key.Enter), Times.Never);
}

#endregion
}
}
4 changes: 4 additions & 0 deletions DPackRx/DPackRx.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@
<Compile Include="Features\Bookmarks\BookmarksSimpleTagger.cs" />
<Compile Include="Features\Bookmarks\IBookmarkCallbackClient.cs" />
<Compile Include="Features\SupportOptions\SupportOptionsFirstTimeUse.cs" />
<Compile Include="Features\SurroundWith\SurroundWithFeature.cs" />
<Compile Include="Helpers\KeyboardHelper.cs" />
<Compile Include="Language\ILanguageRegistrationService.cs" />
<Compile Include="Language\LanguageRegistrationService.cs" />
<Compile Include="Options\IOptionsPersistenceService.cs" />
Expand All @@ -76,6 +78,7 @@
<Compile Include="Features\Bookmarks\IBookmarksService.cs" />
<Compile Include="Services\ICodeModelEvents.cs" />
<Compile Include="Options\IOptionsService.cs" />
<Compile Include="Services\IKeyboardService.cs" />
<Compile Include="Services\ISharedServiceProvider.cs" />
<Compile Include="Services\IShellEventsService.cs" />
<Compile Include="Services\IShellCodeModelService.cs" />
Expand Down Expand Up @@ -133,6 +136,7 @@
<Compile Include="CodeModel\ISolutionProcessor.cs" />
<Compile Include="CodeModel\ProjectProcessor.cs" />
<Compile Include="Services\IWildcardMatch.cs" />
<Compile Include="Services\KeyboardService.cs" />
<Compile Include="Services\SearchToken.cs" />
<Compile Include="Services\SharedServiceProvider.cs" />
<Compile Include="Services\IImageService.cs" />
Expand Down
2 changes: 1 addition & 1 deletion DPackRx/Features/Bookmarks/BookmarksFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public override bool IsValidContext(int commandId)
_shellSelectionService.IsContextActive(ContextType.HTMLSourceEditor) ||
_shellSelectionService.IsContextActive(ContextType.CSSTextEditor));
default:
return false;
return base.IsValidContext(commandId);
}
}

Expand Down
3 changes: 2 additions & 1 deletion DPackRx/Features/IFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public enum KnownFeature // TODO: finish other features

Bookmarks,

//SurroundWith,
[Description("Surround With")]
SurroundWith,

//SolutionStats,

Expand Down
Loading

0 comments on commit 5a31f28

Please sign in to comment.