Outline: 11 definitions
- NullBoardTest class, line 13
- test_null_board_switches_side function, line 14
- test_check_threat_points_from_checker_to_king function, line 21
- ThreatJobTest class, line 27
- setUpClass function, line 29
- tearDownClass function, line 34
- run_job function, line 37
- test_stockfish_finds_mate_threat function, line 40
- test_quiet_start_has_no_threat function, line 43
- test_candidates_decide_what_is_seen function, line 46
- test_in_check_returns_checker function, line 52
| 1 | import shutil |
| 2 | import unittest |
| 3 | |
| 4 | import chess |
| 5 | import chess.engine |
| 6 | |
| 7 | from schach import threat |
| 8 | |
| 9 | # 1.e4 e5 2.Bc4 Nc6 3.Qh5 – Schwarz am Zug, Weiß droht Dxf7# |
| 10 | SCHOLAR = "r1bqkbnr/pppp1ppp/2n5/4p2Q/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 3 3" |
| 11 | |
| 12 | |
| 13 | class NullBoardTest(unittest.TestCase): |
| 14 | def test_null_board_switches_side(self): |
| 15 | board = chess.Board(SCHOLAR) |
| 16 | nb = threat.null_board(board) |
| 17 | self.assertEqual(nb.turn, chess.WHITE) |
| 18 | self.assertFalse(nb.move_stack) |
| 19 | self.assertIn(chess.Move.from_uci("h5f7"), nb.legal_moves) |
| 20 | |
| 21 | def test_check_threat_points_from_checker_to_king(self): |
| 22 | board = chess.Board("rnb1kbnr/pppp1ppp/8/4p3/6Pq/5P2/PPPPP2P/RNBQKBNR w KQkq - 1 3") |
| 23 | self.assertEqual(threat.check_threat(board), chess.Move.from_uci("h4e1")) |
| 24 | |
| 25 | |
| 26 | @unittest.skipUnless(shutil.which("stockfish"), "stockfish nicht installiert") |
| 27 | class ThreatJobTest(unittest.TestCase): |
| 28 | @classmethod |
| 29 | def setUpClass(cls): |
| 30 | cls.engine = chess.engine.SimpleEngine.popen_uci("stockfish") |
| 31 | cls.engine.configure({"Threads": 2, "Hash": 32}) |
| 32 | |
| 33 | @classmethod |
| 34 | def tearDownClass(cls): |
| 35 | cls.engine.quit() |
| 36 | |
| 37 | def run_job(self, fen, candidates=None): |
| 38 | return threat.threat_job(chess.Board(fen), candidates)(self.engine) |
| 39 | |
| 40 | def test_stockfish_finds_mate_threat(self): |
| 41 | self.assertEqual(self.run_job(SCHOLAR), chess.Move.from_uci("h5f7")) |
| 42 | |
| 43 | def test_quiet_start_has_no_threat(self): |
| 44 | self.assertIsNone(self.run_job(chess.STARTING_FEN)) |
| 45 | |
| 46 | def test_candidates_decide_what_is_seen(self): |
| 47 | quiet = [chess.Move.from_uci("a2a3"), chess.Move.from_uci("h2h3")] |
| 48 | self.assertIsNone(self.run_job(SCHOLAR, quiet)) # der „Gegner“ sieht das Matt nicht |
| 49 | seen = quiet + [chess.Move.from_uci("h5f7")] |
| 50 | self.assertEqual(self.run_job(SCHOLAR, seen), chess.Move.from_uci("h5f7")) |
| 51 | |
| 52 | def test_in_check_returns_checker(self): |
| 53 | fen = "rnb1kbnr/pppp1ppp/8/4p3/6Pq/5P2/PPPPP2P/RNBQKBNR w KQkq - 1 3" |
| 54 | self.assertEqual(self.run_job(fen), chess.Move.from_uci("h4e1")) |
| 55 | |
| 56 | |
| 57 | if __name__ == "__main__": |
| 58 | unittest.main() |