1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
# vim: ts=4:sw=4:expandtab
# This file is part of ReText
# Copyright: 2014-2025 Dmitry Shachnev
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import sys
import unittest
from unittest.mock import patch
from markups import MarkdownMarkup, ReStructuredTextMarkup
from PyQt6.QtCore import QEvent, QMimeData, Qt
from PyQt6.QtGui import QImage, QKeyEvent, QTextCursor, QTextDocument
from PyQt6.QtTest import QTest
from PyQt6.QtWidgets import QApplication
from ReText.editor import ReTextEdit, documentIndentLess, documentIndentMore
QApplication.setAttribute(Qt.ApplicationAttribute.AA_ShareOpenGLContexts)
# Keep a reference so it is not garbage collected
app = QApplication.instance() or QApplication(sys.argv)
class SettingsMock:
tabWidth = 4
tabInsertsSpaces = True
class TestIndentation(unittest.TestCase):
def setUp(self):
self.document = QTextDocument()
self.document.setPlainText('foo\nbar\nbaz')
self.settings = SettingsMock()
def test_indentMore(self):
cursor = QTextCursor(self.document)
cursor.setPosition(4)
documentIndentMore(self.document, cursor, self.settings)
self.assertEqual('foo\n bar\nbaz',
self.document.toPlainText())
cursor.setPosition(3)
documentIndentMore(self.document, cursor, self.settings)
self.assertEqual('foo \n bar\nbaz',
self.document.toPlainText())
def test_indentMoreWithTabs(self):
cursor = QTextCursor(self.document)
self.settings.tabInsertsSpaces = False
documentIndentMore(self.document, cursor, self.settings)
self.assertEqual('\tfoo\nbar\nbaz', self.document.toPlainText())
def test_indentMoreWithSelection(self):
cursor = QTextCursor(self.document)
cursor.setPosition(1)
cursor.setPosition(6, QTextCursor.MoveMode.KeepAnchor)
self.assertEqual('oo\u2029ba', # \u2029 is paragraph separator
cursor.selectedText())
documentIndentMore(self.document, cursor, self.settings)
self.assertEqual(' foo\n bar\nbaz',
self.document.toPlainText())
def test_indentLess(self):
self.document.setPlainText(' foo')
cursor = QTextCursor(self.document)
cursor.setPosition(10)
documentIndentLess(self.document, cursor, self.settings)
self.assertEqual(' foo', self.document.toPlainText())
documentIndentLess(self.document, cursor, self.settings)
self.assertEqual('foo', self.document.toPlainText())
def test_indentLessWithSelection(self):
self.document.setPlainText(' foo\n bar\nbaz')
cursor = QTextCursor(self.document)
cursor.setPosition(5)
cursor.setPosition(11, QTextCursor.MoveMode.KeepAnchor)
documentIndentLess(self.document, cursor, self.settings)
self.assertEqual('foo\nbar\nbaz', self.document.toPlainText())
class TestClipboardHandling(unittest.TestCase):
class DummyReTextTab:
def __init__(self):
self.markupClass = None
def getActiveMarkupClass(self):
return self.markupClass
def setUp(self):
self.p = self
self.editor = ReTextEdit(self)
self.dummytab = self.DummyReTextTab()
self.editor.tab = self.dummytab
def _create_image(self):
image = QImage(80, 60, QImage.Format.Format_RGB32)
image.fill(Qt.GlobalColor.green)
return image
def test_pasteText(self):
mimeData = QMimeData()
mimeData.setText('pasted text')
self.editor.insertFromMimeData(mimeData)
self.assertTrue('pasted text' in self.editor.toPlainText())
@patch.object(ReTextEdit, 'getImageFilename', return_value='/tmp/myimage.jpg')
@patch.object(QImage, 'save')
def test_pasteImage_Markdown(self, _mock_image, _mock_editor):
mimeData = QMimeData()
mimeData.setImageData(self._create_image())
app.clipboard().setMimeData(mimeData)
self.dummytab.markupClass = MarkdownMarkup
self.dummytab.fileName = '/tmp/foo.md'
self.editor.pasteImage()
self.assertTrue('' in self.editor.toPlainText())
@patch.object(ReTextEdit, 'getImageFilename', return_value='/tmp/myimage.jpg')
@patch.object(QImage, 'save')
def test_pasteImage_RestructuredText(self, _mock_image, _mock_editor):
mimeData = QMimeData()
mimeData.setImageData(self._create_image())
app.clipboard().setMimeData(mimeData)
self.dummytab.markupClass = ReStructuredTextMarkup
self.dummytab.fileName = '/tmp/foo.rst'
self.editor.pasteImage()
self.assertTrue('.. image:: myimage.jpg' in self.editor.toPlainText())
class TestSurround(unittest.TestCase):
def setUp(self):
self.p = self
self.editor = ReTextEdit(self)
self.document = QTextDocument()
self.document.setPlainText('foo bar baz qux corge grault')
self.cursor = QTextCursor(self.document)
def getText(self, key):
if key == Qt.Key.Key_ParenLeft:
return '('
if key == Qt.Key.Key_BracketLeft:
return '['
if key == Qt.Key.Key_Underscore:
return '_'
if key == Qt.Key.Key_Asterisk:
return '*'
if key == Qt.Key.Key_QuoteDbl:
return '"'
if key == Qt.Key.Key_Apostrophe:
return '\''
def getEvent(self, key):
return QKeyEvent(
QEvent.Type.KeyPress,
key,
Qt.KeyboardModifier.NoModifier,
text=self.getText(key),
)
def test_isSurroundKey(self):
# close keys should not start a surrounding
self.assertFalse(self.editor.isSurroundKey(Qt.Key.Key_ParenRight))
self.assertFalse(self.editor.isSurroundKey(Qt.Key.Key_BracketRight))
self.assertTrue(self.editor.isSurroundKey(Qt.Key.Key_ParenLeft))
self.assertTrue(self.editor.isSurroundKey(Qt.Key.Key_BracketLeft))
self.assertTrue(self.editor.isSurroundKey(Qt.Key.Key_Underscore))
self.assertTrue(self.editor.isSurroundKey(Qt.Key.Key_Asterisk))
self.assertTrue(self.editor.isSurroundKey(Qt.Key.Key_QuoteDbl))
self.assertTrue(self.editor.isSurroundKey(Qt.Key.Key_Apostrophe))
def test_getCloseKey(self):
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_Underscore), Qt.Key.Key_Underscore),
'_',
)
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_Asterisk), Qt.Key.Key_Asterisk),
'*',
)
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_QuoteDbl), Qt.Key.Key_QuoteDbl),
'"',
)
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_Apostrophe), Qt.Key.Key_Apostrophe),
'\'',
)
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_ParenLeft), Qt.Key.Key_ParenLeft),
')',
)
self.assertEqual(
self.editor.getCloseKey(self.getEvent(Qt.Key.Key_BracketLeft), Qt.Key.Key_BracketLeft),
']',
)
def changeCursor(self, posI, posF):
self.cursor.setPosition(posI)
self.cursor.setPosition(posF, QTextCursor.MoveMode.KeepAnchor)
def test_surroundText(self):
self.changeCursor(0, 3)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_Underscore),
Qt.Key.Key_Underscore,
)
self.assertEqual(self.document.toPlainText(), '_foo_ bar baz qux corge grault')
self.changeCursor(6, 9)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_Asterisk),
Qt.Key.Key_Asterisk,
)
self.assertEqual(self.document.toPlainText(), '_foo_ *bar* baz qux corge grault')
self.changeCursor(12, 15)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_QuoteDbl),
Qt.Key.Key_QuoteDbl,
)
self.assertEqual(self.document.toPlainText(), '_foo_ *bar* "baz" qux corge grault')
self.changeCursor(18, 21)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_Apostrophe),
Qt.Key.Key_Apostrophe,
)
self.assertEqual(self.document.toPlainText(), '_foo_ *bar* "baz" \'qux\' corge grault')
self.changeCursor(24, 29)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_ParenLeft),
Qt.Key.Key_ParenLeft,
)
self.assertEqual(self.document.toPlainText(), '_foo_ *bar* "baz" \'qux\' (corge) grault')
self.changeCursor(32, 38)
self.editor.surroundText(
self.cursor,
self.getEvent(Qt.Key.Key_BracketLeft),
Qt.Key.Key_BracketLeft,
)
self.assertEqual(self.document.toPlainText(), '_foo_ *bar* "baz" \'qux\' (corge) [grault]')
class TestOrderedListMode(unittest.TestCase):
class DummyReTextTab:
def __init__(self):
self.markupClass = None
def getActiveMarkupClass(self):
return self.markupClass
def setUp(self):
self.p = self
def test_increment(self):
editor = ReTextEdit(self)
editor.tab = self.DummyReTextTab()
QTest.keyClicks(editor, '1. Hello')
QTest.keyClick(editor, Qt.Key.Key_Return)
QTest.keyClicks(editor, 'World')
self.assertEqual(editor.document().toPlainText(), '1. Hello\n2. World')
def test_repeat(self):
class TestSettings:
orderedListMode = 'repeat'
useFakeVim = False
editor = ReTextEdit(self, settings=TestSettings())
editor.tab = self.DummyReTextTab()
QTest.keyClicks(editor, '1. Hello')
QTest.keyClick(editor, Qt.Key.Key_Return)
QTest.keyClicks(editor, 'World')
self.assertEqual(editor.document().toPlainText(), '1. Hello\n1. World')
if __name__ == '__main__':
unittest.main()
|