-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_parser_document_node_test.c
More file actions
328 lines (238 loc) · 7.67 KB
/
Copy pathhtml_parser_document_node_test.c
File metadata and controls
328 lines (238 loc) · 7.67 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#include <string.h>
#include <stdio.h>
#include "html_parser_assert.h"
#include "html_parser_mem.h"
#include "html_parser_atom.h"
#include "html_parser_fmt.h"
#include "html_parser_text.h"
#include "html_parser_attribute_rep.h"
#include "html_parser_attribute_list.h"
#include "html_parser_document_node.h"
#include "html_parser_tag_lookup.h"
#include "html_parser_tester.h"
#define T Test_T
/* test data */
Lookup_T tbl;
/* test functions */
static int Test_node_new_tag(T t, void *, const void *);
static int Test_node_new_content(T t, void *, const void *);
static int Test_node_type_tag(T t, void *, const void *);
static int Test_node_type_content(T t, void *, const void *);
static int Test_node_has_child(T t, void *, const void *);
static int Test_node_has_sibling(T t, void *, const void *);
static int Test_node_child(T t, void *, const void *);
static int Test_node_sibling(T t, void *, const void *);
static int Test_node_add_child(T t, void *, const void *);
static int Test_node_add_sibling(T t, void *, const void *);
static int Test_node_parent(T t, void *, const void *);
static int Test_node_back(T t, void *, const void *);
static int Test_node_add_parent(T t, void *, const void *);
static int Test_node_print_tag(T t, void *, const void *);
static int Test_node_print_content(T t, void *, const void *);
static int Test_node_free(T t, void *, const void *);
int main(int argc, const char *argv[])
{
T suite;
Node_T tag_node = NULL, content_node = NULL, sibling_node = NULL;
const char *name = "h1";
const char *txt = "The author of this book, it's"
" setting and characters are entirely fictitious."
" There is no such place as the state of Victoria."
" The Australian Labour Party exists only in the"
" imagination of its members.";
Fmt_fprint(stderr, "==> Starting %s <==\n", __FILE__);
tbl = Tag_lookup_init();
suite = Test_init();
Test_add(suite, Test_node_new_tag, (void *) &tag_node,
(const void *) name);
Test_add(suite, Test_node_new_content, (void *) &content_node,
(const void *) txt);
Test_add(suite, Test_node_type_tag, NULL, (const void *) &tag_node);
Test_add(suite, Test_node_type_content, NULL, (const void *) &content_node);
Test_add(suite, Test_node_has_child, NULL, (const void *) &tag_node);
Test_add(suite, Test_node_has_sibling, NULL, (void *) &tag_node);
Test_add(suite, Test_node_add_child, (void *) &tag_node,
(const void *) &content_node);
Test_add(suite, Test_node_add_sibling, (void *) &content_node, NULL);
Test_add(suite, Test_node_child, NULL, (const void *) &tag_node);
Test_add(suite, Test_node_sibling, (void *) &sibling_node,
(const void *) &content_node);
Test_add(suite, Test_node_parent, NULL, NULL);
Test_add(suite, Test_node_back, NULL, NULL);
Test_add(suite, Test_node_add_parent, NULL, NULL);
Test_add(suite, Test_node_print_tag, NULL, (const void *) &tag_node);
Test_add(suite, Test_node_print_content, NULL, (void *) &content_node);
Test_add(suite, Test_node_free, (void *) &tag_node, NULL);
Test_all_run(suite);
Test_print_results(suite);
Test_free(&suite);
Tag_lookup_free(&tbl);
return 0;
}
static int Test_node_new_tag(T t, void *s, const void *chk)
{
Node_T *tn = (Node_T *) s;
const char *n = (const char *) chk;
assert(n && *n);
*tn = Node_new_tag(Tag_lookup_tag(&tbl, n), n, NULL);
assert(*tn);
TEST_FUNC_NAME(t);
return TEST_SUCCESS;
}
static int Test_node_new_content(T t, void *s, const void *chk)
{
Node_T *cn = (Node_T *) s;
const char *c = (const char *) chk;
assert(c && *c);
*cn = Node_new_content(c);
assert(*cn);
TEST_FUNC_NAME(t);
return TEST_SUCCESS;
}
static int Test_node_type_tag(T t, void *s, const void *chk)
{
Node_T *tn = (Node_T *) chk;
assert(C_TAGND == Node_type(*tn));
TEST_FUNC_NAME(t);
return TEST_SUCCESS;
}
static int Test_node_type_content(T t, void *s, const void *chk)
{
Node_T *cn = (Node_T *) chk;
assert(C_CNTND == Node_type(*cn));
TEST_FUNC_NAME(t);
return TEST_SUCCESS;
}
static int Test_node_has_child(T t, void *s, const void *chk)
{
Node_T *tn = (Node_T *) chk;
assert(Node_has_child(tn) == 0);
TEST_FUNC_NAME(t);
return TEST_SUCCESS;
}
static int Test_node_has_sibling(T t, void *s, const void *chk)
{
Node_T *cn = (Node_T *) chk;
assert(Node_has_sibling(cn) == 0);
TEST_FUNC_NAME(t);
return TEST_SUCCESS;
}
static int Test_node_child(T t, void *s, const void *chk)
{
Node_T *tn = (Node_T *) chk;
Node_T *cn = Node_child(tn);
assert(*cn);
TEST_FUNC_NAME(t);
return TEST_SUCCESS;
}
static int Test_node_sibling(T t, void *s, const void *chk)
{
Node_T *sn = (Node_T *) s;
Node_T *cn = (Node_T *) chk;
sn = Node_sibling(cn);
assert(*sn);
TEST_FUNC_NAME(t);
return TEST_SUCCESS;
}
static int Test_node_add_child(T t, void *s, const void *chk)
{
Node_T *tn = (Node_T *) s;
Node_T *cn = (Node_T *) chk;
Node_add_child(tn, cn);
TEST_FUNC_NAME(t);
return Node_has_child(tn);
}
static int Test_node_add_sibling(T t, void *s, const void *chk)
{
Node_T *cn = (Node_T *) s;
Node_T em = Node_new_tag(Tag_lookup_tag(&tbl, "em"), "em", NULL);
Node_add_sibling(cn, &em);
TEST_FUNC_NAME(t);
return Node_has_sibling(cn);
}
static int Test_node_parent(T t, void *s, const void *chk)
{
int f = 0;
Attr_list_T alist = Attr_list_list();
Attr_rep_T attr1 = Attr_rep_new("id", "loading");
Node_T pn;
Node_T cn = Node_new_tag(Tag_lookup_tag(&tbl, "h1"), "h1", NULL);
Node_T *pnp = NULL;
alist = Attr_list_enqueue(alist, attr1);
pn = Node_new_tag(Tag_lookup_tag(&tbl, "body"), "body", &alist);
Node_add_child(&pn, &cn); // add cn as child to pn
pnp = Node_parent(&cn); // get parent of cn
Fmt_fprint(stdout, "pn: '%s'\n", Node_print(&pn));
f = !strcmp(Node_name(pn), Node_name(*pnp));
TEST_FUNC_NAME(t);
Node_free(&pn);
Node_free(&cn);
return f;
}
static int Test_node_back(T t, void *s, const void *chk)
{
int f1, f2 = 0;
Node_T pn = Node_new_tag(Tag_lookup_tag(&tbl, "body"), "body", NULL);
Node_T cn = Node_new_tag(Tag_lookup_tag(&tbl, "h1"), "h1", NULL);
Node_T sn = Node_new_tag(Tag_lookup_tag(&tbl, "p"), "p", NULL);
Node_T *bnp = NULL;
Node_add_child(&pn, &cn); // add cn as child to pn
bnp = Node_back(&cn);
f1 = !strcmp(Node_print(&pn), Node_print(bnp));
Node_add_sibling(&cn, &sn);
bnp = Node_back(&sn);
f2 = !strcmp(Node_print(&cn), Node_print(bnp));
Node_free(&pn);
Node_free(&cn);
Node_free(&sn);
TEST_FUNC_NAME(t);
return f1 && f2;
}
static int Test_node_add_parent(T t, void *s, const void *chk)
{
int f = 0;
Node_T pn = Node_new_tag(Tag_lookup_tag(&tbl, "head"), "head", NULL);
Node_T cn = Node_new_tag(Tag_lookup_tag(&tbl, "title"), "title", NULL);
Node_T sn = Node_new_tag(Tag_lookup_tag(&tbl, "link"), "link", NULL);
Node_T *pnp = NULL;
Node_add_child(&pn, &cn); // add cn as child of pn
Node_add_sibling(&cn, &sn); // add sn as sibling of cn
Node_add_parent(&pn, &sn); // hookup parent of sn
pnp = Node_parent(&sn);
f = !strcmp(Node_print(&pn), Node_print(pnp));
TEST_FUNC_NAME(t);
Node_free(&pn);
Node_free(&cn);
Node_free(&sn);
return f;
}
static int Test_node_print_tag(T t, void *s, const void *chk)
{
Node_T *tn = (Node_T *) chk;
char *msg = (char *) Node_print(tn);
TEST_FUNC_NAME(t);
TEST_FUNC_OUT(t, Fmt_string("%s", msg));
return TEST_SUCCESS;
}
static int Test_node_print_content(T t, void *s, const void *chk)
{
Node_T *cn = (Node_T *) chk;
char *msg = (char *) Node_print(cn);
TEST_FUNC_NAME(t);
TEST_FUNC_OUT(t, Fmt_string("%s", msg));
return TEST_SUCCESS;
}
static int Test_node_free(T t, void *s, const void *chk)
{
Node_T *cn, *sn;
Node_T *top = (Node_T *) s; /* tag_node */
assert(top && *top);
cn = Node_child(top);
sn = Node_sibling(cn);
assert(cn && *cn);
assert(sn && *sn);
Node_free(sn);
Node_free(cn);
TEST_FUNC_NAME(t);
return TEST_SUCCESS;
}