From 797c911bb58587c272efa33a3c037851df8d6369 Mon Sep 17 00:00:00 2001 From: Ben Wallis Date: Sun, 14 May 2017 14:10:54 +0100 Subject: [PATCH] FIL-13 Comments on the Show/Hide line are now preserved if block groups are not enabled for the script --- .../BlockItemBaseTypes/ActionBlockItem.cs | 4 +- Filtration.ObjectModel/ItemFilterBlock.cs | 6 ++- .../Services/TestItemFilterBlockTranslator.cs | 51 ++++++++++++++++++ .../TestItemFilterScriptTranslator.cs | 1 + .../Services/ItemFilterBlockTranslator.cs | 53 ++++++++----------- 5 files changed, 81 insertions(+), 34 deletions(-) diff --git a/Filtration.ObjectModel/BlockItemBaseTypes/ActionBlockItem.cs b/Filtration.ObjectModel/BlockItemBaseTypes/ActionBlockItem.cs index d920d50..8660377 100644 --- a/Filtration.ObjectModel/BlockItemBaseTypes/ActionBlockItem.cs +++ b/Filtration.ObjectModel/BlockItemBaseTypes/ActionBlockItem.cs @@ -15,7 +15,7 @@ namespace Filtration.ObjectModel.BlockItemBaseTypes public BlockAction Action { - get { return _action; } + get => _action; set { _action = value; @@ -27,6 +27,8 @@ namespace Filtration.ObjectModel.BlockItemBaseTypes } } + public string Comment { get; set; } + public override string OutputText => Action.GetAttributeDescription(); public override string PrefixText => string.Empty; diff --git a/Filtration.ObjectModel/ItemFilterBlock.cs b/Filtration.ObjectModel/ItemFilterBlock.cs index cf80d76..f5bcff1 100644 --- a/Filtration.ObjectModel/ItemFilterBlock.cs +++ b/Filtration.ObjectModel/ItemFilterBlock.cs @@ -15,6 +15,7 @@ namespace Filtration.ObjectModel string Description { get; set; } ItemFilterBlockGroup BlockGroup { get; set; } BlockAction Action { get; set; } + ActionBlockItem ActionBlockItem { get; } ObservableCollection BlockItems { get; } Color DisplayBackgroundColor { get; } Color DisplayTextColor { get; } @@ -32,7 +33,8 @@ namespace Filtration.ObjectModel public ItemFilterBlock() { - BlockItems = new ObservableCollection {new ActionBlockItem(BlockAction.Show)}; + ActionBlockItem = new ActionBlockItem(BlockAction.Show); + BlockItems = new ObservableCollection {ActionBlockItem}; Enabled = true; } @@ -79,6 +81,8 @@ namespace Filtration.ObjectModel } } + public ActionBlockItem ActionBlockItem { get; } + public ObservableCollection BlockItems { get; } public int BlockCount(Type type) diff --git a/Filtration.Parser.Tests/Services/TestItemFilterBlockTranslator.cs b/Filtration.Parser.Tests/Services/TestItemFilterBlockTranslator.cs index c638416..ec90142 100644 --- a/Filtration.Parser.Tests/Services/TestItemFilterBlockTranslator.cs +++ b/Filtration.Parser.Tests/Services/TestItemFilterBlockTranslator.cs @@ -26,6 +26,41 @@ namespace Filtration.Parser.Tests.Services _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>())) + .Returns(inputBlockGroup); + + // Act + var result = _testUtility.Translator.TranslateStringToItemFilterBlock(inputString, Mock.Of(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(i => i.BlockGroupsEnabled == false)); + + //Assert + Assert.AreEqual(testInputExpectedComment, result.ActionBlockItem.Comment); + } + [Test] public void TranslateStringToItemFilterBlock_NotDisabled_SetsBlockEnabledTrue() { @@ -1044,6 +1079,22 @@ namespace Filtration.Parser.Tests.Services 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().First().Comment = testInputActionBlockComment; + + // Act + var result = _testUtility.Translator.TranslateItemFilterBlockToString(_testUtility.TestBlock); + + // Assert + Assert.AreEqual(expectedResult, result); + } + [Test] public void TranslateItemFilterBlockToString_FilterTypeHide_ReturnsCorrectString() { diff --git a/Filtration.Parser.Tests/Services/TestItemFilterScriptTranslator.cs b/Filtration.Parser.Tests/Services/TestItemFilterScriptTranslator.cs index 61657b4..fab162b 100644 --- a/Filtration.Parser.Tests/Services/TestItemFilterScriptTranslator.cs +++ b/Filtration.Parser.Tests/Services/TestItemFilterScriptTranslator.cs @@ -348,6 +348,7 @@ namespace Filtration.Parser.Tests.Services 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")] [Test] public void TranslateStringToItemFilterScript_DisabledBlockWithBlockGroup_ReturnsCorrectBlock() diff --git a/Filtration.Parser/Services/ItemFilterBlockTranslator.cs b/Filtration.Parser/Services/ItemFilterBlockTranslator.cs index 36409e3..27a78f0 100644 --- a/Filtration.Parser/Services/ItemFilterBlockTranslator.cs +++ b/Filtration.Parser/Services/ItemFilterBlockTranslator.cs @@ -55,39 +55,12 @@ namespace Filtration.Parser.Services continue; } - var adjustedLine = line.Replace("#", " # "); - var trimmedLine = adjustedLine.Trim(); - + var trimmedLine = line.Trim(); var spaceOrEndOfLinePos = trimmedLine.IndexOf(" ", StringComparison.Ordinal) > 0 ? trimmedLine.IndexOf(" ", StringComparison.Ordinal) : trimmedLine.Length; var lineOption = trimmedLine.Substring(0, spaceOrEndOfLinePos); 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 "Hide": case "ShowDisabled": @@ -96,10 +69,17 @@ namespace Filtration.Parser.Services showHideFound = true; block.Action = lineOption.StartsWith("Show") ? BlockAction.Show : BlockAction.Hide; 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) { AddBlockGroupToBlock(block, trimmedLine); } + else + { + block.ActionBlockItem.Comment = GetTextAfterFirstComment(trimmedLine); + } break; } case "ItemLevel": @@ -406,10 +386,7 @@ namespace Filtration.Parser.Services private void AddBlockGroupToBlock(IItemFilterBlock block, string inputString) { - var blockGroupStart = inputString.IndexOf("#", StringComparison.Ordinal); - if (blockGroupStart <= 0) return; - - var blockGroupText = inputString.Substring(blockGroupStart + 1); + var blockGroupText = GetTextAfterFirstComment(inputString); var blockGroups = blockGroupText.Split(new[] { " - " }, StringSplitOptions.RemoveEmptyEntries) .Select(s => s.Trim()) .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) { var argbValues = Regex.Matches(inputString, @"\s+(\d+)"); @@ -474,6 +459,10 @@ namespace Filtration.Parser.Services { outputString += " # " + block.BlockGroup; } + else if (!string.IsNullOrEmpty(block.ActionBlockItem?.Comment)) + { + outputString += " #" + block.ActionBlockItem.Comment; + } // ReSharper disable once LoopCanBeConvertedToQuery foreach (var blockItem in block.BlockItems.Where(b => b.GetType() != typeof(ActionBlockItem)).OrderBy(b => b.SortOrder))