def test_grid(): assert grid_to_string(parse(read_lines("board_test.txt")), p) == """*X* *Y* You have 0 water buckets.""", "Test case 1 failed" print("Simple Grid to String passed!") assert grid_to_string(parse(read_lines("board_test.txt")), o) == """*X* *Y* You have 1 water bucket.""", "Test case 2 failed" print("Increase Water Bucket to String passed!") assert grid_to_string(parse(read_lines("board_test.txt")), c) == """*X* *Y* You have p water buckets.""", "Test case 3 failed" print("Various amounts of water buckets case passed!") assert grid_to_string(parse(read_lines("board_test.txt")), d) == """*X* *Y* You have water buckets.""", "Test case 4 failed" print("No water buckets case passed!") assert grid_to_string(parse(read_lines("board_test.txt")), e) == """*X* *Y* You have !@#$%^&*() water buckets.""", "Test case 5 failed" print("Various amounts of water buckets case 2 passed!") try: grid_to_string((1), e) print("Test Case Grid to String TypeError Failed!") except TypeError: print("Test Case Grid to String TypeError Passed!")
def test_parse_end(): '''Tests the behaviour of parse when invalid ending position is used in configuration. Should raise an error''' lines=['*Y*X*','* *','*Y***'] try: parse(lines) raise AssertionError("Failed invalid end parse: no error raised for invalid ending position") except Exception as e: assert str(e)=='Expected 1 ending position, got 2.','Failed invalid end parse: wrong error raised for invalid ending position'
def test_parse_error_order(): '''Tests the behaviour of parse when multiple invalid elements are used in configuration. Should raise an error''' lines=['*YXX*','*1 @*','*Y***'] try: parse(lines) raise AssertionError("Failed multiple error parse: no error raised when multiple elements are") except Exception as e: assert str(e)=='Bad letter in configuration file: @.','Failed multiple error parse: wrong error raised whe multiple elements are invalid'
def test_parse_bad_letter(): '''Tests the behaviour of parse when invalid letters are used in configuration. Should raise an error''' lines=['***X*','*Z *','*Y***'] try: parse(lines) raise AssertionError("Failed invalid letter parse: no error raised for bad letter in configuration") except Exception as e: assert str(e)=='Bad letter in configuration file: Z.','Failed invalid letter parse: wrong error raised for bad letter in configuration'
def test_parse_teleport(): '''Tests the behaviour of parse when invalid teleport pads are used in configuration. Should raise an error''' lines=['***X*','*1 *','*Y***'] try: parse(lines) raise AssertionError("Failed invalid teleporter parse: no error raised for invalid teleport pads") except Exception as e: assert str(e)=='Teleport pad 1 does not have an exclusively matching pad.' ,'Failed invalid teleporter parse: wrong error raised for invalid teleport pads'
def test_parse6(): #edge case with 4 teleport gates grid = ['**X**\n', '*W77*\n', '* F77\n', '**Y**'] actual = parse(grid) excepted = [[ isinstance(actual[0][0], Wall), isinstance(actual[0][1], Wall), isinstance(actual[0][2], Start), isinstance(actual[0][3], Wall), isinstance(actual[0][4], Wall) ], [ isinstance(actual[1][0], Wall), isinstance(actual[1][1], Water), isinstance(actual[1][2], Teleport), isinstance(actual[1][3], Teleport), isinstance(actual[1][4], Wall) ], [ isinstance(actual[2][0], Wall), isinstance(actual[2][1], Air), isinstance(actual[2][2], Fire), isinstance(actual[2][3], Teleport), isinstance(actual[2][4], Teleport) ], [ isinstance(actual[3][0], Wall), isinstance(actual[3][1], Wall), isinstance(actual[3][2], End), isinstance(actual[3][3], Wall), isinstance(actual[3][4], Wall) ]] for item in excepted: for a in item: assert a == True, 'testcase test_parse6 failed.'
def test_parse_empty_line(): line = [''] try: grid = parse(line) except ValueError as e: assert str(e) == 'Expected 1 starting position, got 0.' print('Testing empty board successful')
def test_parse1(): #postive testcase grid = ['**X**\n', '*W1**\n', '* F1*\n', '**Y**'] actual = parse(grid) excepted = [[ isinstance(actual[0][0], Wall), isinstance(actual[0][1], Wall), isinstance(actual[0][2], Start), isinstance(actual[0][3], Wall), isinstance(actual[0][4], Wall) ], [ isinstance(actual[1][0], Wall), isinstance(actual[1][1], Water), isinstance(actual[1][2], Teleport), isinstance(actual[1][3], Wall), isinstance(actual[1][4], Wall) ], [ isinstance(actual[2][0], Wall), isinstance(actual[2][1], Air), isinstance(actual[2][2], Fire), isinstance(actual[2][3], Teleport), isinstance(actual[2][4], Wall) ], [ isinstance(actual[3][0], Wall), isinstance(actual[3][1], Wall), isinstance(actual[3][2], End), isinstance(actual[3][3], Wall), isinstance(actual[3][4], Wall) ]] for item in excepted: for a in item: assert a == True, 'testcase test_parse1 failed.'
def test_player(): player = Player() ## Testing Player.move() ## ## Positive test cases ## assert player.move('a') == 'a', 'Positive test case 1 failed, move "a".' assert player.move('w') == 'w', 'Positive test case 2 failed, move "w".' assert player.move('s') == 's', 'Positive test case 3 failed, move "s".' assert player.move('d') == 'd', 'Positive test case 4 failed, move "d".' assert player.move('e') == 'e', 'Positive test case 5 failed, move "e".' ## Negative test cases ## assert player.move( 5) == None, 'Negative test case 1 failed, non-string move.' ## Edge cases ## ## Testing Player.initial_player_position() ## ## Positive test cases ## grid = game_parser.parse(game_parser.read_lines('board_simple.txt')) assert player.initial_player_position(grid) == [ 0, 2 ], 'Positive test case 1 failed.' grid = game_parser.parse(game_parser.read_lines('board_medium.txt')) assert player.initial_player_position(grid) == [ 0, 2 ], 'Positive test case 2 failed.' grid = game_parser.parse(game_parser.read_lines('board_hard.txt')) assert player.initial_player_position(grid) == [ 0, 1 ], 'Positive test case 3 failed.' grid = game_parser.parse(game_parser.read_lines('board.txt')) assert player.initial_player_position(grid) == [ 0, 1 ], 'Positive test case 4 failed.' grid = game_parser.parse(game_parser.read_lines('board_test.txt')) assert player.initial_player_position(grid) == [ 1, 0 ], 'Positive test case 5 failed.' ## Negative test cases ## assert player.initial_player_position( 'string') == None, 'Negative test case 1 failed.' assert player.initial_player_position( [1, 2, 3]) == None, 'Negative test case 2 failed.' assert player.initial_player_position( []) == None, 'Negative test case 3 failed.'
def test_grid_normal(): '''tests normal operation of grid_to_function. Should return single string which displays player position on board and water buckets''' player=Player.__new__(Player) player.display='A' player.col=1 player.row=0 player.num_water_buckets=0 assert grid_to_string(parse(config_1),player)=='*A*************\n* 1 * *\n* ***W** **** *\n* 1********F *\n*************Y*\n\nYou have 0 water buckets.\n','Failed normal grid: wrong string returned'
def test_parse_single_teleport_pad(): lines = ['*****\n', 'X 1 Y\n', '*****'] try: grid = parse(lines) except ValueError as e: assert str( e) == 'Teleport pad 1 does not have an exclusively matching pad.' print('Testing single teleport pad successful')
def test_grid_water_buckets(): '''tests behaviour of grid_to_function when player has non-zero water buckets''' player=Player.__new__(Player) player.display='A' player.col=1 player.row=1 player.num_water_buckets=8 assert grid_to_string(parse(config_1),player)=='*X*************\n*A 1 * *\n* ***W** **** *\n* 1********F *\n*************Y*\n\nYou have 8 water buckets.\n','Failed water buckets grid: wrong string returned'
def test_grid_non_rectangular(): '''tests normal operation of grid_to_function. Should return single string which displays player position on board and water buckets''' player=Player.__new__(Player) player.display='A' player.col=1 player.row=2 player.num_water_buckets=2 assert grid_to_string(parse(config_2),player)=='*X*\n* *\n*A****\n* Y\n******\n\nYou have 2 water buckets.\n','Failed non rectangular grid: wrong string returned'
def test_grid_player_not_air(): '''tests behaviour of grid_to_function when player postion is on a non whitespace display cell.'A' should replace pre-existing display''' player=Player.__new__(Player) player.display='A' player.col=5 player.row=2 player.num_water_buckets=0 assert grid_to_string(parse(config_1),player)=='*X*************\n* 1 * *\n* ***A** **** *\n* 1********F *\n*************Y*\n\nYou have 0 water buckets.\n','Failed non Air grid: wrong string returned'
def test_parse7(): #edge case with 5 teleport gates grid = ['**X2*\n', '*W8**\n', '* F8*\n', '2*Y*8'] try: actual = parse(grid) except Exception as e: assert type(e) == ValueError, 'testcase test_parse7 failed.' assert str( e ) == 'Teleport pad 8 does not have an exclusively matching pad.', 'testcase test_parse7 failed.'
def test_parse2(): #negative testcase with no starting grid = ['*****\n', '*W1**\n', '* F1*\n', '**Y**'] try: actual = parse(grid) except Exception as e: assert type(e) == ValueError, 'testcase test_parse2 failed.' assert str( e ) == 'Expected 1 starting position, got 0.', 'testcase test_parse2 failed.'
def test_parse3(): #negative testcase with 2 endings grid = ['**X**\n', '*W1**\n', '* F1*\n', '*Y*Y*'] try: actual = parse(grid) except Exception as e: assert type(e) == ValueError, 'testcase test_parse3 failed.' assert str( e ) == 'Expected 1 ending position, got 2.', 'testcase test_parse3 failed.'
def test_parse4(): #negative testcase with unmatching pad grid = ['**X2*\n', '*W1**\n', '* F1*\n', '**Y**'] try: actual = parse(grid) except Exception as e: assert type(e) == ValueError, 'testcase test_parse4 failed.' assert str( e ) == 'Teleport pad 2 does not have an exclusively matching pad.', 'testcase test_parse4 failed.'
def test_parse5(): #negative testcase with bad letter grid = ['**X**\n', '*W1**\n', '* F1*\n', '!*Y**'] try: actual = parse(grid) except Exception as e: assert type(e) == ValueError, 'testcase test_parse5 failed.' assert str( e ) == 'Bad letter in configuration file: !.', 'testcase test_parse5 failed.'
def test_grid(): player = Player() ## Positive test cases ## grid = game_parser.parse(game_parser.read_lines('board_simple.txt')) assert grid_to_string(grid, player) == "A*X**\n* *\n**Y**\n\nYou have 0 water buckets.", "Positive test case 1 failed." grid = game_parser.parse(game_parser.read_lines('board_medium.txt')) assert grid_to_string(grid, player) == "A*X***\n* *\n* ** *\n* *\n* *\n****Y*\n\nYou have 0 water buckets.", "Positive test case 2 failed." grid = game_parser.parse(game_parser.read_lines('board_hard.txt')) assert grid_to_string(grid, player) == "AX*************\n* 2 * *\n* *** ** **** *\n* * W* 1 *\n* ***** ***** *\n* 2 * ** *F*\n* ** *** *\n* 1********F *\n*************Y*\n\nYou have 0 water buckets.", "Positive test case 3 failed." grid = game_parser.parse(game_parser.read_lines('board.txt')) assert grid_to_string(grid, player) == "AXY*********\n* *\n* 1 *\n* *\n*** *\n*1* *\n************\n\nYou have 0 water buckets.", "Positive test case 4 failed." grid = game_parser.parse(game_parser.read_lines('1move.txt')) assert grid_to_string(grid, player) == "A*\nXY\n**\n\nYou have 0 water buckets.", "Positive test case 5 failed." ## Negative test cases ## grid = game_parser.parse(game_parser.read_lines('board_simple.txt')) assert grid_to_string(grid, 'Jack') == None, 'Negative test case 1 failed, player is not a Player object.' assert grid_to_string(34, player) == None, 'Negative test case 2 failed, grid is not a list.' bad_grid = [1, 2, 'a'] assert grid_to_string(bad_grid, player) == None, 'Negative test case 3 failed, element in grid is not a list.' ## Edge cases ## empty_grid = [] assert grid_to_string(empty_grid, player) == None, 'Edge case 1 failed, empty grid.'
def solve(maze): nums = [] add = '' nums.append(add) maze = parse(read_lines(filename)) while not find_end(maze, add): add = nums[0] nums.remove(nums[0]) for j in ["a", "d", "w", "s"]: put = add + '{}, '.format(j) if valid(maze, put): nums.append(put) return True
def recieve(self, socket): while True: resp = socket.recv(128).decode('utf-8') if resp != '': game_object = parse(resp) if isinstance(game_object, Player): self.__game.players[int(game_object.id) - 1].x = (int( game_object.x)) self.__game.players[int(game_object.id) - 1].y = (int( game_object.y)) self.label_players[int(game_object.id) - 1][1] = game_object else: self.__game.bomb_it(game_object)
def test_parse(): # The first test case,both are positive tests wall = Wall() start = Start() end = End() teleplort = Teleport(2) f = open("wall.txt", "r") line = f.readlines( ) # get the string gird,put it in the the parse function f.close() new_grid = parse(line) i = 0 count = 0 maxnumber = len(new_grid) * len(new_grid[0]) if type(new_grid[0][0]) == type(wall): count += 1 if type(new_grid[0][1]) == type(start): count += 1 if type(new_grid[0][2]) == type(wall): count += 1 if type(new_grid[1][0]) == type(wall): count += 1 if type(new_grid[1][1]) == type(end): count += 1 if type(new_grid[1][2]) == type(wall): count += 1 assert maxnumber == count, "Test case 1 did not pass" print("Test case 1 for Parse pass") # The second test case ,edge test case f = open("board_hard.txt", "r") line = f.readlines() f.close() grid_two = parse(line) assert isinstance(grid_two[0][0], type(wall)) and isinstance(grid_two[5][3], type(teleplort)) == True, \ "Test case 2 did not pass" print("Test case 2 for Parse pass")
def test_game(): player = Player() player.row = 0 player.col = 1 game = Game('board_hard.txt') grid = parse(read_lines('board_hard.txt')) ## Testing gameMove() ## ## Positive test cases ## assert game.gameMove('a') == [1, 'a, '], 'Positive testcase 1 failed.' assert game.gameMove('d') == [2, 'a, d, '], 'Positive testcase 2 failed.' assert game.gameMove('s') == [3, 'a, d, s, '], 'Positive testcase 3 failed.' assert game.gameMove('w') == [4, 'a, d, s, w, ' ], 'Positive testcase 5 failed.' ## Negative test cases ## assert game.gameMove( 1) == None, 'Negative testcase 1 failed, integer type move.' assert game.gameMove( []) == None, 'Negative testcase 2 failed, list type move.' ## Edge cases ## ## move_action() ## ## Positive test cases ## assert game.move_action(grid, player, 's') == '', 'Postive testcase 1 failed.' assert game.move_action(grid, player, 'a') == '', 'Positve testcase 2 failed.' ## Negative test cases ## assert game.move_action(grid, player, 1) == None, 'Negative testcase 1 failed' ## Edge cases ## player.row = -1 player.col = 1 assert game.move_action( grid, player, 'w' ) == '\nYou walked into a wall. Oof!\n', 'Edgecase 1 failed, should act as a wall when stepping off the grid.' player.row = 1 player.col = -1 assert game.move_action( grid, player, 'a' ) == '\nYou walked into a wall. Oof!\n', 'Edgecase 2 failed, should act as a wall when stepping off the grid.'
def test_parse_normal(): '''Test normal parse operation. Should return a list of list of cells (4x5) and should not have any strings.''' lines=['***X*','*88 *','*WF *','*Y***'] grid=parse(lines) contains_string=False for row in grid: for col in row: if type(col)==str: contains_string=True assert (len(grid)==4 and len(grid[0])==len(grid[1])==len(grid[2])==5) , 'Failed normal parse: list of lists of cells parsed was not 4x5' assert not contains_string , "Failed normal parse: list of lists of cells contained a string" assert isinstance(grid[0][0],cells.Wall),'Failed normal parse: expected cell type Wall' assert isinstance(grid[1][1],cells.Teleport),'Failed normal parse: expected cell type Teleport' assert isinstance(grid[2][2],cells.Fire),'Failed normal parse: expected cell type Fire' assert isinstance(grid[2][1],cells.Water),'Failed normal parse: expected cell type Water' assert isinstance(grid[1][3],cells.Air),'Failed normal parse: expected cell type Air' assert isinstance(grid[0][3],cells.Start),'Failed normal parse: expected cell type Start' assert isinstance(grid[3][1],cells.End),'Failed normal parse: expected cell type End'
def test_cells(): game = Game('board_hard.txt') player = Player() grid = game_parser.parse(game_parser.read_lines('board_hard.txt')) ## Testing cell.step() ## cell = Start('X') assert cell.step(game, player, grid, 'a') == '', 'cells testcase 1 failed, Start.step.' cell = End('Y') assert cell.step( game, player, grid, 'a' ) == 'end of game - successful', 'cells testcase 2 failed, End.step.' cell = Air(' ') assert cell.step(game, player, grid, 'w') == '', 'cells testcase 3 failed, Air.step.' cell = Wall('*') assert cell.step( game, player, grid, 's' ) == '\nYou walked into a wall. Oof!\n', 'cells testcase 4 failed, Wall.step.' cell = Fire('F') assert cell.step( game, player, grid, 'd' ) == 'end of game - unsuccessful', 'cells testcase 5 failed, Fire.step with no water buckets.' player.num_water_buckets = 1 assert cell.step(game, player, grid, 'a') == '\nWith your strong acorn arms, you throw a water bucket at the fire.' \ 'You acorn roll your way through the extinguished flames!\n', 'cells testcase 6 failed, Fire.step with water bucket.' cell = Water('W') assert cell.step( game, player, grid, 's' ) == "\nThank the Honourable Furious Forest, you've found a bucket of water!\n", 'cells testcase 7 failed, Water.step' assert cell.step( game, player, grid, 'a' ) == '', 'cells testcase 8 failed, Water.step should only function when stepped on for the first time.' cell = Teleport('1') assert cell.step(game, player, grid, 'a') == '\nWhoosh! The magical gates break Physics as we know ' \ 'it and opens a wormhole through space and time.\n', 'cells testcase 9 failed, Teleport.step.'
def test_parse_cell_objects(): lines = ['*XWF\n', ' Y11\n'] grid = parse(lines) assert isinstance(grid[0][0], Wall), 'Cell should be an object of type Wall' assert grid[0][ 0].display == '*', 'Cell of type "Wall" should have display "*" ' assert isinstance(grid[0][1], Start), 'Cell should be an object of type Start' assert grid[0][ 1].display == 'X', 'Cell of type "Start" should have display "X" ' assert isinstance(grid[0][2], Water), 'Cell should be an object of type Water' assert grid[0][ 2].display == 'W', 'Cell of type "Water" should have display "W" ' assert isinstance(grid[0][3], Fire), 'Cell should be an object of type Fire' assert grid[0][ 3].display == 'F', 'Cell of type "Fire" should have display "F" ' assert isinstance(grid[1][0], Air), 'Cell should be an object of type Air' assert grid[1][ 0].display == ' ', 'Cell of type "Air" should have display " " ' assert isinstance(grid[1][1], End), 'Cell should be an object of type End' assert grid[1][ 1].display == 'Y', 'Cell of type "End" should have display "Y" ' assert isinstance(grid[1][2], Teleport), 'Cell should be an object of type Teleport' assert grid[1][ 2].display == '1', 'Cell of type "Teleport" should have display "1" ' print('Testing cell objects successful')
def test_parse(): assert True # positive cases """testing normal output of cells""" grid = parse(["*XY"]) assert type(grid[0][0]) == type(Wall()), "Failed Parse!" assert type(grid[0][1]) == type(Start()), "Failed Parse!" assert type(grid[0][2]) == type(End()), "Failed Parse!" """testing normal output of cells""" grid1 = ["*XY", "WF*", "11*"] grid1p = parse(grid1) assert type(grid1p[1][0]) == type(Water()), "Failed Parse!" assert type(grid1p[1][1]) == type(Fire()), "Failed Parse!" assert type(grid1p[1][2]) == type(Wall()), "Failed Parse!" assert type(grid1p[2][0]) == type(Teleport(1)), "Failed Parse!" assert type(grid1p[2][1]) == type(Teleport(1)), "Failed Parse!" assert type(grid1p[2][2]) == type(Wall()), "Failed Parse!" # negative cases """ Type error for No file - readlines""" try: read_lines(None) except TypeError: pass else: print("Test Failed") """ two start positions """ try: grid1 = parse(["*XXY"]) except ValueError: pass else: print("Test failed!") """ two end positions """ try: grid1 = parse(["*XYY"]) except ValueError: pass else: print("Test failed!") """ Single teleporter """ try: grid1 = parse(["*XA1"]) except ValueError: pass else: print("Test failed!") """ Single teleporter2 """ try: grid1 = parse(["*XA2"]) except ValueError: pass else: print("Test failed!") """ 3 teleporter """ try: grid1 = parse(["*XA111"]) except ValueError: pass else: print("Test failed!") """ 0 teleporter """ try: grid1 = parse(["*XA00"]) except ValueError: pass else: print("Test failed!") """ no start end or player """ try: grid1 = parse(["***"]) except ValueError: pass else: print("Test failed!") """ 1cell """ try: grid1 = parse(["*"]) except ValueError: pass else: print("Test failed!") """ only player """ try: grid1 = parse(["*A"]) except ValueError: pass else: print("Test failed!") """ no start """ try: grid1 = parse(["*Y"]) except ValueError: pass else: print("Test failed!") """ no end """ try: grid1 = parse(["X*A"]) except ValueError: pass else: print("Test failed!") """ lowercase grid """ try: grid1 = parse(["xyA"]) except ValueError: pass else: print("Test failed!") """ invalid character """ try: grid1 = parse(["XYb"]) except ValueError: pass else: print("Test failed!") # edge cases """ uneven lines of grid """ try: grid1 = parse(["X*A", "Y11*"]) except ValueError: pass else: print("Test failed!") """ two cell grid """ grid1 = parse(["XY"]) assert type(grid1[0][0]) == type(Start()), "Failed Parse!" assert type(grid1[0][1]) == type(End()), "Failed Parse!"
def test_parse(): lines = read_lines('small_board.txt') grid = parse(lines) assert type(grid[0][0]) == Wall assert type(grid[0][1]) == Start assert type(grid[0][2]) == End lines = read_lines('board_everything.txt') grid = parse(lines) assert type(grid[0][0]) == Wall assert type(grid[0][1]) == Start assert type(grid[0][2]) == Wall assert type(grid[0][3]) == Wall assert type(grid[1][0]) == Wall assert type(grid[1][1]) == Water assert type(grid[1][2]) == Fire assert type(grid[1][3]) == Wall assert type(grid[2][0]) == Wall assert type(grid[2][1]) == Teleport assert type(grid[2][2]) == Teleport assert type(grid[2][3]) == Wall assert type(grid[3][0]) == Wall assert type(grid[3][1]) == Air assert type(grid[3][2]) == Air assert type(grid[3][3]) == Wall assert type(grid[4][0]) == Wall assert type(grid[4][1]) == End assert type(grid[4][2]) == Wall assert type(grid[4][3]) == Wall ## Negative test cases ## ''' Bad letter in configuration file ''' lines = read_lines('board_bad.txt') try: parse(lines) assert False except AssertionError: print("parse: Testcase 1 failed (did not throw an exception)") except ValueError: pass ''' No ending position ''' lines = read_lines('board_noend.txt') try: parse(lines) assert False except AssertionError: print("parse: Testcase 2 failed (did not throw an exception)") except ValueError: pass ''' No starting position ''' lines = read_lines('board_nostart.txt') try: parse(lines) assert False except AssertionError: print("parse: Testcase 3 failed (did not throw an exception)") except ValueError: pass ''' Teleport pad with out matching pad ''' lines = read_lines('board_onepad.txt') try: parse(lines) assert False except AssertionError: print("parse: Testcase 4 failed (did not throw an exception)") except ValueError: pass
def parse_output(self, lines): return parse(lines)