class FileParser: __errorLog = ErrorLog() def __init__(self, fileName): self.fileName = fileName self.driverDataCollector = DriverDataCollector() # Check the file for data and parse the commands def parse(self): # Try opening the file provided as the argument with open(self.fileName) as inputFile: # iterate through each line the file for line in inputFile: # check for empty lines # TODO: make trim a function for strings if len(line.replace(" ", "")) > 0: # ASSUMPTION: The lines in the file do not matter, so I'm separating the driver commands # from the trip commands so that I can process the drivers first then trips. # Another approach could be to just log trips that don't have drivers yet as errors if line.lower().startswith('driver'): self.driverDataCollector.addDriverCommand(line) elif line.lower().startswith('trip'): self.driverDataCollector.addTripCommand(line) else: self.__errorLog.log("Invalid Command: " + line) inputFile.close() return self.driverDataCollector.processCommands()
def test_add_invalid_trip(self): # Arrange driverDataCollector = DriverDataCollector() driverDataCollector.driverCommands = [] driverDataCollector.errors = [] driverDataCollector.tripCommands = [] # Act driverDataCollector.addTripCommand(self.invalidTripCommand) # Assert self.assertEqual(len(driverDataCollector.tripCommands), 0) self.assertEqual(len(driverDataCollector.errors), 1) # Cleanup del driverDataCollector
def test_add_duplicate_driver(self): # Arrange driverDataCollector = DriverDataCollector() driverDataCollector.driverCommands = [] driverDataCollector.errors = [] driverDataCollector.tripCommands = [] # Act driverDataCollector.addDriverCommand(self.validDriverCommand) driverDataCollector.addDriverCommand(self.validDriverCommand) # Assert self.assertEqual(len(driverDataCollector.driverCommands), 1) self.assertEqual(len(driverDataCollector.errors), 1) # Cleanup del driverDataCollector
def test_add_valid_driver(self): # Arrange # TODO: for some reason it appears that each test is using the same memory address # which causes subsequent tests to fail driverDataCollector = DriverDataCollector() driverDataCollector.driverCommands = [] driverDataCollector.errors = [] driverDataCollector.tripCommands = [] # Act driverDataCollector.addDriverCommand(self.validDriverCommand) # Assert self.assertEqual(len(driverDataCollector.driverCommands), 1) self.assertEqual(len(driverDataCollector.errors), 0) # Cleanup del driverDataCollector
def __init__(self, fileName): self.fileName = fileName self.driverDataCollector = DriverDataCollector()