|
| 1 | +import unittest |
| 2 | +from src.mcp_server_calculator.calculator import evaluate |
| 3 | + |
| 4 | +class TestCalculator(unittest.TestCase): |
| 5 | + def test_addition(self): |
| 6 | + self.assertEqual(evaluate("1 + 1"), "2") |
| 7 | + |
| 8 | + def test_subtraction(self): |
| 9 | + self.assertEqual(evaluate("5 - 3"), "2") |
| 10 | + |
| 11 | + def test_multiplication(self): |
| 12 | + self.assertEqual(evaluate("2 * 3"), "6") |
| 13 | + |
| 14 | + def test_division(self): |
| 15 | + self.assertEqual(evaluate("8 / 2"), "4.0") |
| 16 | + |
| 17 | + def test_floor_division(self): |
| 18 | + self.assertEqual(evaluate("7 // 2"), "3") |
| 19 | + |
| 20 | + def test_modulus(self): |
| 21 | + self.assertEqual(evaluate("10 % 3"), "1") |
| 22 | + |
| 23 | + def test_power(self): |
| 24 | + self.assertEqual(evaluate("2 ** 3"), "8") |
| 25 | + |
| 26 | + def test_unary_minus(self): |
| 27 | + self.assertEqual(evaluate("-5"), "-5") |
| 28 | + |
| 29 | + def test_complex_expression(self): |
| 30 | + self.assertEqual(evaluate("2 + 3 * (4 - 1) / 2 ** 2"), "4.25") |
| 31 | + |
| 32 | + def test_parentheses_expression(self): |
| 33 | + self.assertEqual(evaluate("(2 + 3) * 4"), "20") |
| 34 | + |
| 35 | + def test_negative_numbers(self): |
| 36 | + self.assertEqual(evaluate("-2 + 3"), "1") |
| 37 | + self.assertEqual(evaluate("4 * -2"), "-8") |
| 38 | + self.assertEqual(evaluate("-6 / 2"), "-3.0") |
| 39 | + |
| 40 | + def test_floating_point_operations(self): |
| 41 | + self.assertEqual(evaluate("0.5 + 0.25"), "0.75") |
| 42 | + self.assertEqual(evaluate("2.5 * 2"), "5.0") |
| 43 | + self.assertEqual(evaluate("5.0 / 2"), "2.5") |
| 44 | + |
| 45 | + def test_large_numbers(self): |
| 46 | + self.assertEqual(evaluate("123456789 * 987654321"), str(123456789 * 987654321)) |
| 47 | + |
| 48 | + def test_floating_point_precision(self): |
| 49 | + self.assertAlmostEqual(float(evaluate("0.1 + 0.2")), 0.3, places=7) |
| 50 | + |
| 51 | + def test_unsupported_operation(self): |
| 52 | + with self.assertRaises(ValueError): |
| 53 | + evaluate("unknown") |
| 54 | + |
| 55 | + def test_empty_string(self): |
| 56 | + with self.assertRaises(SyntaxError): |
| 57 | + evaluate("") |
| 58 | + |
| 59 | + def test_whitespace_string(self): |
| 60 | + with self.assertRaises(SyntaxError): |
| 61 | + evaluate(" ") |
| 62 | + |
| 63 | + def test_invalid_expression(self): |
| 64 | + with self.assertRaises(SyntaxError): |
| 65 | + evaluate("2 +") |
| 66 | + |
| 67 | + def test_division_by_zero(self): |
| 68 | + with self.assertRaises(ZeroDivisionError): |
| 69 | + evaluate("1 / 0") |
| 70 | + |
| 71 | + def test_floor_division_by_zero(self): |
| 72 | + with self.assertRaises(ZeroDivisionError): |
| 73 | + evaluate("1 // 0") |
| 74 | + |
| 75 | + def test_modulus_by_zero(self): |
| 76 | + with self.assertRaises(ZeroDivisionError): |
| 77 | + evaluate("1 % 0") |
| 78 | + |
| 79 | +if __name__ == '__main__': |
| 80 | + unittest.main() |
0 commit comments