コード例 #1
0
 def test_park_creates_vehicle_with_correct_variable_formatting(self):
     '''Ensures registration numbers are always uppercase and colors are always capialized'''
     parking_lot_size = 1
     parking_lot = ParkingLot(parking_lot_size)
     parking_lot.park("ka-01-hh-1234", "white")
     self.assertEqual(parking_lot.parking_lot_spaces[1].registration_number, "KA-01-HH-1234")
     self.assertEqual(parking_lot.parking_lot_spaces[1].color, "White")
コード例 #2
0
 def test_leave_returns_true_if_slot_exists_and_false_otherwise(self):
     '''Ensures 'leave' only works if called on an actual parking slot'''
     parking_lot_size = 2
     parking_lot = ParkingLot(parking_lot_size)
     self.assertTrue(parking_lot.leave(1))
     self.assertTrue(parking_lot.leave(2))
     self.assertFalse(parking_lot.leave(0))
     self.assertFalse(parking_lot.leave(3))
コード例 #3
0
 def test_park_returns_slot_key_if_slot_is_empty_and_none_if_all_slots_taken(self):
     '''Ensure park function fills slots in proper order and None is slots are full'''
     parking_lot_size = 2
     parking_lot = ParkingLot(parking_lot_size)
     park_return_variable = parking_lot.park("ka-01-hh-1234" , "white")
     self.assertEqual(park_return_variable, 1)
     park_return_variable = parking_lot.park("ka-01-hh-1234" , "white")
     self.assertEqual(park_return_variable, 2)
     park_return_variable = parking_lot.park("ka-01-hh-1234" , "white")
     self.assertIsNone(park_return_variable)
コード例 #4
0
 def test_find_matches_returns_list_of_correct_vehicle_attributes_or_parking_slots(self):
     '''Ensures vehicle attribute searches return the correct information'''
     parking_lot_size = 6
     parking_lot = ParkingLot(parking_lot_size)
     for x in range(6):
         parking_lot.park("AK-01-HH-1234", "White")
     self.assertEqual(parking_lot.find_matches("AK-01-HH-1234","registration_number","slot"),[1,2,3,4,5,6])
     self.assertEqual(parking_lot.find_matches("White","color","slot"),[1,2,3,4,5,6])
     self.assertEqual(parking_lot.find_matches("whit","color","slot"),[])
     self.assertEqual(parking_lot.find_matches("White","color","registration_number"),["AK-01-HH-1234"]*6)
コード例 #5
0
 def test_create_parking_lot_with_expected_number_of_slots(self):
     '''Ensure parking_lot_size dict is created with proper keys when ParkingLot obejct created'''
     parking_lot_size = 6
     parking_lot = ParkingLot(parking_lot_size)
     self.assertEqual(len(parking_lot.parking_lot_spaces), parking_lot_size)
     self.assertIsNone(parking_lot.parking_lot_spaces[1])
     self.assertIsNone(parking_lot.parking_lot_spaces[parking_lot_size])
     self.assertRaises(KeyError, lambda: parking_lot.parking_lot_spaces[0])
     self.assertRaises(KeyError, lambda: parking_lot.parking_lot_spaces[parking_lot_size+1])
コード例 #6
0
class ParkingLotShell(cmd.Cmd):
    '''cmd module subclass used for a CLI for issuing parking lot management commands'''
    intro = "Welcome to the Parking Lot Shell. For detailed information on available commands, type 'help' into the prompt.\n"\
            "For more information on a specific command type 'help [command]'\n"\
            "ex: help park"
    prompt = "(PL_Shell) "
    parking_lot = None

    def emptyline(self):
        """ Overrides empty line function in parent class.
            When return key is pressed on an empty line, the prompt will simply break to a new line. """
        pass

    @exception_decorator
    def do_create_parking_lot(self, s):
        '''Creates a ParkingLot object. Invoked with create_parking_lot command in CLI'''
        self.parking_lot = ParkingLot(s)
        print(f"Created a parking lot with {s} slots")

    def help_create_parking_lot(self):
        '''Prints help information when help create_parking_lot is typed in CLI'''
        print("Creates a ParkingLot object. Expects a single integer as an argument which represents\n"\
              "the number of parking slots the parking lot can accomodate\n"\
              "ex: create_parking_lot 6")

    @exception_decorator
    def do_park(self, s):
        '''Parks a Vehicle in nearest available slot. Invoked with park command in CLI'''
        slot = self.parking_lot.park(*s.split())
        if slot:
            print(f"Allocated slot number: {slot}")
        else:
            print("Sorry, parking lot is full")

    def help_park(self):
        '''Prints help information when help park is typed in CLI'''
        print("Creates a Vehicle object and adds it to the nearest available parking slot in the lot\n"\
              "Expects 2 arguments: The vehicle registration number and the vehicle color, listed in order\n"\
              "ex: park KA-01-HH-9999 White")

    @exception_decorator
    def do_leave(self, s):
        '''Vehicle in designated parking slot leaves. Invoked with leave command in CLI'''
        if self.parking_lot.leave(s):
            print(f"Slot number {s} is free")
        else:
            print("That slot doesn't exist in this parking lot")

    def help_leave(self):
        '''Prints help information when help leave is typed in CLI'''
        print("If a vehicle is in a parking slot, this command empties it.\n"
              "Expects 1 argument: a single integer representing the parking slot\n"\
              "ex: leave 4")

    @exception_decorator
    def do_status(self, s):
        '''Prints vehicle information on all occupied slots. Invoked with status command in CLI'''
        if FILE_OUTPUT:
            status_delimiter = "    "
        else:
            status_delimiter = "\n"
        print(
            f"Slot No.{status_delimiter}Registration No{status_delimiter}Colour"
        )
        for slot in sorted(self.parking_lot.parking_lot_spaces.keys()):
            if self.parking_lot.parking_lot_spaces[slot]:
                print(f"{slot}{status_delimiter}{self.parking_lot.parking_lot_spaces[slot].registration_number}"\
                      f"{status_delimiter}{self.parking_lot.parking_lot_spaces[slot].color}")

    def help_status(self):
        '''Prints help information when help status is typed in CLI'''
        print("Prints vehicle information on all occupied parking slots\n"\
              "Expects no arguments\n"
              "ex: status")

    @exception_decorator
    def do_registration_numbers_for_cars_with_colour(self, s):
        '''Searches for and prints registration numbers of vehicles associated with a color
        Invoked with registration_numbers_for_cars_with_colour command in CLI'''
        matches = self.parking_lot.find_matches(s.strip().capitalize(),
                                                "color", "registration_number")
        print_match_results(matches)

    def help_registration_numbers_for_cars_with_colour(self):
        '''Prints help information when help registration_numbers_for_cars_with_colour is typed in CLI'''
        print(
            "Prints all vehicle registration numbers associated with a vehicle color\n"
            "Expects one argument: the vehicle color\n"
            "ex: help_registration_numbers_for_cars_with_colour White")

    @exception_decorator
    def do_slot_numbers_for_cars_with_colour(self, s):
        '''Searches for and prints locations of vehicles associated with a color
        Invoked with slot_numbers_for_cars_with_colour command in CLI'''
        matches = self.parking_lot.find_matches(s.capitalize(), "color",
                                                "slot")
        print_match_results(matches)

    def help_slot_numbers_for_cars_with_colour(self):
        '''Prints help information when help slot_numbers_for_cars_with_colour is typed in CLI'''
        print("Prints all parking lot slots associated with a vehicle color\n"
              "Expects one argument: the vehicle color\n"
              "ex: lot_numbers_for_cars_with_colour White")

    @exception_decorator
    def do_slot_number_for_registration_number(self, s):
        '''Searches for and prints locations of vehicles associated with a registration number
        Invoked with slot_number_for_registration_number command in CLI'''
        matches = self.parking_lot.find_matches(s.upper(),
                                                "registration_number", "slot")
        print_match_results(matches)

    def help_slot_number_for_registration_number(self):
        '''Prints help information when help slot_number_for_registration_number is typed in CLI'''
        print(
            "Prints parking lot slot associated with a vehicle registration number\n"
            "Expects one argument: the vehicle registration number\n"
            "ex: slot_number_for_registration_number KA-01-HH-3141")
コード例 #7
0
 def do_create_parking_lot(self, s):
     '''Creates a ParkingLot object. Invoked with create_parking_lot command in CLI'''
     self.parking_lot = ParkingLot(s)
     print(f"Created a parking lot with {s} slots")
コード例 #8
0
 def test_leave_converts_strings_to_integer(self):
     '''Ensures 'leave' will convert properly formatted string to an integer'''
     parking_lot_size = 2
     parking_lot = ParkingLot(parking_lot_size)
     self.assertTrue(parking_lot.leave("1"))