def test_singleline_longer_colorize_searchresult(self): formatter = SearchResultFormatter( SearchSettings(colorize=True, maxlinelength=100)) pattern = 'maxlen' filename = 'maxlen.txt' sf = SearchFile(path='.', filename=filename, filetype=FileType.TEXT) linenum = 10 match_start_index = 53 match_end_index = 59 line = '0123456789012345678901234567890123456789012345678901' + \ 'maxlen' + \ '8901234567890123456789012345678901234567890123456789' linesbefore = [] linesafter = [] searchresult = SearchResult(pattern=pattern, file=sf, linenum=linenum, match_start_index=match_start_index, match_end_index=match_end_index, line=line, lines_before=linesbefore, lines_after=linesafter) expectedline = '...89012345678901234567890123456789012345678901' + \ Color.GREEN + 'maxlen' + Color.RESET + \ '89012345678901234567890123456789012345678901...' expectedoutput = "{0:s}: {1:d}: [{2:d}:{3:d}]: {4:s}".format( os.path.join('.', filename), linenum, match_start_index, match_end_index, expectedline) output = formatter.format(searchresult) self.assertEqual(expectedoutput, output)
def test_filter_file_is_hidden_file(self): settings = self.get_settings() searcher = Searcher(settings) f = SearchFile(path='', filename='.gitignore', filetype=FileType.UNKNOWN) self.assertFalse(searcher.filter_file(f))
def test_singleline_searchresult(self): formatter = SearchResultFormatter(SearchSettings(colorize=False)) pattern = "Search" filename = 'Searcher.cs' sf = SearchFile(path=self.cssearch_path, filename=filename, filetype=FileType.CODE) linenum = 10 match_start_index = 15 match_end_index = 23 line = "\tpublic class Searcher\n" linesbefore = [] linesafter = [] searchresult = SearchResult(pattern=pattern, file=sf, linenum=linenum, match_start_index=match_start_index, match_end_index=match_end_index, line=line, lines_before=linesbefore, lines_after=linesafter) expectedoutput = "{0:s}: {1:d}: [{2:d}:{3:d}]: {4:s}".format( os.path.join(self.cssearch_path, filename), linenum, match_start_index, match_end_index, line.strip()) output = formatter.format(searchresult) self.assertEqual(expectedoutput, output)
def test_multiline_searchresult(self): pattern = "Search" filename = 'Searcher.cs' sf = SearchFile(path=self.cssearch_path, filename=filename, filetype=FileType.TEXT) linenum = 10 match_start_index = 15 match_end_index = 23 line = "\tpublic class Searcher\n" linesbefore = ["namespace CsSearch\n", "{\n"] linesafter = ["\t{\n", "\t\tprivate readonly FileTypes _fileTypes;\n"] searchresult = SearchResult(pattern=pattern, file=sf, linenum=linenum, match_start_index=match_start_index, match_end_index=match_end_index, line=line, lines_before=linesbefore, lines_after=linesafter) expectedoutput = """================================================================================ %s: %d: [%d:%d] -------------------------------------------------------------------------------- 8 | namespace CsSearch 9 | { > 10 | \tpublic class Searcher 11 | \t{ 12 | \t\tprivate readonly FileTypes _fileTypes; """ % (os.path.join(self.cssearch_path, filename), linenum, match_start_index, match_end_index) self.assertEqual(expectedoutput, str(searchresult))
def test_binaryfile_searchresult(self): formatter = SearchResultFormatter(SearchSettings()) pattern = "Search" filename = 'Searcher.exe' sf = SearchFile(path=self.cssearch_path, filename=filename, filetype=FileType.BINARY) linenum = 0 match_start_index = 0 match_end_index = 0 line = '' linesbefore = [] linesafter = [] searchresult = SearchResult(pattern=pattern, file=sf, linenum=linenum, match_start_index=match_start_index, match_end_index=match_end_index, line=line, lines_before=linesbefore, lines_after=linesafter) expectedoutput = "{0:s} matches at [{1:d}:{2:d}]".format( os.path.join(self.cssearch_path, filename), match_start_index, match_end_index) output = formatter.format(searchresult) self.assertEqual(expectedoutput, output)
def test_filter_file_nonarchive_archivesonly(self): settings = self.get_settings() settings.archivesonly = True settings.searcharchives = True searcher = Searcher(settings) f = SearchFile(path='', filename='FileUtil.pm', filetype=FileType.TEXT) self.assertFalse(searcher.filter_file(f))
def test_multiline_searchresult(self): formatter = SearchResultFormatter(SearchSettings(colorize=False)) pattern = "Search" filename = 'Searcher.cs' sf = SearchFile(path=self.cssearch_path, filename=filename, filetype=FileType.TEXT) linenum = 10 match_start_index = 15 match_end_index = 23 line = "\tpublic class Searcher\n" linesbefore = ["namespace CsSearch\n", "{\n"] linesafter = ["\t{\n", "\t\tprivate readonly FileTypes _fileTypes;\n"] searchresult = SearchResult(pattern=pattern, file=sf, linenum=linenum, match_start_index=match_start_index, match_end_index=match_end_index, line=line, lines_before=linesbefore, lines_after=linesafter) expectedoutput = "================================================================================\n" + \ '{0:s}: {1:d}: [{2:d}:{3:d}]\n'.format(os.path.join(self.cssearch_path, filename), linenum, match_start_index, match_end_index) + \ """-------------------------------------------------------------------------------- 8 | namespace CsSearch 9 | { > 10 | \tpublic class Searcher 11 | \t{ 12 | \t\tprivate readonly FileTypes _fileTypes; """ output = formatter.format(searchresult) self.assertEqual(expectedoutput, output)
def test_filter_file_archive_archivesonly(self): settings = self.get_settings() settings.archivesonly = True settings.searcharchives = True searcher = Searcher(settings) f = SearchFile(path='', filename='archive.zip', filetype=FileType.ARCHIVE) self.assertTrue(searcher.filter_file(f))
def test_filter_file_archive_no_searcharchives(self): settings = self.get_settings() searcher = Searcher(settings) f = SearchFile(path='', filename='archive.zip', filetype=FileType.ARCHIVE) self.assertFalse(searcher.filter_file(f))
def test_searchfile_rel_path3(self): path = '..' filename = 'searchfile.py' searchfile = SearchFile(path=path, filename=filename, filetype=FileType.CODE) self.assertEqual('../searchfile.py', searchfile.relativepath) self.assertEqual('../searchfile.py', str(searchfile))
def test_filter_file_hidden_includehidden(self): settings = self.get_settings() settings.excludehidden = False searcher = Searcher(settings) f = SearchFile(path='', filename='.gitignore', filetype=FileType.UNKNOWN) self.assertTrue(searcher.filter_file(f))
def test_searchfile_abs_path(self): path = '/Users/cary/src/xsearch/python/pysearch' filename = 'searchfile.py' searchfile = SearchFile(path=path, filename=filename, filetype=FileType.CODE) self.assertEqual( '/Users/cary/src/xsearch/python/pysearch/searchfile.py', searchfile.relativepath) self.assertEqual( '/Users/cary/src/xsearch/python/pysearch/searchfile.py', str(searchfile))
def test_binaryfile_searchresult(self): pattern = "Search" filename = 'Searcher.exe' sf = SearchFile(path=self.cssearch_path, filename=filename, filetype=FileType.BINARY) linenum = 0 match_start_index = 0 match_end_index = 0 line = '' linesbefore = [] linesafter = [] searchresult = SearchResult(pattern=pattern, file=sf, linenum=linenum, match_start_index=match_start_index, match_end_index=match_end_index, line=line, lines_before=linesbefore, lines_after=linesafter) expectedoutput = "%s matches at [%d:%d]" % (os.path.join( self.cssearch_path, filename), match_start_index, match_end_index) self.assertEqual(expectedoutput, str(searchresult))
def test_singleline_searchresult(self): pattern = "Search" filename = 'Searcher.cs' sf = SearchFile(path=self.cssearch_path, filename=filename, filetype=FileType.TEXT) linenum = 10 match_start_index = 15 match_end_index = 23 line = "\tpublic class Searcher\n" linesbefore = [] linesafter = [] searchresult = SearchResult(pattern=pattern, file=sf, linenum=linenum, match_start_index=match_start_index, match_end_index=match_end_index, line=line, lines_before=linesbefore, lines_after=linesafter) expectedoutput = "%s: %d: [%d:%d]: %s" % ( os.path.join(self.cssearch_path, filename), linenum, match_start_index, match_end_index, line.strip()) self.assertEqual(expectedoutput, str(searchresult))
def test_filter_file_matches_by_default(self): settings = self.get_settings() searcher = Searcher(settings) f = SearchFile(path='', filename='FileUtil.pm', filetype=FileType.TEXT) self.assertTrue(searcher.filter_file(f))
def test_is_search_file_matches_by_default(self): settings = self.get_settings() searcher = Searcher(settings) f = SearchFile(path='.', filename='FileUtil.pm', filetype=FileType.CODE) self.assertTrue(searcher.is_search_file(f))
def test_filter_file_not_is_search_file(self): settings = self.get_settings() settings.add_exts('pl', 'in_extensions') searcher = Searcher(settings) f = SearchFile(path='', filename='FileUtil.pm', filetype=FileType.TEXT) self.assertFalse(searcher.filter_file(f))
def test_is_search_file_no_match_out_pattern(self): settings = self.get_settings() settings.add_patterns('Search', 'out_filepatterns') searcher = Searcher(settings) f = SearchFile(path='.', filename='FileUtil.pm', filetype=FileType.CODE) self.assertTrue(searcher.is_search_file(f))
def test_is_search_file_matches_out_extension(self): settings = self.get_settings() settings.add_exts('pm', 'out_extensions') searcher = Searcher(settings) f = SearchFile(path='.', filename='FileUtil.pm', filetype=FileType.CODE) self.assertFalse(searcher.is_search_file(f))