More tests for class Config

This commit is contained in:
Veronica K. B. Olsen
2019-05-16 20:27:56 +02:00
parent 89f118d62d
commit 63c3182a0e
2 changed files with 55 additions and 8 deletions
+15 -8
View File
@@ -117,7 +117,11 @@ class Config:
logger.debug("Loading config file") logger.debug("Loading config file")
confParser = configparser.ConfigParser() confParser = configparser.ConfigParser()
confParser.read_file(open(path.join(self.confPath,self.confFile))) try:
confParser.read_file(open(path.join(self.confPath,self.confFile)))
except Exception as e:
logger.error("Could not load config file")
return False
# Get options # Get options
@@ -182,7 +186,7 @@ class Config:
if confParser.has_option(cnfSec,"recent%d" % i): if confParser.has_option(cnfSec,"recent%d" % i):
self.recentList[i] = confParser.get(cnfSec,"recent%d" % i) self.recentList[i] = confParser.get(cnfSec,"recent%d" % i)
return return True
def saveConfig(self): def saveConfig(self):
@@ -266,13 +270,14 @@ class Config:
return return
def setConfPath(self, newPath): def setConfPath(self, newPath):
if newPath is None: return if newPath is None:
return True
if not path.isfile(newPath): if not path.isfile(newPath):
logger.error("Config: File not found. Using default config path instead.") logger.error("Config: File not found. Using default config path instead.")
return return False
self.confPath = path.dirname(newPath) self.confPath = path.dirname(newPath)
self.confFile = path.basename(newPath) self.confFile = path.basename(newPath)
return return True
def setWinSize(self, newWidth, newHeight): def setWinSize(self, newWidth, newHeight):
if abs(self.winGeometry[self.WIN_WIDTH] - newWidth) >= 10: if abs(self.winGeometry[self.WIN_WIDTH] - newWidth) >= 10:
@@ -281,14 +286,16 @@ class Config:
if abs(self.winGeometry[self.WIN_HEIGHT] - newHeight) >= 10: if abs(self.winGeometry[self.WIN_HEIGHT] - newHeight) >= 10:
self.winGeometry[self.WIN_HEIGHT] = newHeight self.winGeometry[self.WIN_HEIGHT] = newHeight
self.confChanged = True self.confChanged = True
return return True
def setTreeColWidths(self, colWidths): def setTreeColWidths(self, colWidths):
self.treeColWidth = colWidths self.treeColWidth = colWidths
return self.confChanged = True
return True
def setMainPanePos(self, panePos): def setMainPanePos(self, panePos):
self.mainPanePos = panePos self.mainPanePos = panePos
return self.confChanged = True
return True
# End Class Config # End Class Config
+40
View File
@@ -23,7 +23,47 @@ if path.isfile(tmpConf):
def testConfigInit(): def testConfigInit():
assert theConf.initConfig(testTemp) assert theConf.initConfig(testTemp)
assert cmpFiles(tmpConf, refConf, [2]) assert cmpFiles(tmpConf, refConf, [2])
assert not theConf.confChanged
def testConfigSave(): def testConfigSave():
assert theConf.saveConfig() assert theConf.saveConfig()
assert cmpFiles(tmpConf, refConf, [2]) assert cmpFiles(tmpConf, refConf, [2])
assert not theConf.confChanged
def testConfigSetConfPath():
assert theConf.setConfPath(None)
assert not theConf.setConfPath(path.join("somewhere","over","the","rainbow"))
assert theConf.setConfPath(path.join(testTemp,"novelwriter.conf"))
assert theConf.confPath == testTemp
assert theConf.confFile == "novelwriter.conf"
assert not theConf.confChanged
def testConfigLoad():
assert theConf.loadConfig()
assert not theConf.confChanged
def testConfigSetWinSize():
assert theConf.setWinSize(1105, 655)
assert not theConf.confChanged
assert theConf.setWinSize(70,70)
assert theConf.confChanged
assert theConf.setWinSize(1100, 650)
assert theConf.saveConfig()
assert cmpFiles(tmpConf, refConf, [2])
assert not theConf.confChanged
def testConfigSetTreeColWidths():
assert theConf.setTreeColWidths([0, 0, 0])
assert theConf.confChanged
assert theConf.setTreeColWidths([120, 30, 50])
assert theConf.saveConfig()
assert cmpFiles(tmpConf, refConf, [2])
assert not theConf.confChanged
def testConfigSetMainPanePos():
assert theConf.setMainPanePos([0, 0])
assert theConf.confChanged
assert theConf.setMainPanePos([300, 800])
assert theConf.saveConfig()
assert cmpFiles(tmpConf, refConf, [2])
assert not theConf.confChanged