def test_to_json_question_no_answer_no_help(self): question = Question("My test question") encoding = question.to_json() self.assertDictEqual( dict(question="My test question", answers=[], help=""), encoding)
def test_to_json_question_answer_no_help(self): question = Question("Cookie question") answer = Mock() question.add_answer(answer) encoding = question.to_json() self.assertDictEqual( dict(question="Cookie question", answers=[answer], help=""), encoding)
def test_json_encoding_question_no_answer_help(self): question = Question("What is this file for?") question.add_help("There is no help for you") encoding = json.dumps(question.to_json()) self.assertEqual( '{"question": "What is this file for?", "answers": [], "help": "There is no help for you"}', encoding )
def test_json_encoding_question_answer_help_without_ComplexEncoder(self): question = Question("What is this file for?") answer = Answer(True, "A test") question.add_answer(answer) question.add_help("There is no help for you") self.assertRaises(TypeError, json.dumps, question.to_json())
def test_json_encoding_question_multiple_answers_help(self): question = Question("What is this file for?") a1 = Answer(True, "A test") a2 = Answer(False, "A book") a3 = Answer(False, "Nothing special") question.add_answer(a1) question.add_answer(a2) question.add_answer(a3) question.add_help("There is no help for you.") encoding = json.dumps(question.to_json(), cls=ComplexEncoder) self.assertEqual( '{"question": "What is this file for?", "answers": [{"isCorrect": true, "answer": "A test"}, {"isCorrect": false, "answer": "A book"}, {"isCorrect": false, "answer": "Nothing special"}], "help": "There is no help for you."}', encoding )
def test_json_encoding_question_answer_help_with_ComplexEncoder(self): question = Question("What is this file for?") answer = Answer(True, "A test") question.add_answer(answer) question.add_help("There is no help for you") encoding = json.dumps(question.to_json(), cls=ComplexEncoder) self.assertEqual( '{"question": "What is this file for?", "answers": [{"isCorrect": true, "answer": "A test"}], "help": "There is no help for you"}', encoding )
def parse_file_to_quiz(filename: str): """ Parse a quiz file (.quiz extension) and returns a Quiz object """ ret_value = 0 quiz_file = open(filename, 'r') current_line = quiz_file.readline() quiz: Quiz = None current_question: Question = None is_quiz_correct = True index = 0 while (current_line != ""): current_quiz_line = parse_line_to_quiz_line(current_line) index += 1 if (current_quiz_line.line_type == LineType.INCORRECT_FORMAT): print('⚠️ - Wrong format of %s: line %d: %s' % (filename, index, current_line)) is_quiz_correct = False ret_value = 2 elif (is_quiz_correct): if (current_quiz_line.line_type == LineType.NAME_OF_QUIZ): quiz = Quiz(current_quiz_line.text) elif (current_quiz_line.line_type == LineType.QUESTION): current_question = Question(current_quiz_line.text) quiz.add_question(current_question) elif (current_quiz_line.line_type == LineType.CORRECT_ANSWER): answer = Answer(True, current_quiz_line.text) current_question.add_answer(answer) elif (current_quiz_line.line_type == LineType.INCORRECT_ANSWER): answer = Answer(False, current_quiz_line.text) current_question.add_answer(answer) elif (current_quiz_line.line_type == LineType.HELP): question_help = current_quiz_line.text current_question.add_help(question_help) current_line = quiz_file.readline() quiz_file.close() return (ret_value, quiz)