示例#1
0
文件: Form1.cs 项目: 1pindsvin/yagni
        private void okButton_Click(object sender, EventArgs e)
        {
            if(searchTextBox.Text.Length==0)
            {
                return;
            }
            if(processDirectoriesTextBox.Text.Length==0)
            {
                return;
            }
            var directories = processDirectoriesTextBox.Text.Trim().Split(new char[] { ';' });
            var fileExtensions = fileExtensionTextBox.Text.Trim().Split(new char[] {';'});

            IWordReplacer wordReplacer = null;
            if (whenThisTextIsFoundCheckBox.Checked && textThatMustBeFoundTextBox.Text.Length>0)
            {
                wordReplacer = new ConditionalWordReplacer(searchTextBox.Text.Trim(), replaceTextBox.Text.Trim(),
                                            textThatMustBeFoundTextBox.Text);
            }
            else
            {
                wordReplacer = new WordReplacer(searchTextBox.Text.Trim(), replaceTextBox.Text.Trim());
            }

            foreach (var directory in directories)
            {
                var recursiveFileWordReplacer = new RecursiveFileWordReplacer(directory, wordReplacer, fileExtensions);
                recursiveFileWordReplacer.ReplaceAll();
            }
            MessageBox.Show("All done");
        }
        public void WhenSeachNotFoundShouldReturnNull()
        {
            var text = "gryf";
            var search = "gryffe";
            var wordReplacement = "";
            var e = new ConditionalWordReplacer(search, wordReplacement, text);
            e.Replace(text);
            Assert.IsFalse(e.Success());

            var result = e.ReplacedText;
            Assert.AreEqual(null, result);
        }
        public void AssertFalseWhenConditionNotFound()
        {
            const string fox = "fox";
            const string textTemplate = "the quick brown {0}, a sentence with{1}animals followed by another {2}..";
            var text = string.Format(textTemplate, fox, fox, fox);
            text = BuildMultilineText(text);

            const string search = fox;

            const string dog = "dog";
            var expectedResult = string.Format(textTemplate, dog, fox, dog);
            expectedResult = BuildMultilineText(expectedResult);

            const string wordThatDoesNotExistinSentence = "wordThatDoesNotExistinSentence";
            var e = new ConditionalWordReplacer(search, dog, "wordThatDoesNotExistinSentence");

            e.Replace(text);

            Assert.IsFalse(e.Success());
        }
        public void ShouldReplaceAllOccurences()
        {
            const string fox = "fox";
            const string textTemplate = "the quick brown {0}, a sentence with{1}animals followed by another {2}..";
            var text = string.Format(textTemplate, fox, fox, fox);
            text = BuildMultilineText(text);

            const string search = fox;

            const string dog = "dog";
            var expectedResult = string.Format(textTemplate, dog, fox, dog);
            expectedResult = BuildMultilineText(expectedResult);

            var e = new ConditionalWordReplacer(search, dog, "quick");

            e.Replace(text);

            Assert.IsTrue(e.Success());

            var result = e.ReplacedText;
            Assert.AreEqual(expectedResult, result);
        }