summaryrefslogtreecommitdiffstats
path: root/test/tst-dom.c
blob: ecd098a21ccf1c4f04884f051fe29e21aab2c9f4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/* iksemel (XML parser for Jabber)
** Copyright (C) 2000-2003 Gurer Ozen
** This code is free software; you can redistribute it and/or
** modify it under the terms of GNU Lesser General Public License.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

#include "iksemel.h"

iks *my_x;
int nr_tests;

#define PR_TEST printf ("DOM test %d:\n", nr_tests)

void
document (char *xml)
{
	enum ikserror err;
	iksparser *p;

	nr_tests++;
	if (my_x) iks_delete (my_x);
	p = iks_dom_new (&my_x);
	err = iks_parse (p, xml, 0, 1);
	switch (err) {
		case IKS_OK:
			break;
		case IKS_NOMEM:
			PR_TEST;
			puts ("Not enough memory.");
			exit (1);
		case IKS_BADXML:
			PR_TEST;
			printf ("Invalid xml at byte %ld in\n[%s]\n", iks_nr_bytes (p), xml);
			exit (1);
		case IKS_HOOK:
			PR_TEST;
			puts ("Hook.");
	}
	iks_parser_delete (p);
}

void
tag (char *name, ...)
{
	iks *x;
	va_list ap;

	x = my_x;
	va_start (ap, name);
	while (1) {
		char *name = iks_name (x);
		char *tmp = va_arg (ap, char*);
		if (NULL == tmp) break;
		x = iks_find (x, tmp);
		if (!x) {
			PR_TEST;
			printf ("Tag <%s> is not a child of tag <%s>\n", tmp, name);
			exit (1);
		}
	}
	if (!x || NULL == iks_find (x, name)) {
		PR_TEST;
		printf ("Tag <%s> is not a child of tag <%s>\n", name, iks_name (x));
		exit (1);
	}
	va_end (ap);
}

void
cdata (char *data, ...)
{
	iks *x;
	va_list ap;

	x = my_x;
	va_start (ap, data);
	while (1) {
		char *name = iks_name (x);
		char *tmp = va_arg (ap, char*);
		if (NULL == tmp) break;
		x = iks_find (x, tmp);
		if (!x) {
			PR_TEST;
			printf ("Tag <%s> is not a child of tag <%s>\n", tmp, name);
			exit (1);
		}
	}
	if (iks_strcmp ( iks_cdata (iks_child (x)), data) != 0) {
		PR_TEST;
		printf ("CDATA [%s] not found.\n", data);
		exit (1);
	}
	va_end (ap);
}

void
attrib (char *att, char *val, ...)
{
	iks *x;
	va_list ap;

	x = my_x;
	va_start (ap, val);
	while (1) {
		char *name = iks_name (x);
		char *tmp = va_arg (ap, char*);
		if (NULL == tmp) break;
		x = iks_find (x, tmp);
		if (!x) {
			PR_TEST;
			printf ("Tag <%s> is not a child of tag <%s>\n", tmp, name);
			exit (1);
		}
	}
	if (iks_strcmp (val, iks_find_attrib (x, att)) != 0) {
		PR_TEST;
		printf ("Attribute '%s' not found.\n", att);
		exit (1);
	}
	va_end (ap);
}

void
string (char *xml)
{
	char *tmp;

	tmp = iks_string (iks_stack (my_x), my_x);
	if (iks_strcmp (tmp, xml) != 0) {
		PR_TEST;
		printf ("Result:   %s\n", tmp);
		printf ("Expected: %s\n", xml);
		exit (1);
	}
}

static char buf[] =
	"<presence id='JCOM_11' to='lala@j.org' type='available'><status>"
	"&quot; &lt;online&amp;dangerous&gt; &quot;</status>meow<a><b c='d'/>"
	"</a><test/></presence>";

int main (int argc, char *argv[])
{
	document ("<atag></atag>");
	string ("<atag/>");

	document ("<test>lala<b>bold</b>blablabla<a><c/></a></test>");
	tag ("b", 0);
	tag ("c", "a", 0);
	string ("<test>lala<b>bold</b>blablabla<a><c/></a></test>");

	document (buf);
	cdata ("\" <online&dangerous> \"", "status", 0);
	attrib ("c", "d", "a", "b", 0);
	tag ("test", 0);
	string (buf);

	return 0;
}