Skip to content

Commit 0132a3e

Browse files
committedNov 19, 2015
Merge pull request #1 from SebDieBln/SanitizeEntryNames
add a test for makeKeyTokens_()
2 parents 313fe6a + 174fbb9 commit 0132a3e

File tree

2 files changed

+114
-0
lines changed

2 files changed

+114
-0
lines changed
 

‎tests/src/python/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ADD_PYTHON_TEST(PyQgsApplication test_qgsapplication.py)
33
ADD_PYTHON_TEST(PyQgsLocalServer test_qgis_local_server.py)
44
ADD_PYTHON_TEST(PyQgsFontUtils test_qgsfontutils.py)
55
ADD_PYTHON_TEST(PyQgsFeature test_qgsfeature.py)
6+
ADD_PYTHON_TEST(PyQgsProject test_qgsproject.py)
67
ADD_PYTHON_TEST(PyQgsFeatureIterator test_qgsfeatureiterator.py)
78
ADD_PYTHON_TEST(PyQgsGeometry test_qgsgeometry.py)
89
ADD_PYTHON_TEST(PyQgsGeometryAvoidIntersections test_qgsgeometry_avoid_intersections.py)

‎tests/src/python/test_qgsproject.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
# -*- coding: utf-8 -*-
2+
"""QGIS Unit tests for QgsProject.
3+
4+
.. note:: This program is free software; you can redistribute it and/or modify
5+
it under the terms of the GNU General Public License as published by
6+
the Free Software Foundation; either version 2 of the License, or
7+
(at your option) any later version.
8+
"""
9+
__author__ = 'Sebastian Dietrich'
10+
__date__ = '19/11/2015'
11+
__copyright__ = 'Copyright 2015, The QGIS Project'
12+
# This will get replaced with a git SHA1 when you do a git archive
13+
__revision__ = '$Format:%H$'
14+
15+
import qgis
16+
17+
from qgis.core import QgsProject, QgsMessageLog
18+
19+
from utilities import getQgisTestApp, TestCase, unittest
20+
21+
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
22+
23+
24+
class TestQgsProject(TestCase):
25+
26+
def __init__(self, methodName):
27+
"""Run once on class initialisation."""
28+
unittest.TestCase.__init__(self, methodName)
29+
self.messageCaught = False
30+
31+
def test_makeKeyTokens_(self):
32+
# see http://www.w3.org/TR/REC-xml/#d0e804 for a list of valid characters
33+
34+
invalidTokens = []
35+
validTokens = []
36+
37+
# all test tokens will be generated by prepending or inserting characters to this token
38+
validBase = u"valid";
39+
40+
# some invalid characters, not allowed anywhere in a token
41+
# note that '/' must not be added here because it is taken as a separator by makeKeyTokens_()
42+
invalidChars = u"+*,;<>|!$%()=?#\x01";
43+
44+
# generate the characters that are allowed at the start of a token (and at every other position)
45+
validStartChars = u":_";
46+
charRanges = [
47+
(ord(u'a'), ord(u'z')),
48+
(ord(u'A'), ord(u'Z')),
49+
(0x00F8, 0x02FF),
50+
(0x0370, 0x037D),
51+
(0x037F, 0x1FFF),
52+
(0x200C, 0x200D),
53+
(0x2070, 0x218F),
54+
(0x2C00, 0x2FEF),
55+
(0x3001, 0xD7FF),
56+
(0xF900, 0xFDCF),
57+
(0xFDF0, 0xFFFD),
58+
#(0x10000, 0xEFFFF), while actually valid, these are not yet accepted by makeKeyTokens_()
59+
]
60+
for r in charRanges:
61+
for c in range(r[0], r[1]):
62+
validStartChars += unichr(c)
63+
64+
# generate the characters that are only allowed inside a token, not at the start
65+
validInlineChars = u"-.\xB7";
66+
charRanges = [
67+
(ord(u'0'), ord(u'9')),
68+
(0x0300, 0x036F),
69+
(0x203F, 0x2040),
70+
]
71+
for r in charRanges:
72+
for c in range(r[0], r[1]):
73+
validInlineChars += unichr(c)
74+
75+
# test forbidden start characters
76+
for c in invalidChars + validInlineChars:
77+
invalidTokens.append(c + validBase)
78+
79+
# test forbidden inline characters
80+
for c in invalidChars:
81+
invalidTokens.append(validBase[:4] + c + validBase[4:])
82+
83+
# test each allowed start character
84+
for c in validStartChars:
85+
validTokens.append(c + validBase)
86+
87+
# test each allowed inline character
88+
for c in validInlineChars:
89+
validTokens.append(validBase[:4] + c + validBase[4:]);
90+
91+
logger = QgsMessageLog.instance()
92+
logger.messageReceived.connect(self.catchMessage)
93+
prj = QgsProject.instance()
94+
95+
for token in validTokens:
96+
self.messageCaught = False
97+
prj.readEntry("test", token)
98+
myMessage = "valid token '%s' not accepted" % (token)
99+
assert not self.messageCaught, myMessage
100+
101+
for token in invalidTokens:
102+
self.messageCaught = False
103+
prj.readEntry("test", token)
104+
myMessage = "invalid token '%s' accepted" % (token)
105+
assert self.messageCaught, myMessage
106+
107+
logger.messageReceived.disconnect(self.catchMessage)
108+
109+
def catchMessage(self):
110+
self.messageCaught = True
111+
112+
if __name__ == '__main__':
113+
unittest.main()

0 commit comments

Comments
 (0)
Please sign in to comment.