ref: 47a288683dbdc55c5df9b9f65db9909de6021af0
dir: /libxpath/test/t.c/
#include <u.h> #include <libc.h> #include <xml.h> #include <xpath.h> char *tests[] = { "/path//to[2]/node[@a='b']/@attr", "/path/text()", "/html/a", "/html/a[2]/text()", "/html//e", "/html/a[@href='p.php']/text()", "/html/a[position()='2']/text()", /* should error */ "/html/a[position()=2]/text()", "/html/'2'", "/html/2", "/html/'hello'", "/[inval]", nil }; Xml *x; void printelem(Elem *e) { Attr *a; print("el: <%s", e->name); for (a = e->attrs; a; a = a->next) print(" %s='%s'", a->name, a->value); print(" />\n"); } void printstring(char *s) { print("st: %s\n", s); } void printnum(int n) { print("nr: %d\n", n); } void runtest(char *s) { XpResult r; print("====== test ======\n - %s\n", s); r = xmllookpath(x->root, s); print("===== result =====\n"); print("found %d results:\n", r.num); if (r.error) { fprint(2, "err: %r\n"); werrstr(""); } for (int i = 0; i < r.num; i++) { switch (r.type) { case Xelem: if (!r.elems) sysfatal("elems not set"); printelem(r.elems[i]); break; case Xstring: if (!r.strings) sysfatal("strings not set"); printstring(r.strings[i]); break; case Xnum: if (!r.numbers) sysfatal("numbers not set"); printnum(r.numbers[i]); break; } } switch (r.type) { case Xelem: if (r.num && r.elems) free(r.elems); break; case Xstring: if (r.num && r.strings) free(r.strings); break; case Xnum: if (r.num && r.numbers) free(r.numbers); break; } } void main(int argc, char **argv) { USED(argc, argv); char **s; int fd; fd = open("test.xml", OREAD); if (fd < 0) sysfatal("unable to test: %r"); x = xmlparse(fd, 8192, 0); close(fd); // xmldebug = 1; for (s = tests; *s; s++) { runtest(*s); } exits(nil); }