Exemplo n.º 1
0
        public void TestSearchRegexReturnsCorrectNumber()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "dnGR\\wP", GrepSearchOption.CaseSensitive, -1);

            Assert.Equal(results.Count, 1);

            results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "dngr\\wp", GrepSearchOption.CaseSensitive, -1);
            Assert.Equal(results.Count, 0);

            results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "dngr\\wp", GrepSearchOption.CaseSensitive | GrepSearchOption.Multiline, -1);
            Assert.Equal(results.Count, 0);

            results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "dngr\\wp", GrepSearchOption.None, -1);
            Assert.Equal(results.Count, 1);

            results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "string", GrepSearchOption.None, -1);
            Assert.Equal(results.Count, 2);
            Assert.Equal(results[0].Matches.Count, 3);
            Assert.Equal(results[1].Matches.Count, 282);

            results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "string", GrepSearchOption.Multiline, -1);
            Assert.Equal(results.Count, 2);
            Assert.Equal(results[0].Matches.Count, 3);
            Assert.Equal(results[1].Matches.Count, 282);

            results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.Regex, "string", GrepSearchOption.Multiline, -1);
            Assert.Equal(results.Count, 2);
            Assert.Equal(results[0].Matches.Count, 3);
            Assert.Equal(results[1].Matches.Count, 282);

            Assert.Empty(core.Search(null, SearchType.Regex, "string", GrepSearchOption.Multiline, -1));
            Assert.Empty(core.Search(new string[] { }, SearchType.Regex, "string", GrepSearchOption.Multiline, -1));
        }
Exemplo n.º 2
0
        public void TestSearchWithStopAfterFirstMatch()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "public", GrepSearchOption.CaseSensitive, -1);

            Assert.True(results.Count > 1);
            results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "public", GrepSearchOption.StopAfterFirstMatch, -1);
            Assert.Equal(1, results.Count);
            results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "", GrepSearchOption.CaseSensitive, -1);
            Assert.True(results.Count > 1);
            results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "", GrepSearchOption.StopAfterFirstMatch, -1);
            Assert.Equal(1, results.Count);
        }
Exemplo n.º 3
0
        public void TestReplaceWithPattern(SearchType type, bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase9", destFolder + "\\TestCase9", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase9", "test.txt"), type, "here", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 6);

            string testFile = Path.Combine(destFolder, @"TestCase9\test.txt");
            Dictionary <string, string> files = new Dictionary <string, string>
            {
                { testFile, Guid.NewGuid().ToString() + ".txt" }
            };

            core.Replace(files, type, "here", "$(guid)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(destFolder + "\\TestCase9\\test.txt", Encoding.ASCII).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            HashSet <string> uniqueGuids = new HashSet <string>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.Contains(match.Value))
                {
                    uniqueGuids.Add(match.Value);
                }
                else
                {
                    Assert.True(false, "All guides should be unique.");
                }
            }
        }
Exemplo n.º 4
0
        public void TestReplaceAndUndoWorks(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "test-file-code.cs"), SearchType.PlainText, "body", GrepSearchOption.None, -1);

            Assert.Equal(1, results.Count);
            Assert.Equal(2, results[0].SearchResults.Where(r => r.IsContext).Count());

            string testFile = Path.Combine(destFolder, @"TestCase3\test-file-code.cs");
            Dictionary <string, string> files = new Dictionary <string, string>
            {
                { testFile, Guid.NewGuid().ToString() + ".cs" }
            };

            core.Replace(files, SearchType.PlainText, "body", "text", GrepSearchOption.None, -1);
            string content = File.ReadAllText(testFile, Encoding.ASCII);

            Assert.False(content.Contains("body"));
            Assert.True(content.Contains("text"));

            core.Undo(files);
            content = File.ReadAllText(testFile, Encoding.ASCII);
            Assert.False(content.Contains("text"));
            Assert.True(content.Contains("body"));
        }
Exemplo n.º 5
0
        public void TestGuidxReplaceWithPatternRegex()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase9", destinationFolder + "\\TestCase9", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase9", "guidx.txt"), SearchType.Regex, "h\\wre", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 6);
            core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase9", "guidx.txt"), SearchType.Regex, destinationFolder + "\\TestCase9", "h\\wre", "$(guidx)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(destinationFolder + "\\TestCase9\\guidx.txt", Encoding.ASCII).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            Dictionary <string, int> uniqueGuids = new Dictionary <string, int>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.ContainsKey(match.Value))
                {
                    uniqueGuids[match.Value] = 1;
                }
                else
                {
                    uniqueGuids[match.Value]++;
                }
            }
            Assert.Equal(2, uniqueGuids.Keys.Count);
        }
Exemplo n.º 6
0
        public void TestReplaceOnFileWithout_UTF8_BOM(SearchType type, GrepSearchOption option, string searchFor, string replaceWith)
        {
            // Test for Issue #227
            Utils.CopyFiles(sourceFolder + "\\TestCase15", destinationFolder + "\\TestCase15", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase15", "books.xml"),
                                                          type, searchFor, option, -1);

            Assert.Equal(1, results.Count);
            string testFile = Path.Combine(destinationFolder, @"TestCase15\books.xml");
            Dictionary <string, string> files = new Dictionary <string, string>
            {
                { testFile, Guid.NewGuid().ToString() + ".xml" }
            };

            core.Replace(files, type, searchFor, replaceWith, option, -1);

            using (StreamReader reader = new StreamReader(testFile, true))
            {
                Assert.Equal(Encoding.UTF8, reader.CurrentEncoding);
                // check there is no BOM
                int bb = reader.BaseStream.ReadByte();
                Assert.NotEqual(0xEF, bb);
                Assert.Equal('<', bb);
                bb = reader.BaseStream.ReadByte();
                Assert.NotEqual(0xBB, bb);
                bb = reader.BaseStream.ReadByte();
                Assert.NotEqual(0xBF, bb);
            }
            var fileContent = File.ReadAllLines(destinationFolder + "\\TestCase15\\books.xml", Encoding.UTF8);

            Assert.Equal(38, fileContent.Length);
            Assert.Equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", fileContent[0]);
            Assert.Equal("  <book category=\"general\">", fileContent[8]);
        }
Exemplo n.º 7
0
        public void TestRegexCaptureReplace(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase3"), Path.Combine(destinationFolder, "TestCase3"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase3"), "test-file-code.cs"),
                                                          SearchType.Regex, @"-(\d)", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Single(results[0].SearchResults.Where(r => !r.IsContext));

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase3\test-file-code.cs");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.Regex, @"-(\d)", @"$1", GrepSearchOption.None, -1);
            string content = File.ReadAllText(testFile, Encoding.ASCII);

            Assert.DoesNotContain("= -1;", content);
            Assert.Contains("= 1;", content);

            core.Undo(files);
            content = File.ReadAllText(testFile, Encoding.ASCII);
            Assert.DoesNotContain("= 1;", content);
            Assert.Contains("= -1;", content);
        }
Exemplo n.º 8
0
        public void TestSearchAndFilteredReplace(SearchType type, GrepSearchOption option, string searchFor, string replaceWith)
        {
            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase15"), Path.Combine(destinationFolder, "TestCase15"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destinationFolder, "TestCase15"), "books.xml"),
                                                          type, searchFor, option, -1);

            Assert.Single(results);
            Assert.Equal(2, results[0].Matches.Count);

            // mark only the second match for replace
            results[0].Matches[1].ReplaceMatch = true;

            string            testFile = Path.Combine(destinationFolder, "TestCase15", "books.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, type, searchFor, replaceWith, option, -1);

            var fileContent = File.ReadAllText(testFile, Encoding.UTF8);

            Assert.Contains("<year>2003</year>", fileContent);
            Assert.Single(Regex.Matches(fileContent, "2002"));
            Assert.Single(Regex.Matches(fileContent, "2003"));
        }
Exemplo n.º 9
0
        public void TestReplaceWithNewLineWorks(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase8"), Path.Combine(destFolder, "TestCase8"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase8"), "test.txt"), SearchType.Regex, "here", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Single(results[0].SearchResults);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase8\test.txt");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.Regex, "here", "\\\\n", GrepSearchOption.None, -1);
            Assert.Equal(2, File.ReadAllText(testFile, Encoding.ASCII).Trim().Split('\n').Length);
        }
Exemplo n.º 10
0
        public void TestReplaceWithPattern(SearchType type)
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase9", destinationFolder + "\\TestCase9", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase9", "test.txt"), type, "here", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 6);
            core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase9", "test.txt"), type, "here", "$(guid)", GrepSearchOption.None, -1);
            string fileContent = File.ReadAllText(destinationFolder + "\\TestCase9\\test.txt", Encoding.ASCII).Trim();

            Assert.Equal(6, guidPattern.Matches(fileContent).Count);
            HashSet <string> uniqueGuids = new HashSet <string>();

            foreach (Match match in guidPattern.Matches(fileContent))
            {
                if (!uniqueGuids.Contains(match.Value))
                {
                    uniqueGuids.Add(match.Value);
                }
                else
                {
                    Assert.True(false, "All guides should be unique.");
                }
            }
        }
Exemplo n.º 11
0
        public void TestReplaceOnFileWith_UTF8_BOM(SearchType type, GrepSearchOption option, string searchFor, string replaceWith)
        {
            // Test for Issue #227 dnGrep inserting extra BOM, and scrambling file
            Utils.CopyFiles(sourceFolder + "\\TestCase15", destinationFolder + "\\TestCase15", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase15", "books_bom.xml"),
                                                          type, searchFor, option, -1);

            Assert.Equal(1, results.Count);
            core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase15", "books_bom.xml"),
                         type, searchFor, replaceWith, option, -1);

            using (StreamReader reader = new StreamReader(destinationFolder + "\\TestCase15\\books_bom.xml", true))
            {
                Assert.Equal(Encoding.UTF8, reader.CurrentEncoding);
                // check there is a BOM
                int bb = reader.BaseStream.ReadByte();
                Assert.Equal(0xEF, bb);
                bb = reader.BaseStream.ReadByte();
                Assert.Equal(0xBB, bb);
                bb = reader.BaseStream.ReadByte();
                Assert.Equal(0xBF, bb);
                // check that there are not two BOMs
                bb = reader.BaseStream.ReadByte();
                Assert.NotEqual(0xEF, bb);
                Assert.Equal('<', bb);
            }
            var fileContent = File.ReadAllLines(destinationFolder + "\\TestCase15\\books_bom.xml", Encoding.UTF8);

            Assert.Equal(38, fileContent.Length);
            string line1 = fileContent[0].Replace("\ufeff", ""); // remove BOM

            Assert.Equal("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", line1);
            Assert.Equal("  <book category=\"general\">", fileContent[8]);
        }
Exemplo n.º 12
0
        public void TestSearchAndFilteredReplaceXPathWithMissingXmlDeclaration(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase4"), Path.Combine(destFolder, "TestCase4"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase4"), "books_no_decl.xml"),
                                                          SearchType.XPath, "(//@currency)", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Equal(5, results[0].Matches.Count);

            // mark 2nd and 4th matches for replace
            results[0].Matches[1].ReplaceMatch = true;
            results[0].Matches[3].ReplaceMatch = true;

            string            testFile = Path.Combine(destinationFolder, "TestCase4", "books_no_decl.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.XPath, "(//@currency)", "EUR", GrepSearchOption.None, -1);

            var fileContent = File.ReadAllText(testFile, Encoding.UTF8);

            Assert.Equal(2, Regex.Matches(fileContent, "EUR").Count);
        }
Exemplo n.º 13
0
        public void TestSearchAndReplaceXPathWithMissingXmlDeclaration(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase4"), Path.Combine(destFolder, "TestCase4"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase4"), "books_no_decl.xml"),
                                                          SearchType.XPath, "(//@category)[2]", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Single(results[0].Matches);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destinationFolder, "TestCase4", "books_no_decl.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.XPath, "(//@category)[2]", "general", GrepSearchOption.None, -1);

            var fileContent = File.ReadAllLines(testFile, Encoding.UTF8);

            Assert.Equal(37, fileContent.Length);
            Assert.Equal("<bookstore>", fileContent[0]);
            Assert.Equal("  <book category=\"general\">", fileContent[7]);
        }
Exemplo n.º 14
0
        public void TestSearchWithStopAfterFirstMatch(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase3", destFolder + "\\TestCase3", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "public", GrepSearchOption.CaseSensitive, -1);

            Assert.True(results.Count > 1);
            results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "public", GrepSearchOption.StopAfterFirstMatch, -1);
            Assert.Equal(1, results.Count);
            results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "", GrepSearchOption.CaseSensitive, -1);
            Assert.True(results.Count > 1);
            results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "", GrepSearchOption.StopAfterFirstMatch, -1);
            Assert.Equal(1, results.Count);
        }
Exemplo n.º 15
0
        public void TestMultilineSearchAndReplace(string fileName, string newLine, bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase16"), Path.Combine(destinationFolder, "TestCase16"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase16"), fileName),
                                                          SearchType.Regex, @"\w*\.$.P\w*", GrepSearchOption.Multiline | GrepSearchOption.SingleLine, -1);

            Assert.Single(results);
            var hits = results[0].SearchResults.Where(r => !r.IsContext).ToList();

            Assert.Equal(2, hits.Count);
            Assert.Single(hits[0].Matches);
            Assert.Equal("hendrerit.", hits[0].LineText.Substring(hits[0].Matches[0].StartLocation, hits[0].Matches[0].Length));

            Assert.Single(hits[1].Matches);
            Assert.Equal("Phasellus", hits[1].LineText.Substring(hits[1].Matches[0].StartLocation, hits[1].Matches[0].Length));


            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase16", fileName);
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            core.Replace(files, SearchType.Regex, @"\w*\.$.P\w*", $"end.{newLine}Start", GrepSearchOption.Multiline | GrepSearchOption.SingleLine, -1);

            results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase16"), fileName),
                                  SearchType.Regex, @"\w*\.$.S\w*", GrepSearchOption.Multiline | GrepSearchOption.SingleLine, -1);
            Assert.Single(results);
            hits = results[0].SearchResults.Where(r => !r.IsContext).ToList();
            Assert.Equal(2, hits.Count);
            Assert.Single(hits[0].Matches);
            Assert.Equal("end.", hits[0].LineText.Substring(hits[0].Matches[0].StartLocation, hits[0].Matches[0].Length));

            Assert.Single(hits[1].Matches);
            Assert.Equal("Start", hits[1].LineText.Substring(hits[1].Matches[0].StartLocation, hits[1].Matches[0].Length));
        }
Exemplo n.º 16
0
        public void TestSearchXPathReturnsCorrectResultsCount()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase4", destinationFolder + "\\TestCase4", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase4", "app.config"), SearchType.XPath, "//setting", GrepSearchOption.CaseSensitive | GrepSearchOption.Multiline, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 84);
        }
Exemplo n.º 17
0
        public void TestSearchWholeWord_Issue_114_Plain()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase10", destinationFolder + "\\TestCase10", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase10", "issue-114.txt"), SearchType.PlainText, "protected", GrepSearchOption.WholeWord, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 1);
        }
Exemplo n.º 18
0
        public void TestSearchContainsValidPattern()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase3", destinationFolder + "\\TestCase3", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "dnGREP", GrepSearchOption.CaseSensitive, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal("dnGREP", results[0].Pattern);
        }
Exemplo n.º 19
0
        public void SearchWIthMultipleLines(SearchType type)
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase12", destinationFolder + "\\TestCase12", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase12", "issue-165.txt"), type, "asdf\r\nqwer", GrepSearchOption.Multiline, -1);

            Assert.Equal(results[0].Matches.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 5);
        }
Exemplo n.º 20
0
        public void TestSearchXPathReturnsCorrectResultsCount(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase4", destFolder + "\\TestCase4", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase4", "app.config"), SearchType.XPath, "//setting", GrepSearchOption.CaseSensitive | GrepSearchOption.Multiline, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 84);
        }
Exemplo n.º 21
0
        public void TestReplaceWithNewLineWorks()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase8", destinationFolder + "\\TestCase8", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase8", "test.txt"), SearchType.Regex, "here", GrepSearchOption.None, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 1);
            core.Replace(Directory.GetFiles(destinationFolder + "\\TestCase8", "test.txt"), SearchType.Regex, "here", "\\n", GrepSearchOption.None, -1);
            Assert.Equal(File.ReadAllText(destinationFolder + "\\TestCase8\\test.txt", Encoding.ASCII).Trim().Split('\n').Length, 2);
        }
Exemplo n.º 22
0
        public void TestSearchWholeWord_Issue_114_Regex(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase10"), Path.Combine(destFolder, "TestCase10"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase10"), "issue-114.txt"), SearchType.Regex, "protected", GrepSearchOption.WholeWord, -1);

            Assert.Single(results);
            Assert.Single(results[0].SearchResults);
        }
Exemplo n.º 23
0
        public void TestSearchContainsValidPattern(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase3", destFolder + "\\TestCase3", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "dnGREP", GrepSearchOption.CaseSensitive, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal("dnGREP", results[0].Pattern);
        }
Exemplo n.º 24
0
        public void TestSearchWholeWord_Issue_114_Plain(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase10", destFolder + "\\TestCase10", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase10", "issue-114.txt"), SearchType.PlainText, "protected", GrepSearchOption.WholeWord, -1);

            Assert.Equal(results.Count, 1);
            Assert.Equal(results[0].SearchResults.Count, 1);
        }
Exemplo n.º 25
0
        public void SearchWIthMultipleLines(SearchType type, bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase12", destFolder + "\\TestCase12", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase12", "issue-165.txt"), type, "asdf\r\nqwer", GrepSearchOption.Multiline, -1);

            Assert.Equal(1, results.Count);
            Assert.Equal(1, results[0].Matches.Count);
            Assert.Equal(5, results[0].SearchResults.Count);
        }
Exemplo n.º 26
0
        public void TestSearchXPathReturnsCorrectMatchCount(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase4"), Path.Combine(destFolder, "TestCase4"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase4"), "app.config"), SearchType.XPath,
                                                          "//setting", GrepSearchOption.CaseSensitive, -1);

            Assert.Single(results);
            Assert.Equal(28, results[0].Matches.Count);
        }
Exemplo n.º 27
0
        public void TestSearchAndReplaceXPath(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase4"), Path.Combine(destFolder, "TestCase4"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase4"), "app.config"), SearchType.XPath,
                                                          "//appSettings", GrepSearchOption.CaseSensitive, -1);

            Assert.Single(results);
            Assert.Single(results[0].Matches);

            // mark all matches for replace
            foreach (var result in results)
            {
                foreach (var match in result.Matches)
                {
                    match.ReplaceMatch = true;
                }
            }

            string            testFile = Path.Combine(destFolder, @"TestCase4\app.config");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            string replaceText = "<add key=\"test\" value=\"true\" />";

            core.Replace(files, SearchType.XPath, "//appSettings", replaceText, GrepSearchOption.None, -1);

            results = core.Search(new string[] { testFile }, SearchType.XPath, "//add", GrepSearchOption.None, -1);
            Assert.Single(results);
            Assert.Single(results[0].Matches);
            var hit = results[0].SearchResults.Where(r => !r.IsContext).ToArray();

            Assert.Single(hit);
            Assert.Contains("key=\"test\"", hit[0].LineText);
            Assert.Contains("value=\"true\"", hit[0].LineText);
        }
Exemplo n.º 28
0
        public void TestSearchPlainReturnsCorrectNumber(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(sourceFolder + "\\TestCase3", destFolder + "\\TestCase3", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "dnGREP", GrepSearchOption.CaseSensitive, -1);

            Assert.Equal(results.Count, 1);

            results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "dngrep", GrepSearchOption.CaseSensitive, -1);
            Assert.Equal(results.Count, 0);

            results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "dngrep", GrepSearchOption.CaseSensitive | GrepSearchOption.Multiline, -1);
            Assert.Equal(results.Count, 0);

            results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "dngrep", GrepSearchOption.None, -1);
            Assert.Equal(results.Count, 1);

            results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "string", GrepSearchOption.None, -1);
            Assert.Equal(results.Count, 2);
            Assert.Equal(results[0].Matches.Count, 3);
            Assert.Equal(results[1].Matches.Count, 282);

            results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "string", GrepSearchOption.Multiline, -1);
            Assert.Equal(results.Count, 2);
            Assert.Equal(results[0].Matches.Count, 3);
            Assert.Equal(results[1].Matches.Count, 282);

            results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "string", GrepSearchOption.Multiline, -1);
            Assert.Equal(results.Count, 2);
            Assert.Equal(results[0].Matches.Count, 3);
            Assert.Equal(results[1].Matches.Count, 282);

            results = core.Search(Directory.GetFiles(destFolder + "\\TestCase3", "*.*"), SearchType.PlainText, "dngrep", GrepSearchOption.CaseSensitive, -1);
            Assert.Equal(results.Count, 0);

            Assert.Empty(core.Search(null, SearchType.PlainText, "string", GrepSearchOption.Multiline, -1));
            Assert.Empty(core.Search(new string[] { }, SearchType.PlainText, "string", GrepSearchOption.Multiline, -1));
        }
Exemplo n.º 29
0
        public void TestSearchAndReplaceXPathAttribute(bool useLongPath)
        {
            string destFolder = useLongPath ? GetLongPathDestination(Guid.NewGuid().ToString()) : destinationFolder;

            Utils.CopyFiles(Path.Combine(sourceFolder, "TestCase15"), Path.Combine(destFolder, "TestCase15"), null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(Path.Combine(destFolder, "TestCase15"), "books.xml"), SearchType.XPath,
                                                          "/bookstore/book/title[1]/@lang", GrepSearchOption.None, -1);

            Assert.Single(results);
            Assert.Equal(5, results[0].Matches.Count);

            // mark all matches for replace
            foreach (var match in results[0].Matches)
            {
                match.ReplaceMatch = true;
            }

            string            testFile = Path.Combine(destFolder, @"TestCase15\books.xml");
            List <ReplaceDef> files    = new List <ReplaceDef>
            {
                new ReplaceDef(testFile, results[0].Matches)
            };

            string replaceText = "dnGrep";

            core.Replace(files, SearchType.XPath, "/bookstore/book/title[1]/@lang", replaceText, GrepSearchOption.None, -1);

            results = core.Search(new string[] { testFile }, SearchType.XPath, "/bookstore/book/title[1]/@lang", GrepSearchOption.None, -1);
            Assert.Single(results);
            Assert.Equal(5, results[0].Matches.Count);
            var hits = results[0].SearchResults.Where(r => !r.IsContext).ToArray();

            Assert.Equal(5, hits.Length);
            foreach (var line in hits)
            {
                Assert.Contains("lang=\"dnGrep\"", line.LineText);
            }
        }
Exemplo n.º 30
0
        public void TestRegexEolToken_Issue_210_MultiLine()
        {
            Utils.CopyFiles(sourceFolder + "\\TestCase15", destinationFolder + "\\TestCase15", null, null);
            GrepCore core = new GrepCore();
            List <GrepSearchResult> results = core.Search(Directory.GetFiles(destinationFolder + "\\TestCase15", "*.txt"), SearchType.Regex, @"[3-9]\d*?.\d\d\d$", GrepSearchOption.Multiline, -1);

            // should be four test files with no EOL, Windows, Unix, and Mac EOL
            Assert.Equal(4, results.Count);
            Assert.Equal(1, results[0].Matches.Count);
            Assert.Equal(1, results[1].Matches.Count);
            Assert.Equal(1, results[2].Matches.Count);
            Assert.Equal(1, results[3].Matches.Count);
        }