Skip to content

Commit 717ff3b

Browse files
author
Nima Talebi
committed
Expanding the test case to include the POC demo
The POC demo does not actually do much testing yet, other than just working or not working - but it's in place now for future enhancements.
1 parent cc76255 commit 717ff3b

9 files changed

Lines changed: 718 additions & 0 deletions

File tree

unit-tests/Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
## This one is important to get right ...
2+
## We need to link in the libxml2mod.so file from here
3+
PYLIBDIR := /usr/lib64/python2.5/site-packages
4+
PYLIBDIR := /usr/lib/python-support/python-libxml2/python2.5
5+
6+
# Defaults, should be fine
7+
CFLAGS=-I. $(shell xml2-config --cflags) -g -Wall $(shell python-config --cflags)
8+
9+
LIBS=$(shell xml2-config --libs) -lxml2mod $(shell python-config --libs)
10+
LIBDIR=-L $(PYLIBDIR)
11+
12+
.SUFFIX=.c .o .so
13+
14+
all : test
15+
16+
demomodule.so : demo.o dmixml.o
17+
@echo "Linking: $@"
18+
@gcc -fPIC --shared -o $@ $^ $(LIBS) $(LIBDIR)
19+
20+
.c.o :
21+
@echo "Compiling $<"
22+
@gcc -fPIC -c $< $(CFLAGS)
23+
24+
test : demomodule.so
25+
@echo "=========================================="
26+
@echo " Running proof-of-concept code"
27+
@echo "=========================================="
28+
@echo ""
29+
@python unit
30+
31+
clean :
32+
rm -f demomodule.so *.{py[oc],o} *~
33+

unit-tests/POCDemo.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import libxml2
2+
import demomodule # This is our core module
3+
4+
class POCDemo:
5+
"""Demo of a wrapper class to return proper python libxml2 objects"""
6+
7+
def GetXMLdoc(self):
8+
return libxml2.xmlDoc( _obj = demomodule.dump_doc() )
9+
10+
def GetXMLnode(self):
11+
return libxml2.xmlNode( _obj = demomodule.dump_node() )
12+

unit-tests/demo.c

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* This program is free software; you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation; either version 2 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program; if not, write to the Free Software
14+
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
15+
*
16+
* For the avoidance of doubt the "preferred form" of this code is one which
17+
* is in an open unpatent encumbered format. Where cryptographic key signing
18+
* forms part of the process of creating an executable the information
19+
* including keys needed to generate an equivalently functional executable
20+
* are deemed to be part of the source code.
21+
*/
22+
23+
#include "demo.h"
24+
25+
xmlNode *gen_nodes(const char *entry ) {
26+
xmlNode *c_xmlNode_root = NULL;
27+
xmlNode *c_xmlNode_tag = NULL;
28+
29+
// Prepare a root node
30+
c_xmlNode_root = xmlNewNode(NULL, (xmlChar *) "dmixml_demo");
31+
assert( c_xmlNode_root != NULL );
32+
33+
dmixml_AddAttribute(c_xmlNode_root, "entrypoint", "%s", entry);
34+
35+
// Populate XML
36+
dmixml_AddTextChild(c_xmlNode_root, "Test", "Yes, just testing");
37+
38+
c_xmlNode_tag = dmixml_AddTextChild(c_xmlNode_root, "tag1", "Another test");
39+
dmixml_AddAttribute(c_xmlNode_tag, "TestTagID", "%i", 1);
40+
41+
c_xmlNode_tag = c_xmlNode_root;
42+
int i;
43+
for(i = 0; i <= 3; ++i) {
44+
c_xmlNode_tag = xmlNewChild(c_xmlNode_tag, NULL, (xmlChar *) "subtag", NULL);
45+
dmixml_AddAttribute(c_xmlNode_tag, "SubLevel", "%i", i);
46+
}
47+
dmixml_AddTextContent(c_xmlNode_tag, "%s - Adding data to the tag at sublevel %i", "TEST", i-1);
48+
49+
return c_xmlNode_root;
50+
}
51+
52+
53+
54+
55+
PyObject* demo_dump_doc() {
56+
PyObject *py_xmlDoc = NULL;
57+
xmlDoc *c_xmlDoc = NULL;
58+
59+
// Create an XML document
60+
c_xmlDoc = xmlNewDoc((xmlChar *) "1.0");
61+
assert( c_xmlDoc != NULL );
62+
63+
// Generate XML nodes and assign the root node to the document
64+
xmlDocSetRootElement( c_xmlDoc, gen_nodes("demo_dump_doc") );
65+
66+
py_xmlDoc = libxml_xmlDocPtrWrap((xmlDocPtr) c_xmlDoc);
67+
Py_INCREF(py_xmlDoc);
68+
69+
return py_xmlDoc;
70+
}
71+
72+
PyObject* demo_dump_node() {
73+
PyObject *py_xmlNode = NULL;
74+
xmlNode *nodes = NULL;
75+
76+
nodes = gen_nodes("demo_dump_node");
77+
py_xmlNode = libxml_xmlNodePtrWrap((xmlNodePtr) nodes);
78+
Py_INCREF(py_xmlNode);
79+
80+
return py_xmlNode;
81+
}
82+
83+
84+
85+
static PyMethodDef DemoMethods[] = {
86+
{ "dump_doc", demo_dump_doc, METH_NOARGS, (char *)"Return an XML document" },
87+
{ "dump_node", demo_dump_node, METH_NOARGS, (char *)"Retuen an XML node" },
88+
{ NULL, NULL, 0, NULL }
89+
};
90+
91+
PyMODINIT_FUNC initdemomodule(void) {
92+
PyObject *module =
93+
Py_InitModule3((char *)"demomodule", DemoMethods,
94+
"LibXML2 DMIDecode Proof-of-Concept Python Module");
95+
96+
PyObject *version = PyString_FromString("0.10");
97+
Py_INCREF(version);
98+
PyModule_AddObject(module, "version", version);
99+
}

unit-tests/demo.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <Python.h>
2+
#include <stdio.h>
3+
#include <string.h>
4+
#include <assert.h>
5+
6+
#include <libxml/tree.h>
7+
#include "libxml_wrap.h"
8+
9+
#include "dmixml.h"
10+
11+
extern PyObject* demo_dump(void);
12+
PyMODINIT_FUNC initdemomodule(void);
13+
PyObject* demo_dump_doc(void);
14+
PyObject* demo_dump_node(void);

0 commit comments

Comments
 (0)