def test_format_and_save(self): doctest = ( "narrative is a line that exceeds 78 characters which causes " "scrolling in consoles and wraps poorly in email\n" " * item\n\n" " >>> very_very_very_very_very.long_long_long_long(" "method_method_method_method)\n" " True\n\n") self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) checker.format_and_save() expected = ( "narrative is a line that exceeds 78 characters which causes " "scrolling in\nconsoles and wraps poorly in email\n\n" " * item\n\n" " >>> very_very_very_very_very.long_long_long_long(" "method_method_method_method)\n" " True\n\n\n") self.file.seek(0) text = self.file.read() self.assertEqual(expected, text) self.assertEqual(expected, checker.doctest) # Source code issues cannot be fixed by the formatter. checker.check() self.assertEqual( [(6, 'source exceeds 78 characters.')], self.reporter.messages)
def test_doctest_without_issues(self): self.file.write(good_doctest) self.file.flush() checker = DoctestReviewer( self.file.name, good_doctest, self.reporter) checker.check() self.assertEqual([], self.reporter.messages)
def reformat_doctest(self, data): """Reformat the doctest.""" bounds, text = self._get_bounded_text() file_name = self.active_document.get_uri_for_display() reviewer = DoctestReviewer(text, file_name) new_text = reviewer.format() self._put_bounded_text(bounds, new_text)
def test_fix_trailing_whitespace(self): doctest = "narrative \n >>> print 'done' \n" self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) text = checker.format() self.assertEqual("narrative\n\n >>> print 'done'\n\n", text)
def test_fix_bad_indentation_with_source_and_want(self): doctest = "narrative\n\n>>> print (\n... 'done')" self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) text = checker.format() self.assertEqual( "narrative\n\n >>> print (\n ... 'done')\n\n", text)
def test_fix_moin_header_3(self): doctest = "=== Heading ===\n\nnarrative" self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) text = checker.format() self.assertEqual( "Heading\n.......\n\nnarrative", text)
def test_doctest_with_source_comments(self): self.file.write(source_comments_doctest) self.file.flush() checker = DoctestReviewer( self.file.name, source_comments_doctest, self.reporter) checker.check_source_comments() self.assertEqual([ (2, 'Comment belongs in narrative.'), (4, 'Comment belongs in narrative.')], self.reporter.messages)
def test_doctest_with_globs(self): # Doctest runners often setup global identifiers that are not python # execution issues doctest = " >>> ping('text')\n pong text" self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) checker.check() self.assertEqual([], self.reporter.messages)
def test_doctest_with_python_compilation_error(self): doctest = " >>> if (True\n pong text" self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) checker.check() self.assertEqual( [(1, 'Could not compile:\n if (True')], self.reporter.messages)
def test_trailing_whitespace(self): doctest = "narrative \n >>> print 'done'\n" self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) checker.check() self.assertEqual( [(1, 'narrative has trailing whitespace.')], self.reporter.messages)
def test_bad_indentation(self): doctest = "narrative\n>>> print 'done'\n" self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) checker.check() self.assertEqual( [(2, 'source has bad indentation.')], self.reporter.messages)
def test_moin_header(self): doctest = "= Heading =\n\nnarrative" self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) checker.check() self.assertEqual( [(1, 'narrative uses a moin header.')], self.reporter.messages)
def test_doctest_malformed_doctest(self): self.file.write(malformed_doctest) self.file.flush() checker = DoctestReviewer( self.file.name, malformed_doctest, self.reporter) checker.check() expected = ( "line 4 of the docstring for %s has inconsistent leading " "whitespace: 'Narrative without WANT section.'" % self.file.name) self.assertEqual( [(0, expected)], self.reporter.messages)
def test_long_line_narrative(self): doctest = ( "narrative is a line that exceeds 78 characters which causes " "scrolling in consoles and wraps poorly in email\n") self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) checker.check() self.assertEqual( [(1, 'narrative exceeds 78 characters.')], self.reporter.messages)
def test_long_line_source_and_want(self): doctest = ( " >>> very_very_very_very_very.long_long_long_long(" "method_method_method_method,\n" " ... call_call_call_call_call_call_call_call_call_call," "bad_bad_bad_bad_bad)\n") self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) checker.check() self.assertEqual( [(1, 'source exceeds 78 characters.'), (2, 'source exceeds 78 characters.')], self.reporter.messages)
def test_fix_long_line(self): doctest = ( "narrative is a line that exceeds 78 characters which causes " "scrolling in consoles and wraps poorly in email\n" " * item") self.file.write(doctest) self.file.flush() checker = DoctestReviewer( self.file.name, doctest, self.reporter) text = checker.format() expected = ( "narrative is a line that exceeds 78 characters which causes " "scrolling in\nconsoles and wraps poorly in email\n\n" " * item\n\n") self.assertEqual(expected, text)
def check_sources(sources, options, reporter=None): if reporter is None: reporter = Reporter(Reporter.CONSOLE) reporter.call_count = 0 for source in sources: file_path = os.path.normpath(source) if os.path.isdir(source) or not Language.is_editable(source): continue language = Language.get_language(file_path) with open(file_path, 'rt') as file_: text = file_.read() if language is Language.DOCTEST and options.do_format: formatter = DoctestReviewer(text, file_path, reporter) formatter.format_and_save(options.is_interactive) checker = UniversalChecker( file_path, text, language, reporter, options=options) checker.check() return reporter.call_count
def check_sources(sources, options, reporter=None): if reporter is None: reporter = Reporter(Reporter.CONSOLE) reporter.call_count = 0 for source in sources: file_path = os.path.normpath(source) if os.path.isdir(source) or not Language.is_editable(source): continue language = Language.get_language(file_path) with open(file_path, 'rt') as file_: text = file_.read() if language is Language.DOCTEST and options.do_format: formatter = DoctestReviewer(text, file_path, reporter) formatter.format_and_save(options.is_interactive) checker = UniversalChecker(file_path, text, language, reporter, options=options) checker.check() return reporter.call_count