File: test_notification.py

package info (click to toggle)
pyocd 0.37.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 355,132 kB
  • sloc: xml: 3,682,260; python: 61,563; ansic: 112; makefile: 87; asm: 25; sh: 14
file content (114 lines) | stat: -rw-r--r-- 3,996 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# pyOCD debugger
# Copyright (c) 2019 Arm Limited
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import pytest
import six
from enum import Enum

from pyocd.utility.notification import (Notification, Notifier)

# Test both int and string events.
EVENT_A = 1
EVENT_B = "foo"

class Subscriber(object):
    def __init__(self):
        self.was_called = False
        self.last_note = None

    def cb(self, note):
        self.was_called = True
        self.last_note = note

@pytest.fixture
def notifier():
    return Notifier()

@pytest.fixture
def subscriber():
    return Subscriber()

class TestNotification(object):
    def test_basic_sub_and_send_a(self, notifier, subscriber):
        notifier.subscribe(subscriber.cb, EVENT_A)
        notifier.notify(EVENT_A, self)
        assert subscriber.was_called
        assert subscriber.last_note.event == EVENT_A
        assert subscriber.last_note.source == self
        assert subscriber.last_note.data == None

    def test_basic_sub_and_send_b(self, notifier, subscriber):
        notifier.subscribe(subscriber.cb, EVENT_B)
        notifier.notify(EVENT_B, self)
        assert subscriber.was_called
        assert subscriber.last_note.event == EVENT_B
        assert subscriber.last_note.source == self
        assert subscriber.last_note.data == None

    def test_unsub(self, notifier, subscriber):
        notifier.subscribe(subscriber.cb, EVENT_A)
        notifier.unsubscribe(subscriber.cb)
        notifier.notify(EVENT_A, self)
        assert not subscriber.was_called

    def test_unsub2(self, notifier, subscriber):
        notifier.subscribe(subscriber.cb, EVENT_A)
        notifier.unsubscribe(subscriber.cb, events=[EVENT_B])
        notifier.notify(EVENT_A, self)
        assert subscriber.was_called

    def test_multiple_sub(self, notifier, subscriber):
        notifier.subscribe(subscriber.cb, (EVENT_A, EVENT_B))
        notifier.notify(EVENT_A, self)
        assert subscriber.was_called
        assert subscriber.last_note.event == EVENT_A
        assert subscriber.last_note.source == self
        assert subscriber.last_note.data == None
        notifier.notify(EVENT_B, self)
        assert subscriber.was_called
        assert subscriber.last_note.event == EVENT_B
        assert subscriber.last_note.source == self
        assert subscriber.last_note.data == None

    def test_diff_sub(self, notifier, subscriber):
        s2 = Subscriber()
        notifier.subscribe(subscriber.cb, EVENT_A)
        notifier.subscribe(s2.cb, EVENT_B)
        notifier.notify(EVENT_B, self)
        assert not subscriber.was_called
        assert s2.was_called
        assert s2.last_note.event == EVENT_B

    def test_src_sub(self, notifier, subscriber):
        notifier.subscribe(subscriber.cb, EVENT_A, source=self)
        notifier.notify(EVENT_A, self)
        assert subscriber.was_called
        assert subscriber.last_note.event == EVENT_A
        assert subscriber.last_note.source == self
        assert subscriber.last_note.data == None

    def test_src_sub2(self, notifier, subscriber):
        notifier.subscribe(subscriber.cb, EVENT_A, source=self)
        notifier.notify(EVENT_A, notifier)
        assert not subscriber.was_called

    def test_unsub_src(self, notifier, subscriber):
        notifier.subscribe(subscriber.cb, EVENT_A, source=self)
        notifier.unsubscribe(subscriber.cb)
        notifier.notify(EVENT_A, self)
        assert not subscriber.was_called