58import xml.etree.cElementTree 
as etree
 
   62from random 
import shuffle
 
   63from datetime 
import datetime
 
   83    __fieldValueTestCaseSet = 
None 
  100    def __init__(self, ctx, filename, testRunName=None, randomOrder=False):
 
  109        self.
__testRunName = testRunName 
if testRunName 
else str(datetime.now())
 
 
  173    def next(self, verbose=False, saveEachCase=True):
 
  176            failedFieldList=failedFieldList, findAll=
True, verbose=verbose
 
  178            return False, failedFieldList
 
 
  201            print(
"finishing run")
 
 
  216            print(
"going to prev test case")
 
 
  229            print(
"going to case with id ", id)
 
 
  238    def __setTestCase(self):
 
 
  275    __fieldValueTestCaseDict = 
None 
  286        self.
__xmlRoot = etree.Element(
"FieldValueTestCaseSet")
 
 
  301                "There are already field-value test cases in this set. Loading not supported in this case!" 
  305        elif not os.path.isfile(filename):
 
  306            mevis.MLAB.logError(
"File %s does not exist." % (filename))
 
  310            xmlTree = etree.parse(filename)
 
  311            xmlRoot = xmlTree.getroot()
 
  318                for node 
in self.
__xmlRoot.findall(
"FieldValueTestCase"):
 
  319                    testCaseName = node.get(
"name")
 
 
  329        for node 
in self.
__xmlRoot.findall(
"FieldValueTestCase"):
 
  330            name = node.get(
"name")
 
 
  351    def add(self, fieldValueTestCase):
 
  353        name = fieldValueTestCase.getName()
 
  355            mevis.MLAB.logError(
"Test with name %s already known!" % (name))
 
  359            self.
__xmlRoot.append(fieldValueTestCase.save())
 
 
  371    def copy(self, source, destination):
 
  374            mevis.MLAB.logError(
"FieldValueTestCase with name %s not known!" % (source))
 
 
  400            mevis.MLAB.logError(
"FieldValueTestCase with name %s not known!" % (name))
 
 
  424            mevis.MLAB.logError(
"FieldValueTestCase with name %s not known!" % (name))
 
  427            mevis.MLAB.logError(
"FieldValueTestCase with name %s already exists!" % (newName))
 
  436                node.set(
"name", newName)
 
  440                if not fieldValueTestCase 
is None:
 
  441                    fieldValueTestCase.setName(newName)
 
 
  454        fieldValueTestCase = 
None 
  456            mevis.MLAB.logError(
"FieldValueTestCase with name %s not found!" % (name))
 
  464        return fieldValueTestCase
 
 
  469    def __verify(self, xmlRoot):
 
  470        if not xmlRoot.tag == 
"FieldValueTestCaseSet":
 
  473        for fieldValueTestCaseNode 
in xmlRoot:
 
  474            if not fieldValueTestCaseNode.tag == 
"FieldValueTestCase":
 
  475                mevis.MLAB.logError(
"Only FieldValueTestCase nodes allowed.")
 
  477            settingsNode = fieldValueTestCaseNode.find(
"Settings")
 
  478            resultsNode = fieldValueTestCaseNode.find(
"Results")
 
  479            if settingsNode 
is None or resultsNode 
is None:
 
  480                mevis.MLAB.logError(
"Settings and Results node must be defined.")
 
  482            if len(fieldValueTestCaseNode) != 2:
 
  483                mevis.MLAB.logError(
"Wrong number of children (%d instead of 2)." % (len(fieldValueTestCaseNode)))
 
  485            flList = [
False, 
False, 
False]
 
  486            for fieldListNode 
in settingsNode:
 
  487                if fieldListNode.tag == 
"FieldValueList":
 
  488                    name = fieldListNode.get(
"name")
 
  489                    if name == 
"Parameterization" and not flList[0]:
 
  491                    elif name == 
"ExpectedResults" and not flList[1]:
 
  495                            "Only FieldValueLists with name Parameterization or ExpectedResults allowed here." 
  498                elif fieldListNode.tag == 
"FieldList":
 
  499                    name = fieldListNode.get(
"name")
 
  500                    if name == 
"ResultsToSave" and not flList[2]:
 
  503                        mevis.MLAB.logError(
"Only FieldLists with name ResultsToSave allowed here (not %s)." % (name))
 
  507                        "Only FieldValueList and FieldList allowed here (not %s)." % (fieldListNode.tag)
 
  510                for child 
in fieldListNode:
 
  511                    if child.tag != 
"Field":
 
  512                        mevis.MLAB.logError(
"There must only be nodes of type Field.")
 
  514                    if not (child.get(
"module") != 
None and child.get(
"field") != 
None and child.get(
"type") != 
None):
 
  515                        mevis.MLAB.logError(
"Each field must have information on the module, field and type.")
 
  517            if not flList[0] 
or not flList[1] 
or not flList[2]:
 
  519                    "There must be FieldValueLists (with name Parameterization and ExpectedResults) and a FieldList (with name ResultsToSave)." 
  522            for fieldListNode 
in resultsNode:
 
  537    def __getFieldValueTestCaseNode(self, name):
 
  538        for node 
in self.
__xmlRoot.findall(
"FieldValueTestCase"):
 
  539            if node.get(
"name") == name:
 
  541        mevis.MLAB.logError(
"FieldValueTestCase %s not found in XML data-structure!" % (name))
 
 
  565    __fieldValueListDict = 
None 
  593        def add(self, fieldInfo, index=None):
 
  595            if not index 
is None:
 
  598                if index > len(self):
 
  602            return self._add(index, fieldInfo)
 
 
  612            if index >= 0 
and index < len(self):
 
 
  625        def swap(self, indexA, indexB):
 
  627            if indexA >= 0 
and indexA < len(self) 
and indexB >= 0 
and indexB < len(self) 
and indexA != indexB:
 
  629                self[indexA] = self[indexB]
 
 
  641            for fieldNode 
in xmlNode.findall(
"Field"):
 
  642                self.append(self._loadField(fieldNode))
 
 
  650            xmlNode = etree.Element(self.
_type, name=self.
__name)
 
  651            for fieldInfo 
in self:
 
  652                self._saveField(fieldInfo, xmlNode)
 
 
 
  667            FieldValueTestCase.FieldListBase.__init__(self, name)
 
 
  684        def _add(self, index, fieldInfo):
 
  685            self.insert(index, fieldInfo[0:3])
 
 
  694            return (xmlNode.get(
"module"), xmlNode.get(
"field"), xmlNode.get(
"type"))
 
 
  703            etree.SubElement(xmlNode, 
"Field", module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
 
 
 
  715            FieldValueTestCase.FieldListBase.__init__(self, name)
 
 
  724            fieldType = fieldInfo[2]
 
  726            self[index] = (fieldInfo[0], fieldInfo[1], fieldType, fieldValue)
 
 
  734        def _add(self, index, fieldInfo):
 
  735            self.insert(index, fieldInfo)
 
 
  744            fieldType = xmlNode.get(
"type")
 
  746            return (xmlNode.get(
"module"), xmlNode.get(
"field"), fieldType, fieldValue)
 
 
  755            if fieldInfo[2] 
not in (
"Trigger"):
 
  756                etree.SubElement(xmlNode, 
"Field", module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2]).text = (
 
  760                etree.SubElement(xmlNode, 
"Field", module=fieldInfo[0], field=fieldInfo[1], type=fieldInfo[2])
 
 
  769        def __convertValue(self, value, type):
 
  770            if type == 
"Integer":
 
  772            elif type 
in (
"Float", 
"Double"):
 
  774            elif "Vector" in type:
 
  775                value = tuple([float(x) 
for x 
in value[1:-1].split(
",")])
 
  777                value = value == 
"True" 
  778            elif type == 
"Trigger":
 
  781                if type 
not in (
"Enum", 
"String"):
 
  782                    mevis.MLAB.log(
"Maybe type %s should be supported?!" % (type))
 
 
  837            xmlNode = etree.Element(
"FieldValueTestCase", name=self.
__name)
 
  838            settingsNode = etree.SubElement(xmlNode, 
"Settings")
 
  839            resultsNode = etree.SubElement(xmlNode, 
"Results")
 
  841            settingsNode = xmlNode.find(
"Settings")
 
  842            resultsNode = xmlNode.find(
"Results")
 
 
  873            fieldName = 
"%s.%s" % (item[0], item[1])
 
  874            if item[2] 
in (
"Trigger"):
 
  875                self.
__ctx.field(fieldName).touch()
 
  877                changeSet.setFieldValue(fieldName, item[3], verbose=verbose)
 
 
  900            fieldName = 
"%s.%s" % (item[0], item[1])
 
  901            field = self.
__ctx.field(fieldName)
 
  904                    mevis.MLAB.logError(
"Unknown field: %s." % (fieldName))
 
  907                if field.type 
in (
"Float", 
"Double"):
 
  909                    match = abs(field.value - item[3]) < 0.0001
 
  911                    match = field.value == item[3]
 
  913                    failedFieldList.append(fieldName)
 
  916                            "Field %s: Value %s does not match expected value %s!" % (fieldName, field.value, item[3])
 
 
  942            mevis.MLAB.logError(
"Result with name %s available already." % (name))
 
  947                fldName = 
"%s.%s" % (item[0], item[1])
 
  948                if item[2] == 
"Trigger":
 
  949                    self.
__ctx.field(fldName).touch()
 
  952                    fldValue = self.
__ctx.field(fldName).value
 
  953                resList.append(tuple((item[0], item[1], item[2], fldValue)))
 
 
  972            mevis.MLAB.logError(
"Result with name %s available already." % (name))
 
  978                resToSaveList.append(
"%s.%s" % (item[0], item[1]))
 
  979            for item 
in resultList:
 
  980                field = 
"%s.%s" % (item[0], item[1])
 
  981                if not field 
in resToSaveList:
 
  982                    mevis.MLAB.logError(
"Field %s not in list of results to save. Ignoring it." % (field))
 
 
  995            mevis.MLAB.logError(
"No result with name %s available." % (name))
 
 
 1004    def __load(self, xmlNode):
 
 1005        node = xmlNode.find(
"Settings")
 
 1006        for fieldValueListNode 
in node.findall(
"FieldValueList"):
 
 1008        for fieldListNode 
in node.findall(
"FieldList"):
 
 1010        for resultNode 
in xmlNode.findall(
"Results/FieldValueList"):
 
 1011            name = resultNode.get(
"name")
 
 
Class to handle field changes and make them revertable.
 
A class to iterate over the list of field-value test cases of a given set.
 
next(self, verbose=False, saveEachCase=True)
Change to the next field-value test case.
 
goToCaseWithId(self, id, verbose=False)
Go to test case with specified id.
 
getStatusString(self)
Return a string containing the current case id, current case name and number of cases.
 
getNumCases(self)
Return the number of cases of the loaded test case set.
 
getCurrentIndex(self)
Return the index of the current case.
 
prev(self, verbose=False)
Go back to the previous test case.
 
isFinished(self)
Are there any test cases left to iterate?
 
getCurrentCaseName(self)
Return the index of the current case.
 
__init__(self, ctx, filename, testRunName=None, randomOrder=False)
The default constructor.
 
finish(self, verbose=False)
Finish the current test run.
 
A class collecting a set of field-value test cases.
 
get(self, name)
Return the field-value test case with the given name.
 
save(self, filename)
Save the XML data structure.
 
load(self, filename)
Try to load the field-value test case set from the given XML file.
 
getList(self)
Return the list of names of the existing field-value test cases.
 
rename(self, name, newName)
Rename a given field-value test case.
 
copy(self, source, destination)
Create a copy of the given field-value test case with the given new name.
 
__getFieldValueTestCaseNode(self, name)
 
add(self, fieldValueTestCase)
Add the given field-value test case to the FieldValueTestCaseSet.
 
remove(self, name)
Remove the field-value test case with the given name.
 
__init__(self, context)
The default constructor.
 
Superclass for the field lists used in the field-value test case.
 
save(self)
Save the field information to XML.
 
add(self, fieldInfo, index=None)
Add the field with given info to the list.
 
remove(self, index)
Remove the element at the given index.
 
load(self, xmlNode)
Load the given xmlNode.
 
swap(self, indexA, indexB)
Swap the given two elements.
 
__init__(self, name)
Default constructor.
 
_type
The type of list used.
 
List of field information.
 
update(self, index, fieldInfo)
Update the values of the given fields.
 
_saveField(self, fieldInfo, xmlNode)
Save the given field information to the given XML node.
 
_loadField(self, xmlNode)
Load field information from the given XML node.
 
_add(self, index, fieldInfo)
Add the values of the given fields.
 
__init__(self, name)
Default constructor.
 
List of field information including field values.
 
__init__(self, name)
Default constructor.
 
_add(self, index, fieldInfo)
Add the values of the given fields.
 
update(self, index, fieldInfo)
Update the values of the given fields.
 
_loadField(self, xmlNode)
Load field information from the given XML node.
 
_saveField(self, fieldInfo, xmlNode)
Save the given field information to the given XML node.
 
__convertValue(self, value, type)
 
A class implementing the field-value test cases.
 
verifyExpectedResults(self, failedFieldList=[], findAll=False, verbose=False)
Use the given field-value list to verify the given context's field values.
 
__init__(self, name, ctx, xmlNode=None)
The default constructor.
 
addSavedResultList(self, name, resultList)
Add saved results.
 
setName(self, name)
Set the name of the field-value test case.
 
getSavedResultList(self)
Get a sorted list of names of the saved results.
 
getResultsToSave(self)
Return the results to save.
 
getExpectedResults(self)
Getter for the list of expected results.
 
getSavedResult(self, name)
Get the field-value list saved under the given name.
 
save(self, xmlNode=None)
Save the settings of this field-value test case to an XML node.
 
applyParameterization(self, changeSet, verbose=False)
Apply the parameterization to the context given in the constructor.
 
saveResults(self, name, overwrite=False)
Save the given field values to the xml data structure.
 
getParameterization(self)
Return the parameterization set.
 
getName(self)
Return the name of the field-value test case.