Skip to content

Commit

Permalink
Allow more than one search word (#157)
Browse files Browse the repository at this point in the history
  • Loading branch information
hugovk authored Jan 18, 2025
2 parents 5838c2a + c38ea9d commit 80039f8
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 13 deletions.
22 changes: 9 additions & 13 deletions src/em_keyboard/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
21 changes: 21 additions & 0 deletions tests/test_em.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 80039f8

Please sign in to comment.