Skip to content

Commit 6567607

Browse files
committed
Add unit tests
1 parent ab46a2c commit 6567607

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
[project]
22
name = "mcp-server-calculator"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
description = "A Model Context Protocol server for calculating"
55
readme = "README.md"
66
requires-python = ">=3.10"
77
authors = [{ name = "He Jie", email = "[email protected]" }]
88
keywords = ["mcp", "llm", "math", "calculator"]
99
license = { text = "MIT" }
10+
urls = { Source = "https://github.com/githejie/mcp-server-calculator" }
1011
classifiers = [
1112
"Development Status :: 4 - Beta",
1213
"Intended Audience :: Developers",

src/mcp_server_calculator/calculator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def eval_expr(node):
2424
return allowed_operators[type(node.op)](left, right)
2525
elif isinstance(node, ast.UnaryOp) and isinstance(node.op, ast.USub):
2626
return -eval_expr(node.operand)
27-
raise ValueError("Unsupported operation")
27+
raise ValueError(f"Unsupported operation: {ast.dump(node)}")
2828

2929
parsed_expr = ast.parse(expression, mode='eval')
3030
result = eval_expr(parsed_expr.body)

tests/test_calculator.py

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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

Comments
 (0)