def test_single_word_sections_parse_correctly(self): """Make sure we can have a minimal amount of words in each section.""" contents = '\n'.join([ 'def f(foo):', ' """foobar', '', ' Args:', ' foo: foobar', '', ' Returns:', ' bar', '', ' """', ' return "bar"', ]) function = ast.parse(contents).body[0] docstring = ast.get_docstring(function) doc = Docstring(docstring) self.assertEqual( doc.get_section(Sections.SHORT_DESCRIPTION), 'foobar', ) self.assertEqual( doc.get_section(Sections.RETURNS_SECTION), 'Returns:\n bar', ) self.assertEqual( doc.get_items(Sections.ARGUMENTS_SECTION), ['foo'], )
def test_parses_long_description(self): """Make sure we can parse the long description. The long description will include newlines. """ func = '\n'.join([ 'def this_function_has_a_long_description(arg1):', ' """Return the arg, unchanged.', '', ' This function returns the arg, unchanged. There is', ' no particular reason, but this is a good place to check to ', ' see that long descriptions are being parsed correctly. ', ' If they are, I\'m not sure why. There is some magic ', ' going on here, in fact.', '', ' Args:', ' arg1: The value returned.', '', ' Returns:', ' The original argument, unchanged.', '', ' """', ' return arg1', ]) doc = ast.get_docstring(ast.parse(func).body[0]) docstring = Docstring(doc) long_description = docstring.get_section(Sections.LONG_DESCRIPTION) self.assertTrue( long_description.startswith('This function returns'), 'Expected long description to start with "This function returns" ' 'but was {}'.format(repr(long_description[:20])))
def test_can_parse_raises(self): """Make sure we can parse the raises section.""" docstring = '\n'.join([ 'This has a problem.', '', 'Raises:', ' Exception: An exception for generic reasons.', ]) doc = Docstring(docstring) self.assertTrue( 'Exception' in doc.get_section(Sections.RAISES_SECTION))
def test_can_parse_yields(self): """Make sure we can parse the yields section.""" docstring = '\n'.join([ 'Some sort of short description.', '', 'A longer description.', '', 'Yields:', ' The number 5. Always.', ]) doc = Docstring(docstring) self.assertTrue(len(doc.get_section(Sections.YIELDS_SECTION)) > 0)