File: NodeTest.vala

package info (click to toggle)
gxml 0.20.4%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 5,364 kB
  • sloc: xml: 131,277; ansic: 786; javascript: 328; python: 88; makefile: 35; sh: 11
file content (436 lines) | stat: -rw-r--r-- 15,154 bytes parent folder | download | duplicates (2)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
/* -*- Mode: vala; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/* Notation.vala
 *
 * Copyright (C) 2011-2013  Richard Schwarting <aquarichy@gmail.com>
 * Copyright (C) 2011-2015  Daniel Espinosa <esodan@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.

 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.

 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *      Richard Schwarting <aquarichy@gmail.com>
 *      Daniel Espinosa <esodan@gmail.com>
 */

using GXml;

/* For testing, based on:
   https://live.gnome.org/Vala/TestSample

   Using an Element subclass of GXml.xNode to test Node.
*/

class NodeTest : GXmlTest {
	// TODO: test setters?

	public static void add_tests () {
		Test.add_func ("/gxml/domnode/node_name_get", () => {
				// TODO: should Nodes never have a null name?
				xDocument doc = get_doc ();
				GXml.xNode node;

				node = get_elem ("elemname", doc);
				assert (node.node_name == "elemname");

				node = doc.create_attribute ("attrname");
				assert (node.node_name == "attrname");

				node = doc.create_text_node ("some text");
				assert (node.node_name == "#text");

				node = doc.create_cdata_section ("cdata");
				assert (node.node_name == "#cdata-section");

				node = doc.create_entity_reference ("refname");
				assert (node.node_name == "refname");

				// node = doc.create_entity ();
				// assert (node.node_name == ...); // entity name

				node = doc.create_processing_instruction ("target", "data");
				assert (node.node_name == "target");

				node = (xNode) doc.create_comment ("some comment");
				assert (node.name == "#comment");

				assert (doc.node_name == "#document");

				// node = doc.create_document_type ("");
				// assert (node.node_name == ...); // document-type name

				// node = doc.create_document_fragment ("");
				// assert (node.node_name == "#document-fragment");

				// node = doc.create_notation ("some notation");
				// assert (node.node_name == ...); // notation name
			});
		Test.add_func ("/gxml/domnode/node_type_get", () => {
				// TODO: implement commented-out types

				xDocument doc = get_doc ();
				GXml.xNode node;

				node = get_elem ("a", doc);
				assert (node.node_type == NodeType.ELEMENT);

				node = doc.create_attribute ("name");
				assert (node.node_type == NodeType.ATTRIBUTE);

				node = doc.create_text_node ("some text");
				assert (node.node_type == NodeType.TEXT);

				node = doc.create_cdata_section ("cdata");
				assert (node.node_type == NodeType.CDATA_SECTION);

				node = doc.create_entity_reference ("refname");
				assert (node.node_type == NodeType.ENTITY_REFERENCE);

				// node = doc.create_entity ();
				// assert (node.node_type == NodeType.ENTITY);

				node = doc.create_processing_instruction ("target", "data");
				assert (node.node_type == NodeType.PROCESSING_INSTRUCTION);

				node = (xNode) doc.create_comment ("some comment");
				assert (node.node_type == NodeType.COMMENT);

				assert (doc.node_type == NodeType.DOCUMENT);

				// node = doc.create_document_type ("");
				// assert (node.node_type == NodeType.DOCUMENT_TYPE);

				// node = doc.create_document_fragment ("");
				// assert (node.node_type == NodeType.DOCUMENT_FRAGMENT);

				// node = doc.create_notation ("some notation");
				// assert (node.node_type == NodeType.NOTATION);

			});
		Test.add_func ("/gxml/domnode/node_value_get", () => {

				/* See: http://www.w3.org/TR/DOM-Level-1/level-one-core.html */

				xDocument doc = get_doc ();

				GXml.xNode node;

				node = (xElement) doc.create_element ("elem");
				assert (node.node_value == null);

				node = doc.create_attribute ("name");
				((xAttr)node).value = "Harry Potter";
				assert (node.node_value == "Harry Potter");

				node = doc.create_text_node ("text content");
				assert (node.node_value == "text content");

				node = doc.create_cdata_section ("cdata content");
				assert (node.node_value == "cdata content");

				node = doc.create_entity_reference ("refname");
				assert (node.node_value == null);

				// TODO: entity

				node = doc.create_processing_instruction ("target", "proc inst data");
				assert (node.node_value == "proc inst data");

				node = (xNode) doc.create_comment ("some comment");
				assert (node.node_value == "some comment");

				assert (doc.node_value == null);

				/* TODO: xDocument Type, xDocument Fragment, Notation */
				// assert (attr.node_value == "harry");
				/* TODO: figure out a solution.
				   xAttr's node_value doesn't get used when elem is thought of
				   as a Node.
				   GXml.xNode wants to get it from Node's Xml.Node* node,
				   while xAttr wants to get it from xAttr's Xml.Attr* node. :( */
			});
		Test.add_func ("/gxml/domnode/parent_node", () => {
				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("James", doc);
				GXml.xNode child = get_elem ("Harry", doc);

				assert (child.parent_node == null);
				parent.append_child (child);
				assert (child.parent_node == parent);

				GXml.xNode attr = doc.create_attribute ("a");
				assert (attr.parent_node == null);
				assert (doc.parent_node == null);
				// assert (document fragment's parent_node == null); // TODO
			});
		Test.add_func ("/gxml/domnode/child_nodes", () => {
				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("Molly", doc);
				if (parent == null) {
					stdout.printf (@"Parent child Molly not created\n");
					assert_not_reached ();
				}
				GXml.xNode child_0 = get_elem ("Percy", doc);
				if (child_0 == null) {
					stdout.printf (@"Child_0 Percy not created\n");
					assert_not_reached ();
				}
				GXml.xNode child_1 = get_elem ("Ron", doc);
				if (child_1 == null) {
					stdout.printf (@"Child Ron not created\n");
					assert_not_reached ();
				}
				GXml.xNode child_2 = get_elem ("Ginnie", doc);
				if (child_2 == null) {
					stdout.printf (@"Child Ginnie not created\n");
					assert_not_reached ();
				}
				if (parent.child_nodes.length != 0) {
					stdout.printf (@"ChildNode: wrong length. Expected 0 got $(parent.child_nodes.length)\n");
					assert_not_reached ();
				}
				parent.append_child (child_0);
				parent.append_child (child_1);
				parent.append_child (child_2);
				if (parent.child_nodes.length != 3) {
					stdout.printf (@"ChildNode: wrong length. Expected 0 got $(parent.child_nodes.length)\n");
					assert_not_reached ();
				}
				var c0 = parent.child_nodes.@get (0);
				if (c0.node_name != child_0.node_name) {
					stdout.printf (@"ChildNode: wrong child 0.\n");
					assert_not_reached ();
				}
				var c1 = parent.child_nodes.@get (1);
				if (c1.node_name != child_1.node_name) {
					stdout.printf (@"ChildNode: wrong child 1.\n");
					assert_not_reached ();
				}
				var c2 = parent.child_nodes.@get (2);
				if (c2.node_name != child_2.node_name) {
					stdout.printf (@"ChildNode: wrong child 2.\n");
					assert_not_reached ();
				}
			});
		Test.add_func ("/gxml/domnode/first_child", () => {
				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("Molly", doc);
				GXml.xNode child_0 = get_elem ("Percy", doc);
				GXml.xNode child_1 = get_elem ("Ron", doc);
				GXml.xNode child_2 = get_elem ("Ginnie", doc);

				assert (parent.child_nodes.length == 0);
				parent.append_child (child_0);
				parent.append_child (child_1);
				parent.append_child (child_2);

				assert (parent.first_child == child_0);
			});
		Test.add_func ("/gxml/domnode/last_child", () => {
				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("Molly", doc);
				GXml.xNode child_0 = get_elem ("Percy", doc);
				GXml.xNode child_1 = get_elem ("Ron", doc);
				GXml.xNode child_2 = get_elem ("Ginnie", doc);

				assert (parent.child_nodes.length == 0);
				parent.append_child (child_0);
				parent.append_child (child_1);
				parent.append_child (child_2);

				assert (parent.last_child == child_2);
			});
		Test.add_func ("/gxml/domnode/previous_sibling", () => {
				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("Molly", doc);
				GXml.xNode child_0 = get_elem ("Percy", doc);
				GXml.xNode child_1 = get_elem ("Ron", doc);
				GXml.xNode child_2 = get_elem ("Ginnie", doc);

				assert (parent.child_nodes.length == 0);
				parent.append_child (child_0);
				parent.append_child (child_1);
				parent.append_child (child_2);

				assert (child_0.previous_sibling == null);
				assert (child_1.previous_sibling == child_0);
				assert (child_2.previous_sibling == child_1);
			});
		Test.add_func ("/gxml/domnode/next_sibling", () => {
				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("Molly", doc);
				GXml.xNode child_0 = get_elem ("Percy", doc);
				GXml.xNode child_1 = get_elem ("Ron", doc);
				GXml.xNode child_2 = get_elem ("Ginnie", doc);

				assert (parent.child_nodes.length == 0);
				parent.append_child (child_0);
				parent.append_child (child_1);
				parent.append_child (child_2);

				assert (child_0.next_sibling == child_1);
				assert (child_1.next_sibling == child_2);
				assert (child_2.next_sibling == null);
			});
		Test.add_func ("/gxml/domnode/attributes", () => {
				xDocument doc = get_doc ();
				GXml.xNode elem = get_elem ("Hogwarts", doc);
				GXml.xNode attr = get_attr ("Potter", "Lily", doc);

				assert (elem.attributes != null);
				assert (attr.attributes == null);

				// TODO: test more
				// TODO: test compatibility between live changes and stuff
			});
		Test.add_func ("/gxml/domnode/owner_document", () => {
				xDocument doc2 = get_doc ();
				xDocument doc1 = get_doc ();
				GXml.xNode elem = get_elem ("Malfoy", doc1);

				assert (elem.owner_document == doc1);
				assert (elem.owner_document != doc2);
			});
		Test.add_func ("/gxml/domnode/insert_before", () => {
				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("Molly", doc);
				GXml.xNode child_0 = get_elem ("Percy", doc);
				GXml.xNode child_1 = get_elem ("Ron", doc);
				GXml.xNode child_2 = get_elem ("Ginnie", doc);

				assert (parent.child_nodes.length == 0);
				parent.append_child (child_2);
				parent.insert_before (child_0, child_2);
				parent.insert_before (child_1, child_2);

				assert (parent.first_child == child_0);
				assert (parent.last_child == child_2);
				assert (parent.child_nodes.length == 3);
				assert (parent.child_nodes.@get (0) == child_0);
				assert (parent.child_nodes.@get (1) == child_1);
				assert (parent.child_nodes.@get (2) == child_2);
				assert (child_0.previous_sibling == null);
				assert (child_1.previous_sibling == child_0);
				assert (child_2.previous_sibling == child_1);
				assert (child_0.next_sibling == child_1);
				assert (child_1.next_sibling == child_2);
				assert (child_2.next_sibling == null);
			});
		Test.add_func ("/gxml/domnode/replace_child", () => {
				// TODO: for this one, and others that include a ref_child, we want to test passing an irrelevant ref child and a null ref child

				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("Molly", doc);
				GXml.xNode child_0 = get_elem ("Percy", doc);
				GXml.xNode child_1 = get_elem ("Ron", doc);
				GXml.xNode child_2 = get_elem ("Ginnie", doc);

				assert (parent.child_nodes.length == 0);
				parent.append_child (child_0);
				parent.append_child (child_2);

				parent.replace_child (child_1, child_2);

				assert (parent.first_child == child_0);
				assert (parent.last_child == child_1);
				assert (parent.child_nodes.length == 2);
				assert (parent.child_nodes.@get (0) == child_0);
				assert (parent.child_nodes.@get (1) == child_1);
				assert (child_0.previous_sibling == null);
				assert (child_1.previous_sibling == child_0);
				assert (child_0.next_sibling == child_1);
				assert (child_1.next_sibling == null);
			});
		Test.add_func ("/gxml/domnode/remove_child", () => {
				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("Molly", doc);
				GXml.xNode child_0 = get_elem ("Percy", doc);
				GXml.xNode child_1 = get_elem ("Ron", doc);
				GXml.xNode child_2 = get_elem ("Ginnie", doc);

				assert (parent.child_nodes.length == 0);
				parent.append_child (child_0);
				parent.append_child (child_2);
				parent.append_child (child_1);

				parent.remove_child (child_2);

				assert (child_2.previous_sibling == null);
				assert (child_2.next_sibling == null);

				assert (parent.first_child == child_0);
				assert (parent.last_child == child_1);
				assert (parent.child_nodes.length == 2);
				assert (parent.child_nodes.@get (0) == child_0);
				assert (parent.child_nodes.@get (1) == child_1);
				assert (child_0.previous_sibling == null);
				assert (child_1.previous_sibling == child_0);
				assert (child_0.next_sibling == child_1);
				assert (child_1.next_sibling == null);

				parent.remove_child (child_0);

				assert (parent.first_child == child_1);
				assert (parent.last_child == child_1);
				assert (parent.child_nodes.length == 1);
				assert (parent.child_nodes.@get (0) == child_1);
				assert (child_1.previous_sibling == null);
				assert (child_1.next_sibling == null);

				parent.remove_child (child_1);

				assert (parent.first_child == null);
				assert (parent.last_child == null);
				assert (parent.child_nodes.length == 0);
			});
		Test.add_func ("/gxml/domnode/append_child", () => {
				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("Molly", doc);
				GXml.xNode child_0 = get_elem ("Percy", doc);
				GXml.xNode child_1 = get_elem ("Ron", doc);
				GXml.xNode child_2 = get_elem ("Ginnie", doc);

				assert (parent.child_nodes.length == 0);
				parent.append_child (child_0);
				parent.append_child (child_1);
				parent.append_child (child_2);

				assert (parent.first_child == child_0);
				assert (parent.last_child == child_2);
				assert (parent.child_nodes.length == 3);
				assert (parent.child_nodes.@get (0) == child_0);
				assert (parent.child_nodes.@get (1) == child_1);
				assert (parent.child_nodes.@get (2) == child_2);
				assert (child_0.previous_sibling == null);
				assert (child_1.previous_sibling == child_0);
				assert (child_2.previous_sibling == child_1);
				assert (child_0.next_sibling == child_1);
				assert (child_1.next_sibling == child_2);
				assert (child_2.next_sibling == null);
			});
		Test.add_func ("/gxml/domnode/has_child_nodes", () => {
				xDocument doc = get_doc ();
				GXml.xNode parent = get_elem ("Molly", doc);
				GXml.xNode child_0 = get_elem ("Percy", doc);

				assert (parent.has_child_nodes () == false);

				parent.append_child (child_0);

				assert (parent.has_child_nodes () == true);
			});
		Test.add_func ("/gxml/domnode/clone_nodes", () => {
				// STUB
			});

	}
}