|
19 | 19 | requires_debug_ranges, has_no_debug_ranges,
|
20 | 20 | requires_subprocess)
|
21 | 21 | from test.support.os_helper import TESTFN, unlink
|
22 |
| -from test.support.script_helper import assert_python_ok, assert_python_failure |
| 22 | +from test.support.script_helper import assert_python_ok, assert_python_failure, make_script |
23 | 23 | from test.support.import_helper import forget
|
24 | 24 | from test.support import force_not_colorized, force_not_colorized_test_class
|
25 | 25 |
|
@@ -1740,6 +1740,49 @@ def f():
|
1740 | 1740 | ]
|
1741 | 1741 | self.assertEqual(result_lines, expected)
|
1742 | 1742 |
|
| 1743 | +class TestKeywordTypoSuggestions(unittest.TestCase): |
| 1744 | + TYPO_CASES = [ |
| 1745 | + ("with block ad something:\n pass", "and"), |
| 1746 | + ("fur a in b:\n pass", "for"), |
| 1747 | + ("for a in b:\n pass\nelso:\n pass", "else"), |
| 1748 | + ("whille True:\n pass", "while"), |
| 1749 | + ("iff x > 5:\n pass", "if"), |
| 1750 | + ("if x:\n pass\nelseif y:\n pass", "elif"), |
| 1751 | + ("tyo:\n pass\nexcept y:\n pass", "try"), |
| 1752 | + ("classe MyClass:\n pass", "class"), |
| 1753 | + ("impor math", "import"), |
| 1754 | + ("form x import y", "from"), |
| 1755 | + ("defn calculate_sum(a, b):\n return a + b", "def"), |
| 1756 | + ("def foo():\n returm result", "return"), |
| 1757 | + ("lamda x: x ** 2", "lambda"), |
| 1758 | + ("def foo():\n yeld i", "yield"), |
| 1759 | + ("def foo():\n globel counter", "global"), |
| 1760 | + ("frum math import sqrt", "from"), |
| 1761 | + ("asynch def fetch_data():\n pass", "async"), |
| 1762 | + ("async def foo():\n awaid fetch_data()", "await"), |
| 1763 | + ('raisee ValueError("Error")', "raise"), |
| 1764 | + ("[x for x\nin range(3)\nof x]", "if"), |
| 1765 | + ("[123 fur x\nin range(3)\nif x]", "for"), |
| 1766 | + ("for x im n:\n pass", "in"), |
| 1767 | + ] |
| 1768 | + |
| 1769 | + def test_keyword_suggestions_from_file(self): |
| 1770 | + with tempfile.TemporaryDirectory() as script_dir: |
| 1771 | + for i, (code, expected_kw) in enumerate(self.TYPO_CASES): |
| 1772 | + with self.subTest(typo=expected_kw): |
| 1773 | + source = textwrap.dedent(code).strip() |
| 1774 | + script_name = make_script(script_dir, f"script_{i}", source) |
| 1775 | + rc, stdout, stderr = assert_python_failure(script_name) |
| 1776 | + stderr_text = stderr.decode('utf-8') |
| 1777 | + self.assertIn(f"Did you mean '{expected_kw}'", stderr_text) |
| 1778 | + |
| 1779 | + def test_keyword_suggestions_from_command_string(self): |
| 1780 | + for code, expected_kw in self.TYPO_CASES: |
| 1781 | + with self.subTest(typo=expected_kw): |
| 1782 | + source = textwrap.dedent(code).strip() |
| 1783 | + rc, stdout, stderr = assert_python_failure('-c', source) |
| 1784 | + stderr_text = stderr.decode('utf-8') |
| 1785 | + self.assertIn(f"Did you mean '{expected_kw}'", stderr_text) |
1743 | 1786 |
|
1744 | 1787 | @requires_debug_ranges()
|
1745 | 1788 | @force_not_colorized_test_class
|
|
0 commit comments