Example #1
0
    def test_choices_update_options_with_scheme(self):
        scheme = Scheme(choices=["Cat", "Dog", "Fish"])
        question = MultipleChoiceQuestion(
            "What?", choices=["Chicken"], scheme=scheme
        )

        self.assertEqual(
            [
                Option(value="a", expression="Chicken"),
                Option(value="b", expression="Cat"),
                Option(value="c", expression="Dog"),
                Option(value="d", expression="Fish"),
            ],
            question.get_options(),
        )

        # External update
        new_scheme = Scheme(choices=["Cow"])
        question.update_scheme(new_scheme)

        self.assertEqual(
            [
                Option(value="a", expression="Chicken"),
                Option(value="b", expression="Cat"),
                Option(value="c", expression="Dog"),
                Option(value="d", expression="Fish"),
                Option(value="e", expression="Cow"),
            ],
            question.get_options(),
        )

        self.assertEqual([], question.primitive_options)
Example #2
0
    def test_has_correct_answer_option(self, *_):
        options = [
            Option(value="A", expression="Something"),
            Option(value="B", expression="Other Something"),
        ]
        question = Question("What?", options=options, correct_answers=["A"])

        question.ask()
        self.assertFalse(question.has_correct_answer)

        question.ask()
        self.assertTrue(question.has_correct_answer)
Example #3
0
    def test_choices_combined_with_options(self):
        question = MultipleChoiceQuestion(
            "What?",
            choices=["Hello", "World"],
            options=[Option(value="Hello", expression="World")],
        )

        self.assertEqual(
            [
                Option(value="a", expression="Hello"),
                Option(value="b", expression="World"),
                Option(value="Hello", expression="World"),
            ],
            question.get_options(),
        )
Example #4
0
    def test_option_validation(self, mock_stdout, *_):
        question = Question(
            "What?",
            options=[
                Option(value="A", expression="Something"),
                Option(value="B", expression="Other Something"),
            ],
        )

        question.ask()

        mock_stdout.assert_called_with(
            "\nThe selected option is not valid. Available options are: "
            "\nA) Something\nB) Other Something\n"
        )

        self.assertEqual(2, question.attempt)
Example #5
0
    def test_default_instance_attribute_values(self):
        question = MultipleChoiceQuestion(
            "What?", options=[Option(value="Hello", expression="World")]
        )

        self.assertEqual([], question.choices)
        self.assertEqual("horizontal", question.display)
        self.assertEqual("letter", question.style)
        self.assertIsNone(question.style_iterator)
Example #6
0
    def test_question_pre(self):
        question = Question("What?")
        Quiz(questions=[question])

        self.assertEqual(
            "* Question 1/1. [No answer]\n", question.get_question_pre()
        )

        question.answer = "Good answer"
        self.assertEqual(
            "* Question 1/1. [Good answer]\n", question.get_question_pre()
        )

        question.answer = Option(value="A", expression="Best answer.")
        self.assertEqual(
            "* Question 1/1. [A) Best answer.]\n", question.get_question_pre()
        )
Example #7
0
    def test_answer_is_set_as_option(self, *_):
        option = Option(value="A", expression="Hello")
        question = Question("What?", options=[option])

        question.ask()
        self.assertEqual(option, question.answer)
Example #8
0
    def test_choices_combined_with_options_with_scheme(self):
        scheme = Scheme(options=[Option(value="Cat", expression="Meow")])

        question = MultipleChoiceQuestion(
            "What?",
            choices=["Hello", "World"],
            options=[Option(value="Hello", expression="World")],
            scheme=scheme,
        )

        self.assertEqual(
            [
                Option(value="a", expression="Hello"),
                Option(value="b", expression="World"),
                Option(value="Hello", expression="World"),
                Option(value="Cat", expression="Meow"),
            ],
            question.get_options(),
        )

        self.assertEqual(
            [
                Option(value="Hello", expression="World"),
                Option(value="Cat", expression="Meow"),
            ],
            question.primitive_options,
        )

        # External update
        new_scheme = Scheme(options=[Option(value="Dog", expression="Bark")])
        question.update_scheme(new_scheme)

        self.assertEqual(
            [
                Option(value="Hello", expression="World"),
                Option(value="Cat", expression="Meow"),
                Option(value="Dog", expression="Bark"),
            ],
            question.primitive_options,
        )

        self.assertEqual(
            [
                Option(value="a", expression="Hello"),
                Option(value="b", expression="World"),
                Option(value="Hello", expression="World"),
                Option(value="Cat", expression="Meow"),
                Option(value="Dog", expression="Bark"),
            ],
            question.get_options(),
        )