def setUp(self): """setUp for TestFind class""" super().setUp() c = self.c c.findCommands = self.x = x = leoFind.LeoFind(c) x.ftm = StringFindTabManager(c) self.settings = x.default_settings() self.make_test_tree()
def test_inner_search_plain(self): c = self.c x = leoFind.LeoFind(c) def test(table, table_name, nocase, word): test_n = 0 for pattern, s, i, j, expected, expected_i, expected_j in table: test_n += 1 if j == -1: j = len(s) got_i, got_j = x._inner_search_plain(s, i, j, pattern, nocase=nocase, word=word) got = s[got_i:got_j] assert expected == got and got_i == expected_i and got_j == expected_j, ( '\n table: %s' '\n i test: %s' '\n pattern: %r' '\n s: %r' '\n expected: %r' '\n got: %r' '\nexpected i: %s' '\n got i: %s' '\nexpected j: %s' '\n got j: %s' % (table_name, test_n, pattern, s, expected, got, expected_i, got_i, expected_j, got_j)) plain_table = ( # pattern s i, j expected, expected_i, expected_j ('a', 'baca', 0, -1, 'a', 1, 2), ('A', 'bAcde', 0, -1, 'A', 1, 2), ) nocase_table = ( # pattern s i, j expected, expected_i, expected_j ('a', 'abaAca', 0, -1, 'a', 0, 1), ('A', 'abcdca', 0, -1, 'a', 0, 1), ) word_table = ( # pattern s i, j expected, expected_i, expected_j ('a', 'abaAca', 0, -1, '', -1, -1), ('A', 'AA A AAB', 0, -1, 'A', 3, 4), ) test(plain_table, 'plain_table', nocase=False, word=False) test(nocase_table, 'nocase_table', nocase=True, word=False) test(word_table, 'word_table', nocase=False, word=True)
def test_replace_back_slashes(self): c = self.c x = leoFind.LeoFind(c) table = ( ('\\\\', '\\'), ('\\n', '\n'), ('\\t', '\t'), (r'a\bc', r'a\bc'), (r'a\\bc', r'a\bc'), (r'a\tc', 'a\tc'), # Replace \t by a tab. (r'a\nc', 'a\nc'), # Replace \n by a newline. ) for s, expected in table: got = x.replace_back_slashes(s) self.assertEqual(expected, got, msg=s)
def test_clean_init(self): c = self.c x = leoFind.LeoFind(c) table = ( 'ignore_case', 'node_only', 'pattern_match', 'search_headline', 'search_body', 'suboutline_only', 'mark_changes', 'mark_finds', 'whole_word', ) for ivar in table: assert getattr(x, ivar) is None, ivar assert x.reverse is False
def test_inner_search_regex(self): c = self.c x = leoFind.LeoFind(c) def test(table, table_name, back, nocase): for pattern, s, expected in table: flags = re.IGNORECASE if nocase else 0 x.re_obj = re.compile(pattern, flags) pos, new_pos = x._inner_search_regex(s, 0, len(s), pattern, backwards=back, nocase=nocase) got = s[pos:new_pos] assert expected == got, ( '\n table: %s' '\n pattern: %r' '\n s: %r' '\nexpected: %r' '\n got: %r' % (table_name, pattern, s, expected, got)) plain_table = ( # pattern s expected (r'.', 'A', 'A'), (r'A', 'xAy', 'A'), ) nocase_table = ( # pattern s expected (r'.', 'A', 'A'), (r'.', 'a', 'a'), (r'A', 'xay', 'a'), (r'a', 'xAy', 'A'), ) back_table = ( # pattern s expected (r'a.b', 'a1b a2b', 'a2b'), ) test(plain_table, 'plain_table', back=False, nocase=False) test(nocase_table, 'nocase_table', back=False, nocase=True) test(back_table, 'back_table', back=True, nocase=False)