summaryrefslogtreecommitdiff
path: root/Lib/test
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2018-06-15 11:05:15 +0300
committerGitHub <noreply@github.com>2018-06-15 11:05:15 +0300
commit08f127a3cad8ce4eb281d30d9488c91b0fd7cfed (patch)
treefeddd1fbb4470c5536434572c0fbe12beb0b08d8 /Lib/test
parent9e7c92193cc98fd3c2d4751c87851460a33b9118 (diff)
downloadcpython-git-08f127a3cad8ce4eb281d30d9488c91b0fd7cfed.tar.gz
bpo-33851: Fix ast.get_docstring() for a node that lacks a docstring. (GH-7682)
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_ast.py31
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_ast.py b/Lib/test/test_ast.py
index ab32d9d389..7db40e7797 100644
--- a/Lib/test/test_ast.py
+++ b/Lib/test/test_ast.py
@@ -521,13 +521,44 @@ class ASTHelpers_Test(unittest.TestCase):
)
def test_get_docstring(self):
+ node = ast.parse('"""line one\n line two"""')
+ self.assertEqual(ast.get_docstring(node),
+ 'line one\nline two')
+
+ node = ast.parse('class foo:\n """line one\n line two"""')
+ self.assertEqual(ast.get_docstring(node.body[0]),
+ 'line one\nline two')
+
node = ast.parse('def foo():\n """line one\n line two"""')
self.assertEqual(ast.get_docstring(node.body[0]),
'line one\nline two')
node = ast.parse('async def foo():\n """spam\n ham"""')
self.assertEqual(ast.get_docstring(node.body[0]), 'spam\nham')
+
+ def test_get_docstring_none(self):
self.assertIsNone(ast.get_docstring(ast.parse('')))
+ node = ast.parse('x = "not docstring"')
+ self.assertIsNone(ast.get_docstring(node))
+ node = ast.parse('def foo():\n pass')
+ self.assertIsNone(ast.get_docstring(node))
+
+ node = ast.parse('class foo:\n pass')
+ self.assertIsNone(ast.get_docstring(node.body[0]))
+ node = ast.parse('class foo:\n x = "not docstring"')
+ self.assertIsNone(ast.get_docstring(node.body[0]))
+ node = ast.parse('class foo:\n def bar(self): pass')
+ self.assertIsNone(ast.get_docstring(node.body[0]))
+
+ node = ast.parse('def foo():\n pass')
+ self.assertIsNone(ast.get_docstring(node.body[0]))
+ node = ast.parse('def foo():\n x = "not docstring"')
+ self.assertIsNone(ast.get_docstring(node.body[0]))
+
+ node = ast.parse('async def foo():\n pass')
+ self.assertIsNone(ast.get_docstring(node.body[0]))
+ node = ast.parse('async def foo():\n x = "not docstring"')
+ self.assertIsNone(ast.get_docstring(node.body[0]))
def test_literal_eval(self):
self.assertEqual(ast.literal_eval('[1, 2, 3]'), [1, 2, 3])