Ejemplo n.º 1
0
    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())
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    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
        )
Ejemplo n.º 4
0
    def test_json_encoding_quiz_multiple_questions(self):
        quiz = Quiz("I am a test quiz.")
        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.")
        quiz.add_question(question)
        q2 = Question("What is this file not for?")
        a_q2 = Answer(False, "A test")
        a2_q2 = Answer(True, "A book")
        a3_q2 = Answer(True, "Nothing special")
        q2.add_answer(a_q2)
        q2.add_answer(a2_q2)
        q2.add_answer(a3_q2)
        quiz.add_question(q2)

        encoding = json.dumps(quiz.to_json(), cls=ComplexEncoder)

        self.assertEqual(
            '{"quiz": "I am a test quiz.", "questions": [{"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."}, {"question": "What is this file not for?", "answers": [{"isCorrect": false, "answer": "A test"}, {"isCorrect": true, "answer": "A book"}, {"isCorrect": true, "answer": "Nothing special"}], "help": ""}]}',
            encoding
        )
Ejemplo n.º 5
0
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)