public void TranslateItemFilterScriptToString_ExtraLineBetweenBlocksSettingFalse_ReturnsCorrectOutput()
        {
            Settings.Default.ExtraLineBetweenBlocks = false;

            var script = new ItemFilterScript();
            var block1 = new ItemFilterBlock { Description = "Test Filter 1" };
            block1.BlockItems.Add(new ItemLevelBlockItem(FilterPredicateOperator.GreaterThan, 5));

            var block2 = new ItemFilterBlock();
            block2.BlockItems.Add(new QualityBlockItem(FilterPredicateOperator.LessThan, 15));
            block2.BlockItems.Add(new FontSizeBlockItem(7));
            block2.BlockItems.Add(new WidthBlockItem(FilterPredicateOperator.Equal, 3));

            script.ItemFilterBlocks.Add(block1);
            script.ItemFilterBlocks.Add(block2);

            var expectedOutput = "# Script edited with Filtration - https://github.com/ben-wallis/Filtration" + Environment.NewLine +
                                 "# Test Filter 1" + Environment.NewLine +
                                 "Show" + Environment.NewLine +
                                 "    ItemLevel > 5" + Environment.NewLine +
                                 "Show" + Environment.NewLine +
                                 "    Quality < 15" + Environment.NewLine +
                                 "    Width = 3" + Environment.NewLine +
                                 "    SetFontSize 7" + Environment.NewLine;

            var blockTranslator = new ItemFilterBlockTranslator(_testUtility.MockBlockGroupHierarchyBuilder.Object);
            var translator = new ItemFilterScriptTranslator(blockTranslator,
                _testUtility.MockBlockGroupHierarchyBuilder.Object);

            // Act
            var result = translator.TranslateItemFilterScriptToString(script);

            // Assert
            Assert.AreEqual(expectedOutput, result);
        }
Exemplo n.º 2
0
        public void InitialiseForMasterTheme(ItemFilterScript script)
        {
            Components = script.ThemeComponents;
            IsMasterThemeForScript = script;
            _filenameIsFake = true;

        }
        public IItemFilterScriptViewModel NewScript()
        {
            var newScript = new ItemFilterScript();
            var newViewModel = _itemFilterScriptViewModelFactory.Create();
            newViewModel.Initialise(newScript, true);

            return newViewModel;
        }
Exemplo n.º 4
0
        public IThemeEditorViewModel MasterThemeForScript(ItemFilterScript script)
        {
            var themeViewModel = _themeViewModelFactory.Create();
            themeViewModel.InitialiseForMasterTheme(script);
            themeViewModel.FilePath = "<Master Theme> " + Path.GetFileName(script.FilePath);

            return themeViewModel;
        }
 public async Task SaveItemFilterScriptAsync(ItemFilterScript script)
 {
     await Task.Run(() =>
     {
         _fileSystemService.WriteFileFromString(script.FilePath,
             _itemFilterScriptTranslator.TranslateItemFilterScriptToString(script));
     });
 }
Exemplo n.º 6
0
        public IThemeEditorViewModel NewThemeForScript(ItemFilterScript script)
        {
            var themeComponentCollection = script.ThemeComponents.Aggregate(new ThemeComponentCollection(),
                (c, component) =>
                {
                    c.Add(new ThemeComponent(component.ComponentType, component.ComponentName, component.Color));
                    return c;
                });
                
            var themeViewModel = _themeViewModelFactory.Create();
            themeViewModel.InitialiseForNewTheme(themeComponentCollection);
            themeViewModel.FilePath = "Untitled.filtertheme";

            return themeViewModel;
        }
Exemplo n.º 7
0
        public void ApplyThemeToScript(Theme theme, ItemFilterScript script)
        {
            var mismatchedComponents = false;
            foreach (var component in theme.Components)
            {
                var componentMatched = false;
                Type targetType = null;
                switch (component.ComponentType)
                {
                    case ThemeComponentType.BackgroundColor:
                        targetType = typeof (BackgroundColorBlockItem);
                        break;
                    case ThemeComponentType.TextColor:
                        targetType = typeof (TextColorBlockItem);
                        break;
                    case ThemeComponentType.BorderColor:
                        targetType = typeof (BorderColorBlockItem);
                        break;
                }

                foreach (var block in script.ItemFilterBlocks)
                {
                    foreach (var blockItem in block.BlockItems.Where(i => i.GetType() == targetType))
                    {
                        var colorBlockItem = (ColorBlockItem) blockItem;
                        if (colorBlockItem.ThemeComponent != null &&
                            colorBlockItem.ThemeComponent.ComponentName == component.ComponentName)
                        {
                            colorBlockItem.Color = component.Color;
                            componentMatched = true;
                        }
                    }
                }

                if (!componentMatched)
                {
                    mismatchedComponents = true;
                }
            }

            if (mismatchedComponents)
            {
                _messageBoxService.Show("Possible Theme Mismatch",
                    "Not all theme components had matches - are you sure this theme is designed for this script?",
                    MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }
        public void TranslateItemFilterScriptToString_FullScriptWithExistingFiltrationTagline_ReturnsCorrectOutput()
        {
            var script = new ItemFilterScript
            {
                Description = "Script edited with Filtration - https://github.com/ben-wallis/Filtration" + Environment.NewLine +
                              "Test script description" + Environment.NewLine
            };

            var expectedOutput = "# Script edited with Filtration - https://github.com/ben-wallis/Filtration" +
                                 Environment.NewLine +
                                 "# Test script description" + Environment.NewLine + Environment.NewLine;

            // Act
            var result = _testUtility.ScriptTranslator.TranslateItemFilterScriptToString(script);

            // Assert
            Assert.AreEqual(expectedOutput, result);
        }
        public async Task SaveItemFilterScript_CallsTranslatorAndFileSystemService()
        {
            // Arrange
            var testFilePath = "C:\\Test\\File.txt";
            var testScript = new ItemFilterScript {FilePath = testFilePath};
            var testTranslatedScript = "Test translated script";

            var mockFileSystemService = new Mock<IFileSystemService>();
            mockFileSystemService.Setup(s => s.WriteFileFromString(testFilePath, testTranslatedScript)).Verifiable();

            var mockItemFilterScriptTranslator = new Mock<IItemFilterScriptTranslator>();
            mockItemFilterScriptTranslator.Setup(t => t.TranslateItemFilterScriptToString(testScript)).Returns(testTranslatedScript).Verifiable();

            var service = new ItemFilterPersistenceService(mockFileSystemService.Object, mockItemFilterScriptTranslator.Object);

            // Act
            await service.SaveItemFilterScriptAsync(testScript);

            // Assert
            mockFileSystemService.Verify();
            mockItemFilterScriptTranslator.Verify();
        }
        public async Task LoadItemFilterScript_CallsTranslatorAndFileSystemService()
        {
            // Arrange
            const string TestInputPath = "C:\\Test Path\\Script.Filter";
            const string TestScriptString = "This is a test item filter script";
            var testItemFilterScript = new ItemFilterScript();

            var mockFileSystemService = new Mock<IFileSystemService>();
            mockFileSystemService.Setup(s => s.ReadFileAsString(TestInputPath)).Returns(TestScriptString).Verifiable();

            var mockItemFilterScriptTranslator = new Mock<IItemFilterScriptTranslator>();
            mockItemFilterScriptTranslator.Setup(t => t.TranslateStringToItemFilterScript(TestScriptString)).Returns(testItemFilterScript).Verifiable();

            var service = new ItemFilterPersistenceService(mockFileSystemService.Object, mockItemFilterScriptTranslator.Object);

            // Act
            var script = await service.LoadItemFilterScriptAsync(TestInputPath);

            // Assert
            mockFileSystemService.Verify();
            mockItemFilterScriptTranslator.Verify();
            Assert.AreEqual(testItemFilterScript, script);
        }
        public ItemFilterScript TranslateStringToItemFilterScript(string inputString)
        {
            var script = new ItemFilterScript();
            _blockGroupHierarchyBuilder.Initialise(script.ItemFilterBlockGroups.First());

            inputString = inputString.Replace("\t", "");
            if (inputString.Contains("#Disabled Block Start"))
            {
                inputString = PreprocessDisabledBlocks(inputString);
            }

            var conditionBoundaries = IdentifyBlockBoundaries(inputString);

            var lines = Regex.Split(inputString, "\r\n|\r|\n");

            // Process the script header
            for (var i = 0; i < conditionBoundaries.First.Value; i++)
            {
                if (lines[i].StartsWith("#"))
                {
                    script.Description += lines[i].Substring(1).Trim(' ') + Environment.NewLine;
                }
            }

            if (!script.Description.IsNullOrEmpty())
            {
                script.Description = script.Description.TrimEnd('\n').TrimEnd('\r');
            }

            // Extract each block from between boundaries and translate it into a ItemFilterBlock object
            // and add that object to the ItemFilterBlocks list
            for (var boundary = conditionBoundaries.First; boundary != null; boundary = boundary.Next)
            {
                var begin = boundary.Value;
                var end = boundary.Next != null ? boundary.Next.Value : lines.Length;
                var block = new string[end - begin];
                Array.Copy(lines, begin, block, 0, end - begin);
                var blockString = string.Join("\r\n", block);
                script.ItemFilterBlocks.Add(_blockTranslator.TranslateStringToItemFilterBlock(blockString, script.ThemeComponents));
            }

            _blockGroupHierarchyBuilder.Cleanup();
            return script;
        }
        public void Initialise(ItemFilterScript itemFilterScript, bool newScript)
        {
            ItemFilterBlockViewModels.Clear();

            Script = itemFilterScript;
            foreach (var block in Script.ItemFilterBlocks)
            {
                var vm = _itemFilterBlockViewModelFactory.Create();
                vm.Initialise(block, this);
                ItemFilterBlockViewModels.Add(vm);
            }
           
            _filenameIsFake = newScript;

            if (newScript)
            {
                Script.FilePath = "Untitled.filter";
            }

            if (ItemFilterBlockViewModels.Count > 0)
            {
                SelectedBlockViewModel = ItemFilterBlockViewModels.First();
            }

            Title = Filename;
            ContentId = "ScriptContentId";
        }
        public void TranslateItemFilterScriptToString_OneBlock_CallsTranslator()
        {
            // Arrange
            var testScript = new ItemFilterScript();

            var testBlock = new ItemFilterBlock();
            testBlock.BlockItems.Add(new ItemLevelBlockItem(FilterPredicateOperator.Equal, 5));

            const string blockOutput = "Test Script Output";

            testScript.ItemFilterBlocks.Add(testBlock);

            _testUtility.MockItemFilterBlockTranslator.Setup(t => t.TranslateItemFilterBlockToString(testBlock)).Returns(blockOutput).Verifiable();

            // Act
            _testUtility.ScriptTranslator.TranslateItemFilterScriptToString(testScript);

            // Assert
            _testUtility.MockItemFilterBlockTranslator.Verify();
        }
        public string TranslateItemFilterScriptToString(ItemFilterScript script)
        {
            var outputString = string.Empty;

            outputString += "# Script edited with Filtration - https://github.com/ben-wallis/Filtration" +
                            Environment.NewLine;

            if (!string.IsNullOrEmpty(script.Description))
            {
                // ReSharper disable once LoopCanBeConvertedToQuery
                foreach (var line in new LineReader(() => new StringReader(script.Description)))
                {
                    if (!line.Contains("Script edited with Filtration"))
                    {
                        outputString += "# " + line + Environment.NewLine;
                    }
                }
                outputString += Environment.NewLine;
            }

            // ReSharper disable once LoopCanBeConvertedToQuery
            foreach (var block in script.ItemFilterBlocks)
            {
                outputString += _blockTranslator.TranslateItemFilterBlockToString(block) + Environment.NewLine;

                if (Settings.Default.ExtraLineBetweenBlocks)
                {
                    outputString += Environment.NewLine;
                }
            }

            return outputString;
        }
Exemplo n.º 15
0
 public void Initialise(ItemFilterScript itemFilterScript)
 {
     _replaceColorsParameterSet = new ReplaceColorsParameterSet();
     _itemFilterScript = itemFilterScript;
 }
Exemplo n.º 16
0
        public void Initialise(ItemFilterScript itemFilterScript, IItemFilterBlock initialiseFromBlock)
        {
            _replaceColorsParameterSet = new ReplaceColorsParameterSet();

            if (initialiseFromBlock.BlockItems.Count(b => b.GetType() == typeof (TextColorBlockItem)) > 0)
            {
                _replaceColorsParameterSet.ReplaceTextColor = true;
                var existingBlockColor = ((TextColorBlockItem)
                        initialiseFromBlock.BlockItems.First(b => b.GetType() == typeof (TextColorBlockItem))).Color;
                _replaceColorsParameterSet.OldTextColor = existingBlockColor;
                _replaceColorsParameterSet.NewTextColor = existingBlockColor;
            }

            if (initialiseFromBlock.BlockItems.Count(b => b.GetType() == typeof(BackgroundColorBlockItem)) > 0)
            {
                _replaceColorsParameterSet.ReplaceBackgroundColor = true;
                var existingBlockColor = ((BackgroundColorBlockItem)
                        initialiseFromBlock.BlockItems.First(b => b.GetType() == typeof(BackgroundColorBlockItem))).Color;
                _replaceColorsParameterSet.OldBackgroundColor = existingBlockColor;
                _replaceColorsParameterSet.NewBackgroundColor = existingBlockColor;
            }

            if (initialiseFromBlock.BlockItems.Count(b => b.GetType() == typeof(BorderColorBlockItem)) > 0)
            {
                _replaceColorsParameterSet.ReplaceBorderColor = true;
                var existingBlockColor = ((BorderColorBlockItem)
                        initialiseFromBlock.BlockItems.First(b => b.GetType() == typeof(BorderColorBlockItem))).Color;
                _replaceColorsParameterSet.OldBorderColor = existingBlockColor;
                _replaceColorsParameterSet.NewBorderColor = existingBlockColor;
            }

            _itemFilterScript = itemFilterScript;
        }
 public void SaveItemFilterScript(ItemFilterScript script)
 {
     _fileSystemService.WriteFileFromString(script.FilePath,
         _itemFilterScriptTranslator.TranslateItemFilterScriptToString(script));
 }