def test_calculation(): locale_dict = locales['en'] expression = '22 + 4 / (' result = exceptions.catch_calc_errors(lambda: calculate_expr(expression)) print(result) message_text = result_to_message_text(result, locale_dict) return {"ok": True, "message_text": message_text}
def test_catch_parser_exception_normal_float(self): expr = "1.0 + 0.5" tokens_list = parser.tokenize(expr) rpn_list = shunting_yard.infix_to_rpn(tokens_list) result = exceptions.catch_calc_errors( lambda: stack_machine.calculate(rpn_list)) self.assertEqual('success', result['status']) self.assertEqual('1.5', result['result'])
def process_im(locale_dict, message_text): if message_text == "/start": return process_start(locale_dict) elif message_text == "/help": return process_help(locale_dict) else: expression = message_text result = exceptions.catch_calc_errors( lambda: calculate_expr(expression)) return result_to_message_text(result, locale_dict)
def test_catch_division_by_zero(self): rpn_list = [1.0, 0.0, '/'] result = exceptions.catch_calc_errors( lambda: stack_machine.calculate(rpn_list)) self.assertEqual('error', result['status']) self.assertEqual('Division by zero', result['message'])
def test_catch_right_paren_exception(self): expr = "(1 + 2 (- 3) * 4 / 5 (4 (4 ()" tokens_list = parser.tokenize(expr) result = exceptions.catch_calc_errors( lambda: shunting_yard.infix_to_rpn(tokens_list)) self.assertEqual('error', result['status'])
def test_catch_parser_exception(self): expr = "1 + 2 - 3.45 + lkjkl * 4 / 5" result = exceptions.catch_calc_errors(lambda: parser.tokenize(expr)) self.assertEqual('error', result['status'])
def test_catch_calc_exceptions_regular(self): result = exceptions.catch_calc_errors(regular_function) self.assertEqual('success', result['status']) self.assertEqual('calc', result['result'])
def test_catch_calc_exceptions(self): result = exceptions.catch_calc_errors(excepting_function) expression_line = '1 + 2.1 - 23' padding_line = ' ^' expected_location = expression_line + '\n' + padding_line self.assertEqual(expected_location, result['error_location'])