| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
import pytest |
| 18 |
from enum import Enum |
| 19 |
|
| 20 |
from pyocd.utility.notification import (Notification, Notifier) |
| 21 |
|
| 22 |
|
| 23 |
EVENT_A = 1 |
| 24 |
EVENT_B = "foo" |
| 25 |
|
| 26 |
class Subscriber(object): |
| 27 |
def __init__(self): |
| 28 |
self.was_called = False |
| 29 |
self.last_note = None |
| 30 |
|
| 31 |
def cb(self, note): |
| 32 |
self.was_called = True |
| 33 |
self.last_note = note |
| 34 |
|
| 35 |
@pytest.fixture |
| 36 |
def notifier(): |
| 37 |
return Notifier() |
| 38 |
|
| 39 |
@pytest.fixture |
| 40 |
def subscriber(): |
| 41 |
return Subscriber() |
| 42 |
|
| 43 |
class TestNotification(object): |
| 44 |
def test_basic_sub_and_send_a(self, notifier, subscriber): |
| 45 |
notifier.subscribe(subscriber.cb, EVENT_A) |
| 46 |
notifier.notify(EVENT_A, self) |
| 47 |
assert subscriber.was_called |
| 48 |
assert subscriber.last_note.event == EVENT_A |
| 49 |
assert subscriber.last_note.source == self |
| 50 |
assert subscriber.last_note.data == None |
| 51 |
|
| 52 |
def test_basic_sub_and_send_b(self, notifier, subscriber): |
| 53 |
notifier.subscribe(subscriber.cb, EVENT_B) |
| 54 |
notifier.notify(EVENT_B, self) |
| 55 |
assert subscriber.was_called |
| 56 |
assert subscriber.last_note.event == EVENT_B |
| 57 |
assert subscriber.last_note.source == self |
| 58 |
assert subscriber.last_note.data == None |
| 59 |
|
| 60 |
def test_unsub(self, notifier, subscriber): |
| 61 |
notifier.subscribe(subscriber.cb, EVENT_A) |
| 62 |
notifier.unsubscribe(subscriber.cb) |
| 63 |
notifier.notify(EVENT_A, self) |
| 64 |
assert not subscriber.was_called |
| 65 |
|
| 66 |
def test_unsub2(self, notifier, subscriber): |
| 67 |
notifier.subscribe(subscriber.cb, EVENT_A) |
| 68 |
notifier.unsubscribe(subscriber.cb, events=[EVENT_B]) |
| 69 |
notifier.notify(EVENT_A, self) |
| 70 |
assert subscriber.was_called |
| 71 |
|
| 72 |
def test_multiple_sub(self, notifier, subscriber): |
| 73 |
notifier.subscribe(subscriber.cb, (EVENT_A, EVENT_B)) |
| 74 |
notifier.notify(EVENT_A, self) |
| 75 |
assert subscriber.was_called |
| 76 |
assert subscriber.last_note.event == EVENT_A |
| 77 |
assert subscriber.last_note.source == self |
| 78 |
assert subscriber.last_note.data == None |
| 79 |
notifier.notify(EVENT_B, self) |
| 80 |
assert subscriber.was_called |
| 81 |
assert subscriber.last_note.event == EVENT_B |
| 82 |
assert subscriber.last_note.source == self |
| 83 |
assert subscriber.last_note.data == None |
| 84 |
|
| 85 |
def test_diff_sub(self, notifier, subscriber): |
| 86 |
s2 = Subscriber() |
| 87 |
notifier.subscribe(subscriber.cb, EVENT_A) |
| 88 |
notifier.subscribe(s2.cb, EVENT_B) |
| 89 |
notifier.notify(EVENT_B, self) |
| 90 |
assert not subscriber.was_called |
| 91 |
assert s2.was_called |
| 92 |
assert s2.last_note.event == EVENT_B |
| 93 |
|
| 94 |
def test_src_sub(self, notifier, subscriber): |
| 95 |
notifier.subscribe(subscriber.cb, EVENT_A, source=self) |
| 96 |
notifier.notify(EVENT_A, self) |
| 97 |
assert subscriber.was_called |
| 98 |
assert subscriber.last_note.event == EVENT_A |
| 99 |
assert subscriber.last_note.source == self |
| 100 |
assert subscriber.last_note.data == None |
| 101 |
|
| 102 |
def test_src_sub2(self, notifier, subscriber): |
| 103 |
notifier.subscribe(subscriber.cb, EVENT_A, source=self) |
| 104 |
notifier.notify(EVENT_A, notifier) |
| 105 |
assert not subscriber.was_called |
| 106 |
|
| 107 |
def test_unsub_src(self, notifier, subscriber): |
| 108 |
notifier.subscribe(subscriber.cb, EVENT_A, source=self) |
| 109 |
notifier.unsubscribe(subscriber.cb) |
| 110 |
notifier.notify(EVENT_A, self) |
| 111 |
assert not subscriber.was_called |
| 112 |
|
| 113 |
|