App: Fix Metadata Py constructors
Also adds unit tests for the corrected Copy and Default constructors.
This commit is contained in:
@@ -48,36 +48,32 @@ std::string MetadataPy::representation(void) const
|
||||
return str.str();
|
||||
}
|
||||
|
||||
PyObject* MetadataPy::PyMake(struct _typeobject*, PyObject* args, PyObject*) // Python wrapper
|
||||
PyObject* MetadataPy::PyMake(struct _typeobject*, PyObject*, PyObject*) // Python wrapper
|
||||
{
|
||||
// create a new instance of MetadataPy and the Twin object
|
||||
const char* filename;
|
||||
if (!PyArg_ParseTuple(args, "s", &filename))
|
||||
return nullptr;
|
||||
try {
|
||||
auto md = new Metadata(filename);
|
||||
return new MetadataPy(md);
|
||||
}
|
||||
catch (...) {
|
||||
PyErr_SetString(Base::BaseExceptionFreeCADError, "Failed to create Metadata object");
|
||||
return nullptr;
|
||||
}
|
||||
return new MetadataPy(nullptr);
|
||||
}
|
||||
|
||||
// constructor method
|
||||
int MetadataPy::PyInit(PyObject* args, PyObject* /*kwd*/)
|
||||
{
|
||||
if (PyArg_ParseTuple(args, "")) {
|
||||
setTwinPointer(new Metadata());
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Main class constructor -- takes a file path, loads the metadata from it
|
||||
PyErr_Clear();
|
||||
const char* file;
|
||||
if (PyArg_ParseTuple(args, "s", &file)) {
|
||||
App::Metadata a(file);
|
||||
*(getMetadataPtr()) = a;
|
||||
return 0;
|
||||
const char* filename;
|
||||
if (PyArg_ParseTuple(args, "s", &filename)) {
|
||||
try {
|
||||
auto md = new Metadata(filename);
|
||||
setTwinPointer(md);
|
||||
return 0;
|
||||
}
|
||||
catch (...) {
|
||||
PyErr_SetString(Base::BaseExceptionFreeCADError, "Failed to create Metadata object");
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
// Copy constructor
|
||||
@@ -85,11 +81,11 @@ int MetadataPy::PyInit(PyObject* args, PyObject* /*kwd*/)
|
||||
PyObject* o;
|
||||
if (PyArg_ParseTuple(args, "O!", &(App::MetadataPy::Type), &o)) {
|
||||
App::Metadata* a = static_cast<App::MetadataPy*>(o)->getMetadataPtr();
|
||||
*(getMetadataPtr()) = *a;
|
||||
setTwinPointer(new Metadata(*a));
|
||||
return 0;
|
||||
}
|
||||
|
||||
PyErr_SetString(Base::BaseExceptionFreeCADError, "path to metadata file expected");
|
||||
PyErr_SetString(Base::BaseExceptionFreeCADError, "metadata object or path to metadata file expected");
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
@@ -55,6 +55,20 @@ class TestMetadata(unittest.TestCase):
|
||||
tags = md.Tag
|
||||
self.assertEqual(len(tags), 2)
|
||||
|
||||
def test_copy_constructor(self):
|
||||
filename = os.path.join(self.test_dir, "basic_metadata.xml")
|
||||
md = FreeCAD.Metadata(filename)
|
||||
copy_of_md = FreeCAD.Metadata(md)
|
||||
self.assertEqual(md.Name, copy_of_md.Name)
|
||||
self.assertEqual(md.Description, copy_of_md.Description)
|
||||
self.assertEqual(md.Version, copy_of_md.Version)
|
||||
|
||||
def test_default_constructor(self):
|
||||
try:
|
||||
md = FreeCAD.Metadata()
|
||||
except Exception:
|
||||
self.fail("Metadata default constructor failed")
|
||||
|
||||
def test_content_types(self):
|
||||
pass
|
||||
|
||||
|
||||
Reference in New Issue
Block a user