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 @@ ...@@ -20,12 +20,12 @@
import unittest import unittest
import random import random
import os.path import os.path
from hashlib import sha1 from hashlib import sha1
from tempfile import TemporaryDirectory from tempfile import TemporaryDirectory
from org.libreoffice.unotest import UnoInProcess, mkPropertyValues, systemPathToFileUrl from org.libreoffice.unotest import UnoInProcess, mkPropertyValues, systemPathToFileUrl
from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK from com.sun.star.text.ControlCharacter import PARAGRAPH_BREAK
class CheckBookmarks(unittest.TestCase): class CheckBookmarks(unittest.TestCase):
expectedHashes = { expectedHashes = {
'nSetupHash': 0x8f88ee1a13a55d6024f58f470723b5174dfa21bb, 'nSetupHash': 0x8f88ee1a13a55d6024f58f470723b5174dfa21bb,
...@@ -42,7 +42,8 @@ class CheckBookmarks(unittest.TestCase): ...@@ -42,7 +42,8 @@ class CheckBookmarks(unittest.TestCase):
cls._uno.setUp() cls._uno.setUp()
cls._xDoc = cls._uno.openEmptyWriterDoc() cls._xDoc = cls._uno.openEmptyWriterDoc()
smgr = cls._uno.xContext.ServiceManager 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 @classmethod
def tearDownClass(cls): def tearDownClass(cls):
...@@ -55,34 +56,41 @@ class CheckBookmarks(unittest.TestCase): ...@@ -55,34 +56,41 @@ class CheckBookmarks(unittest.TestCase):
def test_bookmarks(self): def test_bookmarks(self):
self.xDoc = self.__class__._xDoc self.xDoc = self.__class__._xDoc
self.xText = self.xDoc.getText() self.xText = self.xDoc.getText()
## setting and testing bookmarks ## setting and testing bookmarks
self.setupBookmarks() self.setupBookmarks()
self.assertEqual(self.expectedHashes['nSetupHash'], self.assertEqual(self.expectedHashes['nSetupHash'],
self.getBookmarksHash(self.xDoc)) self.getBookmarksHash(self.xDoc))
## modifying bookmarks and testing again ## modifying bookmarks and testing again
self.insertRandomParts(200177) self.insertRandomParts(200177)
self.assertEqual(self.expectedHashes['nInsertRandomHash'], self.assertEqual(self.expectedHashes['nInsertRandomHash'],
self.getBookmarksHash(self.xDoc)) self.getBookmarksHash(self.xDoc))
## modifying bookmarks and testing again ## modifying bookmarks and testing again
self.deleteRandomParts(4711) self.deleteRandomParts(4711)
self.assertEqual(self.expectedHashes['nDeleteRandomHash'], self.assertEqual(self.expectedHashes['nDeleteRandomHash'],
self.getBookmarksHash(self.xDoc)) self.getBookmarksHash(self.xDoc))
## adding line breaks and testing again ## adding line breaks and testing again
self.insertLinebreaks(7) self.insertLinebreaks(7)
self.assertEqual(self.expectedHashes['nLinebreakHash'], self.assertEqual(self.expectedHashes['nLinebreakHash'],
self.getBookmarksHash(self.xDoc)) self.getBookmarksHash(self.xDoc))
## reloading document and testing again ## reloading document and testing again
with TemporaryDirectory() as tempdir: with TemporaryDirectory() as tempdir:
xOdfReloadedDoc = self.reloadFrom(tempdir, "writer8", "odt") xOdfReloadedDoc = self.reloadFrom(tempdir, "writer8", "odt")
self.assertEqual(self.expectedHashes['nOdfReloadHash'], self.assertEqual(self.expectedHashes['nOdfReloadHash'],
self.getBookmarksHash(xOdfReloadedDoc)) self.getBookmarksHash(xOdfReloadedDoc))
xOdfReloadedDoc.close(True) xOdfReloadedDoc.close(True)
## reloading document as MS Word 97 doc and testing again ## reloading document as MS Word 97 doc and testing again
## MsWord Hash is unstable over different systems ## MsWord Hash is unstable over different systems
# xMsWordReloadedDoc = self.reloadFrom(tempdir, "MS Word 97", "doc") # xMsWordReloadedDoc = self.reloadFrom(tempdir, "MS Word 97", "doc")
# self.assertEqual(self.expectedHashes['nMsWordReloadHash'], # self.assertEqual(self.expectedHashes['nMsWordReloadHash'],
# self.getBookmarksHash(xMsWordReloadedDoc)) # self.getBookmarksHash(xMsWordReloadedDoc))
# xMsWordReloadedDoc.close(True) # xMsWordReloadedDoc.close(True)
print('tests ok') print('tests ok')
def setupBookmarks(self): def setupBookmarks(self):
...@@ -96,20 +104,24 @@ class CheckBookmarks(unittest.TestCase): ...@@ -96,20 +104,24 @@ class CheckBookmarks(unittest.TestCase):
xBookmark.setName(s) xBookmark.setName(s)
self.xText.insertTextContent(xCursor, xBookmark, True) self.xText.insertTextContent(xCursor, xBookmark, True)
xCursor.End.setString(" ") xCursor.End.setString(" ")
self.xText.insertControlCharacter(xCursor.End, PARAGRAPH_BREAK, False) self.xText.insertControlCharacter(xCursor.End, PARAGRAPH_BREAK, False)
def getBookmarksHash(self, doc): def getBookmarksHash(self, doc):
hash = sha1() _hash = sha1()
xBookmarks = doc.getBookmarks() xBookmarks = doc.getBookmarks()
for xBookmark in xBookmarks: for xBookmark in xBookmarks:
s = '{}:{};'.format(xBookmark.Name, s = '{}:{};'.format(xBookmark.Name,
xBookmark.getAnchor().getString().replace("\r\n", "\n")) xBookmark.getAnchor().getString().replace("\r\n", "\n"))
hash.update(str.encode(s))
return int(hash.hexdigest(), 16) _hash.update(str.encode(s))
return int(_hash.hexdigest(), 16)
def insertRandomParts(self, seed): def insertRandomParts(self, seed):
random.seed(seed) random.seed(seed)
xCursor = self.xText.createTextCursor() xCursor = self.xText.createTextCursor()
for i in range(600): for i in range(600):
xCursor.goRight(random.randrange(100), False) xCursor.goRight(random.randrange(100), False)
xCursor.setString(str(random.getrandbits(64))) xCursor.setString(str(random.getrandbits(64)))
...@@ -117,6 +129,7 @@ class CheckBookmarks(unittest.TestCase): ...@@ -117,6 +129,7 @@ class CheckBookmarks(unittest.TestCase):
def deleteRandomParts(self, seed): def deleteRandomParts(self, seed):
random.seed(seed) random.seed(seed)
xCursor = self.xText.createTextCursor() xCursor = self.xText.createTextCursor()
for i in range(600): for i in range(600):
xCursor.goRight(random.randrange(100), False) xCursor.goRight(random.randrange(100), False)
xCursor.goRight(random.randrange(20), True) xCursor.goRight(random.randrange(20), True)
...@@ -125,6 +138,7 @@ class CheckBookmarks(unittest.TestCase): ...@@ -125,6 +138,7 @@ class CheckBookmarks(unittest.TestCase):
def insertLinebreaks(self, seed): def insertLinebreaks(self, seed):
random.seed(seed) random.seed(seed)
xCursor = self.xText.createTextCursor() xCursor = self.xText.createTextCursor()
for i in range(30): for i in range(30):
xCursor.goRight(random.randrange(300), False) xCursor.goRight(random.randrange(300), False)
self.xText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False) self.xText.insertControlCharacter(xCursor, PARAGRAPH_BREAK, False)
...@@ -138,6 +152,5 @@ class CheckBookmarks(unittest.TestCase): ...@@ -138,6 +152,5 @@ class CheckBookmarks(unittest.TestCase):
load_props = mkPropertyValues(Hidden=True, ReadOnly=False) load_props = mkPropertyValues(Hidden=True, ReadOnly=False)
return desktop.loadComponentFromURL(sFileUrl, "_default", 0, load_props) return desktop.loadComponentFromURL(sFileUrl, "_default", 0, load_props)
if __name__ == '__main__': if __name__ == '__main__':
unittest.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