def test_parse_start(self): prelines = ["This is just other text before the position or", "movelist that should be thrown out before parsing"] # check handling if no move or board given self.assertRaises(analyze.ParseError, analyze.parse_start, prelines) # check board parsing with and without extra lines before pos = board.parse_short_pos(board.Color.GOLD, 4, basic_pos_short) lines = prelines + basic_pos.splitlines() got_board, start = analyze.parse_start(lines) self.assertEqual(got_board, True) self.assertEqual(start, pos) lines = basic_pos.splitlines() got_board, start = analyze.parse_start(lines) self.assertEqual(got_board, True) self.assertEqual(start, pos) # check movelist parsing lines = prelines + basic_movelist.splitlines() got_board, start = analyze.parse_start(lines) self.assertEqual(got_board, False) self.assertEqual(start, lines[2:]) lines = basic_movelist.splitlines() got_board, start = analyze.parse_start(lines) self.assertEqual(got_board, False) self.assertEqual(start, lines) # check stopping early in a movelist lines = basic_movelist.splitlines() got_board, start = analyze.parse_start(lines, "2s") self.assertEqual(got_board, False) self.assertEqual(start, lines[:3])
def test_parse_start(self): prelines = [ "This is just other text before the position or", "movelist that should be thrown out before parsing" ] # check handling if no move or board given self.assertRaises(analyze.ParseError, analyze.parse_start, prelines) # check board parsing with and without extra lines before pos = board.parse_short_pos(board.Color.GOLD, 4, basic_pos_short) lines = prelines + basic_pos.splitlines() got_board, start = analyze.parse_start(lines) self.assertEqual(got_board, True) self.assertEqual(start, pos) lines = basic_pos.splitlines() got_board, start = analyze.parse_start(lines) self.assertEqual(got_board, True) self.assertEqual(start, pos) # check movelist parsing lines = prelines + basic_movelist.splitlines() got_board, start = analyze.parse_start(lines) self.assertEqual(got_board, False) self.assertEqual(start, lines[2:]) lines = basic_movelist.splitlines() got_board, start = analyze.parse_start(lines) self.assertEqual(got_board, False) self.assertEqual(start, lines) # check stopping early in a movelist lines = basic_movelist.splitlines() got_board, start = analyze.parse_start(lines, "2s") self.assertEqual(got_board, False) self.assertEqual(start, lines[:3])
def setposition(self, side_str, pos_str): side = "gswb".find(side_str) % 2 self.position = board.parse_short_pos(side, 4, pos_str) self.insetup = False
def test_parsing(self): self.assertRaises(ValueError, board.parse_move, " ") directional_move = [(0,8), (8, 9), (9, 1), (1, 0)] self.assertEqual(board.parse_move("Ea1n Ea2e Eb2s Eb1w"), directional_move) self.assertEqual(board.parse_move("Eb2w Cc3x Ea2n"), [(9, 8), (8, 16)]) self.assertRaises(ValueError, board.parse_move, "Ed4d") self.assertRaises(ValueError, board.parse_move, "Ea1") module_pos = board.Position(board.Color.GOLD, 4, board.BASIC_SETUP) basic_setup = ["1g"] + BASIC_SETUP_LONG.splitlines() parsed_move, parsed_pos = board.parse_long_pos(basic_setup) self.assertEqual(parsed_move, 1) self.assertEqual(parsed_pos, module_pos) module_pos = board.Position(board.Color.SILVER, 4, board.BASIC_SETUP) silver_setup = ["Extra", "start", "12s"] + BASIC_SETUP_LONG.splitlines() parsed_move, parsed_pos = board.parse_long_pos(silver_setup) self.assertEqual(parsed_move, 12) self.assertEqual(parsed_pos, module_pos) bad_side = ["1f"] + BASIC_SETUP_LONG.splitlines() self.assertRaises(ValueError, board.parse_long_pos, bad_side) partial_move = ["1g Ra2n"] + BASIC_SETUP_LONG.splitlines() self.assertRaises(NotImplementedError, board.parse_long_pos, partial_move) extra_separation = ["1g", ""] + BASIC_SETUP_LONG.splitlines() self.assertRaises(ValueError, board.parse_long_pos, extra_separation) bad_rank = ["1g"] + BASIC_SETUP_LONG.splitlines() bad_rank[4] = "5| . . x . . x . . |" self.assertRaises(ValueError, board.parse_long_pos, bad_rank) bad_piece = ["1g"] + BASIC_SETUP_LONG.splitlines() bad_piece[3] = "7| d h c f m c h d |" self.assertRaises(ValueError, board.parse_long_pos, bad_piece) extra_moves = ["1g"] + BASIC_SETUP_LONG.splitlines() extra_moves += [ "1g Ee2n Md2n", "1s Ed7s Me7s", "# stop parsing", "2g Ee3s" ] parsed_move, parsed_pos = board.parse_long_pos(extra_moves) self.assertEqual(parsed_move, 2) self.assertEqual(parsed_pos.color, board.Color.GOLD) extra_move_board = [ "2g", " +-----------------+", "8| r r r r r r r r |", "7| d h c c h d |", "6| . . x e m x . . |", "5| . . . . . . . . |", "4| . . . . . . . . |", "3| . . x M E x . . |", "2| D H C C H D |", "1| R R R R R R R R |", " +-----------------+", " a b c d e f g h " ] extra_move_num, extra_move_pos = board.parse_long_pos(extra_move_board) self.assertEqual(parsed_pos, extra_move_pos) self.assertRaises(ValueError, board.parse_short_pos, 3, 4, BASIC_SETUP_SHORT) self.assertRaises(ValueError, board.parse_short_pos, board.Color.GOLD, 5, BASIC_SETUP_SHORT) self.assertRaises(ValueError, board.parse_short_pos, board.Color.GOLD, -1, BASIC_SETUP_SHORT) module_pos = board.Position(board.Color.GOLD, 4, board.BASIC_SETUP) parsed_pos = board.parse_short_pos(board.Color.GOLD, 4, BASIC_SETUP_SHORT) self.assertEqual(parsed_pos, module_pos) bad_piece = BASIC_SETUP_SHORT[:15] + "f" + BASIC_SETUP_SHORT[16:] short_args = (board.Color.GOLD, 4, bad_piece) self.assertRaises(ValueError, board.parse_short_pos, *short_args)
def setposition(self, side_str, pos_str): side = "gswb".find(side_str) % 2 self.position = parse_short_pos(side, 4, pos_str) self.insetup = False