@@ -521,13 +521,44 @@ def test_iter_child_nodes(self):
521
521
)
522
522
523
523
def test_get_docstring (self ):
524
+ node = ast .parse ('"""line one\n line two"""' )
525
+ self .assertEqual (ast .get_docstring (node ),
526
+ 'line one\n line two' )
527
+
528
+ node = ast .parse ('class foo:\n """line one\n line two"""' )
529
+ self .assertEqual (ast .get_docstring (node .body [0 ]),
530
+ 'line one\n line two' )
531
+
524
532
node = ast .parse ('def foo():\n """line one\n line two"""' )
525
533
self .assertEqual (ast .get_docstring (node .body [0 ]),
526
534
'line one\n line two' )
527
535
528
536
node = ast .parse ('async def foo():\n """spam\n ham"""' )
529
537
self .assertEqual (ast .get_docstring (node .body [0 ]), 'spam\n ham' )
538
+
539
+ def test_get_docstring_none (self ):
530
540
self .assertIsNone (ast .get_docstring (ast .parse ('' )))
541
+ node = ast .parse ('x = "not docstring"' )
542
+ self .assertIsNone (ast .get_docstring (node ))
543
+ node = ast .parse ('def foo():\n pass' )
544
+ self .assertIsNone (ast .get_docstring (node ))
545
+
546
+ node = ast .parse ('class foo:\n pass' )
547
+ self .assertIsNone (ast .get_docstring (node .body [0 ]))
548
+ node = ast .parse ('class foo:\n x = "not docstring"' )
549
+ self .assertIsNone (ast .get_docstring (node .body [0 ]))
550
+ node = ast .parse ('class foo:\n def bar(self): pass' )
551
+ self .assertIsNone (ast .get_docstring (node .body [0 ]))
552
+
553
+ node = ast .parse ('def foo():\n pass' )
554
+ self .assertIsNone (ast .get_docstring (node .body [0 ]))
555
+ node = ast .parse ('def foo():\n x = "not docstring"' )
556
+ self .assertIsNone (ast .get_docstring (node .body [0 ]))
557
+
558
+ node = ast .parse ('async def foo():\n pass' )
559
+ self .assertIsNone (ast .get_docstring (node .body [0 ]))
560
+ node = ast .parse ('async def foo():\n x = "not docstring"' )
561
+ self .assertIsNone (ast .get_docstring (node .body [0 ]))
531
562
532
563
def test_literal_eval (self ):
533
564
self .assertEqual (ast .literal_eval ('[1, 2, 3]' ), [1 , 2 , 3 ])
0 commit comments