diff --git a/papers/events.py b/papers/events.py index 05b27a2..efd94d6 100644 --- a/papers/events.py +++ b/papers/events.py @@ -2,12 +2,8 @@ _listener = {} class Event(object): - def __init__(self, string): - """This is an example of simple event that can be raised - Inherit from this class and redefine whatever you need, - except the send funtion - """ - self.string = string + """Base event that can be sent to listeners. + """ def send(self): """ This function sends the instance of the class, i.e. the event @@ -36,46 +32,3 @@ class RemoveEvent(Event): self.config = config self.ui = ui self.citekey = citekey - - -if __name__ == "__main__": - - class TestEvent(Event): - def print_one(self): - print 'one' - - @TestEvent.listen(12, 15) - def Display(TestEventInstance, nb1, nb2): - print TestEventInstance.string, nb1, nb2 - - @TestEvent.listen() - def Helloword(TestEventInstance): - print 'Helloword' - - @TestEvent.listen() - def PrintIt(TestEventInstance): - TestEventInstance.print_one() - - class AddEvent(Event): - def __init__(self): - pass - - def add(self, a, b): - return a + b - - @AddEvent.listen() - def DoIt(AddEventInstance): - print AddEventInstance.add(17, 25) - - # using the callback system - myevent = TestEvent('abcdefghijklmnopqrstuvwxyz') - myevent.send() # this one call three function - - addevent = AddEvent() - addevent.send() - - # but also work without the event raising system! - Display(myevent, 12, 15) - Helloword(myevent) - PrintIt(myevent) - DoIt(addevent) diff --git a/tests/test_events.py b/tests/test_events.py new file mode 100644 index 0000000..0b509a3 --- /dev/null +++ b/tests/test_events.py @@ -0,0 +1,65 @@ +from unittest import TestCase + +from papers.events import Event + + +_output = None + + +class TestEvent(Event): + def __init__(self, string): + self.string = string + + def print_one(self): + _output.append('one') + + +class AddEvent(Event): + def __init__(self): + pass + + def add(self, a, b): + return a + b + + +@TestEvent.listen(12, 15) +def display(TestEventInstance, nb1, nb2): + _output.append("%s %s %s" + % (TestEventInstance.string, nb1, nb2)) + + +@TestEvent.listen() +def hello_word(TestEventInstance): + _output.append('Helloword') + + +@TestEvent.listen() +def print_it(TestEventInstance): + TestEventInstance.print_one() + + +@AddEvent.listen() +def do_it(AddEventInstance): + _output.append(AddEventInstance.add(17, 25)) + + +class TestEvents(TestCase): + + def setUp(self): + global _output + _output = [] + + def test_listen_TestEvent(self): + # using the callback system + myevent = TestEvent('abcdefghijklmnopqrstuvwxyz') + myevent.send() # this one call three function + correct = ['abcdefghijklmnopqrstuvwxyz 12 15', + 'Helloword', + 'one'] + self.assertEquals(_output, correct) + + def test_listen_AddEvent(self): + addevent = AddEvent() + addevent.send() + correct = [42] + self.assertEquals(_output, correct)