Refactor, breaking code into modules.
This commit is contained in:
1 parent
2f14532ef1
commit
0639113205
11 files changed
+312
-178
No files matched your search
@@ -0,0 +1,79 @@
|
||||
#!/usr/bin/python
|
||||
|
||||
import unittest
|
||||
|
||||
import dictionary
|
||||
import game
|
||||
|
||||
|
||||
class TestGame(unittest.TestCase):
|
||||
test_words = (
|
||||
"aaaaa",
|
||||
"aabbb",
|
||||
"aaccc",
|
||||
"bbbaa",
|
||||
"bbbbb",
|
||||
"ccaaa",
|
||||
"cccaa",
|
||||
"ccccc",
|
||||
"abcde",
|
||||
)
|
||||
|
||||
def test_select_word(self):
|
||||
word = game.select_word(self.test_words)
|
||||
|
||||
self.assertIn(word, self.test_words)
|
||||
|
||||
word = game.select_word()
|
||||
|
||||
self.assertIn(word, dictionary.words())
|
||||
|
||||
def test_grade_word_correct(self):
|
||||
target = "aaaaa"
|
||||
guess = "aaaaa"
|
||||
|
||||
result = game.grade_word(target, guess, self.test_words)
|
||||
|
||||
expected = [
|
||||
game.LetterResult.CORRECT,
|
||||
game.LetterResult.CORRECT,
|
||||
game.LetterResult.CORRECT,
|
||||
game.LetterResult.CORRECT,
|
||||
game.LetterResult.CORRECT,
|
||||
]
|
||||
|
||||
self.assertEqual(expected, result)
|
||||
|
||||
self.assertTrue(game.success_result(result))
|
||||
|
||||
def test_grade_word_mixed(self):
|
||||
C = game.LetterResult.CORRECT
|
||||
M = game.LetterResult.MOVE
|
||||
I = game.LetterResult.INCORRECT
|
||||
|
||||
test_cases = (
|
||||
("aabbb", "aabbb", [C, C, C, C, C]),
|
||||
("aabbb", "aaccc", [C, C, I, I, I]),
|
||||
("bbbaa", "cccaa", [I, I, I, C, C]),
|
||||
("aabbb", "cccaa", [I, I, I, M, M]),
|
||||
("aabbb", "bbbaa", [M, M, C, M, M]),
|
||||
("aabbb", "ccaaa", [I, I, M, M, I]),
|
||||
("aabbb", "abcde", [C, M, I, I, I]),
|
||||
("aabbb", "ccccc", [I, I, I, I, I]),
|
||||
)
|
||||
|
||||
for target, guess, expected in test_cases:
|
||||
result = game.grade_word(target, guess, self.test_words)
|
||||
|
||||
self.assertEqual(expected, result)
|
||||
|
||||
def test_grade_word_invalid(self):
|
||||
target = "aaaaa"
|
||||
guess = "inval"
|
||||
|
||||
with self.assertRaises(Exception):
|
||||
game.grade_word(target, guess, self.test_words)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user