def test_get_correct_answer_writes_answer_for_both_picture_and_text_questions(self): """ test to check that get_correct_answer_text correctly posts the answer of the argument player's question for both picture and text questions """ game_text_functions.wipe_game_text() text_question_player = {"question": [ "I have rivers, but do not have water. I have dense forests, but no trees and animals. I have cities, but no people live in those cities. What am I?", "A map", "map"]} picture_question_player = { "question": ["/static/img/how-many-balls.jpg", "Can you count the number of balls in picture below?", "30 (16+9+4+1)", "30"]} gameplay_loop.get_correct_answer(text_question_player) all_text = game_text_functions.get_round_text() answer_text = "".join(all_text["answer text"]) answer_should_be = "The correct answer was: A map<br>" self.assertEqual(answer_text, answer_should_be) game_text_functions.wipe_game_text() gameplay_loop.get_correct_answer(picture_question_player) all_text = game_text_functions.get_round_text() answer_text = "".join(all_text["answer text"]) answer_should_be = "The correct answer was: 30 (16+9+4+1)<br>" self.assertEqual(answer_text, answer_should_be)
def test_check_previous_answer_correctly_grades_answer(self): """ test to check that check_previous_player_answer adds 'Correct!' to correct.txt if the players answer contains the questions keyword. If it does not, it should write 'incorrect' """ game_text_functions.wipe_game_text() should_write_incorrect = [{"question": ["What starts with a 'P', ends with an 'E'and has thousands of letters?", "The Post Office", "post"], "answer": ["wrong", "answer"], "previous": True, "incorrect guesses": "", "no": 1}] json_functions.dump_data(should_write_incorrect) gameplay_loop.check_previous_player_answer() with open("active-game-files/correct.txt", "r") as f: result = f.readlines() result_should_be = "<span class='incorrect'>Incorrect</span>" self.assertEqual("".join(result), result_should_be) game_text_functions.wipe_game_text() should_write_incorrect = [{"question": ["What starts with a 'P', ends with an 'E'and has thousands of letters?", "The Post Office", "post"], "answer": ["test", "post"], "previous": True, "incorrect guesses": "", "no": 1}] json_functions.dump_data(should_write_incorrect) gameplay_loop.check_previous_player_answer() with open("active-game-files/correct.txt", "r") as f: result = f.readlines() result_should_be = "<span class='correct'>Correct!</span>" self.assertEqual("".join(result), result_should_be)
def test_ask_question_writes_current_player_question(self): """ test to check that ask_question writes the appropriate question tuple entry for the player with a turn value of True """ game_text_functions.wipe_game_text() test_player_data = [ {"previous": False, "last question correct": True, "no": 1, "incorrect guesses": "", "username": "******", "turn": False, "lives": 2, "question": ["This kind of coat can you put on only when it is wet. What is it?", "A coat of paint", "paint"], "answer": ["a", "paint", "of", "coat"], "score": 5}, {"previous": True, "last question correct": False, "no": 2, "incorrect guesses": "<br>the dictionary", "username": "******", "turn": False, "lives": 1, "question": [ "I am a word. If you pronounce me rightly, it will be wrong. If you pronounce me wrong it is right? What word am I?", "Wrong", "wrong"], "answer": ["the", "dictionary"], "score": 4}, {"previous": False, "last question correct": True, "no": 3, "incorrect guesses": "", "username": "******", "turn": True, "lives": 2, "question": ["It has Eighty-eight keys but can't open a single door? What is it?", "A piano", "piano"], "answer": "", "score": 4}] json_functions.dump_data(test_player_data) gameplay_loop.ask_question() result_should_be = "It has Eighty-eight keys but can't open a single door? What is it?<br>" with open("active-game-files/question.txt", "r") as f: result = f.readlines() self.assertEqual("".join(result), result_should_be)
def render_game(): """ main game loop """ wipe_game_text() game_data = get_json_data() if request.method == "POST": # only runs after form has been submitted once. Won't run first time page is loaded set_new_chosen_and_previous_player() set_previous_answer() was_correct = check_previous_player_answer() update_lives_and_score(was_correct) eliminated_player = eliminate_dead_players() if eliminated_player: get_correct_answer(eliminated_player) if not all_players_gone(): set_player_question() ask_question() game_data = get_json_data() else: # runs the first time the page is loaded, before the users submit any answers. Doesn't run again initialize_used_question() game_data[0]["turn"] = True dump_data(game_data) set_player_question() ask_question() if all_players_gone( ): # adds game over and correct answer text and renders leaderboard template wipe_game_text(True) add_game_over_text() leaderboard_data = get_sorted_scores() round_text = get_round_text() return render_template("leaderboard.html", leaderboard_data=leaderboard_data, answer_text="".join(round_text["answer text"]), game_over_text="".join( round_text["game over text"])) col_size = get_col_size(game_data) col_sm_size = get_col_sm_size(game_data) round_text = get_round_text() game_data = get_json_data() return render_template( "game.html", correct_text="".join(round_text["correct text"]), eliminated_text="".join(round_text["eliminated text"]), answer_text="".join(round_text["answer text"]), host_text="".join(round_text["host text"]), question_text="".join(round_text["question text"]), incorrect_guesses_text="".join(round_text["incorrect guesses text"]), col_size=col_size, col_sm_size=col_sm_size, game_data=game_data)
def test_can_add_host_text(self): """ test to check that add_host_text adds the entered text into host.txt """ game_text_functions.wipe_game_text() test_text = "test text" game_text_functions.add_host_text(test_text) with open("active-game-files/host.txt", "r") as f: result = f.readlines() self.assertEqual("".join(result), "{0}<br>".format(test_text))
def test_game_over_answer_not_wiped(self): """ test to check that answer.txt is not wiped if True is entered as an argument into wipe_game_test() """ sample_text = "sample text" game_text_functions.wipe_game_text() with open("active-game-files/answer.txt", "a") as f: f.writelines(sample_text) game_text_functions.wipe_game_text(True) round_text = game_text_functions.get_round_text() answer_text = "".join(round_text["answer text"]) self.assertEqual(answer_text, sample_text)
def test_elimination_text_correctly_identifies_player(self): """ test to check that add_eliminated_text correctly identifies the username of the player that was eliminated """ game_text_functions.wipe_game_text() test_game_data = [{"no": 1, "lives": 2, "username": "******"}, {"no": 2, "question": ("", "", "", ""), "score": 2, "lives": 0, "username": "******"}, {"no": 3, "lives": 6, "username": "******"}] json_functions.dump_data(test_game_data) json_functions.eliminate_dead_players() with open("active-game-files/eliminated.txt", "r") as f: result = f.readlines() result_should_be = "success has been eliminated" self.assertEqual("".join(result), result_should_be)
def test_can_add_incorrect_guesses(self): """ test to check that add_incorrect_guesses_text writes the incorrect guesses of the player entered as an argument. Guesses should be written to incorrect_guesses.txt """ test_player = {"username": "******", "incorrect guesses": "<br>a wrong answer<br>another incorrect answer"} result_should_be = "<span id='guesses-title'>Incorrect Guesses: </span> <span class='guesses'> <br>a wrong answer<br>another incorrect answer </span>" game_text_functions.wipe_game_text() game_text_functions.add_incorrect_guesses_text(test_player) with open("active-game-files/incorrect_guesses.txt", "r") as f: result = f.readlines() self.assertEqual("".join(result), result_should_be)
def test_wiped_files_content_length_is_zero(self): """ test to check that after calling wipe_files, the length of the content string is 0 """ text = "test text" game_text_functions.add_incorrect_text(text) game_text_functions.add_host_text(text) game_text_functions.add_question_text(text) game_text_functions.add_game_over_text() round_text = game_text_functions.get_round_text() incorrect_text = round_text["correct text"] host_text = round_text["host text"] question_text = round_text["question text"] game_over_text = round_text["game over text"] self.assertTrue(len(incorrect_text) > 0) self.assertTrue(len(host_text) > 0) self.assertTrue(len(question_text) > 0) self.assertTrue(len(game_over_text) > 0) game_text_functions.wipe_game_text() round_text = game_text_functions.get_round_text() incorrect_text = round_text["correct text"] host_text = round_text["host text"] question_text = round_text["question text"] game_over_text = round_text["game over text"] self.assertTrue(len(incorrect_text) == 0) self.assertTrue(len(host_text) == 0) self.assertTrue(len(question_text) == 0) self.assertTrue(len(game_over_text) == 0)