Permalink
Browse files

DocumentationComment: Add assemble()

This function takes in a ``DocumentationComment`` instance and
assembles it to a documentation comment as a string.

To have tests on all corner cases, documentation styles in
default.py has changed.

Also, a default.c is created to test corner cases, for a docstyle
having a different marker. (i.e. marker[1] != "")
  • Loading branch information...
SanketDG committed Jul 30, 2016
1 parent dc35a0a commit 78ff315930ca4029a9025cfcc2db633723c48c27
@@ -197,3 +197,22 @@ def from_metadata(cls, doccomment, docstyle_definition,
return DocumentationComment(assembled_doc, docstyle_definition, indent, return DocumentationComment(assembled_doc, docstyle_definition, indent,
marker, range) marker, range)
def assemble(self):
"""
Assembles parsed documentation to the original documentation.
This function assembles the whole documentation comment, with the
given markers and indentation.
"""
lines = self.documentation.splitlines(keepends=True)
assembled = self.indent + self.marker[0]
if len(lines) == 0:
return self.marker[0] + self.marker[2]
assembled += lines[0]
assembled += ''.join('\n' if line == '\n' and not self.marker[1]
else self.indent + self.marker[1] + line
for line in lines[1:])
return (assembled +
(self.indent if lines[-1][-1] == '\n' else '') +
self.marker[2])
@@ -132,8 +132,8 @@ def test_python_default(self):
'Some more foobar-like text.\n')], 'Some more foobar-like text.\n')],
[self.Description(desc='\nA nice and neat way of ' [self.Description(desc='\nA nice and neat way of '
'documenting code.\n'), 'documenting code.\n'),
self.Parameter(name='radius', desc=' The explosion radius.\n')], self.Parameter(name='radius', desc=' The explosion radius. ')],
[self.Description(desc='\nA function that returns 55.\n')], [self.Description(desc='A function that returns 55.')],
[self.Description(desc='\nDocstring with layouted text.\n\n ' [self.Description(desc='\nDocstring with layouted text.\n\n '
'layouts inside docs are preserved.' 'layouts inside docs are preserved.'
'\nthis is intended.\n')], '\nthis is intended.\n')],
@@ -192,3 +192,20 @@ def test_java_default(self):
desc=' the concatenated string\n')]] desc=' the concatenated string\n')]]
self.assertEqual(expected, parsed_docs) self.assertEqual(expected, parsed_docs)
class DocumentationAssemblyTest(unittest.TestCase):
def test_python_assembly(self):
data = load_testdata("default.py")
docs = "".join(data)
for doc in extract_documentation(data, "python", "default"):
self.assertIn(doc.assemble(), docs)
def test_c_assembly(self):
data = load_testdata("default.c")
docs = "".join(data)
for doc in extract_documentation(data, "c", "doxygen"):
self.assertIn(doc.assemble(), docs)
@@ -0,0 +1,42 @@
#include <stdlib.h>
/**
* This is the main function.
*
* @returns Your favorite number.
*/
int main()
{
// Nonsense comment.
printf("hello world");
return 3 * 0;
}
/*!
* Preserves alignment
* - Main item
* - sub item
* - sub sub item
*/
int myfield;
/** ABC
Another type of comment
... */
/** foobar = barfoo.
* @param x whatever...
*/
int foobar(int x) {
return x * x - x + 1;
}
/** line 1
* line2 */
/** */
/**
* A doc-comment aborted in the middle of writing
* This won't get parsed (hopefully...)
@@ -7,12 +7,9 @@
def foobar_explosion(radius): def foobar_explosion(radius):
""" """
A nice and neat way of documenting code. A nice and neat way of documenting code.
:param radius: The explosion radius. :param radius: The explosion radius. """
"""
def get_55(): def get_55():
""" """A function that returns 55."""
A function that returns 55.
"""
return 55 return 55
return get_55() * radius return get_55() * radius

0 comments on commit 78ff315

Please sign in to comment.