Salome HOME
[Code coverage ParametersPlugin]: Unit tests for parameter remove and for error treating
authorazv <azv@opencascade.com>
Thu, 6 Dec 2018 07:05:26 +0000 (10:05 +0300)
committerazv <azv@opencascade.com>
Thu, 6 Dec 2018 07:05:26 +0000 (10:05 +0300)
src/ParametersAPI/ParametersAPI_Parameter.cpp
src/ParametersAPI/ParametersAPI_Parameter.h
src/ParametersPlugin/CMakeLists.txt
src/ParametersPlugin/ParametersPlugin_Validators.cpp
src/ParametersPlugin/Test/TestParameterDelete.py [new file with mode: 0644]
src/ParametersPlugin/Test/TestParameterErrorMsg.py [new file with mode: 0644]
src/PythonAPI/model/parameter/__init__.py

index dac7f0aa30707da709887f763c9753fce9f98b7d..419f778f10cdbfe058e151c67f6823228bdd6438 100644 (file)
@@ -23,6 +23,7 @@
 #include <ModelHighAPI_Dumper.h>
 #include <ModelHighAPI_Tools.h>
 #include <ModelAPI_ResultParameter.h>
+#include <ModelAPI_Events.h>
 //--------------------------------------------------------------------------------------
 ParametersAPI_Parameter::ParametersAPI_Parameter(
     const std::shared_ptr<ModelAPI_Feature> & theFeature)
@@ -92,3 +93,14 @@ ParameterPtr addParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
   std::shared_ptr<ModelAPI_Feature> aFeature = thePart->addFeature(ParametersAPI_Parameter::ID());
   return ParameterPtr(new ParametersAPI_Parameter(aFeature, theName, theExpression, theComment));
 }
+
+//--------------------------------------------------------------------------------------
+void removeParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
+                     const ParameterPtr & theParameter)
+{
+  FeaturePtr aParam = theParameter->feature();
+  if (aParam) {
+    ModelAPI_ReplaceParameterMessage::send(aParam, 0);
+    thePart->removeFeature(aParam);
+  }
+}
index 7abc34850c0fd9a3123d5e9f853f3914bc0a1ad5..6d444d9f6c4c054a133a17205c2248b494d914ab 100644 (file)
@@ -83,6 +83,13 @@ ParameterPtr addParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
                           const std::string & theExpression,
                           const std::string & theComment = std::string());
 
+/**\ingroup CPPHighAPI
+ * \brief Remove Parameter feature and substitute it by the value in referred features
+ */
+PARAMETERSAPI_EXPORT
+void removeParameter(const std::shared_ptr<ModelAPI_Document> & thePart,
+                     const ParameterPtr & theParameter);
+
 //--------------------------------------------------------------------------------------
 //--------------------------------------------------------------------------------------
 #endif /* SRC_PARAMETERSAPI_PARAMETERSAPI_PARAMETER_H_ */
index 07d2bf0b8ac30f26b0c90dde36c231000208d84c..8cbc233f3725c26fe875ef991d4ea1f09dce937c 100644 (file)
@@ -116,6 +116,8 @@ INSTALL(FILES ${QM_RESOURCES} DESTINATION ${SHAPER_INSTALL_QM_RESOURCES})
 ADD_UNIT_TESTS(TestParameterCreation.py
                TestParameterRename.py
                TestParameterChangeValue.py
+               TestParameterDelete.py
+               TestParameterErrorMsg.py
                TestParametersMgr.py
                Test1806.py
                Test2392.py
index aaa647d9d124d502a4576e52fd2f3c9ae4ffbcdf..dc0c766e66917b94a68fb0c8979f13494922d42e 100644 (file)
@@ -100,8 +100,6 @@ bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttrib
                                                    Events_InfoMessage& theError) const
 {
   FeaturePtr aFeature = std::dynamic_pointer_cast<ModelAPI_Feature>(theAttribute->owner());
-  ResultParameterPtr aParam =
-      std::dynamic_pointer_cast<ModelAPI_ResultParameter>(aFeature->firstResult());
 
   AttributeStringPtr aStrAttr =
       std::dynamic_pointer_cast<ModelAPI_AttributeString>(theAttribute);
@@ -116,11 +114,6 @@ bool ParametersPlugin_ExpressionValidator::isValid(const AttributePtr& theAttrib
     return false;
   }
 
-  if (!aParam.get()) {
-    theError = "Result is empty.";
-    return false;
-  }
-
   theError = aFeature->string(ParametersPlugin_Parameter::EXPRESSION_ERROR_ID())->value();
   return theError.empty();
 }
diff --git a/src/ParametersPlugin/Test/TestParameterDelete.py b/src/ParametersPlugin/Test/TestParameterDelete.py
new file mode 100644 (file)
index 0000000..c1db59d
--- /dev/null
@@ -0,0 +1,53 @@
+## Copyright (C) 2018-20xx  CEA/DEN, EDF R&D
+##
+## This library is free software; you can redistribute it and/or
+## modify it under the terms of the GNU Lesser General Public
+## License as published by the Free Software Foundation; either
+## version 2.1 of the License, or (at your option) any later version.
+##
+## This library is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this library; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+##
+## See http:##www.salome-platform.org/ or
+## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+##
+
+from salome.shaper import model
+from ModelAPI import *
+import math
+
+def assertLineLength(theLine, theLength):
+    dx = theLine.startPoint().x() - theLine.endPoint().x()
+    dy = theLine.startPoint().y() - theLine.endPoint().y()
+    assert(math.fabs(dx**2 + dy**2 - theLength**2)  < 1.e-8), "Line length {} is not equal to expected {}".format(math.sqrt(dx**2 + dy**2), theLength)
+
+
+model.begin()
+partSet = model.moduleDocument()
+Part_1 = model.addPart(partSet)
+Part_1_doc = Part_1.document()
+ParamLen = model.addParameter(Part_1_doc, "Len", "100")
+Sketch_1 = model.addSketch(Part_1_doc, model.defaultPlane("XOY"))
+SketchLine_1 = Sketch_1.addLine(0, -30, 100, -30)
+SketchConstraintLength_1 = Sketch_1.setLength(SketchLine_1.result(), "Len")
+model.do()
+
+aLength = ParamLen.value()
+assertLineLength(SketchLine_1, aLength)
+
+# delete parameter
+model.removeParameter(Part_1_doc, ParamLen)
+model.do()
+
+# move line and check the constraint is still here
+SketchLine_1.startPoint().setValue(SketchLine_1.startPoint().x() + 10., SketchLine_1.startPoint().y())
+model.do()
+assertLineLength(SketchLine_1, aLength)
+
+model.end()
diff --git a/src/ParametersPlugin/Test/TestParameterErrorMsg.py b/src/ParametersPlugin/Test/TestParameterErrorMsg.py
new file mode 100644 (file)
index 0000000..1030c77
--- /dev/null
@@ -0,0 +1,37 @@
+## Copyright (C) 2018-20xx  CEA/DEN, EDF R&D
+##
+## This library is free software; you can redistribute it and/or
+## modify it under the terms of the GNU Lesser General Public
+## License as published by the Free Software Foundation; either
+## version 2.1 of the License, or (at your option) any later version.
+##
+## This library is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+## Lesser General Public License for more details.
+##
+## You should have received a copy of the GNU Lesser General Public
+## License along with this library; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+##
+## See http:##www.salome-platform.org/ or
+## email : webmaster.salome@opencascade.com<mailto:webmaster.salome@opencascade.com>
+##
+
+from salome.shaper import model
+
+model.begin()
+partSet = model.moduleDocument()
+# check error on empty name
+Param1 = model.addParameter(partSet, "", "100")
+assert(Param1.feature().error() != "")
+# check error on empty value
+Param2 = model.addParameter(partSet, "L", "")
+assert(Param2.feature().error() != "")
+# check error if name is not variable
+Param3 = model.addParameter(partSet, "100", "100")
+assert(Param3.feature().error() != "")
+# check error on wrong value expression
+Param4 = model.addParameter(partSet, "N", "+-.so&@")
+assert(Param4.feature().error() != "")
+model.end()
index fb276792dfedfbdaf0d7c7b29dfa0ae377b056b8..12020f85013a0e076d618d52cf3ad61a1b682cdb 100644 (file)
@@ -1,4 +1,4 @@
 """Package for Parameter plugin for the Parametric Geometry API of the Modeler.
 """
 
-from ParametersAPI import addParameter
\ No newline at end of file
+from ParametersAPI import addParameter, removeParameter
\ No newline at end of file