FIL-13 Comments on the Show/Hide line are now preserved if block groups are not enabled for the script

This commit is contained in:
Ben Wallis 2017-05-14 14:10:54 +01:00
parent 8e54cc3b4b
commit 797c911bb5
5 changed files with 81 additions and 34 deletions

View File

@ -15,7 +15,7 @@ namespace Filtration.ObjectModel.BlockItemBaseTypes
public BlockAction Action public BlockAction Action
{ {
get { return _action; } get => _action;
set set
{ {
_action = value; _action = value;
@ -27,6 +27,8 @@ namespace Filtration.ObjectModel.BlockItemBaseTypes
} }
} }
public string Comment { get; set; }
public override string OutputText => Action.GetAttributeDescription(); public override string OutputText => Action.GetAttributeDescription();
public override string PrefixText => string.Empty; public override string PrefixText => string.Empty;

View File

@ -15,6 +15,7 @@ namespace Filtration.ObjectModel
string Description { get; set; } string Description { get; set; }
ItemFilterBlockGroup BlockGroup { get; set; } ItemFilterBlockGroup BlockGroup { get; set; }
BlockAction Action { get; set; } BlockAction Action { get; set; }
ActionBlockItem ActionBlockItem { get; }
ObservableCollection<IItemFilterBlockItem> BlockItems { get; } ObservableCollection<IItemFilterBlockItem> BlockItems { get; }
Color DisplayBackgroundColor { get; } Color DisplayBackgroundColor { get; }
Color DisplayTextColor { get; } Color DisplayTextColor { get; }
@ -32,7 +33,8 @@ namespace Filtration.ObjectModel
public ItemFilterBlock() public ItemFilterBlock()
{ {
BlockItems = new ObservableCollection<IItemFilterBlockItem> {new ActionBlockItem(BlockAction.Show)}; ActionBlockItem = new ActionBlockItem(BlockAction.Show);
BlockItems = new ObservableCollection<IItemFilterBlockItem> {ActionBlockItem};
Enabled = true; Enabled = true;
} }
@ -79,6 +81,8 @@ namespace Filtration.ObjectModel
} }
} }
public ActionBlockItem ActionBlockItem { get; }
public ObservableCollection<IItemFilterBlockItem> BlockItems { get; } public ObservableCollection<IItemFilterBlockItem> BlockItems { get; }
public int BlockCount(Type type) public int BlockCount(Type type)

View File

@ -26,6 +26,41 @@ namespace Filtration.Parser.Tests.Services
_testUtility = new ItemFilterBlockTranslatorTestUtility(); _testUtility = new ItemFilterBlockTranslatorTestUtility();
} }
[Test]
public void TranslateStringToItemFilterBlock_BlockGroupsEnabled_ActionBlockItemCommentIsNull()
{
// Arrange
var inputString = "Show # Test - Test2 - Test3" + Environment.NewLine;
var inputBlockGroup = new ItemFilterBlockGroup("TestBlockGroup", null);
_testUtility.MockBlockGroupHierarchyBuilder
.Setup(b => b.IntegrateStringListIntoBlockGroupHierarchy(It.IsAny<IEnumerable<string>>()))
.Returns(inputBlockGroup);
// Act
var result = _testUtility.Translator.TranslateStringToItemFilterBlock(inputString, Mock.Of<IItemFilterScriptSettings>(i => i.BlockGroupsEnabled));
//Assert
Assert.IsTrue(string.IsNullOrEmpty(result.ActionBlockItem.Comment));
}
[Test]
public void TranslateStringToItemFilterBlock_BlockGroupsDisabled_ActionBlockItemCommentIsSetCorrectly()
{
// Arrange
var testInputExpectedComment = " this is a comment that should be preserved";
var inputString = $"Show #{testInputExpectedComment}" + Environment.NewLine;
// Act
var result = _testUtility.Translator.TranslateStringToItemFilterBlock(inputString, Mock.Of<IItemFilterScriptSettings>(i => i.BlockGroupsEnabled == false));
//Assert
Assert.AreEqual(testInputExpectedComment, result.ActionBlockItem.Comment);
}
[Test] [Test]
public void TranslateStringToItemFilterBlock_NotDisabled_SetsBlockEnabledTrue() public void TranslateStringToItemFilterBlock_NotDisabled_SetsBlockEnabledTrue()
{ {
@ -1044,6 +1079,22 @@ namespace Filtration.Parser.Tests.Services
Assert.AreEqual(expectedResult, result); Assert.AreEqual(expectedResult, result);
} }
[Test]
public void TranslateItemFilterBlockToString_HasActionBlockComment_ReturnsCorrectString()
{
// Arrange
var testInputActionBlockComment = "this is a test";
var expectedResult = $"Show #{testInputActionBlockComment}";
_testUtility.TestBlock.BlockItems.OfType<ActionBlockItem>().First().Comment = testInputActionBlockComment;
// Act
var result = _testUtility.Translator.TranslateItemFilterBlockToString(_testUtility.TestBlock);
// Assert
Assert.AreEqual(expectedResult, result);
}
[Test] [Test]
public void TranslateItemFilterBlockToString_FilterTypeHide_ReturnsCorrectString() public void TranslateItemFilterBlockToString_FilterTypeHide_ReturnsCorrectString()
{ {

View File

@ -348,6 +348,7 @@ namespace Filtration.Parser.Tests.Services
Assert.AreEqual("This is a disabled block", secondBlock.Description); Assert.AreEqual("This is a disabled block", secondBlock.Description);
} }
// TODO: Reinstate this test
[Ignore("Ignored until toggling block group parsing can be controlled from the filter script input")] [Ignore("Ignored until toggling block group parsing can be controlled from the filter script input")]
[Test] [Test]
public void TranslateStringToItemFilterScript_DisabledBlockWithBlockGroup_ReturnsCorrectBlock() public void TranslateStringToItemFilterScript_DisabledBlockWithBlockGroup_ReturnsCorrectBlock()

View File

@ -55,39 +55,12 @@ namespace Filtration.Parser.Services
continue; continue;
} }
var adjustedLine = line.Replace("#", " # "); var trimmedLine = line.Trim();
var trimmedLine = adjustedLine.Trim();
var spaceOrEndOfLinePos = trimmedLine.IndexOf(" ", StringComparison.Ordinal) > 0 ? trimmedLine.IndexOf(" ", StringComparison.Ordinal) : trimmedLine.Length; var spaceOrEndOfLinePos = trimmedLine.IndexOf(" ", StringComparison.Ordinal) > 0 ? trimmedLine.IndexOf(" ", StringComparison.Ordinal) : trimmedLine.Length;
var lineOption = trimmedLine.Substring(0, spaceOrEndOfLinePos); var lineOption = trimmedLine.Substring(0, spaceOrEndOfLinePos);
switch (lineOption) switch (lineOption)
{ {
//case "Show":
// showHideFound = true;
// block.Action = BlockAction.Show;
// block.Enabled = true;
// AddBlockGroupToBlock(block, trimmedLine);
// break;
//case "Hide":
// showHideFound = true;
// block.Action = BlockAction.Hide;
// block.Enabled = true;
// AddBlockGroupToBlock(block, trimmedLine);
// break;
//case "ShowDisabled":
// showHideFound = true;
// block.Action = BlockAction.Show;
// block.Enabled = false;
// AddBlockGroupToBlock(block, trimmedLine);
// break;
//case "HideDisabled":
// showHideFound = true;
// block.Action = BlockAction.Hide;
// block.Enabled = false;
// AddBlockGroupToBlock(block, trimmedLine);
// break;
case "Show": case "Show":
case "Hide": case "Hide":
case "ShowDisabled": case "ShowDisabled":
@ -96,10 +69,17 @@ namespace Filtration.Parser.Services
showHideFound = true; showHideFound = true;
block.Action = lineOption.StartsWith("Show") ? BlockAction.Show : BlockAction.Hide; block.Action = lineOption.StartsWith("Show") ? BlockAction.Show : BlockAction.Hide;
block.Enabled = !lineOption.EndsWith("Disabled"); block.Enabled = !lineOption.EndsWith("Disabled");
// If block groups are enabled for this script, the comment after Show/Hide is parsed as a block
// group hierarchy, if block groups are disabled it is preserved as a simple text comment.
if (itemFilterScriptSettings.BlockGroupsEnabled) if (itemFilterScriptSettings.BlockGroupsEnabled)
{ {
AddBlockGroupToBlock(block, trimmedLine); AddBlockGroupToBlock(block, trimmedLine);
} }
else
{
block.ActionBlockItem.Comment = GetTextAfterFirstComment(trimmedLine);
}
break; break;
} }
case "ItemLevel": case "ItemLevel":
@ -406,10 +386,7 @@ namespace Filtration.Parser.Services
private void AddBlockGroupToBlock(IItemFilterBlock block, string inputString) private void AddBlockGroupToBlock(IItemFilterBlock block, string inputString)
{ {
var blockGroupStart = inputString.IndexOf("#", StringComparison.Ordinal); var blockGroupText = GetTextAfterFirstComment(inputString);
if (blockGroupStart <= 0) return;
var blockGroupText = inputString.Substring(blockGroupStart + 1);
var blockGroups = blockGroupText.Split(new[] { " - " }, StringSplitOptions.RemoveEmptyEntries) var blockGroups = blockGroupText.Split(new[] { " - " }, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim()) .Select(s => s.Trim())
.ToList(); .ToList();
@ -421,6 +398,14 @@ namespace Filtration.Parser.Services
} }
} }
private static string GetTextAfterFirstComment(string inputString)
{
var blockGroupStart = inputString.IndexOf("#", StringComparison.Ordinal);
if (blockGroupStart <= 0) return string.Empty;
return inputString.Substring(blockGroupStart + 1);
}
private static Color GetColorFromString(string inputString) private static Color GetColorFromString(string inputString)
{ {
var argbValues = Regex.Matches(inputString, @"\s+(\d+)"); var argbValues = Regex.Matches(inputString, @"\s+(\d+)");
@ -474,6 +459,10 @@ namespace Filtration.Parser.Services
{ {
outputString += " # " + block.BlockGroup; outputString += " # " + block.BlockGroup;
} }
else if (!string.IsNullOrEmpty(block.ActionBlockItem?.Comment))
{
outputString += " #" + block.ActionBlockItem.Comment;
}
// ReSharper disable once LoopCanBeConvertedToQuery // ReSharper disable once LoopCanBeConvertedToQuery
foreach (var blockItem in block.BlockItems.Where(b => b.GetType() != typeof(ActionBlockItem)).OrderBy(b => b.SortOrder)) foreach (var blockItem in block.BlockItems.Where(b => b.GetType() != typeof(ActionBlockItem)).OrderBy(b => b.SortOrder))