Outline: 11 definitions
- FakeTime class, line 8
- __init__ function, line 9
- __call__ function, line 12
- ClockTest class, line 16
- setUp function, line 17
- test_starts_with_first_move function, line 21
- test_running_and_increment function, line 28
- test_pause_and_start function, line 36
- test_flag function, line 46
- test_set_turn_and_stop function, line 52
- test_time_control_lookup function, line 64
| 1 | import unittest |
| 2 | |
| 3 | import chess |
| 4 | |
| 5 | from schach.clock import GameClock, time_control |
| 6 | |
| 7 | |
| 8 | class FakeTime: |
| 9 | def __init__(self): |
| 10 | self.t = 100.0 |
| 11 | |
| 12 | def __call__(self): |
| 13 | return self.t |
| 14 | |
| 15 | |
| 16 | class ClockTest(unittest.TestCase): |
| 17 | def setUp(self): |
| 18 | self.time = FakeTime() |
| 19 | self.clock = GameClock(5, 3, now=self.time) |
| 20 | |
| 21 | def test_starts_with_first_move(self): |
| 22 | self.time.t += 10 |
| 23 | self.assertEqual(self.clock.remaining()[chess.WHITE], 300000) # läuft noch nicht |
| 24 | self.clock.moved(chess.WHITE) |
| 25 | self.assertEqual(self.clock.running, chess.BLACK) |
| 26 | self.assertEqual(self.clock.remaining()[chess.WHITE], 300000) # erster Zug ohne Zuschlag |
| 27 | |
| 28 | def test_running_and_increment(self): |
| 29 | self.clock.moved(chess.WHITE) |
| 30 | self.time.t += 20 |
| 31 | self.assertEqual(self.clock.remaining()[chess.BLACK], 280000) |
| 32 | self.clock.moved(chess.BLACK) |
| 33 | self.assertEqual(self.clock.left[chess.BLACK], 283000) |
| 34 | self.assertEqual(self.clock.running, chess.WHITE) |
| 35 | |
| 36 | def test_pause_and_start(self): |
| 37 | self.clock.moved(chess.WHITE) |
| 38 | self.time.t += 10 |
| 39 | self.clock.stop() # „Warum?“-Pause |
| 40 | self.time.t += 60 |
| 41 | self.assertEqual(self.clock.remaining()[chess.BLACK], 290000) |
| 42 | self.clock.start(chess.BLACK) |
| 43 | self.time.t += 5 |
| 44 | self.assertEqual(self.clock.remaining()[chess.BLACK], 285000) |
| 45 | |
| 46 | def test_flag(self): |
| 47 | self.clock.moved(chess.WHITE) |
| 48 | self.assertIsNone(self.clock.flagged()) |
| 49 | self.time.t += 301 |
| 50 | self.assertEqual(self.clock.flagged(), chess.BLACK) |
| 51 | |
| 52 | def test_set_turn_and_stop(self): |
| 53 | self.clock.moved(chess.WHITE) |
| 54 | self.time.t += 5 |
| 55 | self.clock.set_turn(chess.WHITE) |
| 56 | self.time.t += 5 |
| 57 | left = self.clock.remaining() |
| 58 | self.assertEqual(left[chess.BLACK], 295000) |
| 59 | self.assertEqual(left[chess.WHITE], 295000) |
| 60 | self.clock.stop() |
| 61 | self.time.t += 100 |
| 62 | self.assertEqual(self.clock.remaining(), {chess.WHITE: 295000, chess.BLACK: 295000}) |
| 63 | |
| 64 | def test_time_control_lookup(self): |
| 65 | self.assertEqual(time_control("10 + 5"), ("10 + 5", 10, 5)) |
| 66 | self.assertEqual(time_control("unbekannt")[1], None) |
| 67 | |
| 68 | |
| 69 | if __name__ == "__main__": |
| 70 | unittest.main() |