"""
Do Not Edit this file. You may and are encouraged to look at it for reference.
"""

import sys
if sys.version_info.major != 3:
    print('You must use Python 3.x version to run this unit test')
    sys.exit(1)

import unittest

import main


class TestCreateCaloricBalance(unittest.TestCase):
    def input_replacement(self, prompt):
        self.assertFalse(self.too_many_inputs)
        self.input_given_prompt = prompt
        r = self.input_response_list[self.input_response_index]
        self.input_response_index += 1
        if self.input_response_index >= len(self.input_response_list):
            self.input_response_index = 0
            self.too_many_inputs = True
        return r


    def print_replacement(self, *text, **kwargs):
        line = " ".join(text) + "\n"
        self.printed_lines.append(line)
        return


    def setUp(self):
        self.too_many_inputs = False
        self.input_given_prompt = None
        self.input_response_index = 0
        self.input_response_list = [""]
        main.input = self.input_replacement

        self.printed_lines = []
        main.print = self.print_replacement
        return


    def test001_createCaloricBalanceExists(self):
        self.assertTrue('createCaloricBalance' in dir(main),
                        'Function "createCaloricBalance" is not defined, check your spelling')


    def test002_createsCaloricBalanceInstance(self):
        from main import createCaloricBalance
        from caloric_balance import CaloricBalance

        gender = 'f'
        age = '23'
        height = '65'
        weight = '130'

        self.input_response_list = [gender + "\n", age + "\n", height + "\n", weight + "\n"]
        cb = createCaloricBalance()
        self.assertTrue(isinstance(cb, CaloricBalance), 'The returned value is not an instance of CaloricBalance')


    def test003_createsCaloricBalanceCheckInstance(self):
        from main import createCaloricBalance

        gender = 'f'
        age = '23'
        height = '65'
        weight = '130'

        self.input_response_list = [
            gender + "\n",
            age + "\n",
            height + "\n",
            weight + "\n"
        ]
        cb = createCaloricBalance()

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


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

    def test004_createsCaloricBalanceBadInput(self):
        from main import createCaloricBalance

        gender = 'f'
        age = '23'
        height = '65'
        weight = '130'

        self.input_response_list = [
            "   ", "  \t\n", gender,
            "0", "-17", "zero", age,
            "   ", "  \t\n", "0", "-17", "zero", height,
            "   ", "  \t\n", "0", "-17", "zero", weight
        ]

        cb = createCaloricBalance()

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

        expected = 1417.9
        actual = cb.getBMR(gender, float(age), float(height), float(weight))
        self.assertAlmostEqual(actual, expected, 2,
            'Your result (%s) is not close enough to (%s)' % (actual, expected))

    def test005_createsCaloricBalanceMale(self):
        from main import createCaloricBalance

        gender = 'm'
        age = '26'
        height = '70.5'
        weight = '185'

        self.input_response_list = [
            "   ", "  \t\n", gender,
            "0", "-17", "zero", age,
            "   ", "  \t\n", "0", "-17", "zero", height,
            "   ", "  \t\n", "0", "-17", "zero", weight
        ]

        cb = createCaloricBalance()

        expected = -1937.1
        actual = cb.getBalance()
        self.assertAlmostEqual(actual, expected, 2,
            'Your result (%s) is not close enough to (%s)' % (actual, expected))

        expected = 1937.1
        actual = cb.getBMR(gender, float(age), float(height), float(weight))
        self.assertAlmostEqual(actual, expected, 2,
            'Your result (%s) is not close enough to (%s)' % (actual, expected))

    def test006_createsCaloricBalanceBadGenderOption(self):
        from main import createCaloricBalance

        gender = 'x'
        age = '26'
        height = '70.5'
        weight = '185'

        self.input_response_list = [
            "   ", "  \t\n", gender,
            "0", "-17", "zero", age,
            "   ", "  \t\n", "0", "-17", "zero", height,
            "   ", "  \t\n", "0", "-17", "zero", weight
        ]

        cb = createCaloricBalance()

        expected = 0.0
        actual = cb.getBalance()
        self.assertEqual(actual, expected,
            'Your result (%s) is not equal to (%s)' % (actual, expected))

        expected = 0.0
        actual = cb.getBMR(gender, float(age), float(height), float(weight))
        self.assertEqual(actual, expected,
            'Your result (%s) is not equal to (%s)' % (actual, expected))

if __name__ == '__main__':
    unittest.main()
