-
-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Pull event name transformation into engine. #402
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class BadEventHandlerException(TypeError): | ||
|
||
def __init__(self, instance, method, event): | ||
object_type = type(instance) | ||
event_type = type(event) | ||
o_name = object_type.__name__ | ||
e_name = event_type.__name__ | ||
article = ['a', 'an'][int(e_name.lower()[0] in "aeiou")] | ||
|
||
message = f""" | ||
{o_name}.{method}() signature incorrect, it should accept {article} {e_name} object and a signal function. | ||
|
||
{e_name} is a dataclass that represents an event. Its attributes | ||
tell you about the event. | ||
|
||
The signal function is a function you can call that accepts an event instance | ||
as its only parameter. Call it to add an event to the queue. You don't have to | ||
use it, but it is a mandatory argument provided by ppb. | ||
|
||
It should look like this: | ||
|
||
def {method}({e_name.lower()}_event: {e_name}, signal_function): | ||
(Your code goes here.) | ||
""" | ||
super().__init__(message) |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,4 @@ | ||
from ppb import eventlib | ||
|
||
|
||
class System(eventlib.EventMixin): | ||
class System: | ||
|
||
def __init__(self, **_): | ||
pass | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,8 @@ | ||
from pytest import mark | ||
from pytest import raises | ||
|
||
from ppb.eventlib import BadEventHandlerException | ||
from ppb.eventlib import camel_to_snake | ||
from ppb.eventlib import EventMixin | ||
|
||
|
||
def test_eventmixin(): | ||
passed_bag = None | ||
passed_fire = None | ||
|
||
class Spam: | ||
pass | ||
|
||
class Eventable(EventMixin): | ||
def on_spam(self, bag, fire_event): | ||
nonlocal passed_bag, passed_fire | ||
passed_fire = fire_event | ||
passed_bag = bag | ||
|
||
bag = Spam() | ||
fire_event = lambda: None | ||
|
||
e = Eventable() | ||
|
||
e.__event__(bag, fire_event) | ||
assert bag is passed_bag | ||
assert fire_event is passed_fire | ||
|
||
|
||
def test_event_mixin_with_bad_signature(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a fan of losing this test, but I also can't think of a good way to handle it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yup, I think I need to puzzle on how to get it. It's a lot heavier of a test now. |
||
|
||
class BadSpam: | ||
pass | ||
|
||
|
||
class Spam: | ||
pass | ||
|
||
|
||
class Eventable(EventMixin): | ||
def on_spam(self, spam_event): | ||
pass | ||
|
||
def on_bad_spam(self, bad_spam_event, signal): | ||
raise TypeError | ||
|
||
e = Eventable() | ||
|
||
with raises(BadEventHandlerException): | ||
e.__event__(Spam(), lambda x: None) | ||
|
||
with raises(TypeError) as exception_info: | ||
e.__event__(BadSpam(), lambda x: None) | ||
|
||
exec = exception_info.value | ||
assert not isinstance(exec, BadEventHandlerException) | ||
|
||
from ppb.errors import BadEventHandlerException | ||
from ppb.utils import camel_to_snake | ||
|
||
@mark.parametrize("text,expected", [ | ||
("CamelCase", "camel_case"), | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Odd phrasing?