Skip to content

Commit d6d61ca

Browse files
preetmishraneiljp
authored andcommitted
views: Improve stream and topic search to do a substring search.
Improved stream/topic search to find if the query is a substring of any stream/topic instead of just trying to find if the query is at the start of any stream/topic. Tests amended. Fixes #509.
1 parent 80b1161 commit d6d61ca

File tree

2 files changed

+32
-12
lines changed

2 files changed

+32
-12
lines changed

tests/ui/test_ui_tools.py

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -410,17 +410,26 @@ def test_init(self, mocker, stream_view):
410410
stream_view, 'SEARCH_STREAMS', stream_view.update_streams)
411411

412412
@pytest.mark.parametrize('new_text, expected_log', [
413-
('f', ['FOO', 'foo', 'fan']),
414-
('foo', ['FOO', 'foo']),
415-
('FOO', ['FOO', 'foo']),
413+
('f', ['FOO', 'FOOBAR', 'foo', 'fan']),
414+
('a', ['FOOBAR', 'fan', 'bar']),
415+
('bar', ['FOOBAR', 'bar']),
416+
('foo', ['FOO', 'FOOBAR', 'foo']),
417+
('FOO', ['FOO', 'FOOBAR', 'foo']),
418+
('test', ['test here']),
419+
('here', ['test here']),
416420
])
417421
def test_update_streams(self, mocker, stream_view, new_text, expected_log):
422+
stream_names = [
423+
'FOO', 'FOOBAR', 'foo', 'fan',
424+
'boo', 'BOO', 'bar', 'test here',
425+
]
418426
self.view.controller.editor_mode = True
419427
new_text = new_text
420428
search_box = "SEARCH_BOX"
421429
stream_view.streams_btn_list = [
422-
mocker.Mock(stream_name=stream_name) for stream_name in [
423-
'FOO', 'foo', 'fan', 'boo', 'BOO']]
430+
mocker.Mock(stream_name=stream_name)
431+
for stream_name in stream_names
432+
]
424433
stream_view.update_streams(search_box, new_text)
425434
assert [stream.stream_name for stream in stream_view.log
426435
] == expected_log
@@ -521,17 +530,26 @@ def test_init(self, mocker, topic_view):
521530
topic_view.topic_search_box])
522531

523532
@pytest.mark.parametrize('new_text, expected_log', [
524-
('f', ['FOO', 'foo', 'fan']),
525-
('foo', ['FOO', 'foo']),
526-
('FOO', ['FOO', 'foo']),
533+
('f', ['FOO', 'FOOBAR', 'foo', 'fan']),
534+
('a', ['FOOBAR', 'fan', 'bar']),
535+
('bar', ['FOOBAR', 'bar']),
536+
('foo', ['FOO', 'FOOBAR', 'foo']),
537+
('FOO', ['FOO', 'FOOBAR', 'foo']),
538+
('(no', ['(no topic)']),
539+
('topic', ['(no topic)']),
527540
])
528541
def test_update_topics(self, mocker, topic_view, new_text, expected_log):
542+
topic_names = [
543+
'FOO', 'FOOBAR', 'foo', 'fan',
544+
'boo', 'BOO', 'bar', '(no topic)',
545+
]
529546
self.view.controller.editor_mode = True
530547
new_text = new_text
531548
search_box = "SEARCH_BOX"
532549
topic_view.topics_btn_list = [
533-
mocker.Mock(topic_name=topic_name) for topic_name in [
534-
'FOO', 'foo', 'fan', 'boo', 'BOO']]
550+
mocker.Mock(topic_name=topic_name)
551+
for topic_name in topic_names
552+
]
535553
topic_view.update_topics(search_box, new_text)
536554
assert [topic.topic_name for topic in topic_view.log
537555
] == expected_log

zulipterminal/ui_tools/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,10 +270,11 @@ def update_streams(self, search_box: Any, new_text: str) -> None:
270270
# wait for any previously started search to finish to avoid
271271
# displaying wrong stream list.
272272
with self.search_lock:
273+
new_text = new_text.lower()
273274
streams_display = [
274275
stream
275276
for stream in self.streams_btn_list.copy()
276-
if stream.stream_name.lower().startswith(new_text.lower())
277+
if new_text in stream.stream_name.lower()
277278
]
278279
self.log.clear()
279280
self.log.extend(streams_display)
@@ -334,10 +335,11 @@ def update_topics(self, search_box: Any, new_text: str) -> None:
334335
# wait for any previously started search to finish to avoid
335336
# displaying wrong topics list.
336337
with self.search_lock:
338+
new_text = new_text.lower()
337339
topics_to_display = [
338340
topic
339341
for topic in self.topics_btn_list.copy()
340-
if topic.topic_name.lower().startswith(new_text.lower())
342+
if new_text in topic.topic_name.lower()
341343
]
342344
self.log.clear()
343345
self.log.extend(topics_to_display)

0 commit comments

Comments
 (0)