New test for OptionsState class

This commit is contained in:
Veronica K. B. Olsen
2020-12-01 23:27:29 +01:00
parent 7ec43e42fd
commit fba4d5d04d
3 changed files with 145 additions and 22 deletions
+3 -15
View File
@@ -176,11 +176,7 @@ class OptionState():
"""
if getGroup in self.theState:
if getName in self.theState[getGroup]:
try:
return self.theState[getGroup][getName]
except Exception as e:
logger.warning(str(e))
return defaultValue
return self.theState[getGroup][getName]
return defaultValue
def getString(self, getGroup, getName, defaultValue):
@@ -189,11 +185,7 @@ class OptionState():
"""
if getGroup in self.theState:
if getName in self.theState[getGroup]:
try:
return str(self.theState[getGroup][getName])
except Exception as e:
logger.warning(str(e))
return defaultValue
return str(self.theState[getGroup][getName])
return defaultValue
def getInt(self, getGroup, getName, defaultValue):
@@ -228,11 +220,7 @@ class OptionState():
"""
if getGroup in self.theState:
if getName in self.theState[getGroup]:
try:
return bool(self.theState[getGroup][getName])
except Exception as e:
logger.warning(str(e))
return defaultValue
return bool(self.theState[getGroup][getName])
return defaultValue
##
+8 -7
View File
@@ -59,11 +59,12 @@ Available markers are:
To filter specific groups of tests, use the `-k` switch.
The commands for the respective test categories are listed below.
| Type | Test Target | Source File(s) | Marker | Filter |
| :--- | :------------- | :------------------ | :-------- | :-------------------- |
| Unit | Core functions | nw/core/tools.py | `-m core` | `-k testCoreTools` |
| Unit | NWDoc class | nw/core/document.py | `-m core` | `-k testCoreDocument` |
| Unit | NWIndex class | nw/core/index.py | `-m core` | `-k testCoreIndex` |
| Unit | NWItem class | nw/core/item.py | `-m core` | `-k testCoreItem` |
| Unit | NWTree class | nw/core/tree.py | `-m core` | `-k testCoreTree` |
| Type | Test Target | Source File(s) | Marker | Filter |
| :--- | :----------------- | :------------------ | :-------- | :-------------------- |
| Unit | Core functions | nw/core/tools.py | `-m core` | `-k testCoreTools` |
| Unit | NWDoc class | nw/core/document.py | `-m core` | `-k testCoreDocument` |
| Unit | NWIndex class | nw/core/index.py | `-m core` | `-k testCoreIndex` |
| Unit | NWItem class | nw/core/item.py | `-m core` | `-k testCoreItem` |
| Unit | NWTree class | nw/core/tree.py | `-m core` | `-k testCoreTree` |
| Unit | OptionsState class | nw/core/options.py | `-m core` | `-k testCoreOptions` |
+134
View File
@@ -0,0 +1,134 @@
# -*- coding: utf-8 -*-
"""novelWriter OptionState Class Tester
"""
import os
import json
import pytest
from nw.core import NWProject
from nw.core.options import OptionState
from nw.constants import nwFiles
@pytest.mark.core
def testCoreOptions_LoadSave(monkeypatch, dummyGUI, tmpDir):
"""Test loading and saving from the OptionState class.
"""
theProject = NWProject(dummyGUI)
theOpts = OptionState(theProject)
# Write a test file
optFile = os.path.join(tmpDir, nwFiles.OPTS_FILE)
with open(optFile, mode="w+", encoding="utf8") as outFile:
json.dump({
"GuiBuildNovel": {
"winWidth": 1000,
"winHeight": 700,
"addNovel": True,
"addNotes": False,
"textFont": "Cantarell",
"dummyItem": None,
},
"DummyGroup": {
"dummyItem": None,
},
}, outFile)
# Load and save with no path set
theProject.projMeta = None
assert not theOpts.loadSettings()
assert not theOpts.saveSettings()
# Set path
theProject.projMeta = tmpDir
assert theProject.projMeta == tmpDir
# Cause open() to fail
def dummyIO(*args, **kwargs):
raise OSError
monkeypatch.setattr("builtins.open", dummyIO)
assert not theOpts.loadSettings()
assert not theOpts.saveSettings()
monkeypatch.undo()
# Load proper
assert theOpts.loadSettings()
# Check that unwanted items have been removed
assert theOpts.theState == {
"GuiBuildNovel": {
"winWidth": 1000,
"winHeight": 700,
"addNovel": True,
"addNotes": False,
"textFont": "Cantarell",
},
}
# Save proper
assert theOpts.saveSettings()
# Load again to check we get the values back
assert theOpts.loadSettings()
assert theOpts.theState == {
"GuiBuildNovel": {
"winWidth": 1000,
"winHeight": 700,
"addNovel": True,
"addNotes": False,
"textFont": "Cantarell",
},
}
# END Test testCoreOptions_LoadSave
@pytest.mark.core
def testCoreOptions_SetGet(monkeypatch, dummyGUI, tmpDir):
"""Test setting and getting values from the OptionState class.
"""
theProject = NWProject(dummyGUI)
theOpts = OptionState(theProject)
# Set invalid values
assert not theOpts.setValue("DummyGroup", "dummyItem", None)
assert not theOpts.setValue("GuiBuildNovel", "dummyItem", None)
# Set valid value
assert theOpts.setValue("GuiBuildNovel", "winWidth", 100)
# Set some values of different types
assert theOpts.setValue("GuiBuildNovel", "winWidth", 100)
assert theOpts.setValue("GuiBuildNovel", "winHeight", 12.34)
assert theOpts.setValue("GuiBuildNovel", "addNovel", True)
assert theOpts.setValue("GuiBuildNovel", "textFont", "Cantarell")
# Generic get, doesn't check type
assert theOpts.getValue("GuiBuildNovel", "winWidth", None) == 100
assert theOpts.getValue("GuiBuildNovel", "winHeight", None) == 12.34
assert theOpts.getValue("GuiBuildNovel", "addNovel", None) is True
assert theOpts.getValue("GuiBuildNovel", "textFont", None) == "Cantarell"
assert theOpts.getValue("GuiBuildNovel", "dummyItem", None) is None
# Get type-specific
assert theOpts.getString("GuiBuildNovel", "winWidth", None) == "100"
assert theOpts.getString("GuiBuildNovel", "dummyItem", None) is None
assert theOpts.getInt("GuiBuildNovel", "winWidth", None) == 100
assert theOpts.getInt("GuiBuildNovel", "textFont", None) is None
assert theOpts.getInt("GuiBuildNovel", "dummyItem", None) is None
assert theOpts.getFloat("GuiBuildNovel", "winWidth", None) == 100.0
assert theOpts.getFloat("GuiBuildNovel", "textFont", None) is None
assert theOpts.getFloat("GuiBuildNovel", "dummyItem", None) is None
assert theOpts.getBool("GuiBuildNovel", "addNovel", None) is True
assert theOpts.getBool("GuiBuildNovel", "dummyItem", None) is None
# Check integer validators
assert theOpts.validIntRange(5, 0, 9, 3) == 5
assert theOpts.validIntRange(5, 0, 4, 3) == 3
assert theOpts.validIntRange(5, 0, 5, 3) == 5
assert theOpts.validIntRange(0, 0, 5, 3) == 0
assert theOpts.validIntTuple(0, (0, 1, 2), 3) == 0
assert theOpts.validIntTuple(5, (0, 1, 2), 3) == 3
# END Test testCoreOptions_SetGet