1: #!/usr/bin/python
2: # -*- coding: utf-8 -*-
3:
4: import sys
5:
6: # import base library
7: import axl
8:
9: def test_01():
10: # do two consecutive parse operations
11: (doc, err) = axl.parse ("<?xml version='1.0' ?><axldoc />", 32)
12: if err:
13: error ("Found error: " + str (err.code) + ", message: " + err.msg)
14: return False
15:
16: (doc, err) = axl.parse ("<?xml version=\"1.0\" \t \n \r encoding='utf-8\" ?> <data />", 63)
17: if err:
18: error ("Found error: " + str (err.code) + ", message: " + err.msg)
19: return False
20:
21: # check doc encoding
22: if doc.encoding != "utf-8":
23: error ("Error, expected to find enconding value utf-8 but found: " + doc.encoding)
24: return False
25:
26: # do a fail parse operation
27: (doc, err) = axl.parse ("aefadsadsf<?xml version=\"1.0\" \t \n \r encoding='utf-8\" ?> <data />", 73)
28: if not err:
29: error ("Expected to find error but found no error report")
30: return False
31:
32: # check error returned
33: if not err.code == -2:
34: error ("Expected to find error code -2 but found: " + str (err.code))
35:
36: # check standalone configuration
37: (doc, err) = axl.parse ("<?xml version='1.0' encoding='utf-8' standalone='yes' ?> <data/>", 65)
38: if err:
39: error ("Expected to find proper document parse but a failure was found: " + err.msg)
40: return False
41:
42: if not doc.standalone:
43: error ("Expected to find standalone enabled but found it disabled")
44: return False
45:
46: return True
47:
48: def test_01b():
49: # parse document
50: (doc, err) = axl.file_parse ("test_01b.xml")
51: if err:
52: error ("Found error: " + str (err.code) + ", message: " + err.msg)
53: return False
54:
55: # get root node
56: node = doc.root
57:
58: # check node name
59: if node.name != "document":
60: error ("Expected to find node name 'document' but found: " + node.name)
61: return False
62:
63: # get first child
64: node = node.first_child
65:
66: # check node name
67: if node.name != "child1":
68: error ("Expected to find node name 'child1' but found: " + node.name)
69: return False
70:
71: # get next
72: node = node.next
73:
74: # check node name
75: if node.name != "child2":
76: error ("Expected to find node name 'child2' but found: " + node.name)
77: return False
78:
79: return True
80:
81: def test_01c():
82: # parse document
83: (doc, err) = axl.file_parse ("test_01b.xml")
84: if err:
85: error ("Found error: " + str (err.code) + ", message: " + err.msg)
86: return False
87:
88: # get root node
89: node = doc.root
90:
91: # check node name
92: if node.name != "document":
93: error ("Expected to find node name 'document' but found: " + node.name)
94: return False
95:
96: # get first child
97: node = node.first_child
98:
99: # check node name
100: if node.name != "child1":
101: error ("Expected to find node name 'child1' but found: " + node.name)
102: return False
103:
104: # get parent
105: parent = node.parent
106:
107: # check node name
108: if parent.name != "document":
109: error ("Expected to find node name 'document' but found: " + parent.name)
110: return False
111:
112: node = node.next
113:
114: # check node name
115: if node.name != "child2":
116: error ("Expected to find node name 'child2' but found: " + node.name)
117: return False
118:
119:
120: # check parent again
121: parent = node.parent
122:
123: # check node name
124: if parent.name != "document":
125: error ("Expected to find node name 'document' but found: " + parent.name)
126: return False
127:
128: node = node.next
129:
130: # check node name
131: if node.name != "child3":
132: error ("Expected to find node name 'child3' but found: " + node.name)
133: return False
134:
135: # check parent again
136: parent = node.parent
137:
138: # check node name
139: if parent.name != "document":
140: error ("Expected to find node name 'document' but found: " + parent.name)
141: return False
142:
143: node = node.next
144:
145: # check node name
146: if node.name != "child4":
147: error ("Expected to find node name 'child4' but found: " + node.name)
148: return False
149:
150: # check parent again
151: parent = node.parent
152:
153: # check node name
154: if parent.name != "document":
155: error ("Expected to find node name 'document' but found: " + parent.name)
156: return False
157:
158: node = node.next
159:
160: # check node name
161: if node.name != "child5":
162: error ("Expected to find node name 'child5' but found: " + node.name)
163: return False
164:
165: # check next is none
166: if node.next:
167: error ("Expected to find none value for the next child node found next to child5")
168: return False
169:
170: node = node.previous
171:
172: # check node name
173: if node.name != "child4":
174: error ("Expected to find node name 'child4' but found: " + node.name)
175: return False
176:
177: # check node name
178: if parent.name != "document":
179: error ("Expected to find node name 'document' but found: " + parent.name)
180: return False
181:
182: node = node.previous
183:
184: # check node name
185: if node.name != "child3":
186: error ("Expected to find node name 'child3' but found: " + node.name)
187: return False
188:
189: # check node name
190: if parent.name != "document":
191: error ("Expected to find node name 'document' but found: " + parent.name)
192: return False
193:
194: node = node.previous
195:
196: # check node name
197: if node.name != "child2":
198: error ("Expected to find node name 'child2' but found: " + node.name)
199: return False
200:
201: # check node name
202: if parent.name != "document":
203: error ("Expected to find node name 'document' but found: " + parent.name)
204: return False
205:
206: node = node.previous
207:
208: # check node name
209: if node.name != "child1":
210: error ("Expected to find node name 'child1' but found: " + node.name)
211: return False
212:
213: # check node name
214: if parent.name != "document":
215: error ("Expected to find node name 'document' but found: " + parent.name)
216: return False
217:
218: # check previous is none
219: if node.previous:
220: error ("Expected to find none value for the previous child node found previous to child1")
221: return False
222:
223: # now test called api
224: node = doc.root.first_child
225:
226: # get <child5>
227: node = node.next_called ("child5")
228: if node.name != "child5":
229: error ("Expected to find child node with name child5 but found: " + node.name)
230: return False
231:
232: # get <child5>
233: node = node.next_called ("child5")
234: if node:
235: error ("Expected to find none node after calling next_called with child5 but found: " + node.name)
236: return False
237:
238: node = doc.root.first_child.next_called ("child5")
239: if not node:
240: error ("Expected to find a xml node but found none")
241: return False
242: if node.name != "child5":
243: error ("Expected to find child node child5 but found: " + node.name)
244: return False
245:
246: # get <child1>
247: node = node.previous_called ("child1")
248: if not node:
249: error ("Expected to find a xml node child1 but found none")
250: return False
251: if node.name != "child1":
252: error ("Expected to find child node child1 but found: " + node.name)
253: return False
254:
255: node = node.previous_called ("child1")
256: if node:
257: error ("Expected to not find a xml node but found somethind defined: " + node.name)
258: return False
259:
260: return True
261:
262: def test_01d():
263: (doc, err) = axl.file_parse ("test_01d.xml")
264: if err:
265: error ("Expected to find proper parse operation but found an error: " + err.msg)
266: return False
267:
268: child = doc.root.nth_child (0)
269: if child.name != "child1":
270: error ("Expected to find child1 node but found: " + child.name)
271: return False
272:
273: child = doc.root.nth_child (1)
274: if child.name != "child2":
275: error ("Expected to find child2 node but found: " + child.name)
276: return False
277:
278: child = doc.root.nth_child (2)
279: if child.name != "child3":
280: error ("(2) Expected to find child3 node but found: " + child.name)
281: return False
282:
283: child = doc.root.nth_child (3)
284: if child.name != "child4":
285: error ("Expected to find child4 node but found: " + child.name)
286: return False
287:
288: child = doc.root.nth_child (4)
289: if child.name != "child5":
290: error ("Expected to find child5 node but found: " + child.name)
291: return False
292:
293: # get the child 0 located at the child 2
294: child = doc.root.nth_child (2).nth_child (0)
295:
296: if child.name != "a":
297: error ("Expected to find node name <a> but found: " + child.name)
298: return False
299:
300: child = doc.root.nth_child (2).nth_child (1)
301:
302: if child.name != "b":
303: error ("Expected to find node name <b> but found: " + child.name)
304: return False
305:
306: child = doc.root.nth_child (2).nth_child (2)
307:
308: if child.name != "c":
309: error ("Expected to find node name <c> but found: " + child.name)
310: return False
311:
312: child = doc.root.nth_child (2).nth_child (3)
313:
314: if child.name != "f":
315: error ("Expected to find node name <f> but found: " + child.name)
316: return False
317:
318:
319: child = doc.root.nth_child (2).nth_child (4)
320:
321: if child.name != "g":
322: error ("Expected to find node name <g> but found: " + child.name)
323: return False
324:
325: return True
326:
327: def test_01f():
328:
329: # parse document
330: (doc, err) = axl.file_parse ("test_01f.xml")
331: if err:
332: error ("Expected proper document parse but found a failure: " + err.msg)
333: return False
334:
335: # get root content
336: (content, size) = doc.root.content
337:
338: # check content
339: if content != " ":
340: error ("Expected to find ' ' but found: " + content)
341: return False
342:
343: # parse another document
344: (doc, err) = axl.file_parse ("test_01f2.xml")
345: if err:
346: error ("Expected proper document parse but found a failure: " + err.msg)
347: return False
348:
349: # get content from node id found as child of the root node
350: (content, size) = doc.root.child_called ("id").content
351:
352: # check content
353: if content != " ":
354: error ("(2) Expected to find ' ' but found: " + content)
355: return False
356:
357: (content, size) = doc.root.child_called ("id2").content
358:
359: # check content
360: if content != "":
361: error ("Expected to find '' but found: " + content)
362: return False
363:
364: return True
365:
366: def test_03():
367: (doc, err) = axl.parse ("<?xml version='1.0' ?>\n\
368: <complex>\n\
369: <data>\n\
370: <row>10</row><row>20</row><row>30</row><row>40</row>\n\
371: </data>\n\
372: <data2>\n\
373: <td> 23 </td>\n\
374: </data2>\n\
375: </complex>", -1)
376:
377: if err:
378: error ("Expected to find proper document load, but a failure was found: " + err.msg)
379: return False
380:
381: if doc.root.name != "complex":
382: error ("Expected to find complex node name but found: " + doc.root.name)
383: return False
384:
385: node = doc.get ("/complex/data2/td")
386: if not node:
387: error ("Expected to find node found at /complex/data2/td")
388: return False
389:
390: # check node name
391: if node.name != "td":
392: error ("Expected to find node name td but found: " + node.name)
393: return False
394:
395: node = doc.get ("/complex/data3/td")
396: if node:
397: error ("Expected to not find node in wrong path but found: " + node.name)
398: return False
399:
400: (content, size) = doc.get ("/complex/data2/td").content
401: if content != " 23 ":
402: error ("Expected to find content ' 23 ' but found: " + content)
403: return False
404:
405: node = doc.get ("complex/data3/td")
406: if node:
407: error ("Expected to find no node but found: " + node.name)
408: return False
409:
410: return True
411:
412: def test_04():
413: (doc, err) = axl.parse ("<?xml version='1.0' ?>" +
414: " <?test \"my content\" ?>" +
415: " <complex>" +
416: " <data>" +
417: " <row>" +
418: " <!-- A comment inside the middle of the document " +
419: " more comments ... " +
420: " more comments \n \r <td>..</td> -->" +
421: " <td>10</td>" +
422: " <test />" +
423: " \n \n \r <?test \"my content\" ?> \n \n" +
424: " <?test \"my content\" ?>" +
425: " <more>" +
426: " <test3 attr='2.0' />" +
427: " </more>" +
428: " </row>" +
429: " <test2 />" +
430: " <non-xml-document>" +
431: " \n \r \n \t" +
432: " <![CDATA[<xml><<<<<<>>>>>><<<>>>><<<<<<>>>]]>"
433: " \r \n \r \t" +
434: " </non-xml-document>" +
435: " </data>" +
436: " <!-- <data>" +
437: " <row>" +
438: " A comment inside the middle of the document " +
439: " more comments ... " +
440: " more comments \n \r <td>..</td> " +
441: " <td>10</td>" +
442: " <test />" +
443: " <more>" +
444: " <test2 attr='2.0' attr2='3.0' attr4='4.0'/>" +
445: " </more>" +
446: " </row>" +
447: " <test2 />" +
448: " </data> -->" +
449: " </complex>", -1)
450:
451: if err:
452: error ("Expected to find proper parse result but found a failure: " + err.msg)
453: return False
454:
455: if doc.get ("/complex/data/row/td").content[0] != "10":
456: error ("Expected to find content 10 but found: " + doc.get ("/complex/data/row/td").content[0])
457: return False
458:
459: node = doc.get ("/complex/data/row/more/test3")
460:
461: if not node.has_attr ("attr"):
462: error ("Expected to find attribute attr but not found")
463: return False
464:
465: # now check attr content
466: if node.attr ("attr") != "2.0":
467: error ("Expected to find attribute value 2.0 but found: " + node.attr ("attr"))
468: return False
469:
470: # check pi instructions support
471: if not doc.has_pi ("test"):
472: error ("Expected to find pi instruction test but not found")
473: return False
474:
475: if doc.pi ("test") != "\"my content\"":
476: error ("Expected to find 'my content' but found: " + doc.pi ("test"))
477: return False
478:
479: # check node content
480: if doc.get ("/complex/data/non-xml-document").content[0] != "<xml><<<<<<>>>>>><<<>>>><<<<<<>>>":
481: error ("Expected to find <xml><<<<<<>>>>>><<<>>>><<<<<<>>> but found: " + doc.get ("/complex/data/non-xml-document").content[0])
482: return False
483:
484: return True
485:
486: def test_05():
487:
488: (doc, err) = axl.file_parse ("test.xml")
489:
490: if doc.get ("/complex/data/row/td").content[0] != "10":
491: error ("Expected to find 10 but found: " + doc.get ("/complex/data/row/td").content[0])
492: return False
493:
494: (doc, err) = axl.file_parse ("test2.xml")
495: if err:
496: error ("Expected to find proper document parsing, but error was found: " + err.msg)
497: return False
498:
499: (dtd, err) = axl.dtd_file_parse ("test.dtd")
500: if err:
501: error ("Expected to find proper dtd parsing, but error was found: " + err.msg)
502: return False
503:
504: # validate content
505: err = dtd.validate (doc)
506: if err:
507: error ("Expected to find proper DTD validation, but a failure was found: " + err.msg)
508: return False
509:
510: return True
511:
512: def test_22():
513:
514: # create a document
515: doc = axl.Doc ()
516:
517: # set root node
518: doc.root = axl.Node ("root-node")
519:
520: # check if the node has an attribute not available
521: if doc.root.has_attr ("attribute-not-found"):
522: error ("Expected to not find attribute: attribute-not-found, but found it")
523: return False
524:
525: # check none value
526: if doc.root.attr ("attribute-not-found"):
527: error ("Expected to find None value associated to missing attribute, but found something defined")
528: return False
529:
530: # configure attribute
531: doc.root.attr ("attribute1", "value1")
532:
533: # check if the node has an attribute available
534: if not doc.root.has_attr ("attribute1"):
535: error ("Expected to find attribute: attribute1, but it wasn't found")
536: return False
537:
538: if not doc.root.attr ("attribute1") == "value1":
539: error ("Expected to find value1 as value associated to attribute1 but found: " + doc.root.attr ("attribute1"))
540: return False
541:
542: # set more attributes
543: doc.root.attr ("attribute2", "value2")
544: doc.root.attr ("attribute3", "value3")
545: doc.root.attr ("attribute4", "value4")
546: doc.root.attr ("attribute5", "value5")
547: doc.root.attr ("attribute6", "value6")
548:
549: # check attr iterator
550: cursor = doc.root.attr_cursor_new ()
551: while cursor.has_item ():
552: if cursor.key == "attribute2" and cursor.value != "value2":
553: error ("Expected to find value2 for attribute2 but found: " + cursor.value)
554: return False
555: if cursor.key == "attribute3" and cursor.value != "value3":
556: error ("Expected to find value3 for attribute3 but found: " + cursor.value)
557: return False
558: if cursor.key == "attribute4" and cursor.value != "value4":
559: error ("Expected to find value4 for attribute4 but found: " + cursor.value)
560: return False
561: if cursor.key == "attribute5" and cursor.value != "value5":
562: error ("Expected to find value5 for attribute5 but found: " + cursor.value)
563: return False
564: if cursor.key == "attribute6" and cursor.value != "value6":
565: error ("Expected to find value6 for attribute6 but found: " + cursor.value)
566: return False
567:
568: # next cursor
569: cursor.next ()
570:
571: return True
572:
573: def test_33():
574:
575: # creates a document with default version, default encoding and standalone = true
576: doc = axl.Doc ()
577:
578: # create a node
579: node = axl.Node ("test")
580:
581: # set as root
582: doc.root = node
583:
584: iterator = 0
585: while iterator < 2:
586: # get a temp reference to the current node
587: temp = doc.root
588:
589: # create a new root
590: node = axl.Node ("test")
591:
592: # set new root
593: doc.root = node
594:
595: # set new child
596: node.set_child (temp)
597:
598: # next position
599: iterator += 1
600:
601: # now create empty nodes
602: node = axl.Node ("test")
603:
604: return True
605:
606: def py_test_01():
607:
608: # parse content
609: (doc, err) = axl.parse ("<content />")
610: if err:
611: error ("Expected to find proper parse operation but found an error: " + err.msg)
612: return False
613:
614: # get the node
615: node = doc.root
616:
617: # get document containing node
618: doc2 = node.doc
619:
620: if doc2.root.name != "content":
621: error ("Expected to find node name: content but found: " + doc2.root.name)
622: return False
623:
624: return True
625:
626: ###########################
627: # intraestructure support #
628: ###########################
629:
630: def info (msg):
631: print "[ INFO ] : " + msg
632:
633: def error (msg):
634: print "[ ERROR ] : " + msg
635:
636: def ok (msg):
637: print "[ OK ] : " + msg
638:
639: def run_all_tests ():
640: test_count = 0
641: for test in tests:
642:
643: # print log
644: info ("TEST-" + str(test_count) + ": Running " + test[1])
645:
646: # call test
647: if not test[0]():
648: error ("detected test failure at: " + test[1])
649: return False
650:
651: # next test
652: test_count += 1
653:
654: ok ("All tests ok!")
655: return True
656:
657: # declare list of tests available
658: tests = [
659: # note test functions starting with test_ are using the same reg test
660: # as defined by test_01.c (axl reg test). Tests that are specific to
661: # py-axl must start with py_test_.
662: (test_01, "Check PyVortex context initialization"),
663: (test_01b, "Check Basic XML parsing, XML document position"),
664: (test_01c, "Check Basic XML parsing, XML document traversing"),
665: (test_01d, "Check Basic XML parsing, node nth access"),
666: (test_01f, "Check Basic XML parsing, white space node content"),
667: (test_03, "Check complex xml error detection"),
668: (test_04, "Check complex xml parsing"),
669: (test_05, "Check DTD basic parsing"),
670: (test_22, "Check Axl node attributes"),
671: (test_33, "Check Recursive root node replace"),
672: (py_test_01, "Check PyNode type attributes"),
673: ]
674:
675: info (" LibAxl: Another XML library (regression test).")
676: info (" Copyright (C) 2008 Advanced Software Production Line, S.L.")
677: info (" Axl regression tests: version=" + axl.VERSION)
678: info (" To gather information about time performance you can use:")
679: info ("")
680: info (" >> time ./test_01.py")
681: info ("")
682: info (" To gather information about memory consumed (and leaks) use:")
683: info ("")
684: info (" >> valgrind --leak-check=yes --show-reachable=yes --suppressions=suppressions.valgrind --gen-suppressions=yes ./test_01.py ")
685: info ("")
686: info (" Report bugs to:")
687: info ("")
688: info (" <axl@lists.aspl.es> Axl mailing list")
689: info ("")
690:
691: if __name__ == '__main__':
692: iterator = 0
693: for arg in sys.argv:
694: # according to the argument position, take the value
695: if iterator == 1:
696: host = arg
697: elif iterator == 2:
698: port = arg
699:
700: # next iterator
701: iterator += 1
702:
703: # call to run all tests
704: run_all_tests ()
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>