From c38ea9d4d1bc9023a3895ca83a451ba6f61e7fef Mon Sep 17 00:00:00 2001 From: Taneli Hukkinen <3275109+hukkin@users.noreply.github.com> Date: Sat, 18 Jan 2025 18:06:24 +0200 Subject: [PATCH] Allow more than one search word --- src/em_keyboard/__init__.py | 22 +++++++++------------- tests/test_em.py | 21 +++++++++++++++++++++ 2 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/em_keyboard/__init__.py b/src/em_keyboard/__init__.py index 9cf1a6a..5aeb462 100644 --- a/src/em_keyboard/__init__.py +++ b/src/em_keyboard/__init__.py @@ -64,18 +64,14 @@ def translate(lookup: EmojiDict, code: str) -> list[str] | list[None]: return output -def do_find(lookup: EmojiDict, term: str) -> list: - """Match term against keywords.""" - output = [] - seen = set() - - for emoji, keywords in lookup.items(): - for keyword in keywords: - if term in keyword and emoji not in seen: - output.append((keywords[0], emoji)) - seen.add(emoji) - - return output +def do_find(lookup: EmojiDict, terms: tuple[str, ...]) -> list[tuple[str, str]]: + """Match terms against keywords.""" + assert terms, "at least one search term required" + return [ + (keywords[0], emoji) + for emoji, keywords in lookup.items() + if all(any(term in kw for kw in keywords) for term in terms) + ] def clean_name(name: str) -> str: @@ -128,7 +124,7 @@ def cli() -> None: # Search mode. if args.search: # Lookup the search term. - found = do_find(lookup, names[0]) + found = do_find(lookup, names) # print them to the screen. for name, emoji in found: diff --git a/tests/test_em.py b/tests/test_em.py index 6127114..7fb3ef8 100644 --- a/tests/test_em.py +++ b/tests/test_em.py @@ -139,6 +139,27 @@ def test_search_not_found(mock_print: MagicMock, mock_argparse: MagicMock) -> No assert e.value.code == 1 +@patch("em_keyboard.argparse.ArgumentParser.parse_args") +@patch("builtins.print") +def test_search_multi_word(mock_print: MagicMock, mock_argparse: MagicMock) -> None: + # Arrange + mock_argparse.return_value = argparse.Namespace( + name=["big", "tent"], no_copy=None, search=True, random=False + ) + + # Act + with pytest.raises(SystemExit) as e: + cli() + + # Assert + if copier: + mock_print.assert_called_once_with("Copied! 🎪 circus_tent") + else: + mock_print.assert_called_once_with("🎪 circus_tent") + assert e.type is SystemExit + assert e.value.code == 0 + + @patch("em_keyboard.argparse.ArgumentParser.parse_args") @patch("builtins.print") def test_random(mock_print: MagicMock, mock_argparse: MagicMock) -> None: