Unit test for word switching

merge-requests/30/head
Bob Mottram 2021-07-06 17:29:03 +01:00
parent f51b7194f4
commit 0eb405ef5b
2 changed files with 50 additions and 25 deletions

View File

@ -202,29 +202,29 @@ def dangerousCSS(filename: str, allowLocalNetworkAccess: bool) -> bool:
return False return False
def switchWords(baseDir: str, nickname: str, domain: str, content: str) -> str: def switchWords(baseDir: str, nickname: str, domain: str, content: str,
rules: [] = []) -> str:
"""Performs word replacements. eg. Trump -> The Orange Menace """Performs word replacements. eg. Trump -> The Orange Menace
""" """
if isPGPEncrypted(content) or containsPGPPublicKey(content): if isPGPEncrypted(content) or containsPGPPublicKey(content):
return content return content
if not rules:
switchWordsFilename = baseDir + '/accounts/' + \ switchWordsFilename = baseDir + '/accounts/' + \
nickname + '@' + domain + '/replacewords.txt' nickname + '@' + domain + '/replacewords.txt'
if not os.path.isfile(switchWordsFilename): if not os.path.isfile(switchWordsFilename):
return content return content
with open(switchWordsFilename, 'r') as fp: with open(switchWordsFilename, 'r') as fp:
for line in fp: rules = fp.readlines()
for line in rules:
replaceStr = line.replace('\n', '').replace('\r', '') replaceStr = line.replace('\n', '').replace('\r', '')
splitters = ('->', ':', ',', ';', '-')
wordTransform = None wordTransform = None
if '->' in replaceStr: for splitStr in splitters:
wordTransform = replaceStr.split('->') if splitStr in replaceStr:
elif ':' in replaceStr: wordTransform = replaceStr.split(splitStr)
wordTransform = replaceStr.split(':') break
elif ',' in replaceStr:
wordTransform = replaceStr.split(',')
elif ';' in replaceStr:
wordTransform = replaceStr.split(';')
elif '-' in replaceStr:
wordTransform = replaceStr.split('-')
if not wordTransform: if not wordTransform:
continue continue
if len(wordTransform) == 2: if len(wordTransform) == 2:

View File

@ -94,6 +94,7 @@ from inbox import jsonPostAllowsComments
from inbox import validInbox from inbox import validInbox
from inbox import validInboxFilenames from inbox import validInboxFilenames
from categories import guessHashtagCategory from categories import guessHashtagCategory
from content import switchWords
from content import extractTextFieldsInPOST from content import extractTextFieldsInPOST
from content import validHashTag from content import validHashTag
from content import htmlReplaceEmailQuote from content import htmlReplaceEmailQuote
@ -4117,9 +4118,33 @@ def _testUserAgentDomain() -> None:
assert userAgentDomain(userAgent, False) is None assert userAgentDomain(userAgent, False) is None
def _testSwitchWords() -> None:
print('testSwitchWords')
rules = [
"rock -> hamster",
"orange -> lemon"
]
baseDir = os.getcwd()
nickname = 'testuser'
domain = 'testdomain.com'
content = 'This is a test'
result = switchWords(baseDir, nickname, domain, content, rules)
assert result == content
content = 'This is orange test'
result = switchWords(baseDir, nickname, domain, content, rules)
assert result == 'This is lemon test'
content = 'This is a test rock'
result = switchWords(baseDir, nickname, domain, content, rules)
assert result == 'This is a test hamster'
def runAllTests(): def runAllTests():
print('Running tests...') print('Running tests...')
updateDefaultThemesList(os.getcwd()) updateDefaultThemesList(os.getcwd())
_testSwitchWords()
_testFunctions() _testFunctions()
_testUserAgentDomain() _testUserAgentDomain()
_testRoles() _testRoles()