test_base.py
Go to the documentation of this file.
1 #! /usr/bin/python
2 """
3 test_xbee.py
4 
5 By Paul Malmsten, 2010
6 pmalmsten@gmail.com
7 
8 Tests the XBeeBase superclass module for XBee API conformance.
9 """
10 import unittest
11 from xbee.base import XBeeBase
12 from xbee.tests.Fake import FakeDevice, FakeReadDevice
13 
14 class TestWriteToDevice(unittest.TestCase):
15  """
16  XBeeBase class should properly._write binary data in a valid API
17  frame to a given serial device.
18  """
19 
20  def test_write(self):
21  """
22  _write method should write the expected data to the serial
23  device
24  """
25  device = FakeDevice()
26 
27  xbee = XBeeBase(device)
28  xbee._write('\x00')
29 
30  # Check resuting state of fake device
31  expected_frame = '\x7E\x00\x01\x00\xFF'
32  self.assertEqual(device.data, expected_frame)
33 
34  def test_write_again(self):
35  """
36  _write method should write the expected data to the serial
37  device
38  """
39  device = FakeDevice()
40 
41  xbee = XBeeBase(device)
42  xbee._write('\x00\x01\x02')
43 
44  # Check resuting state of fake device
45  expected_frame = '\x7E\x00\x03\x00\x01\x02\xFC'
46  self.assertEqual(device.data, expected_frame)
47 
48  def test_write_escaped(self):
49  """
50  _write method should write the expected data to the serial
51  device
52  """
53  device = FakeDevice()
54 
55  xbee = XBeeBase(device,escaped=True)
56  xbee._write('\x7E\x01\x7D\x11\x13')
57 
58  # Check resuting state of fake device
59  expected_frame = '\x7E\x00\x05\x7D\x5E\x01\x7D\x5D\x7D\x31\x7D\x33\xDF'
60  self.assertEqual(device.data, expected_frame)
61 
62 class TestReadFromDevice(unittest.TestCase):
63  """
64  XBeeBase class should properly read and extract data from a valid
65  API frame
66  """
67  def test_read(self):
68  """
69  _wait_for_frame should properly read a frame of data
70  """
71  device = FakeReadDevice('\x7E\x00\x01\x00\xFF')
72  xbee = XBeeBase(device)
73 
74  frame = xbee._wait_for_frame()
75  self.assertEqual(frame.data, '\x00')
76 
78  """
79  _wait_for_frame should skip invalid data
80  """
81  device = FakeReadDevice(
82  '\x7E\x00\x01\x00\xFA' + '\x7E\x00\x01\x05\xFA')
83  xbee = XBeeBase(device)
84 
85  frame = xbee._wait_for_frame()
86  self.assertEqual(frame.data, '\x05')
87 
88  def test_read_escaped(self):
89  """
90  _wait_for_frame should properly read a frame of data
91  Verify that API mode 2 escaped bytes are read correctly
92  """
93  device = FakeReadDevice('\x7E\x00\x04\x7D\x5E\x7D\x5D\x7D\x31\x7D\x33\xE0')
94 
95  xbee = XBeeBase(device,escaped=True)
96 
97  frame = xbee._wait_for_frame()
98  self.assertEqual(frame.data, '\x7E\x7D\x11\x13')
99 
100 class TestNotImplementedFeatures(unittest.TestCase):
101  """
102  In order to properly use the XBeeBase class for most situations,
103  it must be subclassed with the proper attributes definined. If
104  this is not the case, then a NotImplemented exception should be
105  raised as appropriate.
106  """
107 
108  def setUp(self):
109  """
110  Set up a base class XBeeBase object which does not have
111  api_commands or api_responses defined
112  """
113  self.xbee = XBeeBase(None)
114 
116  """
117  _build_command should raise NotImplemented
118  """
119  self.assertRaises(NotImplementedError, self.xbee._build_command, "at")
120 
122  """
123  split_command should raise NotImplemented
124  """
125  self.assertRaises(NotImplementedError, self.xbee._split_response, "\00")
126 
127  def test_shorthand(self):
128  """
129  Shorthand calls should raise NotImplementedError
130  """
131  try:
132  self.xbee.at
133  except NotImplementedError:
134  pass
135  else:
136  self.fail("Shorthand call on XBeeBase base class should raise NotImplementedError")
137 
138 class TestAsyncCallback(unittest.TestCase):
139  """
140  XBeeBase constructor should accept an optional callback function
141  argument. When provided, this will put the module into a threaded
142  mode, in which it will call the provided function with any API
143  frame data received.
144 
145  As it would be very difficult to sanely test an asynchonous callback
146  routine with a synchronous test process, proper callback behavior
147  is not tested automatically at this time. Theoretically, the
148  callback implementation logic is simple, but use it at your own risk.
149  """
150 
151  def setUp(self):
152  self.xbee = None
153  self.serial = FakeReadDevice([], silent_on_empty=True)
154  self.callback = lambda data: None
155 
156  def tearDown(self):
157  # Ensure proper thread shutdown before continuing
158  self.xbee.halt()
159 
161  """
162  XBeeBase constructor should accept a callback function
163  """
164  self.xbee = XBeeBase(self.serial, callback=self.callback)
165 
166 class TestInitialization(unittest.TestCase):
167  """
168  Ensures that XBeeBase objects are properly constructed
169  """
170 
171  def setUp(self):
172  self.base = XBeeBase(None)
173 
175  """
176  Even when a callback method is not supplied to the XBeeBase
177  constructor, it must be properly initalized as a
178  threading.Thread object
179  """
180  self.assertFalse(self.base.is_alive())
181 
182 if __name__ == '__main__':
183  unittest.main()
Definition: base.py:1


rosserial_xbee
Author(s): Adam Stambler
autogenerated on Fri Jun 7 2019 22:03:11