Debian Codegraph @

test/unit/test_notification.py

raw symbols SCIP
1 # pyOCD debugger
2 # Copyright (c) 2019 Arm Limited
3 # SPDX-License-Identifier: Apache-2.0
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 import pytest
18 from enum import Enum
19
20 from pyocd.utility.notification import (Notification, Notifier)
21
22 # Test both int and string events.
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