Beispiel #1
0
    def test_enable(self):
        event = Event('Enable manually')
        event._Event__is_enable = False  # force override __is_enable property
        self.assertFalse(event.is_enable)

        event.enable()
        self.assertTrue(event.is_enable)
Beispiel #2
0
    def test_publish_to_empty_event(self):
        event = Event('I am empty inside :(')

        self.assertEqual(len(event.pub_sub), 0)

        with Loop(event.publish('enter the void')) as loop:
            result = loop.run_until_complete()

        self.assertListEqual(result, [])
Beispiel #3
0
    def test_get_name_from_other_event(self):
        optimus = Event('Optimus Prime')
        clone = Event(optimus)

        # name has been cloned
        self.assertEqual(optimus.name, clone.name)

        # but are not the same
        self.assertNotEqual(optimus, clone)
        self.assertIsNot(optimus, clone)
Beispiel #4
0
    def test_unsubscribe_with_publisher(self):
        event = Event('unsubscribe nuke')

        # noinspection PyShadowingNames,PyUnusedLocal
        @event.subscribe(Publisher('nuke'))
        async def i_feel_no_regret(message, publisher, event):
            return ['received', publisher, event]

        self.assertEqual(len(event.pub_sub), 1)

        event.unsubscribe(i_feel_no_regret, Publisher('nuke'))
        self.assertEqual(len(event.pub_sub), 0)
Beispiel #5
0
    def test_unsubscribe(self):
        event = Event('unsubscribe me')

        # noinspection PyShadowingNames,PyUnusedLocal
        @event.subscribe()
        async def i_will_do_it(message, publisher, event):
            return ['received', publisher, event]

        self.assertEqual(len(event.pub_sub), 1)

        event.unsubscribe(i_will_do_it)
        self.assertEqual(len(event.pub_sub), 0)
Beispiel #6
0
    def test_publish_on_disabled_event(self):
        event = Event('disabled')
        event.disable()

        # noinspection PyShadowingNames
        @event.subscribe()
        async def funny_handler(message, publisher, event):
            return [message, publisher, event]

        with Loop(event.publish('sad message')) as loop:
            result = loop.run_until_complete()

        self.assertIsNone(result)
Beispiel #7
0
    def test_decorator_attached_to_event(self):
        event = Event('builtin decorator')
        self.assertEqual(len(event.pub_sub), 0)

        @event.subscribe()
        async def better_handler():
            return 'better_handler'

        self.assertEqual(len(event.pub_sub), 1)
        self.assertIsNone(event.pub_sub[0].publisher)
        self.assertIs(event.pub_sub[0].subscriber, better_handler)
Beispiel #8
0
    def test_standalone_decorator(self):
        event = Event('sub standalone')
        self.assertEqual(len(event.pub_sub), 0)

        @subscribe(event)
        async def simple_handler():
            return 'simple_handler'

        self.assertEqual(len(event.pub_sub), 1)
        self.assertIsNone(event.pub_sub[0].publisher)
        self.assertIs(event.pub_sub[0].subscriber, simple_handler)
Beispiel #9
0
    def test_publish_to_all(self):
        event = Event('publish to all')

        # noinspection PyShadowingNames,PyUnusedLocal
        @event.subscribe()
        async def first_all(message, publisher, event):
            return [message, publisher, event]

        # noinspection PyShadowingNames,PyUnusedLocal
        @event.subscribe()
        async def second_all(message, publisher, event):
            return [message, publisher, event]

        with Loop(event.publish('test message')) as loop:
            result = loop.run_until_complete()

        self.assertEqual(len(result), 2)
        for r in result:
            self.assertEqual(r[0], 'test message')
            self.assertIsNone(r[1])
            self.assertEqual(r[2], 'publish to all')
Beispiel #10
0
    def test_standalone_decorator_with_set_publisher(self):
        event = Event('sub to specific publisher')
        self.assertEqual(len(event.pub_sub), 0)

        @event.subscribe(publisher='specific')
        async def nice_handler():
            return 'nice_handler'

        self.assertEqual(len(event.pub_sub), 1)
        self.assertEqual(event.pub_sub[0].publisher, 'specific')
        self.assertEqual(event.pub_sub[0].publisher, Publisher('specific'))
        self.assertIs(event.pub_sub[0].subscriber, nice_handler)
Beispiel #11
0
    def test_publish_to_all_but_specific(self):
        event = Event('publish to all but omit')

        # noinspection PyShadowingNames,PyUnusedLocal
        @event.subscribe(publisher=Publisher('omit'))
        async def first_sp(message, publisher, event):
            return ['omitted', publisher, event]

        # noinspection PyShadowingNames,PyUnusedLocal
        @event.subscribe()
        async def second_sp(message, publisher, event):
            return ['received', publisher, event]

        with Loop(event.publish('secret message',
                                Publisher('broadcast'))) as loop:
            result = loop.run_until_complete()

        self.assertEqual(len(result), 1)
        result = result.pop()
        self.assertEqual(result[0], 'received')
        self.assertEqual(result[1], Publisher('broadcast'))
        self.assertEqual(result[2], 'publish to all but omit')
Beispiel #12
0
    def test_toggle(self):
        event = Event('Toggle event')
        self.assertTrue(event.is_enable)

        event.toggle()
        self.assertFalse(event.is_enable)

        event.toggle()
        self.assertTrue(event.is_enable)
Beispiel #13
0
 def test_raise_on_bad_naming(self):
     with self.assertRaises(exceptions.NamingError):
         Event({'name': 'test'})
Beispiel #14
0
 def test_new_event_without_name(self):
     event = Event()
     self.assertEqual(event.name, 'Event')
Beispiel #15
0
    def test_disable(self):
        event = Event('Disable manually')
        self.assertTrue(event.is_enable)

        event.disable()
        self.assertFalse(event.is_enable)
Beispiel #16
0
 def test_enable_by_default(self):
     event = Event('Enable by default')
     self.assertTrue(event.is_enable)