Unit test: extended the standalone unit test tool to be able to open source in an editor window.

Wed, 20 Jun 2018 18:45:07 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Wed, 20 Jun 2018 18:45:07 +0200
changeset 6359
e78be0616e6e
parent 6358
1a274e1ef9c0
child 6360
73033fa6a99c

Unit test: extended the standalone unit test tool to be able to open source in an editor window.

APIs/Python3/eric6.api file | annotate | diff | comparison | revisions
Documentation/Help/source.qch file | annotate | diff | comparison | revisions
Documentation/Help/source.qhp file | annotate | diff | comparison | revisions
Documentation/Source/eric6.PyUnit.UnittestDialog.html file | annotate | diff | comparison | revisions
Documentation/Source/eric6.QScintilla.MiniEditor.html file | annotate | diff | comparison | revisions
PyUnit/UnittestDialog.py file | annotate | diff | comparison | revisions
QScintilla/MiniEditor.py file | annotate | diff | comparison | revisions
changelog file | annotate | diff | comparison | revisions
eric6_unittest.py file | annotate | diff | comparison | revisions
--- a/APIs/Python3/eric6.api	Wed Jun 20 18:34:44 2018 +0200
+++ b/APIs/Python3/eric6.api	Wed Jun 20 18:45:07 2018 +0200
@@ -8155,6 +8155,7 @@
 eric6.PyUnit.UnittestDialog.QtTestResult.startTest?4(test)
 eric6.PyUnit.UnittestDialog.QtTestResult.stopTest?4(test)
 eric6.PyUnit.UnittestDialog.QtTestResult?1(parent)
+eric6.PyUnit.UnittestDialog.UnittestDialog.closeEvent?4(event)
 eric6.PyUnit.UnittestDialog.UnittestDialog.hasFailedTests?4()
 eric6.PyUnit.UnittestDialog.UnittestDialog.insertProg?4(prog)
 eric6.PyUnit.UnittestDialog.UnittestDialog.insertTestName?4(testName)
@@ -8944,6 +8945,7 @@
 eric6.QScintilla.MiniEditor.MiniEditor.getFileName?4()
 eric6.QScintilla.MiniEditor.MiniEditor.getLanguage?4()
 eric6.QScintilla.MiniEditor.MiniEditor.getSRHistory?4(key)
+eric6.QScintilla.MiniEditor.MiniEditor.gotoLine?4(line, pos=1)
 eric6.QScintilla.MiniEditor.MiniEditor.setLanguage?4(filename, initTextDisplay=True, pyname="")
 eric6.QScintilla.MiniEditor.MiniEditor.setSearchIndicator?4(startPos, indicLength)
 eric6.QScintilla.MiniEditor.MiniEditor.setText?4(txt, filetype=None)
Binary file Documentation/Help/source.qch has changed
--- a/Documentation/Help/source.qhp	Wed Jun 20 18:34:44 2018 +0200
+++ b/Documentation/Help/source.qhp	Wed Jun 20 18:45:07 2018 +0200
@@ -11030,6 +11030,7 @@
       <keyword name="MiniEditor.getFileName" id="MiniEditor.getFileName" ref="eric6.QScintilla.MiniEditor.html#MiniEditor.getFileName" />
       <keyword name="MiniEditor.getLanguage" id="MiniEditor.getLanguage" ref="eric6.QScintilla.MiniEditor.html#MiniEditor.getLanguage" />
       <keyword name="MiniEditor.getSRHistory" id="MiniEditor.getSRHistory" ref="eric6.QScintilla.MiniEditor.html#MiniEditor.getSRHistory" />
+      <keyword name="MiniEditor.gotoLine" id="MiniEditor.gotoLine" ref="eric6.QScintilla.MiniEditor.html#MiniEditor.gotoLine" />
       <keyword name="MiniEditor.setLanguage" id="MiniEditor.setLanguage" ref="eric6.QScintilla.MiniEditor.html#MiniEditor.setLanguage" />
       <keyword name="MiniEditor.setSearchIndicator" id="MiniEditor.setSearchIndicator" ref="eric6.QScintilla.MiniEditor.html#MiniEditor.setSearchIndicator" />
       <keyword name="MiniEditor.setText" id="MiniEditor.setText" ref="eric6.QScintilla.MiniEditor.html#MiniEditor.setText" />
@@ -16487,10 +16488,12 @@
       <keyword name="UnittestDialog (Constructor)" id="UnittestDialog (Constructor)" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.__init__" />
       <keyword name="UnittestDialog (Module)" id="UnittestDialog (Module)" ref="eric6.PyUnit.UnittestDialog.html" />
       <keyword name="UnittestDialog.__UTPrepared" id="UnittestDialog.__UTPrepared" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.__UTPrepared" />
+      <keyword name="UnittestDialog.__openEditor" id="UnittestDialog.__openEditor" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.__openEditor" />
       <keyword name="UnittestDialog.__setProgressColor" id="UnittestDialog.__setProgressColor" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.__setProgressColor" />
       <keyword name="UnittestDialog.__setRunningMode" id="UnittestDialog.__setRunningMode" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.__setRunningMode" />
       <keyword name="UnittestDialog.__setStoppedMode" id="UnittestDialog.__setStoppedMode" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.__setStoppedMode" />
       <keyword name="UnittestDialog.__showSource" id="UnittestDialog.__showSource" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.__showSource" />
+      <keyword name="UnittestDialog.closeEvent" id="UnittestDialog.closeEvent" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.closeEvent" />
       <keyword name="UnittestDialog.hasFailedTests" id="UnittestDialog.hasFailedTests" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.hasFailedTests" />
       <keyword name="UnittestDialog.insertProg" id="UnittestDialog.insertProg" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.insertProg" />
       <keyword name="UnittestDialog.insertTestName" id="UnittestDialog.insertTestName" ref="eric6.PyUnit.UnittestDialog.html#UnittestDialog.insertTestName" />
--- a/Documentation/Source/eric6.PyUnit.UnittestDialog.html	Wed Jun 20 18:34:44 2018 +0200
+++ b/Documentation/Source/eric6.PyUnit.UnittestDialog.html	Wed Jun 20 18:45:07 2018 +0200
@@ -50,7 +50,7 @@
 <p>
     A TestResult derivative to work with a graphical GUI.
 </p><p>
-    For more details see pyunit.py of the standard python distribution.
+    For more details see pyunit.py of the standard Python distribution.
 </p>
 <h3>Derived from</h3>
 unittest.TestResult
@@ -223,6 +223,9 @@
 <td><a href="#UnittestDialog.__UTPrepared">__UTPrepared</a></td>
 <td>Private slot to handle the utPrepared signal.</td>
 </tr><tr>
+<td><a href="#UnittestDialog.__openEditor">__openEditor</a></td>
+<td>Private method to open an editor window for the given file.</td>
+</tr><tr>
 <td><a href="#UnittestDialog.__setProgressColor">__setProgressColor</a></td>
 <td>Private methode to set the color of the progress color label.</td>
 </tr><tr>
@@ -235,6 +238,9 @@
 <td><a href="#UnittestDialog.__showSource">__showSource</a></td>
 <td>Private slot to show the source of a traceback in an eric6 editor.</td>
 </tr><tr>
+<td><a href="#UnittestDialog.closeEvent">closeEvent</a></td>
+<td>Protected method to handle the close event.</td>
+</tr><tr>
 <td><a href="#UnittestDialog.hasFailedTests">hasFailedTests</a></td>
 <td>Public method to check, if there are failed tests from the last run.</td>
 </tr><tr>
@@ -343,6 +349,22 @@
 <dd>
 value of exception occured during preparation (string)
 </dd>
+</dl><a NAME="UnittestDialog.__openEditor" ID="UnittestDialog.__openEditor"></a>
+<h4>UnittestDialog.__openEditor</h4>
+<b>__openEditor</b>(<i>filename, linenumber</i>)
+<p>
+        Private method to open an editor window for the given file.
+</p><p>
+        Note: This method opens an editor window when the unittest dialog
+        is called as a standalone application.
+</p><dl>
+<dt><i>filename</i> (str)</dt>
+<dd>
+path of the file to be opened
+</dd><dt><i>linenumber</i> (int)</dt>
+<dd>
+line number to place the cursor at
+</dd>
 </dl><a NAME="UnittestDialog.__setProgressColor" ID="UnittestDialog.__setProgressColor"></a>
 <h4>UnittestDialog.__setProgressColor</h4>
 <b>__setProgressColor</b>(<i>color</i>)
@@ -368,7 +390,17 @@
 <b>__showSource</b>(<i></i>)
 <p>
         Private slot to show the source of a traceback in an eric6 editor.
-</p><a NAME="UnittestDialog.hasFailedTests" ID="UnittestDialog.hasFailedTests"></a>
+</p><a NAME="UnittestDialog.closeEvent" ID="UnittestDialog.closeEvent"></a>
+<h4>UnittestDialog.closeEvent</h4>
+<b>closeEvent</b>(<i>event</i>)
+<p>
+        Protected method to handle the close event.
+</p><dl>
+<dt><i>event</i> (QCloseEvent)</dt>
+<dd>
+close event
+</dd>
+</dl><a NAME="UnittestDialog.hasFailedTests" ID="UnittestDialog.hasFailedTests"></a>
 <h4>UnittestDialog.hasFailedTests</h4>
 <b>hasFailedTests</b>(<i></i>)
 <p>
--- a/Documentation/Source/eric6.QScintilla.MiniEditor.html	Wed Jun 20 18:34:44 2018 +0200
+++ b/Documentation/Source/eric6.QScintilla.MiniEditor.html	Wed Jun 20 18:45:07 2018 +0200
@@ -279,6 +279,9 @@
 <td><a href="#MiniEditor.getSRHistory">getSRHistory</a></td>
 <td>Public method to get the search or replace history list.</td>
 </tr><tr>
+<td><a href="#MiniEditor.gotoLine">gotoLine</a></td>
+<td>Public slot to jump to the beginning of a line.</td>
+</tr><tr>
 <td><a href="#MiniEditor.setLanguage">setLanguage</a></td>
 <td>Public method to set a lexer language.</td>
 </tr><tr>
@@ -938,6 +941,19 @@
 <dd>
 the requested history list (list of strings)
 </dd>
+</dl><a NAME="MiniEditor.gotoLine" ID="MiniEditor.gotoLine"></a>
+<h4>MiniEditor.gotoLine</h4>
+<b>gotoLine</b>(<i>line, pos=1</i>)
+<p>
+        Public slot to jump to the beginning of a line.
+</p><dl>
+<dt><i>line</i> (int)</dt>
+<dd>
+line number to go to
+</dd><dt><i>pos=</i> (int)</dt>
+<dd>
+position in line to go to
+</dd>
 </dl><a NAME="MiniEditor.setLanguage" ID="MiniEditor.setLanguage"></a>
 <h4>MiniEditor.setLanguage</h4>
 <b>setLanguage</b>(<i>filename, initTextDisplay=True, pyname=""</i>)
--- a/PyUnit/UnittestDialog.py	Wed Jun 20 18:34:44 2018 +0200
+++ b/PyUnit/UnittestDialog.py	Wed Jun 20 18:45:07 2018 +0200
@@ -139,6 +139,8 @@
             self.dbs.utTestFailedExpected.connect(self.testFailedExpected)
             self.dbs.utTestSucceededUnexpected.connect(
                 self.testSucceededUnexpected)
+        
+        self.__editors = []
     
     def keyPressEvent(self, evt):
         """
@@ -636,18 +638,14 @@
         
         ui.showButton = ui.buttonBox.addButton(
             self.tr("Show Source"), QDialogButtonBox.ActionRole)
+        ui.showButton.clicked.connect(self.__showSource)
+        
         ui.buttonBox.button(QDialogButtonBox.Close).setDefault(True)
         
         self.dlg.setWindowTitle(text)
         ui.testLabel.setText(test)
         ui.traceback.setPlainText(tracebackText)
         
-        # one more button if called from eric
-        if self.dbs:
-            ui.showButton.clicked.connect(self.__showSource)
-        else:
-            ui.showButton.hide()
-
         # and now fire it up
         self.dlg.show()
         self.dlg.exec_()
@@ -656,9 +654,6 @@
         """
         Private slot to show the source of a traceback in an eric6 editor.
         """
-        if not self.dbs:
-            return
-            
         # get the error info
         tracebackLines = self.dlg.traceback.toPlainText().splitlines()
         # find the last entry matching the pattern
@@ -669,7 +664,11 @@
                 break
         if fmatch:
             fn, ln = fmatch.group(1, 2)
-            self.unittestFile.emit(fn, int(ln), 1)
+            if self.dbs:
+                # running as part of eric IDE
+                self.unittestFile.emit(fn, int(ln), 1)
+            else:
+                self.__openEditor(fn, int(ln))
     
     def hasFailedTests(self):
         """
@@ -678,13 +677,48 @@
         @return flag indicating the presence of failed tests (boolean)
         """
         return bool(self.__failedTests)
+    
+    def __openEditor(self, filename, linenumber):
+        """
+        Private method to open an editor window for the given file.
+        
+        Note: This method opens an editor window when the unittest dialog
+        is called as a standalone application.
+        
+        @param filename path of the file to be opened
+        @type str
+        @param linenumber line number to place the cursor at
+        @type int
+        """
+        from QScintilla.MiniEditor import MiniEditor
+        editor = MiniEditor(filename, "Python3", self)
+        editor.gotoLine(linenumber)
+        editor.show()
+        
+        self.__editors.append(editor)
+    
+    def closeEvent(self, event):
+        """
+        Protected method to handle the close event.
+        
+        @param event close event
+        @type QCloseEvent
+        """
+        event.accept()
+        
+        for editor in self.__editors:
+            try:
+                editor.close()
+            except Exception:
+                # ignore all exceptions
+                pass
 
 
 class QtTestResult(unittest.TestResult):
     """
     A TestResult derivative to work with a graphical GUI.
     
-    For more details see pyunit.py of the standard python distribution.
+    For more details see pyunit.py of the standard Python distribution.
     """
     def __init__(self, parent):
         """
--- a/QScintilla/MiniEditor.py	Wed Jun 20 18:34:44 2018 +0200
+++ b/QScintilla/MiniEditor.py	Wed Jun 20 18:45:07 2018 +0200
@@ -3335,6 +3335,20 @@
         self.__textEdit.setModified(False)
         self.setWindowModified(False)
     
+    def gotoLine(self, line, pos=1):
+        """
+        Public slot to jump to the beginning of a line.
+        
+        @param line line number to go to
+        @type int
+        @keyparam pos position in line to go to
+        @type int
+        """
+        self.__textEdit.setCursorPosition(line - 1, pos - 1)
+        self.__textEdit.ensureLineVisible(line - 1)
+        self.__textEdit.setFirstVisibleLine(line - 1)
+        self.__textEdit.ensureCursorVisible()
+    
     #######################################################################
     ## Methods implementing the interface to EditorConfig
     #######################################################################
--- a/changelog	Wed Jun 20 18:34:44 2018 +0200
+++ b/changelog	Wed Jun 20 18:45:07 2018 +0200
@@ -10,6 +10,9 @@
 - pip Interface
   -- added support for the '--user' option of install and list commands
   -- changed to use the new VirtualEnv Manager
+- Unit Test
+  -- extended the standalone unit test tool to be able to open source in an
+     editor window
 - VirtualEnv Manager
   -- added a manager for virtual environments
   -- changed a few places to use the newly introduced virtual environments
--- a/eric6_unittest.py	Wed Jun 20 18:34:44 2018 +0200
+++ b/eric6_unittest.py	Wed Jun 20 18:45:07 2018 +0200
@@ -43,6 +43,10 @@
 
 from Toolbox import Startup
 
+# make Third-Party package available as a packages repository
+sys.path.insert(2, os.path.join(os.path.dirname(__file__),
+                                "ThirdParty", "EditorConfig"))
+
 
 def createMainWidget(argv):
     """

eric ide

mercurial