Fake.py
Go to the documentation of this file.
1 #! /usr/bin/python
2 """
3 Fake.py
4 
5 By Paul Malmsten, 2010
6 pmalmsten@gmail.com
7 
8 Provides fake device objects for other unit tests.
9 """
10 import sys
11 
12 class FakeDevice:
13  """
14  Represents a fake serial port for testing purposes
15  """
16  def __init__(self):
17  self.data = ''
18 
19  def write(self, data):
20  """
21  Writes data to the fake port for later evaluation
22  """
23  self.data = data
24 
26  """
27  Represents a fake serial port which can be read from in a similar
28  fashion to the real thing
29  """
30 
31  def __init__(self, data, silent_on_empty=False):
32  self.data = data
33  self.read_index = 0
34  self.silent_on_empty = silent_on_empty
35 
36  def read(self, length=1):
37  """
38  Read the indicated number of bytes from the port
39  """
40  # If too many bytes would be read, raise exception
41  if self.read_index + length > len(self.data):
42  if self.silent_on_empty:
43  sys.exit(0)
44  else:
45  raise ValueError("Not enough bytes exist!")
46 
47  read_data = self.data[self.read_index:self.read_index + length]
48  self.read_index += length
49 
50  return read_data
51 
52  def inWaiting(self):
53  """
54  Returns the number of bytes available to be read
55  """
56  return len(self.data) - self.read_index
def __init__(self, data, silent_on_empty=False)
Definition: Fake.py:31
def write(self, data)
Definition: Fake.py:19
def __init__(self)
Definition: Fake.py:16
def read(self, length=1)
Definition: Fake.py:36


rosserial_xbee
Author(s): Adam Stambler
autogenerated on Mon Jun 10 2019 14:53:52