aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Lib/test/test_exceptions.py
diff options
context:
space:
mode:
authorNikita Sobolev <mail@sobolevn.me>2023-08-16 13:35:38 +0300
committerGitHub <noreply@github.com>2023-08-16 11:35:38 +0100
commitbdd8ddfda166d1ed49744d61dcc486d62a9ac890 (patch)
tree6a168ac0ae042454e8dce5bf457dc45bcb47d0a2 /Lib/test/test_exceptions.py
parentfd9d70a94de5b0756b52b9ae21e236e25545db4f (diff)
downloadcpython-bdd8ddfda166d1ed49744d61dcc486d62a9ac890.tar.gz
cpython-bdd8ddfda166d1ed49744d61dcc486d62a9ac890.zip
gh-105724: Add location information to `assert` errors (GH-105935)
Diffstat (limited to 'Lib/test/test_exceptions.py')
-rw-r--r--Lib/test/test_exceptions.py117
1 files changed, 117 insertions, 0 deletions
diff --git a/Lib/test/test_exceptions.py b/Lib/test/test_exceptions.py
index f3554f1c4bb..764122ed4ef 100644
--- a/Lib/test/test_exceptions.py
+++ b/Lib/test/test_exceptions.py
@@ -1931,6 +1931,123 @@ class ImportErrorTests(unittest.TestCase):
self.assertEqual(exc.name, orig.name)
self.assertEqual(exc.path, orig.path)
+
+class AssertionErrorTests(unittest.TestCase):
+ def tearDown(self):
+ unlink(TESTFN)
+
+ def write_source(self, source):
+ with open(TESTFN, 'w') as testfile:
+ testfile.write(dedent(source))
+ _rc, _out, err = script_helper.assert_python_failure('-Wd', '-X', 'utf8', TESTFN)
+ return err.decode('utf-8').splitlines()
+
+ def test_assertion_error_location(self):
+ cases = [
+ ('assert None',
+ [
+ ' assert None',
+ ' ^^^^',
+ 'AssertionError',
+ ],
+ ),
+ ('assert 0',
+ [
+ ' assert 0',
+ ' ^',
+ 'AssertionError',
+ ],
+ ),
+ ('assert 1 > 2',
+ [
+ ' assert 1 > 2',
+ ' ^^^^^',
+ 'AssertionError',
+ ],
+ ),
+ ('assert 1 > 2 and 3 > 2',
+ [
+ ' assert 1 > 2 and 3 > 2',
+ ' ^^^^^^^^^^^^^^^',
+ 'AssertionError',
+ ],
+ ),
+ ('assert 1 > 2, "message"',
+ [
+ ' assert 1 > 2, "message"',
+ ' ^^^^^',
+ 'AssertionError: message',
+ ],
+ ),
+
+ # Multiline:
+ ("""
+ assert (
+ 1 > 2)
+ """,
+ [
+ ' 1 > 2)',
+ ' ^^^^^',
+ 'AssertionError',
+ ],
+ ),
+ ("""
+ assert (
+ 1 > 2), "Message"
+ """,
+ [
+ ' 1 > 2), "Message"',
+ ' ^^^^^',
+ 'AssertionError: Message',
+ ],
+ ),
+ ("""
+ assert (
+ 1 > 2), \\
+ "Message"
+ """,
+ [
+ ' 1 > 2), \\',
+ ' ^^^^^',
+ 'AssertionError: Message',
+ ],
+ ),
+ ]
+ for source, expected in cases:
+ with self.subTest(source):
+ result = self.write_source(source)
+ self.assertEqual(result[-3:], expected)
+
+ def test_multiline_not_highlighted(self):
+ cases = [
+ ("""
+ assert (
+ 1 > 2
+ )
+ """,
+ [
+ ' 1 > 2',
+ 'AssertionError',
+ ],
+ ),
+ ("""
+ assert (
+ 1 < 2 and
+ 3 > 4
+ )
+ """,
+ [
+ ' 1 < 2 and',
+ 'AssertionError',
+ ],
+ ),
+ ]
+ for source, expected in cases:
+ with self.subTest(source):
+ result = self.write_source(source)
+ self.assertEqual(result[-2:], expected)
+
+
class SyntaxErrorTests(unittest.TestCase):
def test_range_of_offsets(self):
cases = [