コード例 #1
0
        public void ScanTextWithOverlappingTermHit()
        {
            MockTermTable table     = new MockTermTable("file1");
            MockTerm      bigTerm   = new MockTerm("abc def", 1, "fooClass", "fooComment", "fooRecommended", table);
            MockTerm      smallTerm = new MockTerm("abc", 1, "fooClass", "fooComment", "fooRecommended", table);

            table.AddSearchTerm(bigTerm);
            table.AddSearchTerm(smallTerm);
            IList <IScanHit> hits = InternalScanText("foo abc def bar", new ITermTable[] { table });

            Assert.AreEqual(1, hits.Count, "Search returned wrong number of hits.");
            Assert.AreEqual(bigTerm, hits[0].Term, "Term property of hit is incorrect.");
            Assert.AreEqual(0, hits[0].Line, "Line property of hit is incorrect.");
            Assert.AreEqual("foo abc def bar", hits[0].LineText, "LineText property of hit is incorrect.");
            Assert.AreEqual(4, hits[0].Column, "Column property of hit is incorrect.");
        }
コード例 #2
0
        public void ScanTextWithDuplicateTerms()
        {
            MockTermTable table1 = new MockTermTable("file1");
            MockTerm      term1  = new MockTerm("foo", 1, "fooClass", "fooComment", "fooRecommended", table1);

            term1.AddExclusion("foo exclusion 1");
            term1.AddExclusion("foo exclusion 2");
            table1.AddSearchTerm(term1);

            MockTermTable table2 = new MockTermTable("file1");
            MockTerm      term2  = new MockTerm("foo", 1, "fooClass2", "fooComment2", "fooRecommended", table2);

            term2.AddExclusion("foo exclusion 2");
            term2.AddExclusion("foo exclusion 3");
            table2.AddSearchTerm(term2);

            IList <IScanHit> hits = InternalScanText("'foo exclusion 2' should be excluded, 'foo exclusion 1' and 'foo exclusion 3' should not", new ITermTable[] { table1, table2 });

            Assert.AreEqual(2, hits.Count, "Search returned wrong number of hits.");
            Assert.AreEqual(term2, hits[0].Term, "Term property of hit 0 is incorrect.");
            Assert.AreEqual(0, hits[0].Line, "Line property of hit 0 is incorrect.");
            Assert.AreEqual(39, hits[0].Column, "Column property of hit 0 is incorrect.");
            Assert.IsNotNull(hits[0].Warning, "Hit 0 did not have a warning.");
            Assert.AreEqual(term1, hits[1].Term, "Term property of hit 1 is incorrect.");
            Assert.AreEqual(0, hits[1].Line, "Line property of hit 1 is incorrect.");
            Assert.AreEqual(61, hits[1].Column, "Column property of hit 1 is incorrect.");
            Assert.IsNotNull(hits[1].Warning, "Hit 0 did not have a warning.");
        }
コード例 #3
0
        public void TestReset()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("foo", 1, "fooClass", "fooComment", "fooRecommended", table);

            table.AddSearchTerm(term);

            CodeSweep.Scanner.MatchFinder_Accessor accessor = new CodeSweep.Scanner.MatchFinder_Accessor(new ITermTable[] { table });

            IList <IScanHit> hits1 = InternalScanText("foo bar ", accessor);

            Assert.AreEqual(1, hits1.Count, "First search returned wrong number of hits.");
            Assert.AreEqual(0, hits1[0].Column, "Column property of first hit is incorrect.");

            IList <IScanHit> hits2 = InternalScanText("foo bar", accessor);

            // This is what happens if you DON'T call Reset...
            Assert.AreEqual(1, hits2.Count, "Second search returned wrong number of hits.");
            Assert.AreEqual(8, hits2[0].Column, "Column property of second hit is incorrect.");

            accessor.Reset();

            IList <IScanHit> hits3 = InternalScanText("foo bar", accessor);

            Assert.AreEqual(1, hits3.Count, "Third search returned wrong number of hits.");
            Assert.AreEqual(0, hits3[0].Column, "Column property of third hit is incorrect.");
        }
コード例 #4
0
        private void InternalScanWithSomeFailingFiles(bool useCallback)
        {
            IScanner target = Factory.GetScanner();

            MockTermTable table = new MockTermTable("file");

            table.AddSearchTerm(new MockTerm("foo", 1, "", "", "", table));
            table.AddSearchTerm(new MockTerm("bar", 1, "", "", "", table));

            List <string> filePaths = new List <string>();

            filePaths.Add(Utilities.CreateTempTxtFile("line 1\nfoo\nline 3"));
            filePaths.Add(Utilities.CreateTempTxtFile("line 1\nline 2\nline 3"));
            filePaths.Add(Utilities.CreateTempTxtFile("bar"));

            IMultiFileScanResult scanResults;

            if (useCallback)
            {
                List <IScanResult> callbackResults = new List <IScanResult>();
                scanResults = target.Scan(filePaths, new ITermTable[] { table }, delegate(IScanResult result) { callbackResults.Add(result); });

                int i = 0;
                foreach (IScanResult result in scanResults.Results)
                {
                    Assert.AreEqual(callbackResults[i++], result);
                }
            }
            else
            {
                scanResults = target.Scan(filePaths, new ITermTable[] { table });
            }

            Assert.AreEqual(scanResults.Attempted, 3, "Attempted != 3 in return value from Scanner.Scanner.Scan with 1 passing / 2 failing files.");
            Assert.AreEqual(scanResults.FailedScan, 2, "FailedScan != 2 in return value from Scanner.Scanner.Scan with 1 passing / 2 failing files.");
            Assert.AreEqual(scanResults.PassedScan, 1, "PassedScan != 1 in return value from Scanner.Scanner.Scan with 1 passing / 2 failing files.");
            Assert.AreEqual(scanResults.UnableToScan, 0, "UnableToScan != 0 in return value from Scanner.Scanner.Scan with 1 passing / 2 failing files.");
            Assert.IsNotNull(scanResults.Results, "Results property was null in return value from Scanner.Scanner.Scan with 1 passing / 2 failing files.");

            int count = 0;

            foreach (IScanResult scanResult in scanResults.Results)
            {
                ++count;
            }
            Assert.AreEqual(count, 3, "Results list did not contain 3 entries in return value from Scanner.Scanner.Scan with 1 passing / 2 failing files.");
        }
コード例 #5
0
        public void ScanTextWithOtherTermsAppearingInExclusion()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term1 = new MockTerm("foo", 1, "fooClass", "fooComment", "fooRecommended", table);

            term1.AddExclusion("a foo is not a bar");
            MockTerm term2 = new MockTerm("bar", 1, "barClass", "barComment", "fooRecommended", table);

            table.AddSearchTerm(term1);
            table.AddSearchTerm(term2);
            IList <IScanHit> hits = InternalScanText("a foo is not a bar", new ITermTable[] { table });

            Assert.AreEqual(1, hits.Count, "Search returned wrong number of hits.");
            Assert.AreEqual(term2, hits[0].Term, "Term property of hit is incorrect.");
            Assert.AreEqual(0, hits[0].Line, "Line property of hit is incorrect.");
            Assert.AreEqual(15, hits[0].Column, "Column property of hit is incorrect.");
        }
コード例 #6
0
        public void ScanTextWithAdjacentTerms()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term1 = new MockTerm("foo.", 1, "fooClass", "fooComment", "fooRecommended", table);
            MockTerm      term2 = new MockTerm(".bar", 1, "barClass", "barComment", "fooRecommended", table);

            table.AddSearchTerm(term1);
            table.AddSearchTerm(term2);
            IList <IScanHit> hits = InternalScanText("foo..bar", new ITermTable[] { table });

            Assert.AreEqual(2, hits.Count, "Search returned wrong number of hits.");
            Assert.AreEqual(term1, hits[0].Term, "Term property of hit 0 is incorrect.");
            Assert.AreEqual(0, hits[0].Line, "Line property of hit 0 is incorrect.");
            Assert.AreEqual(0, hits[0].Column, "Column property of hit 0 is incorrect.");
            Assert.AreEqual(term2, hits[1].Term, "Term property of hit 1 is incorrect.");
            Assert.AreEqual(0, hits[1].Line, "Line property of hit 1 is incorrect.");
            Assert.AreEqual(4, hits[1].Column, "Column property of hit 1 is incorrect.");
        }
コード例 #7
0
        private bool TestTokenMatchWithSpecificSurround(string before, string after)
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("foo", 1, "fooClass", "fooComment", "fooRecommended", table);

            table.AddSearchTerm(term);
            IList <IScanHit> hits = InternalScanText(before + "foo" + after, new ITermTable[] { table });

            return(hits.Count == 1);
        }
コード例 #8
0
        public void ScanTextWithTermAppearingMultipleTimesInExclusion()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("smurf", 1, "fooClass", "fooComment", "fooRecommended", table);

            term.AddExclusion("a smurf a day keeps the smurf away");
            table.AddSearchTerm(term);
            IList <IScanHit> hits = InternalScanText("foo - a smurf a day keeps the smurf away - bar", new ITermTable[] { table });

            Assert.AreEqual(0, hits.Count, "Search returned wrong number of hits.");
        }
コード例 #9
0
        public void ScanTextWithTermInMiddleOfExclusion()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("abcdef", 1, "fooClass", "fooComment", "fooRecommended", table);

            term.AddExclusion("this time the term (abcdef) is in the middle");
            table.AddSearchTerm(term);
            IList <IScanHit> hits = InternalScanText("foo - this time the term (abcdef) is in the middle - bar", new ITermTable[] { table });

            Assert.AreEqual(0, hits.Count, "Search returned wrong number of hits.");
        }
コード例 #10
0
        public void ScanTextWithTermAtEndOfExclusion()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("abcdef", 1, "fooClass", "fooComment", "fooRecommended", table);

            term.AddExclusion("the text at the end of this exclusion is abcdef");
            table.AddSearchTerm(term);
            IList <IScanHit> hits = InternalScanText("the text at the end of this exclusion is abcdef", new ITermTable[] { table });

            Assert.AreEqual(0, hits.Count, "Search returned wrong number of hits.");
        }
コード例 #11
0
        public void ScanTextWithOverlappingExclusionsWhenSecondExclusionIsInvalidatedFirst()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term1 = new MockTerm("foo", 1, "fooClass", "fooComment", "fooRecommended", table);

            term1.AddExclusion("abc foo then bar and some more text");
            MockTerm term2 = new MockTerm("bar", 1, "barClass", "barComment", "fooRecommended", table);

            term2.AddExclusion("foo then bar and some more");
            table.AddSearchTerm(term1);
            table.AddSearchTerm(term2);
            IList <IScanHit> hits = InternalScanText("abc foo then bar and some morINVALIDATED", new ITermTable[] { table });

            Assert.AreEqual(2, hits.Count, "Search returned wrong number of hits.");
            Assert.AreEqual(term1, hits[0].Term, "Term property of hit 0 is incorrect.");
            Assert.AreEqual(0, hits[0].Line, "Line property of hit 0 is incorrect.");
            Assert.AreEqual(4, hits[0].Column, "Column property of hit 0 is incorrect.");
            Assert.AreEqual(term2, hits[1].Term, "Term property of hit 1 is incorrect.");
            Assert.AreEqual(0, hits[1].Line, "Line property of hit 1 is incorrect.");
            Assert.AreEqual(13, hits[1].Column, "Column property of hit 1 is incorrect.");
        }
コード例 #12
0
        public void ScanTextWithSingleCharTermHit()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("d", 1, "fooClass", "fooComment", "fooRecommended", table);

            table.AddSearchTerm(term);
            IList <IScanHit> hits = InternalScanText("the term I want to find\nis \"d\".  Is it here?", new ITermTable[] { table });

            Assert.AreEqual(1, hits.Count, "Failed to find a single-character term.");
            Assert.AreEqual(term, hits[0].Term, "Term property of hit is incorrect.");
            Assert.AreEqual(1, hits[0].Line, "Line property of hit is incorrect.");
            Assert.AreEqual("is \"d\".  Is it here?", hits[0].LineText, "LineText property of hit is incorrect.");
            Assert.AreEqual(4, hits[0].Column, "Column property of hit is incorrect.");
        }
コード例 #13
0
        public void ScanTextWithDifferentCase()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("foo", 1, "fooClass", "fooComment", "fooRecommended", table);

            term.AddExclusion("this Foo is excluded");
            table.AddSearchTerm(term);
            IList <IScanHit> hits = InternalScanText("THIS FOO IS EXCLUDED, BUT THIS FOO IS NOT", new ITermTable[] { table });

            Assert.AreEqual(1, hits.Count, "Search returned wrong number of hits.");
            Assert.AreEqual(term, hits[0].Term, "Term property of hit 0 is incorrect.");
            Assert.AreEqual(0, hits[0].Line, "Line property of hit 0 is incorrect.");
            Assert.AreEqual(31, hits[0].Column, "Column property of hit 0 is incorrect.");
        }
コード例 #14
0
        public void ScanTextWithTermPartiallyOverlappingExclusion()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("foo", 1, "fooClass", "fooComment", "fooRecommended", table);

            term.AddExclusion("a foo is not a fo");
            table.AddSearchTerm(term);
            IList <IScanHit> hits = InternalScanText("a foo is not a foo bar", new ITermTable[] { table });

            Assert.AreEqual(1, hits.Count, "Search returned wrong number of hits.");
            Assert.AreEqual(term, hits[0].Term, "Term property of hit 0 is incorrect.");
            Assert.AreEqual(0, hits[0].Line, "Line property of hit 0 is incorrect.");
            Assert.AreEqual(15, hits[0].Column, "Column property of hit 0 is incorrect.");
        }
コード例 #15
0
        public void ScanTextWithExclusionPresentButSeparateFromTerm()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("foo", 1, "fooClass", "fooComment", "fooRecommended", table);

            term.AddExclusion("this foo is excluded");
            table.AddSearchTerm(term);
            IList <IScanHit> hits = InternalScanText("this foo is excluded; this foo is not", new ITermTable[] { table });

            Assert.AreEqual(1, hits.Count, "Search returned wrong number of hits.");
            Assert.AreEqual(term, hits[0].Term, "Term property of hit 0 is incorrect.");
            Assert.AreEqual(0, hits[0].Line, "Line property of hit 0 is incorrect.");
            Assert.AreEqual(27, hits[0].Column, "Column property of hit 0 is incorrect.");
        }
コード例 #16
0
        public void ScanTextWithMultipleExclusions()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("foo", 1, "fooClass", "fooComment", "fooRecommended", table);

            term.AddExclusion("first foo");
            term.AddExclusion("second foo");
            term.AddExclusion("third foo");
            table.AddSearchTerm(term);
            IList <IScanHit> hits = InternalScanText("this first-foo will not exclude, but this second foo will, as will this third foo", new ITermTable[] { table });

            Assert.AreEqual(1, hits.Count, "Search returned wrong number of hits.");
            Assert.AreEqual(term, hits[0].Term, "Term property of hit is incorrect.");
            Assert.AreEqual(0, hits[0].Line, "Line property of hit is incorrect.");
            Assert.AreEqual(11, hits[0].Column, "Column property of hit is incorrect.");
        }
コード例 #17
0
        public void ScanWithDifferentEncodings()
        {
            List <string> filePaths = new List <string>();

            filePaths.Add(Utilities.CreateTempTxtFile("first file", Encoding.Unicode));
            filePaths.Add(Utilities.CreateTempTxtFile("second file", Encoding.BigEndianUnicode));
            filePaths.Add(Utilities.CreateTempTxtFile("third file", Encoding.UTF32));
            filePaths.Add(Utilities.CreateTempTxtFile("fourth file", Encoding.UTF8));
            filePaths.Add(Utilities.CreateTempTxtFile("fifth file", Encoding.UTF7));
            filePaths.Add(Utilities.CreateTempTxtFile("sixth file", Encoding.ASCII));

            // TODO: what about different code pages within the ASCII encoding?

            List <ITermTable> termTables = new List <ITermTable>();

            MockTermTable table = new MockTermTable("file");

            table.AddSearchTerm(new MockTerm("first", 1, "class", "comment", "recommended", table));
            table.AddSearchTerm(new MockTerm("second", 1, "class", "comment", "recommended", table));
            table.AddSearchTerm(new MockTerm("third", 1, "class", "comment", "recommended", table));
            table.AddSearchTerm(new MockTerm("fourth", 1, "class", "comment", "recommended", table));
            table.AddSearchTerm(new MockTerm("fifth", 1, "class", "comment", "recommended", table));
            table.AddSearchTerm(new MockTerm("sixth", 1, "class", "comment", "recommended", table));

            termTables.Add(table);

            Scanner_Accessor accessor = new Scanner_Accessor();

            IMultiFileScanResult result = accessor.Scan(filePaths, termTables);

            Assert.AreEqual(6, result.Attempted, "Attempted count incorrect.");
            Assert.AreEqual(6, result.FailedScan, "FailedScan count incorrect.");
            Assert.AreEqual(0, result.PassedScan, "PassedScan count incorrect.");
            Assert.AreEqual(0, result.UnableToScan, "UnableToScan count incorrect.");

            int fileIndex = 0;

            foreach (IScanResult scanResult in result.Results)
            {
                int count = 0;
                foreach (IScanHit hits in scanResult.Results)
                {
                    ++count;
                }
                Assert.AreEqual(1, count, "Result " + fileIndex.ToString() + " did not have the expected number of hits.");
                ++fileIndex;
            }
        }
コード例 #18
0
        public void ScanTextWithDifferentNewlines()
        {
            MockTermTable table = new MockTermTable("file1");
            MockTerm      term  = new MockTerm("foo", 1, "fooClass", "fooComment", "fooRecommended", table);

            table.AddSearchTerm(term);
            IList <IScanHit> hits = InternalScanText("\nthis foo is on line 1\rthis foo is on line 2\r\nthis foo is on line 3\n\rthis foo is on line 5\n\nthis foo is on line 7\r\rthis foo is on line 9", new ITermTable[] { table });

            Assert.AreEqual(6, hits.Count, "Search returned wrong number of hits.");
            Assert.AreEqual(1, hits[0].Line, "Line property of hit 0 is incorrect.");
            Assert.AreEqual("this foo is on line 1", hits[0].LineText, "LineText property of hit 0 is incorrect.");
            Assert.AreEqual(2, hits[1].Line, "Line property of hit 1 is incorrect.");
            Assert.AreEqual("this foo is on line 2", hits[1].LineText, "LineText property of hit 1 is incorrect.");
            Assert.AreEqual(3, hits[2].Line, "Line property of hit 2 is incorrect.");
            Assert.AreEqual("this foo is on line 3", hits[2].LineText, "LineText property of hit 2 is incorrect.");
            Assert.AreEqual(5, hits[3].Line, "Line property of hit 3 is incorrect.");
            Assert.AreEqual("this foo is on line 5", hits[3].LineText, "LineText property of hit 3 is incorrect.");
            Assert.AreEqual(7, hits[4].Line, "Line property of hit 4 is incorrect.");
            Assert.AreEqual("this foo is on line 7", hits[4].LineText, "LineText property of hit 4 is incorrect.");
            Assert.AreEqual(9, hits[5].Line, "Line property of hit 5 is incorrect.");
            Assert.AreEqual("this foo is on line 9", hits[5].LineText, "LineText property of hit 5 is incorrect.");
        }