Ejemplo n.º 1
0
def process_calculator(
        e: TextMessageEventObject,
        output_error: bool = False) -> List[HandledMessageEvent]:
    if e.text and e.text[-1] == "=":
        BotFeatureUsageDataManager.record_usage(BotFeature.TXT_FN_CALCULATOR,
                                                e.channel_oid, e.user_model.id)

        expr = e.text[:-1]

        return calculate_expression(expr, output_error)
    else:
        return []
Ejemplo n.º 2
0
    def test_force_output_error(self):
        expr = "5+"
        result = calculate_expression(expr, output_error=True)

        if not result:
            self.fail("No error output")

        result = result[0]

        self.assertEqual(expr, result.calc_expr,
                         "Calculation expression not match.")
        self.assertFalse(result.latex_available,
                         "LaTeX should not be available.")
        self.assertFalse(result.has_evaluated,
                         "Expression should not be evaluated.")
Ejemplo n.º 3
0
    def test_solve_equation(self):
        expr = "solve(x-1)"
        result = calculate_expression(expr)

        if not result:
            self.fail("No calculation result")

        result = result[0]

        self.assertEqual(expr, result.calc_expr,
                         "Calculation expression not match.")
        self.assertEqual("[1]", result.calc_result,
                         "Calculation result not match.")
        self.assertTrue(result.latex_available, "LaTeX should be available.")
        self.assertFalse(result.has_evaluated,
                         "Expression should not be evaluated.")
Ejemplo n.º 4
0
    def test_multiline(self):
        expr = "a = 7\na + 1"
        result = calculate_expression(expr)

        if not result:
            self.fail("No calculation result")

        result = result[0]

        self.assertEqual("a + 1", result.calc_expr,
                         "Calculation expression not match.")
        self.assertEqual("8", result.calc_result,
                         "Calculation result not match.")
        self.assertFalse(result.latex_available,
                         "LaTeX should not be available.")
        self.assertFalse(result.has_evaluated,
                         "Expression should not be evaluated.")
Ejemplo n.º 5
0
    def test_calculate_rational(self):
        expr = "7/2 + 1"
        result = calculate_expression(expr)

        if not result:
            self.fail("No calculation result")

        result = result[0]

        self.assertEqual(expr, result.calc_expr,
                         "Calculation expression not match.")
        self.assertEqual("9/2", result.calc_result,
                         "Calculation result not match.")
        self.assertTrue(result.latex_available, "LaTeX should be available.")
        self.assertTrue(result.has_evaluated,
                        "Expression should be evaluated.")
        self.assertEqual(4.5, result.evaluated, "Evaluated result not match.")
Ejemplo n.º 6
0
    def test_calculate_float(self):
        expr = "5.5+5"
        result = calculate_expression(expr)

        if not result:
            self.fail("No calculation result")

        result = result[0]

        self.assertEqual(expr, result.calc_expr,
                         "Calculation expression not match.")
        self.assertEqual("10.5", result.calc_result,
                         "Calculation result not match.")
        self.assertFalse(result.latex_available,
                         "LaTeX should not be available.")
        self.assertFalse(result.has_evaluated,
                         "Expression should not be evaluated.")
Ejemplo n.º 7
0
    def test_malformed(self):
        expr = "=_="
        result = calculate_expression(expr)

        if result:
            self.fail(f"Got a calculation result.\n{result[0].to_json()}")
Ejemplo n.º 8
0
def calculator(e: TextMessageEventObject, expr: str):
    return calculate_expression(expr, output_error=True)