Add more check functions

This commit is contained in:
Veronica K. B. Olsen
2021-01-29 13:22:38 +01:00
parent 4e7888259f
commit 286efade59
2 changed files with 125 additions and 5 deletions
+34 -4
View File
@@ -30,7 +30,9 @@ from datetime import datetime
from PyQt5.QtWidgets import qApp
from nw.constants import nwConst, nwUnicode
from nw.constants import (
nwConst, nwUnicode, nwItemClass, nwItemType, nwItemLayout
)
logger = logging.getLogger(__name__)
@@ -103,11 +105,39 @@ def isHandle(theString):
return False
if len(theString) != 13:
return False
invalidChar = False
for c in theString:
if c not in "0123456789abcdef":
invalidChar = True
return not invalidChar
return False
return True
def isTitleTag(theString):
"""Check if a string is a valid title string.
"""
if not isinstance(theString, str):
return False
if len(theString) != 7:
return False
if not theString.startswith("T"):
return False
for c in theString[1:]:
if c not in "0123456789":
return False
return True
def isItemClass(theString):
"""Check if an item is a calid nwItemClass identifier.
"""
return theString in nwItemClass.__members__
def isItemType(theString):
"""Check if an item is a calid nwItemType identifier.
"""
return theString in nwItemType.__members__
def isItemLayout(theString):
"""Check if an item is a calid nwItemLayout identifier.
"""
return theString in nwItemLayout.__members__
def hexToInt(value, default=0):
"""Convert a hex string to an integer.
+91 -1
View File
@@ -26,7 +26,8 @@ import pytest
from nw.common import (
checkString, checkBool, checkInt, colRange, formatInt, transferCase,
fuzzyTime, checkHandle, formatTimeStamp, formatTime, hexToInt,
makeFileNameSafe
makeFileNameSafe, isHandle, isTitleTag, isItemClass, isItemType,
isItemLayout
)
from tools import cmpList
@@ -88,6 +89,95 @@ def testBaseCommon_CheckHandle():
# END Test testBaseCommon_CheckHandle
@pytest.mark.base
def testBaseCommon_IsHandle():
"""Test the isHandle function.
"""
assert isHandle("47666c91c7ccf")
assert not isHandle("47666C91C7CCF")
assert not isHandle("h7666c91c7ccf")
assert not isHandle("None")
assert not isHandle(None)
assert not isHandle("STUFF")
# END Test testBaseCommon_IsHandle
@pytest.mark.base
def testBaseCommon_IsTitleTag():
"""Test the isItemClass function.
"""
assert isTitleTag("T123456")
assert not isTitleTag("t123456")
assert not isTitleTag("S123456")
assert not isTitleTag("T12345A")
assert not isTitleTag("T1234567")
assert not isTitleTag("None")
assert not isTitleTag(None)
assert not isTitleTag("STUFF")
# END Test testBaseCommon_IsTitleTag
@pytest.mark.base
def testBaseCommon_IsItemClass():
"""Test the isItemClass function.
"""
assert isItemClass("NO_CLASS")
assert isItemClass("NOVEL")
assert isItemClass("PLOT")
assert isItemClass("CHARACTER")
assert isItemClass("WORLD")
assert isItemClass("TIMELINE")
assert isItemClass("OBJECT")
assert isItemClass("ENTITY")
assert isItemClass("CUSTOM")
assert isItemClass("ARCHIVE")
assert isItemClass("TRASH")
assert not isItemClass("None")
assert not isItemClass(None)
assert not isItemClass("STUFF")
# END Test testBaseCommon_IsItemClass
@pytest.mark.base
def testBaseCommon_IsItemType():
"""Test the isItemType function.
"""
assert isItemType("NO_TYPE")
assert isItemType("ROOT")
assert isItemType("FOLDER")
assert isItemType("FILE")
assert isItemType("TRASH")
assert not isItemType("None")
assert not isItemType(None)
assert not isItemType("STUFF")
# END Test testBaseCommon_IsItemType
@pytest.mark.base
def testBaseCommon_IsItemLayout():
"""Test the isItemLayout function.
"""
assert isItemLayout("NO_LAYOUT")
assert isItemLayout("TITLE")
assert isItemLayout("BOOK")
assert isItemLayout("PAGE")
assert isItemLayout("PARTITION")
assert isItemLayout("UNNUMBERED")
assert isItemLayout("CHAPTER")
assert isItemLayout("SCENE")
assert isItemLayout("NOTE")
assert not isItemLayout("None")
assert not isItemLayout(None)
assert not isItemLayout("STUFF")
# END Test testBaseCommon_IsItemLayout
@pytest.mark.base
def testBaseCommon_HexToInt():
"""Test the hexToInt function.