Skip to content
Permalink
Browse files

added a test for makeKeyTokens_()

  • Loading branch information
SebDieBln committed Nov 19, 2015
1 parent 313fe6a commit 174fbb97ceac4848f7fba4b75ef964c009c5679e
Showing with 114 additions and 0 deletions.
  1. +1 −0 tests/src/python/CMakeLists.txt
  2. +113 −0 tests/src/python/test_qgsproject.py
@@ -3,6 +3,7 @@ ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)
ADD_PYTHON_TEST(PyQgsLocalServer test_qgis_local_server.py)
ADD_PYTHON_TEST(PyQgsFontUtils test_qgsfontutils.py)
ADD_PYTHON_TEST(PyQgsFeature test_qgsfeature.py)
ADD_PYTHON_TEST(PyQgsProject test_qgsproject.py)
ADD_PYTHON_TEST(PyQgsFeatureIterator test_qgsfeatureiterator.py)
ADD_PYTHON_TEST(PyQgsGeometry test_qgsgeometry.py)
ADD_PYTHON_TEST(PyQgsGeometryAvoidIntersections test_qgsgeometry_avoid_intersections.py)
@@ -0,0 +1,113 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsProject.
.. 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__ = 'Sebastian Dietrich'
__date__ = '19/11/2015'
__copyright__ = 'Copyright 2015, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$'

import qgis

from qgis.core import QgsProject, QgsMessageLog

from utilities import getQgisTestApp, TestCase, unittest

QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()


class TestQgsProject(TestCase):

def __init__(self, methodName):
"""Run once on class initialisation."""
unittest.TestCase.__init__(self, methodName)
self.messageCaught = False

def test_makeKeyTokens_(self):
# see http://www.w3.org/TR/REC-xml/#d0e804 for a list of valid characters

invalidTokens = []
validTokens = []

# all test tokens will be generated by prepending or inserting characters to this token
validBase = u"valid";

# some invalid characters, not allowed anywhere in a token
# note that '/' must not be added here because it is taken as a separator by makeKeyTokens_()
invalidChars = u"+*,;<>|!$%()=?#\x01";

# generate the characters that are allowed at the start of a token (and at every other position)
validStartChars = u":_";
charRanges = [
(ord(u'a'), ord(u'z')),
(ord(u'A'), ord(u'Z')),
(0x00F8, 0x02FF),
(0x0370, 0x037D),
(0x037F, 0x1FFF),
(0x200C, 0x200D),
(0x2070, 0x218F),
(0x2C00, 0x2FEF),
(0x3001, 0xD7FF),
(0xF900, 0xFDCF),
(0xFDF0, 0xFFFD),
#(0x10000, 0xEFFFF), while actually valid, these are not yet accepted by makeKeyTokens_()
]
for r in charRanges:
for c in range(r[0], r[1]):
validStartChars += unichr(c)

# generate the characters that are only allowed inside a token, not at the start
validInlineChars = u"-.\xB7";
charRanges = [
(ord(u'0'), ord(u'9')),
(0x0300, 0x036F),
(0x203F, 0x2040),
]
for r in charRanges:
for c in range(r[0], r[1]):
validInlineChars += unichr(c)

# test forbidden start characters
for c in invalidChars + validInlineChars:
invalidTokens.append(c + validBase)

# test forbidden inline characters
for c in invalidChars:
invalidTokens.append(validBase[:4] + c + validBase[4:])

# test each allowed start character
for c in validStartChars:
validTokens.append(c + validBase)

# test each allowed inline character
for c in validInlineChars:
validTokens.append(validBase[:4] + c + validBase[4:]);

logger = QgsMessageLog.instance()
logger.messageReceived.connect(self.catchMessage)
prj = QgsProject.instance()

for token in validTokens:
self.messageCaught = False
prj.readEntry("test", token)
myMessage = "valid token '%s' not accepted" % (token)
assert not self.messageCaught, myMessage

for token in invalidTokens:
self.messageCaught = False
prj.readEntry("test", token)
myMessage = "invalid token '%s' accepted" % (token)
assert self.messageCaught, myMessage

logger.messageReceived.disconnect(self.catchMessage)

def catchMessage(self):
self.messageCaught = True

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

0 comments on commit 174fbb9

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