diff options
author | John Mark Bell <jmb@netsurf-browser.org> | 2007-07-15 22:09:59 +0000 |
---|---|---|
committer | John Mark Bell <jmb@netsurf-browser.org> | 2007-07-15 22:09:59 +0000 |
commit | 2df119603f5a560ffb6cafe85f6377076276a34a (patch) | |
tree | 3fc1d17c70eae41963ea5c408d3ce51bd8558a98 | |
parent | 957e6ed786a8a7451b9f9ab600caa06c1e979cc1 (diff) | |
download | libdom-2df119603f5a560ffb6cafe85f6377076276a34a.tar.gz libdom-2df119603f5a560ffb6cafe85f6377076276a34a.tar.bz2 |
Implement TestObject class and other test utilities (testutils.h)
Rudimentary test of libxml binding, so I can see what's needed
svn path=/trunk/dom/; revision=3419
-rw-r--r-- | test/Makefile | 7 | ||||
-rw-r--r-- | test/binding.c | 29 | ||||
-rw-r--r-- | test/data/binding/staff.xml | 57 | ||||
-rw-r--r-- | test/testutils.h | 145 |
4 files changed, 235 insertions, 3 deletions
diff --git a/test/Makefile b/test/Makefile index d9bb137..56e0e3f 100644 --- a/test/Makefile +++ b/test/Makefile @@ -19,7 +19,8 @@ # test Execute any test cases # Extend toolchain settings -CFLAGS += -I${TOP}/src/ -I$(CURDIR) +CFLAGS += -I${TOP}/src/ -I${TOP}/bindings/xml/ -I$(CURDIR) +LDFLAGS += `pkg-config --libs libxml-2.0` # Release output RELEASE = @@ -28,7 +29,7 @@ RELEASE = DEBUG = # Objects -OBJS = +OBJS = binding .PHONY: clean debug export release setup test @@ -56,5 +57,5 @@ test: $(OBJS) %: %.c @${ECHO} ${ECHOFLAGS} "==> $<" @${CC} -c -g ${CFLAGS} -o $@.o $< - @${LD} -g -o $@ $@.o ${LDFLAGS} -ldom-debug + @${LD} -g -o $@ $@.o ${LDFLAGS} -ldom-libxml-debug -ldom-debug @${RM} ${RMFLAGS} $@.o diff --git a/test/binding.c b/test/binding.c new file mode 100644 index 0000000..803ecaa --- /dev/null +++ b/test/binding.c @@ -0,0 +1,29 @@ +#include <dom/dom.h> +#include "testutils.h" + +int main(int argc, char **argv) +{ + struct dom_document *doc; + struct dom_element *element; + struct dom_string *elementName; + dom_exception err; + TestObject *staff; + + staff = test_object_create(argc, argv, "staff", false); + assert(staff != NULL); + + doc = test_object_get_doc(staff); + assert(doc != NULL); + + err = dom_document_get_element(doc, &element); + assert(err == DOM_NO_ERR); + assert(element != NULL); + + err = dom_element_get_tag_name(element, &elementName); + assert(err == DOM_NO_ERR); + assert(elementName != NULL); + + printf("PASS\n"); + + return 0; +} diff --git a/test/data/binding/staff.xml b/test/data/binding/staff.xml new file mode 100644 index 0000000..f89c510 --- /dev/null +++ b/test/data/binding/staff.xml @@ -0,0 +1,57 @@ +<?xml version="1.0"?><?TEST-STYLE PIDATA?> +<!DOCTYPE staff SYSTEM "staff.dtd" [ + <!ENTITY ent1 "es"> + <!ENTITY ent2 "1900 Dallas Road"> + <!ENTITY ent3 "Texas"> + <!ENTITY ent4 "<entElement domestic='Yes'>Element data</entElement><?PItarget PIdata?>"> + <!ENTITY ent5 PUBLIC "entityURI" "entityFile" NDATA notation1> + <!ENTITY ent1 "This entity should be discarded"> + <!NOTATION notation1 PUBLIC "notation1File"> + <!NOTATION notation2 SYSTEM "notation2File"> +]> +<!-- This is comment number 1.--> +<staff> + <employee> + <employeeId>EMP0001</employeeId> + <name>Margaret Martin</name> + <position>Accountant</position> + <salary>56,000</salary> + <gender>Female</gender> + <address domestic="Yes">1230 North Ave. Dallas, Texas 98551</address> + </employee> + <employee> + <employeeId>EMP0002</employeeId> + <name>Martha Raynolds<![CDATA[This is a CDATASection with EntityReference number 2 &ent2;]]> +<![CDATA[This is an adjacent CDATASection with a reference to a tab &tab;]]></name> + <position>Secretary</position> + <salary>35,000</salary> + <gender>Female</gender> + <address domestic="Yes" street="Yes">&ent2; Dallas, &ent3; + 98554</address> + </employee> + <employee> + <employeeId>EMP0003</employeeId> + <name>Roger + Jones</name> + <position>Department Manager</position> + <salary>100,000</salary> + <gender>&ent4;</gender> + <address domestic="Yes" street="No">PO Box 27 Irving, texas 98553</address> + </employee> + <employee> + <employeeId>EMP0004</employeeId> + <name>Jeny Oconnor</name> + <position>Personnel Director</position> + <salary>95,000</salary> + <gender>Female</gender> + <address domestic="Yes" street="Y&ent1;">27 South Road. Dallas, Texas 98556</address> + </employee> + <employee> + <employeeId>EMP0005</employeeId> + <name>Robert Myers</name> + <position>Computer Specialist</position> + <salary>90,000</salary> + <gender>male</gender> + <address street="Yes">1821 Nordic. Road, Irving Texas 98558</address> + </employee> + </staff> diff --git a/test/testutils.h b/test/testutils.h new file mode 100644 index 0000000..ab3b29d --- /dev/null +++ b/test/testutils.h @@ -0,0 +1,145 @@ +#ifndef dom_test_testutils_h_ +#define dom_test_testutils_h_ + +#include <stdbool.h> +#include <stdio.h> +#include <stdlib.h> +#include <inttypes.h> + +#include "xmlbinding.h" +#include "xmlparser.h" + +#ifndef UNUSED +#define UNUSED(x) ((x) = (x)) +#endif + +/* Redefine assert, so we can simply use the standard assert mechanism + * within testcases and exit with the right output for the testrunner + * to do the right thing. */ +void __assert2(const char *expr, const char *function, + const char *file, int line); + +void __assert2(const char *expr, const char *function, + const char *file, int line) +{ + UNUSED(function); + UNUSED(file); + + printf("FAIL - %s at line %d\n", expr, line); + + exit(EXIT_FAILURE); +} + +#define assert(expr) \ + ((void) ((expr) || (__assert2 (#expr, __func__, __FILE__, __LINE__), 0))) + +static void *myrealloc(void *ptr, size_t len, void *pw) +{ + UNUSED(pw); + + return realloc(ptr, len); +} + +typedef struct TestObject { + xml_parser *parser; + struct dom_document *doc; +} TestObject; + +TestObject *test_object_create(int argc, char **argv, + const char *uri, bool will_be_modified); +struct dom_document *test_object_get_doc(TestObject *obj); +const char *test_object_get_mimetype(TestObject *obj); + +TestObject *test_object_create(int argc, char **argv, + const char *uri, bool will_be_modified) +{ + static bool xml_parser_initialised; + + char fnbuf[1024]; +#define CHUNK_SIZE 4096 + uint8_t buf[CHUNK_SIZE]; + FILE *fp; + size_t len; + TestObject *ret; + + UNUSED(will_be_modified); + + if (argc != 2) { + printf("Usage: %s <datapath>\n", argv[0]); + exit(EXIT_FAILURE); + } + + if (xml_parser_initialised == false) { + assert(xml_dom_binding_initialise(myrealloc, NULL) == XML_OK); + + xml_parser_initialised = true; + } + + snprintf(fnbuf, sizeof fnbuf, "%s/%s.xml", argv[1], uri); + + ret = malloc(sizeof(TestObject)); + if (ret == NULL) + return NULL; + + ret->parser = xml_parser_create(NULL, "UTF-8", myrealloc, NULL); + if (ret->parser == NULL) { + free(ret); + return NULL; + } + + fp = fopen(fnbuf, "r"); + if (fp == NULL) { + xml_parser_destroy(ret->parser); + free(ret); + return NULL; + } + + fseek(fp, 0, SEEK_END); + len = ftell(fp); + fseek(fp, 0, SEEK_SET); + + while (len > CHUNK_SIZE) { + fread(buf, 1, CHUNK_SIZE, fp); + + assert(xml_parser_parse_chunk(ret->parser, buf, + CHUNK_SIZE) == XML_OK); + + len -= CHUNK_SIZE; + } + + if (len > 0) { + fread(buf, 1, len, fp); + + assert(xml_parser_parse_chunk(ret->parser, buf, + len) == XML_OK); + + len = 0; + + assert(xml_parser_completed(ret->parser) == XML_OK); + } + + fclose(fp); + + ret->doc = xml_parser_get_document(ret->parser); + + xml_parser_destroy(ret->parser); + ret->parser = NULL; + + return ret; + +#undef CHUNK_SIZE +} + +struct dom_document *test_object_get_doc(TestObject *obj) +{ + return obj->doc; +} + +const char *test_object_get_mimetype(TestObject *obj) +{ + UNUSED(obj); + + return "text/xml"; +} + +#endif |