def test_extract_documentation_C(self): data = load_testdata('data.c') # No built-in documentation for C. with self.assertRaises(KeyError): tuple(extract_documentation(data, 'C', 'default')) docstyle_C_doxygen = DocstyleDefinition.load('C', 'doxygen') expected_results = (DocumentationComment( ('\n' ' This is the main function.\n' '\n' ' @returns Your favorite number.\n'), docstyle_C_doxygen, '', docstyle_C_doxygen.markers[0], TextPosition(3, 1)), DocumentationComment(('\n' ' Preserves alignment\n' ' - Main item\n' ' - sub item\n' ' - sub sub item\n'), docstyle_C_doxygen, '', docstyle_C_doxygen.markers[2], TextPosition(15, 1)), DocumentationComment( (' ABC\n' ' Another type of comment\n' '\n' ' ...'), docstyle_C_doxygen, '', docstyle_C_doxygen.markers[1], TextPosition(23, 1)), DocumentationComment((' foobar = barfoo.\n' ' @param x whatever...\n'), docstyle_C_doxygen, '', docstyle_C_doxygen.markers[0], TextPosition(28, 1))) self.assertEqual(tuple(extract_documentation(data, 'C', 'doxygen')), expected_results)
def test_fields(self): c_doxygen = DocstyleDefinition.load('C', 'doxygen') uut = DocumentationComment('my doc', c_doxygen, ' ', ('/**', '*', '*/'), TextPosition(3, 1)) self.assertEqual(uut.documentation, 'my doc') self.assertEqual(uut.language, 'c') self.assertEqual(uut.docstyle, 'doxygen') self.assertEqual(uut.indent, ' ') self.assertEqual(str(uut), 'my doc') self.assertEqual(uut.marker, ('/**', '*', '*/')) self.assertEqual(uut.position, TextPosition(3, 1)) python_doxygen = DocstyleDefinition.load('python', 'doxygen') python_doxygen_metadata = self.Metadata('@param ', ' ', '@raises ', ' ', '@return ') uut = DocumentationComment('qwertzuiop', python_doxygen, '\t', ('##', '#', '#'), None) self.assertEqual(uut.documentation, 'qwertzuiop') self.assertEqual(uut.language, 'python') self.assertEqual(uut.docstyle, 'doxygen') self.assertEqual(uut.indent, '\t') self.assertEqual(str(uut), 'qwertzuiop') self.assertEqual(uut.marker, ('##', '#', '#')) self.assertEqual(uut.range, None) self.assertEqual(uut.position, None) self.assertEqual(uut.metadata, python_doxygen_metadata)
def test_fail_instantation(self): with self.assertRaises(ValueError): TextRange(TextPosition(3, 4), TextPosition(2, 8)) with self.assertRaises(ValueError): TextRange(TextPosition(0, 10), TextPosition(0, 7)) with self.assertRaises(TypeError): TextRange(None, TextPosition(20, 80)) with self.assertRaises(TypeError): TextRange("string", TextPosition(200, 800)) with self.assertRaises(TypeError): TextRange(TextPosition(5, 0), "schtring")
def test_fail_instantiation(self): with self.assertRaises(ValueError): TextRange(TextPosition(3, 4), TextPosition(2, 8)) with self.assertRaises(ValueError): TextRange(TextPosition(1, 10), TextPosition(1, 7)) with self.assertRaises(TypeError): TextRange(None, TextPosition(20, 80)) with self.assertRaises(TypeError): TextRange('string', TextPosition(200, 800)) with self.assertRaises(TypeError): TextRange(TextPosition(5, 1), 'schtring')
def _extract_doc_comment_from_line(content, line, column, regex, marker_dict, docstyle_definition): cur_line = content[line] begin_match = regex.search(cur_line, column) if begin_match: column = begin_match.end() indent = begin_match.group('indent') for marker in marker_dict[begin_match.group('marker')]: doc_comment = _extract_doc_comment(content, line, column, marker) if doc_comment is not None: end_line, end_column, documentation = doc_comment position = TextPosition(line + 1, len(indent) + 1) doc = DocumentationComment(documentation, docstyle_definition, indent, marker, position) return end_line, end_column, doc return line + 1, 0, None
def test_from_values(self): # Check if invalid ranges still fail. with self.assertRaises(ValueError): TextRange.from_values(0, 10, 0, 7) uut = TextRange.from_values(1, 0, 7, 3) self.assertEqual(uut.start, TextPosition(1, 0)) self.assertEqual(uut.end, TextPosition(7, 3)) uut = TextRange.from_values(1, 0, None, 88) self.assertEqual(uut.start, TextPosition(1, 0)) self.assertEqual(uut.end, TextPosition(1, 0)) uut = TextRange.from_values(1, 0, 7, None) self.assertEqual(uut.start, TextPosition(1, 0)) self.assertEqual(uut.end, TextPosition(7, None)) # Test defaults. uut = TextRange.from_values() self.assertEqual(uut.start, TextPosition(None, None)) self.assertEqual(uut.end, TextPosition(None, None))
def test_fail_instantiation(self): with self.assertRaises(ValueError): TextPosition(None, 2) with self.assertRaises(ZeroOffsetError): TextPosition(0, 2) with self.assertRaises(ZeroOffsetError): TextPosition(2, 0) with self.assertRaises(ZeroOffsetError): TextPosition(0, 0) with self.assertRaises(TypeError): TextPosition('hello', 3) with self.assertRaises(TypeError): TextPosition(4, 'world') with self.assertRaises(TypeError): TextPosition('double', 'string')
def test_expand_none(self): start_position = TextPosition(2, 2) end_position = TextPosition(3, 2) file = ["abc\n", "def\n", "ghi\n"] text_range = TextRange(start_position, end_position) self.assertEqual(text_range.expand(file), text_range)
def test_expand_full(self): empty_position = TextPosition() file = ["abc\n", "def\n", "ghi\n"] empty_range = TextRange(empty_position, empty_position) full_range = TextRange.from_values(1, 1, 3, 4) self.assertEqual(empty_range.expand(file), full_range)
def test_extract_documentation_PYTHON3(self): data = load_testdata('data.py') docstyle_PYTHON3_default = DocstyleDefinition.load( 'PYTHON3', 'default') docstyle_PYTHON3_doxygen = DocstyleDefinition.load( 'PYTHON3', 'doxygen') expected = ( DocumentationComment(('\n' 'Module description.\n' '\n' 'Some more foobar-like text.\n'), docstyle_PYTHON3_default, '', docstyle_PYTHON3_default.markers[0], TextPosition(1, 1)), DocumentationComment(('\n' 'A nice and neat way of documenting code.\n' ':param radius: The explosion radius.\n'), docstyle_PYTHON3_default, ' ' * 4, docstyle_PYTHON3_default.markers[0], TextPosition(8, 5)), DocumentationComment('\nA function that returns 55.\n', docstyle_PYTHON3_default, ' ' * 8, docstyle_PYTHON3_default.markers[0], TextPosition(13, 9)), DocumentationComment( ('\n' 'Docstring with layouted text.\n' '\n' ' layouts inside docs are preserved for these ' 'documentation styles.\n' 'this is intended.\n'), docstyle_PYTHON3_default, '', docstyle_PYTHON3_default.markers[0], TextPosition(19, 1)), DocumentationComment( (' Docstring directly besides triple quotes.\n' ' Continues here. '), docstyle_PYTHON3_default, '', docstyle_PYTHON3_default.markers[0], TextPosition(26, 1)), DocumentationComment(('super\n' ' nicely\n' 'short'), docstyle_PYTHON3_default, '', docstyle_PYTHON3_default.markers[0], TextPosition(40, 1)), DocumentationComment(('\n' 'A bad indented docstring\n' ' Improper indentation.\n' ':param impact: The force of Impact.\n'), docstyle_PYTHON3_default, ' ' * 4, docstyle_PYTHON3_default.markers[0], TextPosition(45, 5)), ) self.assertEqual( tuple(extract_documentation(data, 'PYTHON3', 'default')), expected) # Change only the docstyle in expected results. expected = list( DocumentationComment(r.documentation, docstyle_PYTHON3_doxygen, r.indent, r.marker, r.position) for r in expected) expected.insert( 5, DocumentationComment( (' Alternate documentation style in doxygen.\n' ' Subtext\n' ' More subtext (not correctly aligned)\n' ' sub-sub-text\n' '\n'), docstyle_PYTHON3_doxygen, '', docstyle_PYTHON3_doxygen.markers[1], TextPosition(30, 1))) self.assertEqual( list(extract_documentation(data, 'PYTHON3', 'doxygen')), expected)
def setUp(self): self.pos = [TextPosition(1, 1), TextPosition(3, 1), TextPosition(3, 3), TextPosition(4, 3), TextPosition(5, 3)]