Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add QgsCodeEditorPython unittests
  • Loading branch information
YoannQDQ authored and nyalldawson committed Mar 31, 2023
1 parent 0a26096 commit 8b01c62
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/src/python/CMakeLists.txt
Expand Up @@ -47,6 +47,7 @@ ADD_PYTHON_TEST(PyQgsBox3d test_qgsbox3d.py)
ADD_PYTHON_TEST(PyQgsCategorizedSymbolRenderer test_qgscategorizedsymbolrenderer.py)
ADD_PYTHON_TEST(PyQgsCheckableComboBox test_qgscheckablecombobox.py)
ADD_PYTHON_TEST(PyQgsClassificationMethod test_qgsclassificationmethod.py)
ADD_PYTHON_TEST(PyQgsCodeEditorPython test_qgscodeeditorpython.py)
ADD_PYTHON_TEST(PyQgsCodeEditor test_qgscodeeditor.py)
ADD_PYTHON_TEST(PyQgsCodeEditorColorScheme test_qgscodeeditorcolorscheme.py)
ADD_PYTHON_TEST(PyQgsColorButton test_qgscolorbutton.py)
Expand Down
209 changes: 209 additions & 0 deletions tests/src/python/test_qgscodeeditorpython.py
@@ -0,0 +1,209 @@
"""QGIS Unit tests for QgsCodeEditorPython
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""
__author__ = 'Yoann Quenach de Quivillic'
__date__ = '31/03/2023'
__copyright__ = 'Copyright 2023, The QGIS Project'


import qgis # NOQA
from qgis.PyQt.QtCore import QCoreApplication, Qt
from qgis.PyQt.QtTest import QTest
from qgis.core import QgsSettings
from qgis.gui import QgsCodeEditorPython
from qgis.testing import start_app, unittest


COMPLETIONS_PAIRS = {
"(": ")",
"[": "]",
"{": "}",
"'": "'",
"\"": "\""
}
COMPLETIONS_SINGLE_CHARACTERS = ["`", "*"]


class TestQgsCodeEditorPython(unittest.TestCase):

@classmethod
def setUpClass(cls):
"""Run before all tests"""
QCoreApplication.setOrganizationName("QGIS_Test")
QCoreApplication.setOrganizationDomain("QGIS_TestQgsCodeEditorPython.com")
QCoreApplication.setApplicationName("QGIS_TestQgsCodeEditorPython")
start_app()

def setUp(self):
# Ensure that the settings are cleared before each test
QgsSettings().clear()

def testAutoSurround(self):
self.assertEqual(QgsSettings().value('pythonConsole/autoSurround'), None)

editor = QgsCodeEditorPython()

test_string = "Hello World"

for opening, closing in COMPLETIONS_PAIRS.items():

editor.setText(test_string)

# Select the whole text
editor.selectAll()

# Type the opening character
QTest.keyClicks(editor, opening)
self.assertEqual(editor.text(), opening + test_string + closing)

for character in COMPLETIONS_SINGLE_CHARACTERS:

editor.setText(test_string)

# Type the character
editor.selectAll()
QTest.keyClicks(editor, character)
self.assertEqual(editor.text(), character + test_string + character)

# Check nested autosurround
editor.setText(test_string)
editor.selectAll()
for opening, closing in COMPLETIONS_PAIRS.items():

text = editor.text()
# Select the whole text
QTest.keyClicks(editor, opening)
self.assertEqual(editor.text(), text.replace(test_string, opening + test_string + closing))

# Check multiline autosurround with quotes (should insert triple quotes)
test_string = "Hello\nWorld"
editor.setText(test_string)
editor.selectAll()
QTest.keyClicks(editor, "'")
self.assertEqual(editor.text(), f"'''{test_string}'''")

editor.setText(test_string)
editor.selectAll()
QTest.keyClicks(editor, "\"")
self.assertEqual(editor.text(), f'"""{test_string}"""')

# Check disabled autosurround
test_string = "will not be surrounded"
QgsSettings().setValue('pythonConsole/autoSurround', False)
editor.setText(test_string)
editor.selectAll()
QTest.keyClicks(editor, "(")
self.assertEqual(editor.text(), "(")

def testAutoCloseBrackets(self):

self.assertEqual(QgsSettings().value('pythonConsole/autoCloseBracket'), None)

editor = QgsCodeEditorPython()

for opening, closing in COMPLETIONS_PAIRS.items():

# Type the opening character, should insert both characters
QTest.keyClicks(editor, opening)
self.assertEqual(editor.text(), opening + closing)
self.assertEqual(editor.getCursorPosition(), (0, 1))

# Typing the closing character should just move the cursor
QTest.keyClicks(editor, closing)
self.assertEqual(editor.text(), opening + closing)
self.assertEqual(editor.getCursorPosition(), (0, 2))

# Pressing backspace while inside an opening/closing pair should remove both characters
QTest.keyClick(editor, Qt.Key_Left)
self.assertEqual(editor.getCursorPosition(), (0, 1))
QTest.keyClick(editor, Qt.Key_Backspace)
self.assertEqual(editor.text(), "")

editor.clear()

# Check nested brackets
for i, (opening, closing) in enumerate(COMPLETIONS_PAIRS.items()):
# Type the opening character, should insert both characters
self.assertEqual(editor.getCursorPosition(), (0, i))
text = editor.text()
QTest.keyClicks(editor, opening)
self.assertEqual(editor.text(), text[:i] + opening + closing + text[i:])

# Check do not auto close brackets when the following character is not a space, a colon or a closing bracket
editor.setText("(")
editor.setCursorPosition(0, 0)
QTest.keyClicks(editor, "[")
self.assertEqual(editor.text(), "[(")
editor.clear()

editor.setText(")")
editor.setCursorPosition(0, 0)
QTest.keyClicks(editor, "[")
self.assertEqual(editor.text(), "[])")
editor.clear()

editor.setText("a")
editor.setCursorPosition(0, 0)
QTest.keyClicks(editor, "[")
self.assertEqual(editor.text(), "[a")
editor.clear()

editor.setText(" a")
editor.setCursorPosition(0, 0)
QTest.keyClicks(editor, "[")
self.assertEqual(editor.text(), "[] a")
editor.clear()

editor.setText("a:")
editor.setCursorPosition(0, 0)
QTest.keyClicks(editor, "{")
self.assertEqual(editor.text(), "{a:")
editor.clear()

editor.setText("a:")
editor.setCursorPosition(0, 1)
QTest.keyClicks(editor, "{")
self.assertEqual(editor.text(), "a{}:")
editor.clear()

# Check disabled auto close brackets
QgsSettings().setValue('pythonConsole/autoCloseBracket', False)

QTest.keyClicks(editor, "{")
self.assertEqual(editor.text(), "{")
editor.clear()

def testToggleComment(self):

editor = QgsCodeEditorPython()

# Check single line comment
editor.setText("#Hello World")
QTest.keyClick(editor, ':', Qt.ControlModifier)
self.assertEqual(editor.text(), "Hello World")
QTest.keyClick(editor, ':', Qt.ControlModifier)
self.assertEqual(editor.text(), "# Hello World")

# Check multiline comment
editor.setText("Hello\nQGIS\nWorld")
editor.setSelection(0, 0, 1, 4)
QTest.keyClick(editor, ':', Qt.ControlModifier)
self.assertEqual(editor.text(), "# Hello\n# QGIS\nWorld")
QTest.keyClick(editor, ':', Qt.ControlModifier)
self.assertEqual(editor.text(), "Hello\nQGIS\nWorld")

# Check multiline comment with already commented lines
editor.setText("Hello\n# QGIS\nWorld")
editor.setSelection(0, 0, 2, 4)
QTest.keyClick(editor, ':', Qt.ControlModifier)
self.assertEqual(editor.text(), "# Hello\n# # QGIS\n# World")
QTest.keyClick(editor, ':', Qt.ControlModifier)
self.assertEqual(editor.text(), "Hello\n# QGIS\nWorld")


if __name__ == '__main__':
unittest.main()

0 comments on commit 8b01c62

Please sign in to comment.