Example #1
0
    def test002_formatMenuContainsAllActions(self):
        from main import formatActivityMenu
        actual = formatActivityMenu()
        self.assertTrue(type(actual) is list, 'formatActivityMenu did not return a list. It should return a list of strings.')
        self.assertGreaterEqual(len(actual), 5,
            'You should have at least 5 lines. 1+ for instructions, 4+ for chosen activities.')

        menu = formatActivityMenu()
        options = []
        for line in menu:
            matches = re.findall("\[([a-z0-9]+)\]", line)
            if len(matches) > 0:
                options += matches

        self.assertGreaterEqual(len(options), 4,
            "Not enough options located in your instructions. Each option should contain '[?]' where the value of ? consists of letters (a-z) and/or numbers (0-9).")
    def test002_recordActivityAction_Activity1_UpdatesBalance(self):
        from main import formatActivityMenu

        menu = formatActivityMenu()
        options = []
        for line in menu:
            matches = re.findall("\[([a-z0-9]+)\]", line)
            if len(matches) > 0:
                options += matches

        self.assertGreaterEqual(
            len(options), 4,
            'Unable to find at least 4 options to choose from. Check your formatActivityMenu function'
        )

        self.checkOption(menu, options[0])
    def test006_illegalAction_activityActions(self):
        from main import formatActivityMenu
        from main import applyAction
        from caloric_balance import CaloricBalance
        import re

        menu = formatActivityMenu()
        options = []
        for line in menu:
            matches = re.findall("\[([a-z0-9]+)\]", line)
            if len(matches) > 0:
                options += matches

        self.assertGreaterEqual(4, len(options),
                                'Have you finished formatActivityMenu?')

        for option in options:
            if option not in ['f', 'a', 'q']:
                self.setUp()
                cb = CaloricBalance('f', 23.0, 65.0, 130.0)
                obal = cb.getBalance()
                applyAction(cb, option)
                self.assertEqual(
                    obal, cb.getBalance(),
                    'applyAction should not change the caloric balance.')
                self.assertFalse(
                    self.eat_food_called,
                    "An illegal option (%s) was provided, eatFoodAction should not have been called."
                    % option)
                self.assertFalse(
                    self.record_activity_called,
                    "An illegal option (%s) was provided, recordActivityAction should not have been called."
                    % option)
                self.assertFalse(
                    self.quit_called,
                    "An illegal option (%s) was provided, quitAction should not have been called."
                    % option)
                lines = "".join(self.printed_lines)
                self.assertGreaterEqual(
                    len(self.printed_lines), 1,
                    'You should have printed an error message to the user.\n' +
                    'Your printed lines were:\n%s' % lines)
    def test008_recordActivityAction_badOption(self):
        from main import recordActivityAction
        from main import formatActivityMenu
        from caloric_balance import CaloricBalance

        menu = formatActivityMenu()
        options = []
        for line in menu:
            matches = re.findall("\[([a-z0-9]+)\]", line)
            if len(matches) > 0:
                options += matches

        self.assertGreaterEqual(
            len(options), 4,
            'Unable to find at least 4 options to choose from. Check your formatActivityMenu function'
        )

        # join all the options together to get a bad option
        joinedoption = "".join(options)

        cb = CaloricBalance('f', 23.0, 65.0, 130.0)
        expected = -1417.9
        actual = cb.getBalance()
        self.assertAlmostEqual(
            actual, expected, 2,
            'Your result (%s) is not close enough to (%s)' %
            (actual, expected))

        self.input_response_list = [joinedoption]
        recordActivityAction(cb)

        actual2 = cb.getBalance()
        self.assertEqual(
            actual, actual2,
            'Your recordActivityAction should not have updated the caloric balance, because the user gave a bad option'
        )

        printed_lines = self.printed_lines
        self.assertGreaterEqual(len(printed_lines),
                                len(menu) + 1,
                                'You did not print a message to the user.')