Skip to content

Commit

Permalink
Merge pull request #117 from CactusDev/test/magic-commands
Browse files Browse the repository at this point in the history
Magic command tests
  • Loading branch information
Innectic committed Dec 24, 2016
2 parents c59fae9 + dccc0ca commit a7b8519
Show file tree
Hide file tree
Showing 9 changed files with 608 additions and 477 deletions.
11 changes: 11 additions & 0 deletions cactusbot/commands/magic/cactus.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ async def github(self, project=None):
("url", "https://github.com/CactusDev/CactusBot/issues",
"github.com/CactusDev/CactusBot/issues")
)
elif project.lower() in ("cactusdev", "cactus"):
return MessagePacket(
"Check out the CactusDev GitHub organization at: ",
("url", "https://github.com/CactusDev", "github.com/CactusDev")
)
elif project.lower() in ("api", "cactusapi"):
return MessagePacket(
"Check out the GitHub repository for CactusAPI at: ",
Expand All @@ -60,6 +65,12 @@ async def github(self, project=None):
("url", "https://github.com/CactusDev/Sepal",
"github.com/CactusDev/Sepal")
)
elif project.lower() in ("assets", "art"):
return MessagePacket(
"Check out the CactusDev assets at: ",
("url", "https://github.com/CactusDev/CactusAssets",
"github.com/CactusDev/CactusAssets")
)
return MessagePacket("Unknown project '{0}'.".format(project))

@Command.command()
Expand Down
1 change: 1 addition & 0 deletions cactusbot/commands/magic/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def join(iterable, delimeter):
yield item


@Command.command()
class Temmie(Command):
"awwAwa!!"

Expand Down
8 changes: 5 additions & 3 deletions cactusbot/packets/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,18 @@ def split(self, seperator=' ', maximum=None):

for index, character in enumerate(component.text):
if len(result) == maximum:
new.data = new.text = new.text + component.text[index:]
new_text = new.text + component.text[index:]
new = new._replace(data=new_text, text=new_text)
break

if character == seperator:
components.append(new._replace())
result.append(components.copy())
components.clear()
new.data = new.text = ""
new = new._replace(data="", text="")
else:
new.data = new.text = new.text + character
new_text = new.text + character
new = new._replace(data=new_text, text=new_text)

components.append(new)

Expand Down
50 changes: 50 additions & 0 deletions tests/commands/test_cactus.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import pytest

from cactusbot.commands.magic import Cactus


@pytest.mark.asyncio
async def test_default():
assert (await Cactus()).text == "Ohai! I'm CactusBot! :cactus:"


@pytest.mark.asyncio
async def test_docs():
assert (await Cactus("docs")).text == ("Check out my documentation at "
"cactusbot.rtfd.org.")


@pytest.mark.asyncio
async def test_twitter():
assert (await Cactus("twitter")).text == (
"You can follow the team behind CactusBot at: "
"twitter.com/CactusDevTeam")


@pytest.mark.asyncio
async def test_github():
assert (await Cactus("github")).text == (
"Check out my GitHub repository at: github.com/CactusDev/CactusBot")
assert (await Cactus("github", "cactusbot")).text == (
"Check out my GitHub repository at: github.com/CactusDev/CactusBot")
assert (await Cactus("github", "CactusDev")).text == (
"Check out the CactusDev GitHub organization at: github.com/CactusDev")
assert (await Cactus("github", "issue")).text == (
"Create a GitHub issue at: github.com/CactusDev/CactusBot/issues")
assert (await Cactus("github", "CactusAPI")).text == (
"Check out the GitHub repository for CactusAPI at: "
"github.com/CactusDev/CactusAPI")
assert (await Cactus("github", "SEPAL")).text == (
"Check out the GitHub repository for Sepal at: "
"github.com/CactusDev/Sepal")
assert (await Cactus("github", "assets")).text == (
"Check out the CactusDev assets at: github.com/CactusDev/CactusAssets")
assert (await Cactus("github", "nonexistant")).text == (
"Unknown project 'nonexistant'.")


@pytest.mark.asyncio
async def test_help():
assert (await Cactus("help")) == (
"Try our docs (!cactus docs). "
"If that doesn't help, tweet at us (!cactus twitter)!")
59 changes: 59 additions & 0 deletions tests/commands/test_cube.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import pytest

from cactusbot.commands.magic import Cube, Temmie
from cactusbot.packets import MessagePacket

async def verify_cube(packet, expected):
_, *args = packet[1:].text.split()
response = (await Cube(*args, username=packet.user, packet=packet))
if isinstance(response, str):
assert response == expected
elif isinstance(response, MessagePacket):
assert response.text == expected
else:
raise TypeError


@pytest.mark.asyncio
async def test_cube():

await verify_cube(
MessagePacket("!cube", user="Username"),
"Username³"
)

await verify_cube(
MessagePacket("!cube 2"),
"8. Whoa, that's 2Cubed!"
)

await verify_cube(
MessagePacket("!cube a b c d e f g h"),
' · '.join([n + '³' for n in "a b c d e f g h".split()])
)

await verify_cube(
MessagePacket("!cube a b c d e f g h i"),
"Whoa, that's 2 many cubes!"
)

await verify_cube(
MessagePacket("!cube 3 eggs and 4 slices of toast"),
"27 · eggs³ · and³ · 64 · slices³ · of³ · toast³"
)

await verify_cube(
MessagePacket("!cube lots of taco salad ", ("emoji", "😃")),
"lots³ · of³ · taco³ · salad³ · 😃³"
)


@pytest.mark.asyncio
async def test_temmie():

await Temmie()

assert (await Temmie("hoi")).text == "hOI!!!!!! i'm tEMMIE!!"

assert not (await Temmie("hoi")).action
assert (await Temmie("flakes")).action
218 changes: 111 additions & 107 deletions tests/handlers/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,111 +3,115 @@
from cactusbot.handlers import CommandHandler
from cactusbot.packets import MessagePacket

command_handler = CommandHandler("TestChannel")

class TestCommandHandler:

command_handler = CommandHandler("TestChannel")

def verify(self, message, expected, *args, **kwargs):
"""Verify target substitutions."""
actual = self.command_handler._inject(
MessagePacket(
*message if isinstance(message, list) else (message,)),
*args, **kwargs
).text
assert actual == expected

@pytest.mark.asyncio
async def test_on_message(self):
assert (await self.command_handler.on_message(
MessagePacket("!cactus")
)).text == "Ohai! I'm CactusBot! :cactus:"

def test_inject_argn(self):

self.verify(
"Let's raid %ARG1%!",
"Let's raid GreatStreamer!",
"raid", "GreatStreamer"
)

self.verify(
"Let's raid %ARG1%! #%ARG2%",
"Let's raid GreatStreamer! #ChannelRaid",
"raid", "GreatStreamer", "ChannelRaid"
)

self.verify(
"Let's raid %ARG1%!",
"Not enough arguments!",
"raid"
)

self.verify(
"This is the !%ARG0% command.",
"This is the !test command.",
"test", "arg1", "arg2"
)

self.verify(
"%ARG1|upper% IS AMAZING!",
"SALAD IS AMAZING!",
"amazing", "salad", "taco"
)

self.verify(
"If you reverse %ARG1%, you get %ARG1|reverse%!",
"If you reverse potato, you get otatop!",
"reverse", "potato"
)

self.verify(
["Let's raid %ARG1%! ", ("link", "beam.pro/%ARG1|tag%")],
"Let's raid @Streamer! beam.pro/Streamer",
"raid", "@Streamer"
)

def test_inject_args(self):

self.verify(
"Have some %ARGS%!",
"Have some hamster-powered floofle waffles!",
"gift", *"hamster-powered floofle waffles".split()
)

self.verify(
"Have some %ARGS%.",
"Not enough arguments!",
"give"
)

def test_inject_user(self):

self.verify(
"Ohai, %USER%!",
"Ohai, SomeUser!",
"ohai", username="SomeUser"
)

self.verify(
"Ohai, %USER%!",
"Ohai, %USER%!",
"ohai"
)

def test_inject_count(self):
pass

def test_inject_channel(self):

self.verify(
"Welcome to %CHANNEL%'s stream!",
"Welcome to GreatStreamer's stream!",
"welcome", channel="GreatStreamer"
)

self.verify(
"Welcome to %CHANNEL%'s stream!",
"Welcome to %CHANNEL%'s stream!",
"welcome"
)

def verify(message, expected, *args, **kwargs):
"""Verify target substitutions."""
actual = command_handler._inject(
MessagePacket(
*message if isinstance(message, list) else (message,)),
*args, **kwargs
).text
assert actual == expected


@pytest.mark.asyncio
async def test_on_message():
assert (await command_handler.on_message(
MessagePacket("!cactus")
)).text == "Ohai! I'm CactusBot! :cactus:"


def test_inject_argn():

verify(
"Let's raid %ARG1%!",
"Let's raid GreatStreamer!",
"raid", "GreatStreamer"
)

verify(
"Let's raid %ARG1%! #%ARG2%",
"Let's raid GreatStreamer! #ChannelRaid",
"raid", "GreatStreamer", "ChannelRaid"
)

verify(
"Let's raid %ARG1%!",
"Not enough arguments!",
"raid"
)

verify(
"This is the !%ARG0% command.",
"This is the !test command.",
"test", "arg1", "arg2"
)

verify(
"%ARG1|upper% IS AMAZING!",
"SALAD IS AMAZING!",
"amazing", "salad", "taco"
)

verify(
"If you reverse %ARG1%, you get %ARG1|reverse%!",
"If you reverse potato, you get otatop!",
"reverse", "potato"
)

verify(
["Let's raid %ARG1%! ", ("link", "beam.pro/%ARG1|tag%")],
"Let's raid @Streamer! beam.pro/Streamer",
"raid", "@Streamer"
)


def test_inject_args():

verify(
"Have some %ARGS%!",
"Have some hamster-powered floofle waffles!",
"gift", *"hamster-powered floofle waffles".split()
)

verify(
"Have some %ARGS%.",
"Not enough arguments!",
"give"
)


def test_inject_user():

verify(
"Ohai, %USER%!",
"Ohai, SomeUser!",
"ohai", username="SomeUser"
)

verify(
"Ohai, %USER%!",
"Ohai, %USER%!",
"ohai"
)


def test_inject_count():
pass


def test_inject_channel():

verify(
"Welcome to %CHANNEL%'s stream!",
"Welcome to GreatStreamer's stream!",
"welcome", channel="GreatStreamer"
)

verify(
"Welcome to %CHANNEL%'s stream!",
"Welcome to %CHANNEL%'s stream!",
"welcome"
)

0 comments on commit a7b8519

Please sign in to comment.