Files
FWS-Kitty/kitty_tests/scrollback_search.py
T
BlackAngelTV af032e3d87
CI / check (push) Successful in 3m26s
style(scrollback-search): sort imports to satisfy ruff
The lint job failed on the unsorted import blocks, which blocked the release
checks for the 0.1.7 bump.
2026-11-25 20:41:42 +01:00

84 lines
3.1 KiB
Python

#!/usr/bin/env python
# License: GPLv3 Copyright: 2026, Kovid Goyal <kovid at kovidgoyal.net>
import re
import time
from kitty.scrollback_search import (
InvalidQuery,
Match,
compile_query,
find_matches,
parse_query,
scroll_offset_for,
)
from .base import BaseTest
class TestScrollbackSearch(BaseTest):
def test_parse_query(self):
q = parse_query('needle')
self.ae((q.text, q.case_sensitive, q.is_regex), ('needle', False, False))
self.assertTrue(compile_query(q).flags & re.IGNORECASE)
q = parse_query(' padded ')
self.ae(q.text, 'padded')
def test_parse_query_modifiers(self):
q = parse_query('\\c Needle')
self.ae((q.text, q.case_sensitive, q.is_regex), ('Needle', True, False))
self.ae(compile_query(q).flags & re.IGNORECASE, 0)
q = parse_query('\\r a.c')
self.ae((q.text, q.case_sensitive, q.is_regex), ('a.c', False, True))
for raw in ('\\r\\c a+b', '\\c\\r a+b', '\\C\\R a+b'):
q = parse_query(raw)
self.ae((q.text, q.case_sensitive, q.is_regex), ('a+b', True, True), raw)
with self.assertRaises(InvalidQuery):
parse_query('')
with self.assertRaises(InvalidQuery):
parse_query(' ')
with self.assertRaises(InvalidQuery):
parse_query('\\c')
with self.assertRaises(InvalidQuery):
compile_query(parse_query('\\r ('))
def test_find_matches_plain(self):
lines = ['needle at start', 'Needle again', 'nothing', 'a needle here']
matches = find_matches(lines, parse_query('needle'))
self.ae([(m.line, m.start, m.end) for m in matches], [(0, 0, 6), (1, 0, 6), (3, 2, 8)])
matches = find_matches(lines, parse_query('\\c Needle'))
self.ae([(m.line, m.start, m.end) for m in matches], [(1, 0, 6)])
# Regex metacharacters are literal in a plain query
self.ae(find_matches(['a+b', 'aab'], parse_query('a+b')), [Match(0, 0, 3)])
def test_find_matches_regex(self):
matches = find_matches(['abc abc', 'nothing here'], parse_query('\\r a.c'))
self.ae([(m.line, m.start, m.end) for m in matches], [(0, 0, 3), (0, 4, 7)])
# Empty matches are skipped and do not loop forever
self.ae(find_matches(['abc'], parse_query('\\r x*')), [])
def test_scroll_offset(self):
self.ae(scroll_offset_for(Match(0, 0, 1), 100, 10), 90)
self.ae(scroll_offset_for(Match(50, 0, 1), 100, 10), 43)
self.ae(scroll_offset_for(Match(99, 0, 1), 100, 10), 0)
# No scrollback: the visible screen is always shown as is
self.ae(scroll_offset_for(Match(3, 0, 1), 5, 5), 0)
def test_performance_on_large_scrollback(self):
lines = [f'line {i} with some ordinary text' for i in range(10_000)]
for i in range(0, 10_000, 10):
lines[i] = 'needle ' + lines[i]
query = parse_query('needle')
start = time.monotonic()
matches = find_matches(lines, query)
elapsed = time.monotonic() - start
self.ae(len(matches), 1000)
self.assertLess(elapsed, 0.05, f'searching 10 000 lines took {elapsed * 1000:.1f} ms')