Test: add test feature for unit tests

This commit is contained in:
wmayer
2022-08-22 15:46:40 +02:00
parent 71015d23d0
commit 8efe30c8a9
4 changed files with 101 additions and 0 deletions

View File

@@ -1996,6 +1996,7 @@ void Application::initTypes()
App::FeatureTestException ::init();
App::FeatureTestColumn ::init();
App::FeatureTestPlacement ::init();
App::FeatureTestAttribute ::init();
// Feature class
App::FeaturePython ::init();

View File

@@ -24,10 +24,14 @@
#include "PreCompiled.h"
#ifndef _PreComp_
#include <boost/core/ignore_unused.hpp>
#include <sstream>
#endif
#include <Base/Console.h>
#include <Base/Exception.h>
#include <Base/Interpreter.h>
#include <Base/Unit.h>
#include <CXX/Objects.hxx>
#include "FeatureTest.h"
#include "Material.h"
@@ -235,3 +239,56 @@ DocumentObjectExecReturn *FeatureTestPlacement::execute()
MultRight.setValue(q1.multRight(p2));
return nullptr;
}
// ----------------------------------------------------------------------------
PROPERTY_SOURCE(App::FeatureTestAttribute, App::DocumentObject)
FeatureTestAttribute::FeatureTestAttribute()
{
ADD_PROPERTY(Object, (Py::Object()));
ADD_PROPERTY(Attribute, ("Name"));
}
FeatureTestAttribute::~FeatureTestAttribute()
{
Base::PyGILStateLocker lock;
try {
Object.getValue().getAttr("Name");
#if PYCXX_VERSION_MAJOR >= 7
Py::ifPyErrorThrowCxxException();
#else
if (PyErr_Occurred())
throw Py::RuntimeError();
#endif
}
catch (Py::RuntimeError& e) {
e.clear();
}
catch (Py::Exception& e) {
e.clear();
Base::Console().Error("Unexpected exception in ~FeatureTestRemoval()\n");
}
}
DocumentObjectExecReturn *FeatureTestAttribute::execute()
{
Base::PyGILStateLocker lock;
try {
Object.getValue().getAttr(Attribute.getValue());
#if PYCXX_VERSION_MAJOR >= 7
Py::ifPyErrorThrowCxxException();
#else
if (PyErr_Occurred())
throw Py::AttributeError();
#endif
}
catch (Py::AttributeError& e) {
e.clear();
std::stringstream str;
str << "No such attribute '" << Attribute.getValue() << "'";
throw Base::AttributeError(str.str());
}
return StdReturn;
}

View File

@@ -27,6 +27,7 @@
#include "DocumentObject.h"
#include "PropertyGeo.h"
#include "PropertyLinks.h"
#include "PropertyPythonObject.h"
#include "PropertyUnits.h"
@@ -171,6 +172,19 @@ public:
//@}
};
class FeatureTestAttribute : public DocumentObject
{
PROPERTY_HEADER_WITH_OVERRIDE(App::FeatureTestAttribute);
public:
FeatureTestAttribute();
~FeatureTestAttribute() override;
DocumentObjectExecReturn *execute() override;
App::PropertyPythonObject Object;
App::PropertyString Attribute;
};
} //namespace App

View File

@@ -2306,3 +2306,32 @@ class FeatureTestColumn(unittest.TestCase):
def tearDown(self):
FreeCAD.closeDocument("TestColumn")
class FeatureTestAttribute(unittest.TestCase):
def setUp(self):
self.doc = FreeCAD.newDocument("TestAttribute")
self.doc.UndoMode = 0
def testValidAttribute(self):
obj = self.doc.addObject("App::FeatureTestAttribute", "Attribute")
obj.Object = obj
obj.Attribute = "Name"
self.doc.recompute()
self.assertIn("Up-to-date", obj.State)
def testInvalidAttribute(self):
obj = self.doc.addObject("App::FeatureTestAttribute", "Attribute")
obj.Object = obj
obj.Attribute = "Name123"
self.doc.recompute()
self.assertIn("Invalid", obj.State)
self.assertIn("Touched", obj.State)
def testRemoval(self):
obj = self.doc.addObject("App::FeatureTestAttribute", "Attribute")
obj.Object = obj
self.assertEqual(self.doc.removeObject("Attribute"), None)
def tearDown(self):
FreeCAD.closeDocument("TestAttribute")