MCPcopy Create free account
hub / github.com/catid/supercharger / TestAddMissingColons

Class TestAddMissingColons

codegen/test_missing_colons.py:5–54  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3from fix_ast_errors import fix_ast_errors
4
5class TestAddMissingColons(unittest.TestCase):
6 def check_syntax(self, code_string):
7 try:
8 ast.parse(code_string)
9 return True
10 except SyntaxError:
11 return False
12
13 def run_test_cases(self, test_cases, test_type, syntax_check=True):
14 for i, (input_str, expected_output) in enumerate(test_cases):
15 with self.subTest(input=input_str, expected=expected_output, test_type=test_type):
16 fixed_code = fix_ast_errors(input_str)
17 self.assertEqual(fixed_code, expected_output, msg=f"\n\nFailed test {i} in {test_type}. Expected output:\n\n{expected_output}\n\nGot:\n\n{fixed_code}\n\n")
18 if syntax_check:
19 self.assertTrue(self.check_syntax(fixed_code), msg=f"\n\nFailed test {i} in {test_type}. Syntax checker found a problem with the output: {fixed_code}")
20
21 def test_basic_cases(self):
22 test_cases = [
23 ("def foo(x)\n pass", "def foo(x):\n pass"),
24 ("if x > 0\n pass\nelif x < 0\n pass", "if x > 0:\n pass\nelif x < 0:\n pass"),
25 ("if x > 0\n pass\nelse\n pass", "if x > 0:\n pass\nelse:\n pass"),
26 ("for i in range(10)\n pass", "for i in range(10):\n pass"),
27 ("while True\n pass", "while True:\n pass"),
28 ("with open('file.txt') as f\n pass", "with open('file.txt') as f:\n pass"),
29 ("class MyClass\n def f():\n pass", "class MyClass:\n def f():\n pass"),
30 ("print(f\"The area of a circle with radius {radius} is {area:.2f}\")", "print(f\"The area of a circle with radius {radius} is {area:.2f}\")")
31 ]
32
33 self.run_test_cases(test_cases, "Basic", syntax_check=False)
34
35 def test_single_line(self):
36 test_cases = [
37 ("x = 1\n", "x = 1\n"),
38 ("def func(x):\n pass\n", "def func(x):\n pass\n"),
39 ("if x == 2\n print(x)\n", "if x == 2:\n print(x)\n"),
40 ("for i in range(10)\n print(i)\n", "for i in range(10):\n print(i)\n"),
41 ("while x < 10\n x += 1\n", "while x < 10:\n x += 1\n"),
42 ]
43
44 self.run_test_cases(test_cases, "Single-Line")
45
46 def test_multi_line(self):
47 test_cases = [
48 ("if x == 2\n print(x)\nelse:\n print('Error')\n", "if x == 2:\n print(x)\nelse:\n print('Error')\n"),
49 ('if x > 0: print("x is positive")\nif y < 0: print("y is negative")\n', 'if x > 0: print("x is positive")\nif y < 0: print("y is negative")\n'),
50 ("for i in range(10)\n if i % 2 == 0:\n print(i)\n", "for i in range(10):\n if i % 2 == 0:\n print(i)\n"),
51 ("with open('file.txt') as f\n data = f.read()\n print(data)\n", "with open('file.txt') as f:\n data = f.read()\n print(data)\n")
52 ]
53
54 self.run_test_cases(test_cases, "Multi-Line")
55
56if __name__ == "__main__":
57 unittest.main()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected