Integrated unit testing framework with scons on Linux.

I needed this to make sure that BKE_copy_images works properly,
probably will be useful in future.

Using Check framework (http://check.sourceforge.net/doc/check.html/index.html).

WITH_BF_UNIT_TEST option builds 'alltest' program under [BUILDDIR]/bin, 
which, when executed, runs unit tests, currently only 1.

Example output:
----------------------------------------------------------------------
Running suite(s): Image
0%: Checks: 1, Failures: 1, Errors: 0
tests/alltest.c:74:F:Core:test_copy_images:0: Expected //bar/image.png to be translated to /tmp/bar/image.png, got /tmp/bar/image.pn.
----------------------------------------------------------------------

Spent lots of time (a couple of days actually :) to figure out how to
link the test program with Blender libraries. As it turned out there
are circular dependencies among Blender libraries. GCC by default
doesn't expect circular dependencies - dependant libs should precede
libs they depend on.

The magical --start-group linker option helped to solve this
(http://stephane.carrez.free.fr/doc/ld_2.html#IDX122).

Also:

- added bpy.util module. bpy.sys.* functions will move here later
- added bpy.util.copy_images that uses BKE_copy_images
- export_obj.py uses bpy.util.copy_images
This commit is contained in:
2009-07-07 08:38:18 +00:00
parent 2a23fda9d5
commit ad0b2c87d5
9 changed files with 291 additions and 15 deletions

View File

@@ -92,6 +92,9 @@ void BPY_update_modules( void )
PyObject *mod= PyImport_ImportModuleLevel("bpy", NULL, NULL, NULL, 0);
PyModule_AddObject( mod, "data", BPY_rna_module() );
PyModule_AddObject( mod, "types", BPY_rna_types() );
PyModule_AddObject( mod, "util", BPY_util_module() );
/* XXX this will move to bpy.util */
PyModule_AddObject( mod, "sys", BPY_sys_module() );
}

View File

@@ -25,12 +25,17 @@
#include "DNA_listBase.h"
#include "RNA_access.h"
#include "bpy_util.h"
#include "BLI_dynstr.h"
#include "bpy_rna.h"
#include "MEM_guardedalloc.h"
#include "BLI_dynstr.h"
#include "BLI_listbase.h"
#include "BKE_report.h"
#include "BKE_image.h"
#include "BKE_context.h"
bContext* __py_context = NULL;
bContext* BPy_GetContext(void) { return __py_context; };
void BPy_SetContext(bContext *C) { __py_context= C; };
@@ -464,3 +469,127 @@ int BPy_errors_to_report(ReportList *reports)
Py_DECREF(pystring);
return 1;
}
/* bpy.util module */
static PyObject *bpy_util_copy_images(PyObject *self, PyObject *args);
struct PyMethodDef bpy_util_methods[] = {
{"copy_images", bpy_util_copy_images, METH_VARARGS, NULL},
{NULL, NULL, 0, NULL}
};
#if PY_VERSION_HEX >= 0x03000000
static struct PyModuleDef bpy_util_module = {
PyModuleDef_HEAD_INIT,
"bpyutil",
NULL,
-1,
bpy_util_methods,
NULL, NULL, NULL, NULL
};
#endif
PyObject *BPY_util_module( void )
{
PyObject *submodule, *dict;
#if PY_VERSION_HEX >= 0x03000000
submodule= PyModule_Create(&bpy_util_module);
#else /* Py2.x */
submodule= Py_InitModule3("bpyutil", bpy_util_methods, NULL);
#endif
dict = PyModule_GetDict(submodule);
return submodule;
}
/*
copy_images(images, dest_dir)
return filenames
*/
static PyObject *bpy_util_copy_images(PyObject *self, PyObject *args)
{
const char *dest_dir;
ListBase *images;
ListBase *paths;
LinkData *link;
PyObject *seq;
PyObject *ret;
PyObject *item;
int i;
int len;
/* check args/types */
if (!PyArg_ParseTuple(args, "Os", &seq, &dest_dir)) {
PyErr_SetString(PyExc_TypeError, "Invalid arguments.");
return NULL;
}
/* expecting a sequence of Image objects */
if (!PySequence_Check(seq)) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of images.");
return NULL;
}
/* create image list */
len= PySequence_Size(seq);
if (!len) {
PyErr_SetString(PyExc_TypeError, "At least one image should be specified.");
return NULL;
}
/* make sure all sequence items are Image */
for(i= 0; i < len; i++) {
item= PySequence_GetItem(seq, i);
if (!BPy_StructRNA_Check(item) || ((BPy_StructRNA*)item)->ptr.type != &RNA_Image) {
PyErr_SetString(PyExc_TypeError, "Expected a sequence of Image objects.");
return NULL;
}
}
images= MEM_callocN(sizeof(*images), "ListBase of images");
for(i= 0; i < len; i++) {
BPy_StructRNA* srna;
item= PySequence_GetItem(seq, i);
srna= (BPy_StructRNA*)item;
link= MEM_callocN(sizeof(LinkData), "LinkData image");
link->data= srna->ptr.data;
BLI_addtail(images, link);
Py_DECREF(item);
}
paths= MEM_callocN(sizeof(*paths), "ListBase of image paths");
/* call BKE_copy_images */
BKE_copy_images(images, dest_dir, paths);
/* convert filenames */
ret= PyList_New(0);
len= BLI_countlist(paths);
for(link= paths->first, i= 0; link; link++, i++) {
if (link->data) {
item= PyUnicode_FromString(link->data);
PyList_Append(ret, item);
Py_DECREF(item);
}
else {
PyList_Append(ret, Py_None);
}
}
/* free memory */
BLI_freelistN(images);
BLI_freelistN(paths);
/* return filenames */
return ret;
}

View File

@@ -81,4 +81,6 @@ int BPy_errors_to_report(struct ReportList *reports);
struct bContext *BPy_GetContext(void);
void BPy_SetContext(struct bContext *C);
PyObject *BPY_util_module(void);
#endif