| 1 | import unittest |
| 2 | |
| 3 | import chess |
| 4 | |
| 5 | from schach import clipboard, savegames |
| 6 | |
| 7 | ITALIAN = """[Event "Club"] |
| 8 | [White "Florian"] |
| 9 | [Black "Max"] |
| 10 | [Result "1-0"] |
| 11 | |
| 12 | 1. e4 e5 2. Nf3 Nc6 3. Bc4 Bc5 1-0 |
| 13 | """ |
| 14 | |
| 15 | |
| 16 | class ParseTest(unittest.TestCase): |
| 17 | def test_empty_and_noise(self): |
| 18 | self.assertIsNone(clipboard.parse("")) |
| 19 | self.assertIsNone(clipboard.parse("Hallo Welt")) |
| 20 | self.assertIsNone(clipboard.parse("https://lichess.org/training")) |
| 21 | |
| 22 | def test_fen(self): |
| 23 | fen = "r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3" |
| 24 | pasted = clipboard.parse(fen) |
| 25 | self.assertEqual(pasted.kind, "fen") |
| 26 | self.assertEqual(pasted.state.board.fen(), fen) |
| 27 | self.assertEqual(pasted.state.human, chess.WHITE) |
| 28 | self.assertEqual(pasted.state.mode, "maia") |
| 29 | |
| 30 | def test_placement_only_fen_means_white_to_move(self): |
| 31 | pasted = clipboard.parse("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR") |
| 32 | self.assertEqual(pasted.kind, "fen") |
| 33 | self.assertEqual(pasted.state.board.turn, chess.WHITE) |
| 34 | |
| 35 | def test_invalid_fen_is_rejected(self): |
| 36 | self.assertIsNone(clipboard.parse("8/8/8/8/8/8/8/8 w - - 0 1")) # keine Könige |
| 37 | |
| 38 | def test_foreign_pgn_keeps_variations(self): |
| 39 | pasted = clipboard.parse('[White "A"]\n[Black "B"]\n\n1. e4 e5 (1... c5 {Sizilianisch} 2. Nf3) 2. Nf3 *') |
| 40 | (move, comment, shapes, children, _name), = pasted.state.variations[0] |
| 41 | self.assertEqual((move, comment, shapes), (chess.Move.from_uci("c7c5"), "Sizilianisch", ())) |
| 42 | self.assertEqual(children[0][0], chess.Move.from_uci("g1f3")) |
| 43 | |
| 44 | def test_foreign_pgn_keeps_names(self): |
| 45 | pasted = clipboard.parse(ITALIAN) |
| 46 | self.assertEqual(pasted.kind, "pgn") |
| 47 | state = pasted.state |
| 48 | self.assertEqual(state.mode, "review") |
| 49 | self.assertEqual(state.names, ("Florian", "Max")) |
| 50 | self.assertEqual(len(state.board.move_stack), 6) |
| 51 | self.assertEqual(state.final_text, "Weiß hat gewonnen.") |
| 52 | self.assertFalse(pasted.more_games) |
| 53 | |
| 54 | def test_own_lichess_name_picks_side(self): |
| 55 | state = clipboard.parse(ITALIAN, username="max").state |
| 56 | self.assertEqual(state.human, chess.BLACK) |
| 57 | self.assertEqual(state.opponent, "Florian") |
| 58 | self.assertEqual(state.names, ()) |
| 59 | |
| 60 | def test_bare_movetext(self): |
| 61 | pasted = clipboard.parse("1. d4 d5 2. c4") |
| 62 | self.assertEqual(pasted.kind, "pgn") |
| 63 | self.assertEqual(len(pasted.state.board.move_stack), 3) |
| 64 | |
| 65 | def test_multiple_games(self): |
| 66 | self.assertTrue(clipboard.parse(ITALIAN + "\n" + ITALIAN).more_games) |
| 67 | |
| 68 | def test_roundtrip_own_pgn(self): |
| 69 | board = chess.Board() |
| 70 | for san in ("e4", "e5", "Qh5", "Nc6"): |
| 71 | board.push_san(san) |
| 72 | state = savegames.GameState(board=board, human=chess.BLACK, elo=900, symbols={2: "?!"}, evals={3: 61.0}) |
| 73 | pasted = clipboard.parse(savegames.to_pgn(state)) |
| 74 | self.assertEqual(pasted.state.human, chess.BLACK) |
| 75 | self.assertEqual(pasted.state.symbols, {2: "?!"}) |
| 76 | self.assertEqual(pasted.state.evals, {3: 61.0}) |
| 77 | self.assertEqual(pasted.state.mode, "maia") |
| 78 | |
| 79 | def test_foreign_names_roundtrip(self): |
| 80 | state = clipboard.parse(ITALIAN).state |
| 81 | again = savegames.from_pgn(savegames.to_pgn(state)) |
| 82 | self.assertEqual(again.names, ("Florian", "Max")) |
| 83 | |
| 84 | def test_lichess_links(self): |
| 85 | for text, expected in [ |
| 86 | ("https://lichess.org/abcd1234", ("abcd1234", None)), |
| 87 | ("lichess.org/abcd1234/black", ("abcd1234", chess.BLACK)), |
| 88 | ("https://lichess.org/abcd1234wxyz", ("abcd1234", None)), |
| 89 | ("https://lichess.org/abcd1234#12", ("abcd1234", None)), |
| 90 | ]: |
| 91 | pasted = clipboard.parse(text) |
| 92 | self.assertEqual(pasted.kind, "lichess", text) |
| 93 | self.assertEqual((pasted.game_id, pasted.orientation), expected, text) |
| 94 | |
| 95 | |
| 96 | if __name__ == "__main__": |
| 97 | unittest.main() |