From 442565db50f56816910cac84851771a36a3fa747 Mon Sep 17 00:00:00 2001 From: azv Date: Tue, 18 Dec 2018 14:43:20 +0300 Subject: [PATCH] [Code coverage ExchangePlugin]: Error messages coverage --- .../ExchangePlugin_ExportFeature.cpp | 2 + .../ExchangePlugin_ImportFeature.cpp | 4 ++ src/ExchangePlugin/Test/TestExport.py | 32 ++++++++++++---- src/ExchangePlugin/Test/TestImport.py | 38 ++++++++++++------- 4 files changed, 56 insertions(+), 20 deletions(-) diff --git a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp index 407887dbc..66ada442a 100644 --- a/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ExportFeature.cpp @@ -411,9 +411,11 @@ void ExchangePlugin_ExportFeature::exportXAO(const std::string& theFileName) return; } +// LCOV_EXCL_START } catch (XAO::XAO_Exception& e) { std::string anError = e.what(); setError("An error occurred while exporting " + theFileName + ": " + anError); return; } +// LCOV_EXCL_STOP } diff --git a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp index b6464f07f..9ab9c61b2 100644 --- a/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp +++ b/src/ExchangePlugin/ExchangePlugin_ImportFeature.cpp @@ -332,11 +332,13 @@ void ExchangePlugin_ImportFeature::importXAO(const std::string& theFileName) ModelAPI_EventCreator::get()->sendReordered( std::dynamic_pointer_cast(aRefListOfGroups->owner())); +// LCOV_EXCL_START } catch (XAO::XAO_Exception& e) { std::string anError = e.what(); setError("An error occurred while importing " + theFileName + ": " + anError); return; } +// LCOV_EXCL_STOP } //============================================================================ @@ -373,6 +375,7 @@ std::shared_ptr ExchangePlugin_ImportFeature::subFeature( return aRes; } +// LCOV_EXCL_START int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const { std::shared_ptr aRefList = std::dynamic_pointer_cast< @@ -390,6 +393,7 @@ int ExchangePlugin_ImportFeature::subFeatureId(const int theIndex) const } return aResultIndex; } +// LCOV_EXCL_STOP bool ExchangePlugin_ImportFeature::isSub(ObjectPtr theObject) const { diff --git a/src/ExchangePlugin/Test/TestExport.py b/src/ExchangePlugin/Test/TestExport.py index 468cee23e..6af4500ff 100644 --- a/src/ExchangePlugin/Test/TestExport.py +++ b/src/ExchangePlugin/Test/TestExport.py @@ -50,7 +50,7 @@ def removeFile(theFileName): #========================================================================= # Common test function #========================================================================= -def testExport(theType, theFormat, theFile, theVolume, theDelta): +def testExport(theType, theFormat, theFile, theVolume, theDelta, theErrorExpected = False): # Import a reference part aSession.startOperation("Add part") aPartFeature = aSession.moduleDocument().addFeature("Part") @@ -76,12 +76,22 @@ def testExport(theType, theFormat, theFile, theVolume, theDelta): aSelectionListAttr.append(anImportFeature.firstResult(), anImportFeature.firstResult().shape()) aSession.finishOperation() - assert os.path.exists(theFile) + if theErrorExpected: + assert anExportFeature.error() != "" + aPart.removeFeature(anExportFeature) + else: + assert os.path.exists(theFile) - # Test exported file by importing - testImport(theType, theFile, theVolume, theDelta) + # Test exported file by importing + testImport(theType, theFile, theVolume, theDelta) + +def testExportXAO(theFile, theEmptyFormat = False): + type = "XAO" + format = "XAO" + if theEmptyFormat: + type = "Regular" + format = "" -def testExportXAO(theFile): # Import a reference part aSession.startOperation("Add part") aPartFeature = aSession.moduleDocument().addFeature("Part") @@ -140,8 +150,8 @@ def testExportXAO(theFile): aSession.startOperation("Export to XAO") anExportFeature = aPart.addFeature("Export") anExportFeature.string("xao_file_path").setValue(theFile) - anExportFeature.string("file_format").setValue("XAO") - anExportFeature.string("ExportType").setValue("XAO") + anExportFeature.string("file_format").setValue(type) + anExportFeature.string("ExportType").setValue(type) anExportFeature.string("xao_author").setValue("me") anExportFeature.string("xao_geometry_name").setValue("mygeom") aSession.finishOperation() @@ -159,22 +169,30 @@ if __name__ == '__main__': aRealVolume = 3.78827401738e-06 testExport("BREP", "BREP", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 10 ** -17) testExport("BRP", "BREP", os.path.join(tmp_dir, "screw_export.brp"), aRealVolume, 10 ** -17) + testExport("Regular", "", os.path.join(tmp_dir, "screw_export.brep"), aRealVolume, 10 ** -17) #========================================================================= # Export a shape into STEP #========================================================================= testExport("STEP", "STEP", os.path.join(tmp_dir, "screw_export.step"), 3.78825807533e-06, 10 ** -17) testExport("STP", "STEP", os.path.join(tmp_dir, "screw_export.stp"), 3.78825807533e-06, 10 ** -17) + testExport("Regular", "", os.path.join(tmp_dir, "screw_export.step"), 3.78825807533e-06, 10 ** -17) #========================================================================= # Export a shape into IGES #========================================================================= testExport("IGES-5.1", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.iges"), 3.78829613776e-06, 10 ** -17) testExport("IGS-5.1", "IGES-5.1", os.path.join(tmp_dir, "screw_export-5.1.igs"), 3.78829613776e-06, 10 ** -17) + testExport("Regular", "", os.path.join(tmp_dir, "screw_export-5.1.iges"), 3.78829613776e-06, 10 ** -17) testExport("IGES-5.3", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.iges"), 3.78827401651e-06, 10 ** -17) testExport("IGS-5.3", "IGES-5.3", os.path.join(tmp_dir, "screw_export-5.3.igs"), 3.78827401651e-06, 10 ** -17) #========================================================================= # Export a shape into XAO #========================================================================= testExportXAO(os.path.join(tmp_dir, "export.xao")) + testExportXAO(os.path.join(tmp_dir, "export.xao"), True) + #========================================================================= + # Check error when export to unsupported format + #========================================================================= + testExport("Regular", "", os.path.join(tmp_dir, "screw_export.dwg"), 3.78825807533e-06, 10 ** -17, True) #========================================================================= # End of test #========================================================================= diff --git a/src/ExchangePlugin/Test/TestImport.py b/src/ExchangePlugin/Test/TestImport.py index e32246013..f51836649 100644 --- a/src/ExchangePlugin/Test/TestImport.py +++ b/src/ExchangePlugin/Test/TestImport.py @@ -44,7 +44,7 @@ def getShapePath(path): shapes_dir = os.path.join(os.getenv("DATA_DIR"), "Shapes") return os.path.join(shapes_dir, path) -def testImport(theType, theFile, theVolume, theDelta): +def testImport(theType, theFile, theVolume, theDelta, theErrorExpected = False): # Create a part for import aSession.startOperation("Create part for import") aPartFeature = aSession.moduleDocument().addFeature("Part") @@ -61,18 +61,21 @@ def testImport(theType, theFile, theVolume, theDelta): file.setValue(theFile) aSession.finishOperation() - # Check results - assert anImportFeature.error() == '', "{0}: The error after execution: {1}".format(theType, anImportFeature.error()) - assert len(anImportFeature.results()) == 1, "{0}: Wrong number of results: expected = 1, real = {1}".format(theType, len(anImportFeature.results())) - aResultBody = modelAPI_ResultBody(anImportFeature.firstResult()) - assert aResultBody, "{0}: The result is not a body".format(theType) - aShape = aResultBody.shape() - assert aShape, "{0}: The body does not have a shape".format(theType) - - # Check shape volume - aRefVolume = theVolume - aResVolume = GeomAlgoAPI_ShapeTools.volume(aShape) - assert (math.fabs(aResVolume - aRefVolume) < theDelta), "{0}: The volume is wrong: expected = {1}, real = {2}".format(theType, aRefVolume, aResVolume) + if theErrorExpected: + assert anImportFeature.error() != '' + else: + # Check results + assert anImportFeature.error() == '', "{0}: The error after execution: {1}".format(theType, anImportFeature.error()) + assert len(anImportFeature.results()) == 1, "{0}: Wrong number of results: expected = 1, real = {1}".format(theType, len(anImportFeature.results())) + aResultBody = modelAPI_ResultBody(anImportFeature.firstResult()) + assert aResultBody, "{0}: The result is not a body".format(theType) + aShape = aResultBody.shape() + assert aShape, "{0}: The body does not have a shape".format(theType) + + # Check shape volume + aRefVolume = theVolume + aResVolume = GeomAlgoAPI_ShapeTools.volume(aShape) + assert (math.fabs(aResVolume - aRefVolume) < theDelta), "{0}: The volume is wrong: expected = {1}, real = {2}".format(theType, aRefVolume, aResVolume) def testImportXAO(): # Create a part for import @@ -154,6 +157,15 @@ if __name__ == '__main__': #========================================================================= testImportXAO() + #========================================================================= + # Check import errors + #========================================================================= + testImport("BREP", "", 0, 10 ** -25, True) + shape_path = getShapePath("Brep/solid.dwg") + testImport("BREP", shape_path, 0, 10 ** -25, True) + shape_path = getShapePath("Xao/wrong_file.xao") + testImport("XAO", shape_path, 0, 10 ** -25, True) + #========================================================================= # End of test #========================================================================= -- 2.39.2