Skip to content

Commit

Permalink
NK-100 Разделитель в меню плагина
Browse files Browse the repository at this point in the history
- Запрет на два разделителя подряд
- Запрет на разделитель в первом пункте меню
- Тесты на функции
  • Loading branch information
schadin committed Nov 23, 2016
1 parent 8d5797e commit 2f2d208
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
5 changes: 5 additions & 0 deletions NppKate/Common/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ public class CommandManager : ICommandManager
{
const int DefaultCapacity = 5;
private volatile int HiddenIndex = -1;
private volatile bool _LastItemIsSeparator = true;

private Dictionary<string, List<CommandMenuItem>> _commandIndexes;

Expand Down Expand Up @@ -69,6 +70,9 @@ public string GetNameByIndex(string module, int commandIndex)

public int RegisterCommand(string module, string name, Action commandHandler = null, bool isCheckedWithStart = false, ShortcutKey? shortcut = null)
{
if (_LastItemIsSeparator && "-".Equals(name))
return 0;

if (!_commandIndexes.ContainsKey(module))
{
_commandIndexes.Add(module, new List<CommandMenuItem>(DefaultCapacity));
Expand All @@ -86,6 +90,7 @@ public int RegisterCommand(string module, string name, Action commandHandler = n
Name = name,
CommandIndex = cmdIndex
});
_LastItemIsSeparator = "-".Equals(name);
return cmdIndex;
}

Expand Down
4 changes: 4 additions & 0 deletions NppKate/Common/IModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,16 @@ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;

namespace NppKate.Common
{
public interface IModule
{
void Init(IModuleManager manager);
void Final();

[Obsolete("Property is deprecated", false)]
bool IsNeedRun { get; }
}
}
56 changes: 56 additions & 0 deletions Test/NppKateTest/CommandManager/CmdManagerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using NppKate.Common;

namespace NppKateTest.CommandManager
{
[TestClass]
public class CmdManagerTest
{
const string Module = "Test";
const string Module2 = "Test2";
[TestMethod]
public void CheckDoubleSeparator()
{
var cmdManager = new NppKate.Common.CommandManager();
cmdManager.RegisterCommand(Module, "1");
cmdManager.RegisterSeparator(Module);
cmdManager.RegisterSeparator(Module);

Assert.AreEqual<int>(cmdManager.GetCommandsByModule(Module).Count, 2);
}

[TestMethod]
public void CheckFirstSeparator()
{
var cmdManager = new NppKate.Common.CommandManager();
cmdManager.RegisterSeparator(Module);

Assert.AreEqual<int>(cmdManager.GetCommandsByModule(Module).Count, 0);
}

private void AddCommands(ICommandManager cmd, int count, string module)
{
for (int i = 0; i < count; i++)
{
cmd.RegisterCommand(module, $"command_{i}");
}
}

[TestMethod]
public void AddOneCommand()
{
var cmdManager = new NppKate.Common.CommandManager();
AddCommands(cmdManager, 1, Module);
Assert.AreEqual<int>(cmdManager.GetCommandsByModule(Module).Count, 1);
}
[TestMethod]
public void AddManyCommand()
{
var cmdManager = new NppKate.Common.CommandManager();
var count = 10;
AddCommands(cmdManager, count, Module);
Assert.AreEqual<int>(cmdManager.GetCommandsByModule(Module).Count, count);
}
}
}
1 change: 1 addition & 0 deletions test/NppKateTest/NppKateTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
</Otherwise>
</Choose>
<ItemGroup>
<Compile Include="CommandManager\CmdManagerTest.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SnippetTest\SnippetTest.cs" />
<Compile Include="SnippetTest\SnippetTextBuilderTest.cs" />
Expand Down

0 comments on commit 2f2d208

Please sign in to comment.