Skip to content

Commit

Permalink
Merge pull request #484 from UQComputingSociety/yelling
Browse files Browse the repository at this point in the history
Create test_yelling.py
  • Loading branch information
nicklambourne committed Oct 2, 2019
2 parents 295e8c7 + ed96722 commit f5f1df5
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
42 changes: 42 additions & 0 deletions test/test_yelling.py
@@ -0,0 +1,42 @@
from test.conftest import MockUQCSBot, TEST_CHANNEL_ID
from unittest.mock import patch


@patch("uqcsbot.scripts.yelling.in_yelling", new=lambda chan: True)
@patch("uqcsbot.scripts.yelling.is_human", new=lambda chan: True)
def test_minuscule(uqcsbot: MockUQCSBot):
"""
test minuscule string
"""
uqcsbot.post_message(TEST_CHANNEL_ID, 'wintermute')
assert len(uqcsbot.test_messages.get(TEST_CHANNEL_ID, [])) == 2


@patch("uqcsbot.scripts.yelling.in_yelling", new=lambda chan: True)
@patch("uqcsbot.scripts.yelling.is_human", new=lambda chan: True)
def test_majuscule(uqcsbot: MockUQCSBot):
"""
test majuscule string
"""
uqcsbot.post_message(TEST_CHANNEL_ID, 'WINTERMUTE')
assert len(uqcsbot.test_messages.get(TEST_CHANNEL_ID, [])) == 1


@patch("uqcsbot.scripts.yelling.in_yelling", new=lambda chan: True)
@patch("uqcsbot.scripts.yelling.is_human", new=lambda chan: True)
def test_mixed(uqcsbot: MockUQCSBot):
"""
test mixed case string
"""
uqcsbot.post_message(TEST_CHANNEL_ID, 'wiNTErMUTe')
assert len(uqcsbot.test_messages.get(TEST_CHANNEL_ID, [])) == 2


@patch("uqcsbot.scripts.yelling.in_yelling", new=lambda chan: False)
@patch("uqcsbot.scripts.yelling.is_human", new=lambda chan: True)
def test_channel(uqcsbot: MockUQCSBot):
"""
tests outside of #yeling
"""
uqcsbot.post_message(TEST_CHANNEL_ID, 'wintermute')
assert len(uqcsbot.test_messages.get(TEST_CHANNEL_ID, [])) == 1
20 changes: 18 additions & 2 deletions uqcsbot/scripts/yelling.py
Expand Up @@ -4,6 +4,22 @@
from re import sub, UNICODE


def in_yelling(channel):
"""
checks that channel is #yelling
exists for test mocking
"""
return bot.channels.get(channel).name == "yelling"


def is_human(user):
"""
checks that the user is not a bot
exists for test mocking
"""
return user is not None and not user.is_bot


def mutate_minuscule(message: str) -> str:
"""
Randomly mutates 40% of minuscule letters to other minuscule letters
Expand Down Expand Up @@ -36,7 +52,7 @@ def yelling(event: dict):

# ensure in #yelling channel
channel = event.get("channel")
if bot.channels.get(channel).name != "yelling":
if not in_yelling(channel):
return

# ensure message proper
Expand All @@ -45,7 +61,7 @@ def yelling(event: dict):

# ensure user proper
user = bot.users.get(event.get("user"))
if user is None or user.is_bot:
if not is_human(user):
return

text = sub(r":[\w\-\+']+:", lambda m: m.group(0).upper(), event['text'], flags=UNICODE)
Expand Down

0 comments on commit f5f1df5

Please sign in to comment.