def test_new_game(self): game = HangmanGame(word='moo') for event in ('m', 'o', '-'): game.event(event) self.assertEqual( game.state(), mkstate('moo', 'mo', 'Flawless victory!')) self.assertEqual(game.exit_code, game.DONE_WANTS_NEW)
def test_displaying_word(self): game = HangmanGame('word') game.event('w') game.event('r') board = game.draw_board() _msg, word, _guesses, _prompt, _end = board.split("\n") self.assertEqual(word, "Word: w_r_")
def test_incorrect_guesses(self): game = HangmanGame(word='moo') game.event('f') game.event('g') self.assertFalse(game.won()) self.assertEqual( game.state(), mkstate('moo', 'fg', "Word contains no 'g'. :("))
def test_repeated_guesses(self): game = HangmanGame(word='moo') game.event('f') game.event('f') self.assertFalse(game.won()) self.assertEqual( game.state(), mkstate('moo', 'f', "You've already guessed 'f'."))
def test_easy_game(self): game = HangmanGame(word='moo') game.event('m') game.event('o') self.assertTrue(game.won()) self.assertEqual( game.state(), mkstate('moo', 'mo', 'Flawless victory!'))
def test_new_game(self): game = HangmanGame(word='moo') for event in ('m', 'o', '-'): game.event(event) self.assertEqual(game.state(), mkstate('moo', 'mo', 'Flawless victory!')) self.assertEqual(game.exit_code, game.DONE_WANTS_NEW)
def test_draw_board(self): game = HangmanGame('word') board = game.draw_board() msg, word, guesses, prompt, end = board.split("\n") self.assertEqual(msg, "New game!") self.assertEqual(word, "Word: ____") self.assertEqual(guesses, "Letters guessed so far: ") self.assertEqual(prompt, "Enter next guess (0 to quit):")
def test_draw_board_at_end_of_game(self): game = HangmanGame('m') game.event('m') board = game.draw_board() msg, word, guesses, prompt, end = board.split("\n") self.assertEqual(msg, "Flawless victory!") self.assertEqual(word, "Word: m") self.assertEqual(guesses, "Letters guessed so far: m") self.assertEqual(prompt, "Enter anything to start a new game" " (0 to quit):")
def test_button_mashing(self): game = HangmanGame(word='moo') for event in string.lowercase.replace('o', ''): game.event(event) game.event('o') self.assertTrue(game.won()) self.assertEqual(game.state(), "moo:%s:Button mashing!" % string.lowercase)
def test_from_state_non_ascii(self): game = HangmanGame.from_state( mkstate("b\xc3\xa4r".decode("utf-8"), "xyz", "Eep?")) self.assertEqual(game.word, u"b\u00e4r") self.assertEqual(game.guesses, set("xyz")) self.assertEqual(game.msg, "Eep?") self.assertEqual(game.exit_code, game.NOT_DONE)
def test_displaying_guesses(self): game = HangmanGame('word') game.event('w') board = game.draw_board() msg, _word, _guesses, _prompt, _end = board.split("\n") self.assertEqual(msg, "Word contains at least one 'w'! :D") game.event('w') board = game.draw_board() msg, _word, _guesses, _prompt, _end = board.split("\n") self.assertEqual(msg, "You've already guessed 'w'.") game.event('x') board = game.draw_board() msg, _word, _guesses, _prompt, _end = board.split("\n") self.assertEqual(msg, "Word contains no 'x'. :(")
def test_button_mashing(self): game = HangmanGame(word='moo') for event in string.lowercase.replace('o', ''): game.event(event) game.event('o') self.assertTrue(game.won()) self.assertEqual( game.state(), mkstate('moo', string.lowercase, "Button mashing!"))
def test_garbage_input(self): game = HangmanGame(word="zoo") for garbage in [":", "!", "\x00", "+", "abc", ""]: game.event(garbage) self.assertEqual(game.guesses, set()) game.event('z') game.event('o') self.assertTrue(game.won())
def test_easy_game(self): game = HangmanGame(word='moo') game.event('m') game.event('o') self.assertTrue(game.won()) self.assertEqual(game.state(), mkstate('moo', 'mo', 'Flawless victory!'))
def test_incorrect_guesses(self): game = HangmanGame(word='moo') game.event('f') game.event('g') self.assertFalse(game.won()) self.assertEqual(game.state(), mkstate('moo', 'fg', "Word contains no 'g'. :("))
def test_repeated_guesses(self): game = HangmanGame(word='moo') game.event('f') game.event('f') self.assertFalse(game.won()) self.assertEqual(game.state(), mkstate('moo', 'f', "You've already guessed 'f'."))
def test_from_state(self): game = HangmanGame.from_state("bar:xyz:Eep?") self.assertEqual(game.word, "bar") self.assertEqual(game.guesses, set("xyz")) self.assertEqual(game.msg, "Eep?") self.assertEqual(game.exit_code, game.NOT_DONE)
def test_repeated_guesses(self): game = HangmanGame(word='moo') game.event('f') game.event('f') self.assertFalse(game.won()) self.assertTrue(game.state().startswith("moo:f:You've already"))
def test_incorrect_guesses(self): game = HangmanGame(word='moo') game.event('f') game.event('g') self.assertFalse(game.won()) self.assertTrue(game.state().startswith("moo:fg:Word contains no"))
def test_easy_game(self): game = HangmanGame(word='moo') game.event('m') game.event('o') self.assertTrue(game.won()) self.assertTrue(game.state().startswith("moo:mo:Flawless"))
def test_exit(self): game = HangmanGame('elephant') game.event('0') self.assertEqual(game.exit_code, game.DONE) self.assertEqual(game.draw_board(), "Adieu!")
def test_from_state(self): game = HangmanGame.from_state(mkstate("bar", "xyz", "Eep?")) self.assertEqual(game.word, "bar") self.assertEqual(game.guesses, set("xyz")) self.assertEqual(game.msg, "Eep?") self.assertEqual(game.exit_code, game.NOT_DONE)