-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import re | ||
|
||
def name_thread(message: str) -> str | None: | ||
"""Returns a name for a thread created from a given message. Example messages include: [eslint, nextjs], [eslint,nextjs], [ Java ], [:css:], [:css: CSS], [eslint,nextjs, :css: CSS]. Expected output includes: eslint, nextjs, eslint,nextjs, Java, css, CSS, eslint,nextjs, CSS.""" | ||
|
||
""" | ||
1. Check if the message includes text in brackets, if yes: | ||
a. Remove brackets | ||
b. Remove spaces | ||
c. Check if the message includes text between colons | ||
- if yes, and there is other text in the message, then remove the colons and text between them | ||
- if yes, and there is no other text in the message, then remove the colons | ||
d. Insert spaces after commas if there are any | ||
e. Capitalize the first letter of the message | ||
d. Return the message | ||
2. If the message does not include text in brackets, then return string "xyz" | ||
""" | ||
|
||
|
||
if message.startswith("[") : | ||
content = message[1:-1] | ||
content = content.replace(" ", "") | ||
|
||
emoji_regex = re.compile(r":\w+:") | ||
|
||
if re.search(emoji_regex, content) is not None: | ||
# if there is other text in the message, then remove the colons and text between them | ||
emoji = re.search(emoji_regex, content).group() | ||
if content != emoji: | ||
content = content.replace(emoji, "") | ||
# if there is no other text in the message, then remove the colons | ||
else: | ||
content = content.replace(":", "") | ||
content = content.replace(",", ", ") | ||
return content | ||
else: | ||
return "xyz" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import pytest | ||
from datetime import datetime | ||
|
||
from juniorguru_chick.lib.threads import name_thread | ||
|
||
DAYS = ["Pondělní", "Úterní", "Středeční", | ||
"Čtvrteční", "Páteční", "Sobotní", "Nedělní"] | ||
|
||
class Message: | ||
def __init__(self, display_name, message): | ||
self.author = lambda: None | ||
self.author.display_name = display_name | ||
self.content = message | ||
|
||
def test_name_thread_no_brackets(): | ||
content = "Hello this is my thread" | ||
weekday = datetime.now().weekday() | ||
name_template = "{weekday} objev od {author}" | ||
expected_name = f"{DAYS[weekday]} objev od Jana" | ||
message = Message("Jana", content) | ||
|
||
assert name_thread(message, name_template) == expected_name | ||
|
||
# test if brackets are not closed and if there is no text in brackets | ||
@pytest.mark.parametrize('content', [ | ||
pytest.param("[ Hello this is my thread", id="brackets not closed"), | ||
pytest.param("[] Hello this is my thread", id="no text in brackets"), | ||
pytest.param("Hello this is my thread]", id="brackets not opened")]) | ||
def test_brackets_used_incorrectly(content): | ||
weekday = datetime.now().weekday() | ||
name_template = "{weekday} objev od {author}" | ||
expected_name = f"{DAYS[weekday]} objev od Jana" | ||
message = Message("Jana", content) | ||
|
||
assert name_thread(message, name_template) == expected_name | ||
|
||
# test if there is no text in brackets | ||
def test_no_text_in_brackets(): | ||
content = "[] Hello this is my thread" | ||
weekday = datetime.now().weekday() | ||
name_template = "{weekday} objev od {author}" | ||
expected_name = f"{DAYS[weekday]} objev od Jana" | ||
message = Message("Jana", content) | ||
|
||
assert name_thread(message, name_template) == expected_name | ||
|
||
# test if there is no text in brackets | ||
@pytest.mark.parametrize('content, expected_name', [ | ||
pytest.param("[eslint, nextjs]", "eslint, nextjs", id="comma separated strings with spaces"), | ||
pytest.param("[eslint,nextjs]", "eslint,nextjs", id="comma separated strings without spaces"), | ||
pytest.param("[ Java ]", "Java", id="whitespaces around string"), | ||
pytest.param("[:css: CSS]", ":css: CSS", id="starting emoji"), | ||
pytest.param("[eslint,nextjs, :css: CSS, ruby]", "eslint, nextjs, :css: CSS, ruby", id="emoji in the middle"), | ||
pytest.param("[ eslint ] Hello", "eslint", id="brackets at the beginning")]) | ||
def test_parse_message_in_brackets(content, expected_name): | ||
message = Message("Jana", content) | ||
name_template = "{weekday} objev od {author}" | ||
|
||
assert name_thread(message, name_template) == expected_name |