Go to the documentation of this file.00001
00002 """
00003 Fake.py
00004
00005 By Paul Malmsten, 2010
00006 pmalmsten@gmail.com
00007
00008 Provides fake device objects for other unit tests.
00009 """
00010 import sys
00011
00012 class FakeDevice:
00013 """
00014 Represents a fake serial port for testing purposes
00015 """
00016 def __init__(self):
00017 self.data = ''
00018
00019 def write(self, data):
00020 """
00021 Writes data to the fake port for later evaluation
00022 """
00023 self.data = data
00024
00025 class FakeReadDevice:
00026 """
00027 Represents a fake serial port which can be read from in a similar
00028 fashion to the real thing
00029 """
00030
00031 def __init__(self, data, silent_on_empty=False):
00032 self.data = data
00033 self.read_index = 0
00034 self.silent_on_empty = silent_on_empty
00035
00036 def read(self, length=1):
00037 """
00038 Read the indicated number of bytes from the port
00039 """
00040
00041 if self.read_index + length > len(self.data):
00042 if self.silent_on_empty:
00043 sys.exit(0)
00044 else:
00045 raise ValueError("Not enough bytes exist!")
00046
00047 read_data = self.data[self.read_index:self.read_index + length]
00048 self.read_index += length
00049
00050 return read_data
00051
00052 def inWaiting(self):
00053 """
00054 Returns the number of bytes available to be read
00055 """
00056 return len(self.data) - self.read_index