1# Copyright 2021-2023 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# ----------------------------------------------------------------------------- 16# Imports 17# ----------------------------------------------------------------------------- 18import contextlib 19import logging 20import os 21from unittest.mock import MagicMock 22 23from pyee import EventEmitter 24 25from bumble import utils 26 27 28# ----------------------------------------------------------------------------- 29def test_on() -> None: 30 emitter = EventEmitter() 31 with contextlib.closing(utils.EventWatcher()) as context: 32 mock = MagicMock() 33 context.on(emitter, 'event', mock) 34 35 emitter.emit('event') 36 37 assert not emitter.listeners('event') 38 assert mock.call_count == 1 39 40 41# ----------------------------------------------------------------------------- 42def test_on_decorator() -> None: 43 emitter = EventEmitter() 44 with contextlib.closing(utils.EventWatcher()) as context: 45 mock = MagicMock() 46 47 @context.on(emitter, 'event') 48 def on_event(*_) -> None: 49 mock() 50 51 emitter.emit('event') 52 53 assert not emitter.listeners('event') 54 assert mock.call_count == 1 55 56 57# ----------------------------------------------------------------------------- 58def test_multiple_handlers() -> None: 59 emitter = EventEmitter() 60 with contextlib.closing(utils.EventWatcher()) as context: 61 mock = MagicMock() 62 63 context.once(emitter, 'a', mock) 64 context.once(emitter, 'b', mock) 65 66 emitter.emit('b', 'b') 67 68 assert not emitter.listeners('a') 69 assert not emitter.listeners('b') 70 71 mock.assert_called_once_with('b') 72 73 74# ----------------------------------------------------------------------------- 75def test_open_int_enums(): 76 class Foo(utils.OpenIntEnum): 77 FOO = 1 78 BAR = 2 79 BLA = 3 80 81 x = Foo(1) 82 assert x.name == "FOO" 83 assert x.value == 1 84 assert int(x) == 1 85 assert x == 1 86 assert x + 1 == 2 87 88 x = Foo(4) 89 assert x.name == "Foo[4]" 90 assert x.value == 4 91 assert int(x) == 4 92 assert x == 4 93 assert x + 1 == 5 94 95 print(list(Foo)) 96 97 98# ----------------------------------------------------------------------------- 99def run_tests(): 100 test_on() 101 test_on_decorator() 102 test_multiple_handlers() 103 104 105# ----------------------------------------------------------------------------- 106if __name__ == '__main__': 107 logging.basicConfig(level=os.environ.get('BUMBLE_LOGLEVEL', 'INFO').upper()) 108 run_tests() 109 test_open_int_enums() 110