Skip to content

Commit 556b858

Browse files
committedMay 12, 2020
More tests
1 parent b941e1c commit 556b858

File tree

3 files changed

+143
-0
lines changed

3 files changed

+143
-0
lines changed
 

‎tests/src/python/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,9 @@ ADD_PYTHON_TEST(PyQgsSymbol test_qgssymbol.py)
255255
ADD_PYTHON_TEST(PyQgsSymbolLayerUtils test_qgssymbollayerutils.py)
256256
ADD_PYTHON_TEST(PyQgsTaskManager test_qgstaskmanager.py)
257257
ADD_PYTHON_TEST(PyQgsTemporalUtils test_qgstemporalutils.py)
258+
ADD_PYTHON_TEST(PyQgsTextBlock test_qgstextblock.py)
258259
ADD_PYTHON_TEST(PyQgsTextDocument test_qgstextdocument.py)
260+
ADD_PYTHON_TEST(PyQgsTextFragment test_qgstextfragment.py)
259261
ADD_PYTHON_TEST(PyQgsTextFormatWidget test_qgstextformatwidget.py)
260262
ADD_PYTHON_TEST(PyQgsTreeWidgetItem test_qgstreewidgetitem.py)
261263
ADD_PYTHON_TEST(PyQgsUnitTypes test_qgsunittypes.py)

‎tests/src/python/test_qgstextblock.py

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsTextBlock.
3+
4+
Run with: ctest -V -R QgsTextBlock
5+
6+
.. note:: This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation; either version 2 of the License, or
9+
(at your option) any later version.
10+
"""
11+
__author__ = 'Nyall Dawson'
12+
__date__ = '12/05/2020'
13+
__copyright__ = 'Copyright 2020, The QGIS Project'
14+
15+
import qgis # NOQA
16+
17+
from qgis.core import (
18+
QgsTextBlock,
19+
QgsTextFragment
20+
)
21+
from qgis.testing import start_app, unittest
22+
23+
start_app()
24+
25+
26+
class TestQgsTextBlock(unittest.TestCase):
27+
28+
def testConstructors(self):
29+
# empty
30+
block = QgsTextBlock()
31+
self.assertEqual(len(block), 0)
32+
33+
# single fragment block
34+
fragment = QgsTextFragment('ludicrous gibs!')
35+
block = QgsTextBlock(fragment)
36+
self.assertEqual(len(block), 1)
37+
self.assertEqual(block[0].text(), fragment.text())
38+
39+
def testAppend(self):
40+
block = QgsTextBlock()
41+
self.assertEqual(len(block), 0)
42+
43+
frag = QgsTextFragment('a')
44+
block.append(frag)
45+
self.assertEqual(len(block), 1)
46+
self.assertEqual(block[0].text(), 'a')
47+
frag = QgsTextFragment('b')
48+
block.append(frag)
49+
self.assertEqual(len(block), 2)
50+
self.assertEqual(block[0].text(), 'a')
51+
self.assertEqual(block[1].text(), 'b')
52+
53+
def testAt(self):
54+
block = QgsTextBlock()
55+
block.append(QgsTextFragment('a'))
56+
block.append(QgsTextFragment('b'))
57+
self.assertEqual(len(block), 2)
58+
59+
self.assertEqual(block.at(0).text(), 'a')
60+
self.assertEqual(block.at(1).text(), 'b')
61+
with self.assertRaises(KeyError):
62+
block.at(2)
63+
with self.assertRaises(KeyError):
64+
block.at(-1)
65+
66+
self.assertEqual(block[0].text(), 'a')
67+
self.assertEqual(block[1].text(), 'b')
68+
with self.assertRaises(IndexError):
69+
_ = block[2]
70+
71+
self.assertEqual(block[-1].text(), 'b')
72+
self.assertEqual(block[-2].text(), 'a')
73+
74+
def testClear(self):
75+
block = QgsTextBlock()
76+
block.append(QgsTextFragment('a'))
77+
block.append(QgsTextFragment('b'))
78+
self.assertEqual(len(block), 2)
79+
self.assertFalse(block.empty())
80+
81+
block.clear()
82+
self.assertEqual(len(block), 0)
83+
self.assertTrue(block.empty())
84+
85+
86+
if __name__ == '__main__':
87+
unittest.main()
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsTextFragment.
3+
4+
Run with: ctest -V -R QgsTextFragment
5+
6+
.. note:: This program is free software; you can redistribute it and/or modify
7+
it under the terms of the GNU General Public License as published by
8+
the Free Software Foundation; either version 2 of the License, or
9+
(at your option) any later version.
10+
"""
11+
__author__ = 'Nyall Dawson'
12+
__date__ = '12/05/2020'
13+
__copyright__ = 'Copyright 2020, The QGIS Project'
14+
15+
import qgis # NOQA
16+
17+
from qgis.core import (
18+
QgsTextFragment,
19+
QgsTextCharacterFormat
20+
)
21+
from qgis.PyQt.QtGui import QColor
22+
from qgis.testing import start_app, unittest
23+
24+
start_app()
25+
26+
27+
class TestQgsTextFragment(unittest.TestCase):
28+
29+
def testConstructors(self):
30+
# empty
31+
frag = QgsTextFragment()
32+
self.assertFalse(frag.text())
33+
34+
fragment = QgsTextFragment('ludicrous gibs!')
35+
self.assertEqual(fragment.text(), 'ludicrous gibs!')
36+
37+
def testSetText(self):
38+
fragment = QgsTextFragment()
39+
fragment.setText('ludicrous gibs!')
40+
self.assertEqual(fragment.text(), 'ludicrous gibs!')
41+
42+
def testSetCharacterFormat(self):
43+
fragment = QgsTextFragment('a')
44+
45+
self.assertFalse(fragment.characterFormat().textColor().isValid())
46+
format = QgsTextCharacterFormat()
47+
format.setTextColor(QColor(255, 0, 0))
48+
fragment.setCharacterFormat(format)
49+
self.assertTrue(fragment.characterFormat().textColor().isValid())
50+
self.assertEqual(fragment.characterFormat().textColor().name(), '#ff0000')
51+
52+
53+
if __name__ == '__main__':
54+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.