src
xbee
helpers
dispatch
tests
test_dispatch.py
Go to the documentation of this file.
1
"""
2
test_dispatch.py
3
4
By Paul Malmsten, 2010
5
pmalmsten@gmail.com
6
7
Tests the Dispatch module.
8
"""
9
import
unittest
10
from
xbee.helpers.dispatch
import
Dispatch
11
from
xbee.helpers.dispatch.tests.fake
import
FakeXBee
12
13
class
CallbackCheck
(object):
14
def
__init__
(self):
15
self.
called
=
False
16
17
def
call
(self, name, data):
18
self.
called
=
True
19
20
class
TestDispatch
(unittest.TestCase):
21
"""
22
Tests xbee.helpers.dispatch for expected behavior
23
"""
24
25
def
setUp
(self):
26
self.
xbee
=
FakeXBee
(
None
)
27
self.
dispatch
=
Dispatch
(xbee=self.
xbee
)
28
self.
callback_check
=
CallbackCheck
()
29
30
def
test_callback_is_called_when_registered
(self):
31
"""
32
After registerring a callback function with a filter function,
33
the callback should be called when data arrives.
34
"""
35
self.
dispatch
.register(
"test1"
, self.
callback_check
.call,
lambda
data:
True
)
36
self.
dispatch
.run(oneshot=
True
)
37
self.assertTrue(self.
callback_check
.called)
38
39
def
test_callback_not_called_when_filter_not_satisfied
(self):
40
"""
41
After registerring a callback function with a filter function,
42
the callback should not be called if a packet which does not
43
satisfy the callback's filter arrives.
44
"""
45
self.
dispatch
.register(
"test1"
, self.
callback_check
.call,
lambda
data:
False
)
46
self.
dispatch
.run(oneshot=
True
)
47
self.assertFalse(self.
callback_check
.called)
48
49
def
test_multiple_callbacks
(self):
50
"""
51
Many callbacks should be called on the same packet if each
52
callback's filter method is satisfied.
53
"""
54
callbacks = []
55
56
for
i
in
range(0,10):
57
check =
CallbackCheck
()
58
callbacks.append(check)
59
self.
dispatch
.register(
"test%d"
% i, check.call,
lambda
data:
True
)
60
61
self.
dispatch
.run(oneshot=
True
)
62
63
for
callback
in
callbacks:
64
if
not
callback.called:
65
self.fail(
"All callback methods should be called"
)
66
67
def
test_callback_name_collisions_raise_valueerror
(self):
68
"""
69
If a call to register() results in attempting to register a
70
callback with the same name as another callback should result
71
in a ValueError exception being raised.
72
"""
73
self.
dispatch
.register(
"test"
,
None
,
None
)
74
self.assertRaises(ValueError, self.
dispatch
.register,
"test"
,
None
,
None
)
75
76
77
class
TestHeadlessDispatch
(unittest.TestCase):
78
"""
79
Tests Dispatch functionality when it is not constructed with a serial
80
port or an XBee
81
"""
82
def
setUp
(self):
83
self.
headless
=
Dispatch
()
84
85
def
test_dispatch_can_be_created
(self):
86
"""
87
A user may construct a Dispatch with neither a serial port nor
88
an XBee. This allows one to configure an XBee to asynchronously
89
call dispatch() whenever a packet arrives.
90
"""
91
pass
92
93
def
test_run_raises_exception
(self):
94
"""
95
A ValueError must be raised by a headless Dispatch instance if
96
a user attempts to call run().
97
"""
98
self.assertRaises(ValueError, self.headless.run)
xbee.helpers.dispatch.tests.test_dispatch.TestDispatch.callback_check
callback_check
Definition:
test_dispatch.py:28
xbee.helpers.dispatch.tests.test_dispatch.CallbackCheck.call
def call(self, name, data)
Definition:
test_dispatch.py:17
xbee.helpers.dispatch.tests.test_dispatch.TestHeadlessDispatch.test_run_raises_exception
def test_run_raises_exception(self)
Definition:
test_dispatch.py:93
xbee.helpers.dispatch.tests.test_dispatch.TestDispatch.test_callback_not_called_when_filter_not_satisfied
def test_callback_not_called_when_filter_not_satisfied(self)
Definition:
test_dispatch.py:39
xbee.helpers.dispatch.tests.test_dispatch.TestHeadlessDispatch.headless
headless
Definition:
test_dispatch.py:83
xbee.helpers.dispatch.tests.test_dispatch.TestDispatch.dispatch
dispatch
Definition:
test_dispatch.py:27
xbee.helpers.dispatch.tests.test_dispatch.TestHeadlessDispatch.setUp
def setUp(self)
Definition:
test_dispatch.py:82
xbee.helpers.dispatch.tests.test_dispatch.TestHeadlessDispatch
Definition:
test_dispatch.py:77
xbee.helpers.dispatch.tests.test_dispatch.TestDispatch.test_callback_name_collisions_raise_valueerror
def test_callback_name_collisions_raise_valueerror(self)
Definition:
test_dispatch.py:67
xbee.helpers.dispatch.tests.test_dispatch.TestHeadlessDispatch.test_dispatch_can_be_created
def test_dispatch_can_be_created(self)
Definition:
test_dispatch.py:85
xbee.helpers.dispatch.dispatch.Dispatch
Definition:
dispatch.py:14
xbee.helpers.dispatch.tests.test_dispatch.TestDispatch.xbee
xbee
Definition:
test_dispatch.py:26
xbee.helpers.dispatch
Definition:
xbee/helpers/dispatch/__init__.py:1
xbee.helpers.dispatch.tests.test_dispatch.CallbackCheck.__init__
def __init__(self)
Definition:
test_dispatch.py:14
xbee.helpers.dispatch.tests.test_dispatch.TestDispatch.setUp
def setUp(self)
Definition:
test_dispatch.py:25
xbee.helpers.dispatch.tests.test_dispatch.TestDispatch
Definition:
test_dispatch.py:20
xbee.helpers.dispatch.tests.test_dispatch.CallbackCheck
Definition:
test_dispatch.py:13
xbee.helpers.dispatch.tests.fake.FakeXBee
Definition:
fake.py:10
xbee.helpers.dispatch.tests.fake
Definition:
fake.py:1
xbee.helpers.dispatch.tests.test_dispatch.CallbackCheck.called
called
Definition:
test_dispatch.py:15
xbee.helpers.dispatch.tests.test_dispatch.TestDispatch.test_callback_is_called_when_registered
def test_callback_is_called_when_registered(self)
Definition:
test_dispatch.py:30
xbee.helpers.dispatch.tests.test_dispatch.TestDispatch.test_multiple_callbacks
def test_multiple_callbacks(self)
Definition:
test_dispatch.py:49
rosserial_xbee
Author(s): Adam Stambler
autogenerated on Wed Mar 2 2022 00:58:25