Skip to content
Permalink
Browse files

Complete tests for QgsTextDocument

  • Loading branch information
nyalldawson committed May 12, 2020
1 parent 180bbec commit b941e1c32509ea602b3b72ecc8176ae26a22f04d
Showing with 98 additions and 8 deletions.
  1. +98 −1 tests/src/python/test_qgstextdocument.py
  2. +0 −7 tests/src/python/test_qgstextrenderer.py
@@ -14,14 +14,111 @@

import qgis # NOQA

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

start_app()


class TestQgsTextDocument(unittest.TestCase):

def testConstructors(self):
# empty
doc = QgsTextDocument()
self.assertEqual(len(doc), 0)

# single block document
block = QgsTextBlock()
doc = QgsTextDocument(block)
self.assertEqual(len(doc), 1)
self.assertEqual(len(doc[0]), 0)

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

def testFromPlainText(self):
doc = QgsTextDocument.fromPlainText(['a', 'b c d', 'e'])
self.assertEqual(len(doc), 3)
self.assertEqual(len(doc[0]), 1)
self.assertEqual(doc[0][0].text(), 'a')
self.assertEqual(len(doc[1]), 1)
self.assertEqual(doc[1][0].text(), 'b c d')
self.assertEqual(len(doc[2]), 1)
self.assertEqual(doc[2][0].text(), 'e')

def testFromHtml(self):
doc = QgsTextDocument.fromHtml(['abc<div style="color: red"><b style="text-decoration: underline">def</b> ghi<div>jkl</div></div>', 'b c d', 'e'])
self.assertEqual(len(doc), 5)
self.assertEqual(len(doc[0]), 1)
self.assertEqual(doc[0][0].text(), 'abc')
self.assertEqual(doc[0][0].characterFormat().underline(), QgsTextCharacterFormat.BooleanValue.NotSet)
self.assertFalse(doc[0][0].characterFormat().textColor().isValid())
self.assertEqual(len(doc[1]), 2)
self.assertEqual(doc[1][0].text(), 'def')
self.assertEqual(doc[1][0].characterFormat().underline(), QgsTextCharacterFormat.BooleanValue.SetTrue)
self.assertEqual(doc[1][0].characterFormat().textColor().name(), '#ff0000')
self.assertEqual(doc[1][1].text(), ' ghi')
self.assertEqual(doc[1][1].characterFormat().underline(), QgsTextCharacterFormat.BooleanValue.NotSet)
self.assertEqual(doc[1][1].characterFormat().textColor().name(), '#ff0000')
self.assertEqual(len(doc[2]), 1)
self.assertEqual(doc[2][0].text(), 'jkl')
self.assertEqual(len(doc[3]), 1)
self.assertEqual(doc[3][0].text(), 'b c d')
self.assertEqual(len(doc[4]), 1)
self.assertEqual(doc[4][0].text(), 'e')

def testAppend(self):
doc = QgsTextDocument()
self.assertEqual(len(doc), 0)

block = QgsTextBlock()
doc.append(block)
self.assertEqual(len(doc), 1)
self.assertEqual(len(doc[0]), 0)

def testAt(self):
doc = QgsTextDocument()
self.assertEqual(len(doc), 0)

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

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

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

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

def testToPlainText(self):
self.assertEqual(QgsTextDocument.fromHtml(['']).toPlainText(), [])
self.assertEqual(QgsTextDocument.fromHtml(['abc']).toPlainText(), ['abc'])
self.assertEqual(QgsTextDocument.fromHtml(['abc\ndef']).toPlainText(), ['abc def'])
self.assertEqual(QgsTextDocument.fromHtml(['abc<b>def</b>']).toPlainText(), ['abcdef'])
self.assertEqual(QgsTextDocument.fromHtml(['abc<div><b>def</b><div>ghi</div></div>']).toPlainText(), ['abc', 'def', 'ghi'])

def testSplitLines(self):
doc = QgsTextDocument.fromHtml(['abc def'])
self.assertEqual(len(doc), 1)
@@ -61,13 +61,6 @@ def tearDown(self):
with open(report_file_path, 'a') as report_file:
report_file.write(self.report)

def testExtractBlocksFromHtml(self):
self.assertEqual(QgsTextRenderer.extractTextBlocksFromHtml(''), [])
self.assertEqual(QgsTextRenderer.extractTextBlocksFromHtml('abc'), ['abc'])
self.assertEqual(QgsTextRenderer.extractTextBlocksFromHtml('abc\ndef'), ['abc def'])
self.assertEqual(QgsTextRenderer.extractTextBlocksFromHtml('abc<b>def</b>'), ['abcdef'])
self.assertEqual(QgsTextRenderer.extractTextBlocksFromHtml('abc<div><b>def</b><div>ghi</div></div>'), ['abc', 'def', 'ghi'])

def createBufferSettings(self):
s = QgsTextBufferSettings()
s.setEnabled(True)

0 comments on commit b941e1c

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