test_frame.py
Go to the documentation of this file.
1 #! /usr/bin/python
2 """
3 test_frame.py
4 
5 Paul Malmsten, 2010
6 pmalmsten@gmail.com
7 
8 Tests frame module for proper behavior
9 """
10 import unittest
11 from xbee.frame import APIFrame
12 
13 class TestAPIFrameGeneration(unittest.TestCase):
14  """
15  XBee class must be able to create a valid API frame given binary
16  data, in byte string form.
17  """
18  def test_single_byte(self):
19  """
20  create a frame containing a single byte
21  """
22  data = '\x00'
23  # start byte, two length bytes, data byte, checksum
24  expected_frame = '\x7E\x00\x01\x00\xFF'
25 
26  frame = APIFrame(data).output()
27  self.assertEqual(frame, expected_frame)
28 
29 class TestAPIFrameParsing(unittest.TestCase):
30  """
31  XBee class must be able to read and validate the data contained
32  by a valid API frame.
33  """
34 
36  """
37  remaining_bytes() should provide accurate indication
38  of remaining bytes required before parsing a packet
39  """
40  api_frame = APIFrame()
41 
42  frame = '\x7E\x00\x04\x00\x00\x00\x00\xFF'
43  self.assertEqual(api_frame.remaining_bytes(), 3)
44  api_frame.fill(frame[0])
45  self.assertEqual(api_frame.remaining_bytes(), 2)
46  api_frame.fill(frame[1])
47  self.assertEqual(api_frame.remaining_bytes(), 1)
48  api_frame.fill(frame[2])
49  self.assertEqual(api_frame.remaining_bytes(), 5)
50  api_frame.fill(frame[3])
51  self.assertEqual(api_frame.remaining_bytes(), 4)
52 
53  def test_single_byte(self):
54  """
55  read a frame containing a single byte
56  """
57  api_frame = APIFrame()
58 
59  frame = '\x7E\x00\x01\x00\xFF'
60  expected_data = '\x00'
61 
62  for byte in frame:
63  api_frame.fill(byte)
64  api_frame.parse()
65 
66  self.assertEqual(api_frame.data, expected_data)
67 
69  """
70  when an invalid frame is read, an exception must be raised
71  """
72  api_frame = APIFrame()
73  frame = '\x7E\x00\x01\x00\xF6'
74 
75  for byte in frame:
76  api_frame.fill(byte)
77 
78  self.assertRaises(ValueError, api_frame.parse)
79 
80 class TestEscapedOutput(unittest.TestCase):
81  """
82  APIFrame class must properly escape data
83  """
84 
85  def test_escape_method(self):
86  """
87  APIFrame.escape() must work as expected
88  """
89  test_data = APIFrame.START_BYTE
90  new_data = APIFrame.escape(test_data)
91  self.assertEqual(new_data, APIFrame.ESCAPE_BYTE + '\x5e')


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