|
| 1 | +import unittest |
| 2 | + |
| 3 | +from high_scores import HighScores |
| 4 | + |
| 5 | + |
| 6 | +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.0.0 |
| 7 | + |
| 8 | + |
| 9 | +class HighScoreTest(unittest.TestCase): |
| 10 | + def test_list_of_scores(self): |
| 11 | + scores = [30, 50, 20, 70] |
| 12 | + expected = [30, 50, 20, 70] |
| 13 | + self.assertEqual(HighScores(scores).scores, expected) |
| 14 | + |
| 15 | + def test_latest_score(self): |
| 16 | + scores = [100, 0, 90, 30] |
| 17 | + expected = 30 |
| 18 | + self.assertEqual(HighScores(scores).latest(), expected) |
| 19 | + |
| 20 | + def test_highest_score(self): |
| 21 | + scores = [40, 100, 70] |
| 22 | + expected = 100 |
| 23 | + self.assertEqual(HighScores(scores).highest(), expected) |
| 24 | + |
| 25 | + def test_top_3_scores(self): |
| 26 | + scores = [50, 30, 10] |
| 27 | + expected = [50, 30, 10] |
| 28 | + self.assertEqual(HighScores(scores).top(), expected) |
| 29 | + |
| 30 | + def test_personal_bests_highest_to_lowest(self): |
| 31 | + scores = [20, 10, 30] |
| 32 | + expected = [30, 20, 10] |
| 33 | + self.assertEqual(HighScores(scores).top(), expected) |
| 34 | + |
| 35 | + def test_personal_bests_when_there_is_a_tie(self): |
| 36 | + scores = [40, 20, 40, 30] |
| 37 | + expected = [40, 40, 30] |
| 38 | + self.assertEqual(HighScores(scores).top(), expected) |
| 39 | + |
| 40 | + def test_personal_bests_when_there_are_less_than_3(self): |
| 41 | + scores = [30, 70] |
| 42 | + expected = [70, 30] |
| 43 | + self.assertEqual(HighScores(scores).top(), expected) |
| 44 | + |
| 45 | + def test_personal_bests_when_there_is_only_one(self): |
| 46 | + scores = [40] |
| 47 | + expected = [40] |
| 48 | + self.assertEqual(HighScores(scores).top(), expected) |
| 49 | + |
| 50 | + def test_personal_bests_from_a_long_list(self): |
| 51 | + scores = [10, 30, 90, 30, 100, 20, 10, 0, 30, 40, 40, 70, 70] |
| 52 | + expected = [100, 90, 70] |
| 53 | + self.assertEqual(HighScores(scores).top(), expected) |
| 54 | + |
| 55 | + def test_message_for_new_personal_best(self): |
| 56 | + scores = [20, 40, 0, 30, 70] |
| 57 | + expected = "Your latest score was 70. That's your personal best!" |
| 58 | + self.assertEqual(HighScores(scores).report(), expected) |
| 59 | + |
| 60 | + def test_message_when_latest_score_is_not_the_highest_score(self): |
| 61 | + scores = [20, 100, 0, 30, 70] |
| 62 | + expected = ( |
| 63 | + "Your latest score was 70. That's 30 short of your personal best!" |
| 64 | + ) |
| 65 | + self.assertEqual(HighScores(scores).report(), expected) |
| 66 | + |
| 67 | + def test_message_for_repeated_personal_best(self): |
| 68 | + scores = [20, 70, 50, 70, 30] |
| 69 | + expected = ( |
| 70 | + "Your latest score was 30. That's 40 short of your personal best!" |
| 71 | + ) |
| 72 | + self.assertEqual(HighScores(scores).report(), expected) |
| 73 | + |
| 74 | + |
| 75 | +if __name__ == "__main__": |
| 76 | + unittest.main() |
0 commit comments