Skip to content
Permalink
Browse files

More tests

  • Loading branch information
nyalldawson committed May 12, 2020
1 parent b941e1c commit 556b858dfae30ec4cdb970194dc4b7893b22e44e
Showing with 143 additions and 0 deletions.
  1. +2 −0 tests/src/python/CMakeLists.txt
  2. +87 −0 tests/src/python/test_qgstextblock.py
  3. +54 −0 tests/src/python/test_qgstextfragment.py
@@ -255,7 +255,9 @@ ADD_PYTHON_TEST(PyQgsSymbol test_qgssymbol.py)
ADD_PYTHON_TEST(PyQgsSymbolLayerUtils test_qgssymbollayerutils.py)
ADD_PYTHON_TEST(PyQgsTaskManager test_qgstaskmanager.py)
ADD_PYTHON_TEST(PyQgsTemporalUtils test_qgstemporalutils.py)
ADD_PYTHON_TEST(PyQgsTextBlock test_qgstextblock.py)
ADD_PYTHON_TEST(PyQgsTextDocument test_qgstextdocument.py)
ADD_PYTHON_TEST(PyQgsTextFragment test_qgstextfragment.py)
ADD_PYTHON_TEST(PyQgsTextFormatWidget test_qgstextformatwidget.py)
ADD_PYTHON_TEST(PyQgsTreeWidgetItem test_qgstreewidgetitem.py)
ADD_PYTHON_TEST(PyQgsUnitTypes test_qgsunittypes.py)
@@ -0,0 +1,87 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsTextBlock.
Run with: ctest -V -R QgsTextBlock
.. 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__ = 'Nyall Dawson'
__date__ = '12/05/2020'
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA

from qgis.core import (
QgsTextBlock,
QgsTextFragment
)
from qgis.testing import start_app, unittest

start_app()


class TestQgsTextBlock(unittest.TestCase):

def testConstructors(self):
# empty
block = QgsTextBlock()
self.assertEqual(len(block), 0)

# single fragment block
fragment = QgsTextFragment('ludicrous gibs!')
block = QgsTextBlock(fragment)
self.assertEqual(len(block), 1)
self.assertEqual(block[0].text(), fragment.text())

def testAppend(self):
block = QgsTextBlock()
self.assertEqual(len(block), 0)

frag = QgsTextFragment('a')
block.append(frag)
self.assertEqual(len(block), 1)
self.assertEqual(block[0].text(), 'a')
frag = QgsTextFragment('b')
block.append(frag)
self.assertEqual(len(block), 2)
self.assertEqual(block[0].text(), 'a')
self.assertEqual(block[1].text(), 'b')

def testAt(self):
block = QgsTextBlock()
block.append(QgsTextFragment('a'))
block.append(QgsTextFragment('b'))
self.assertEqual(len(block), 2)

self.assertEqual(block.at(0).text(), 'a')
self.assertEqual(block.at(1).text(), 'b')
with self.assertRaises(KeyError):
block.at(2)
with self.assertRaises(KeyError):
block.at(-1)

self.assertEqual(block[0].text(), 'a')
self.assertEqual(block[1].text(), 'b')
with self.assertRaises(IndexError):
_ = block[2]

self.assertEqual(block[-1].text(), 'b')
self.assertEqual(block[-2].text(), 'a')

def testClear(self):
block = QgsTextBlock()
block.append(QgsTextFragment('a'))
block.append(QgsTextFragment('b'))
self.assertEqual(len(block), 2)
self.assertFalse(block.empty())

block.clear()
self.assertEqual(len(block), 0)
self.assertTrue(block.empty())


if __name__ == '__main__':
unittest.main()
@@ -0,0 +1,54 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsTextFragment.
Run with: ctest -V -R QgsTextFragment
.. 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__ = 'Nyall Dawson'
__date__ = '12/05/2020'
__copyright__ = 'Copyright 2020, The QGIS Project'

import qgis # NOQA

from qgis.core import (
QgsTextFragment,
QgsTextCharacterFormat
)
from qgis.PyQt.QtGui import QColor
from qgis.testing import start_app, unittest

start_app()


class TestQgsTextFragment(unittest.TestCase):

def testConstructors(self):
# empty
frag = QgsTextFragment()
self.assertFalse(frag.text())

fragment = QgsTextFragment('ludicrous gibs!')
self.assertEqual(fragment.text(), 'ludicrous gibs!')

def testSetText(self):
fragment = QgsTextFragment()
fragment.setText('ludicrous gibs!')
self.assertEqual(fragment.text(), 'ludicrous gibs!')

def testSetCharacterFormat(self):
fragment = QgsTextFragment('a')

self.assertFalse(fragment.characterFormat().textColor().isValid())
format = QgsTextCharacterFormat()
format.setTextColor(QColor(255, 0, 0))
fragment.setCharacterFormat(format)
self.assertTrue(fragment.characterFormat().textColor().isValid())
self.assertEqual(fragment.characterFormat().textColor().name(), '#ff0000')


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

0 comments on commit 556b858

Please sign in to comment.
You can’t perform that action at this time.