Kaydet (Commit) 6e9b4cf3 authored tarafından Hamish McIntyre-Bhatty's avatar Hamish McIntyre-Bhatty Kaydeden (comit) Samuel Mehrbrodt

tdf#97361 Make check_bookmarks.py more pythonic

Keep lines within the correct length and add a little bit
of whitespace to improve readability.

Change-Id: I0cf26886b25079a970938b728004c96c9fa3e26f
Reviewed-on: https://gerrit.libreoffice.org/61622
Tested-by: Jenkins
Reviewed-by: 's avatarSamuel Mehrbrodt <Samuel.Mehrbrodt@cib.de>
üst f3b743a4
......@@ -20,12 +20,12 @@
import unittest
import random
import os.path
from hashlib import sha1
from tempfile import TemporaryDirectory
from org.libreoffice.unotest import UnoInProcess, mkPropertyValues, systemPathToFileUrl
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
class CheckBookmarks(unittest.TestCase):
expectedHashes = {
'nSetupHash': 0x8f88ee1a13a55d6024f58f470723b5174dfa21bb,
......@@ -42,7 +42,8 @@ class CheckBookmarks(unittest.TestCase):
cls._uno.setUp()
cls._xDoc = cls._uno.openEmptyWriterDoc()
smgr = cls._uno.xContext.ServiceManager
cls._desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", cls._uno.xContext)
cls._desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop",
cls._uno.xContext)
@classmethod
def tearDownClass(cls):
......@@ -55,34 +56,41 @@ class CheckBookmarks(unittest.TestCase):
def test_bookmarks(self):
self.xDoc = self.__class__._xDoc
self.xText = self.xDoc.getText()
## setting and testing bookmarks
self.setupBookmarks()
self.assertEqual(self.expectedHashes['nSetupHash'],
self.getBookmarksHash(self.xDoc))
## modifying bookmarks and testing again
self.insertRandomParts(200177)
self.assertEqual(self.expectedHashes['nInsertRandomHash'],
self.getBookmarksHash(self.xDoc))
## modifying bookmarks and testing again
self.deleteRandomParts(4711)
self.assertEqual(self.expectedHashes['nDeleteRandomHash'],
self.getBookmarksHash(self.xDoc))
## adding line breaks and testing again
self.insertLinebreaks(7)
self.assertEqual(self.expectedHashes['nLinebreakHash'],
self.getBookmarksHash(self.xDoc))
## reloading document and testing again
with TemporaryDirectory() as tempdir:
xOdfReloadedDoc = self.reloadFrom(tempdir, "writer8", "odt")
self.assertEqual(self.expectedHashes['nOdfReloadHash'],
self.getBookmarksHash(xOdfReloadedDoc))
xOdfReloadedDoc.close(True)
## reloading document as MS Word 97 doc and testing again
## MsWord Hash is unstable over different systems
# xMsWordReloadedDoc = self.reloadFrom(tempdir, "MS Word 97", "doc")
# self.assertEqual(self.expectedHashes['nMsWordReloadHash'],
# self.getBookmarksHash(xMsWordReloadedDoc))
# xMsWordReloadedDoc.close(True)
print('tests ok')
def setupBookmarks(self):
......@@ -96,20 +104,24 @@ class CheckBookmarks(unittest.TestCase):
xBookmark.setName(s)
self.xText.insertTextContent(xCursor, xBookmark, True)
xCursor.End.setString(" ")
self.xText.insertControlCharacter(xCursor.End, PARAGRAPH_BREAK, False)
def getBookmarksHash(self, doc):
hash = sha1()
_hash = sha1()
xBookmarks = doc.getBookmarks()
for xBookmark in xBookmarks:
s = '{}:{};'.format(xBookmark.Name,
xBookmark.getAnchor().getString().replace("\r\n", "\n"))
hash.update(str.encode(s))
return int(hash.hexdigest(), 16)
xBookmark.getAnchor().getString().replace("\r\n", "\n"))
_hash.update(str.encode(s))
return int(_hash.hexdigest(), 16)
def insertRandomParts(self, seed):
random.seed(seed)
xCursor = self.xText.createTextCursor()
for i in range(600):
xCursor.goRight(random.randrange(100), False)
xCursor.setString(str(random.getrandbits(64)))
......@@ -117,6 +129,7 @@ class CheckBookmarks(unittest.TestCase):
def deleteRandomParts(self, seed):
random.seed(seed)
xCursor = self.xText.createTextCursor()
for i in range(600):
xCursor.goRight(random.randrange(100), False)
xCursor.goRight(random.randrange(20), True)
......@@ -125,6 +138,7 @@ class CheckBookmarks(unittest.TestCase):
def insertLinebreaks(self, seed):
random.seed(seed)
xCursor = self.xText.createTextCursor()
for i in range(30):
xCursor.goRight(random.randrange(300), False)
self.xText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False)
......@@ -138,6 +152,5 @@ class CheckBookmarks(unittest.TestCase):
load_props = mkPropertyValues(Hidden=True, ReadOnly=False)
return desktop.loadComponentFromURL(sFileUrl, "_default", 0, load_props)
if __name__ == '__main__':
unittest.main()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment