2
0

tst-dom.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* iksemel (XML parser for Jabber)
  2. ** Copyright (C) 2000-2003 Gurer Ozen <madcat@e-kolay.net>
  3. ** This code is free software; you can redistribute it and/or
  4. ** modify it under the terms of GNU Lesser General Public License.
  5. */
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <stdarg.h>
  10. #include "iksemel.h"
  11. iks *my_x;
  12. int nr_tests;
  13. #define PR_TEST printf ("DOM test %d:\n", nr_tests)
  14. void
  15. document (char *xml)
  16. {
  17. enum ikserror err;
  18. iksparser *p;
  19. nr_tests++;
  20. if (my_x) iks_delete (my_x);
  21. p = iks_dom_new (&my_x);
  22. err = iks_parse (p, xml, 0, 1);
  23. switch (err) {
  24. case IKS_OK:
  25. break;
  26. case IKS_NOMEM:
  27. PR_TEST;
  28. puts ("Not enough memory.");
  29. exit (1);
  30. case IKS_BADXML:
  31. PR_TEST;
  32. printf ("Invalid xml at byte %ld in\n[%s]\n", iks_nr_bytes (p), xml);
  33. exit (1);
  34. case IKS_HOOK:
  35. PR_TEST;
  36. puts ("Hook.");
  37. }
  38. iks_parser_delete (p);
  39. }
  40. void
  41. tag (char *name, ...)
  42. {
  43. iks *x;
  44. va_list ap;
  45. x = my_x;
  46. va_start (ap, name);
  47. while (1) {
  48. char *name = iks_name (x);
  49. char *tmp = va_arg (ap, char*);
  50. if (NULL == tmp) break;
  51. x = iks_find (x, tmp);
  52. if (!x) {
  53. PR_TEST;
  54. printf ("Tag <%s> is not a child of tag <%s>\n", tmp, name);
  55. exit (1);
  56. }
  57. }
  58. if (!x || NULL == iks_find (x, name)) {
  59. PR_TEST;
  60. printf ("Tag <%s> is not a child of tag <%s>\n", name, iks_name (x));
  61. exit (1);
  62. }
  63. va_end (ap);
  64. }
  65. void
  66. cdata (char *data, ...)
  67. {
  68. iks *x;
  69. va_list ap;
  70. x = my_x;
  71. va_start (ap, data);
  72. while (1) {
  73. char *name = iks_name (x);
  74. char *tmp = va_arg (ap, char*);
  75. if (NULL == tmp) break;
  76. x = iks_find (x, tmp);
  77. if (!x) {
  78. PR_TEST;
  79. printf ("Tag <%s> is not a child of tag <%s>\n", tmp, name);
  80. exit (1);
  81. }
  82. }
  83. if (iks_strcmp ( iks_cdata (iks_child (x)), data) != 0) {
  84. PR_TEST;
  85. printf ("CDATA [%s] not found.\n", data);
  86. exit (1);
  87. }
  88. va_end (ap);
  89. }
  90. void
  91. attrib (char *att, char *val, ...)
  92. {
  93. iks *x;
  94. va_list ap;
  95. x = my_x;
  96. va_start (ap, val);
  97. while (1) {
  98. char *name = iks_name (x);
  99. char *tmp = va_arg (ap, char*);
  100. if (NULL == tmp) break;
  101. x = iks_find (x, tmp);
  102. if (!x) {
  103. PR_TEST;
  104. printf ("Tag <%s> is not a child of tag <%s>\n", tmp, name);
  105. exit (1);
  106. }
  107. }
  108. if (iks_strcmp (val, iks_find_attrib (x, att)) != 0) {
  109. PR_TEST;
  110. printf ("Attribute '%s' not found.\n", att);
  111. exit (1);
  112. }
  113. va_end (ap);
  114. }
  115. void
  116. string (char *xml)
  117. {
  118. char *tmp;
  119. tmp = iks_string (iks_stack (my_x), my_x);
  120. if (iks_strcmp (tmp, xml) != 0) {
  121. PR_TEST;
  122. printf ("Result: %s\n", tmp);
  123. printf ("Expected: %s\n", xml);
  124. exit (1);
  125. }
  126. }
  127. static char buf[] =
  128. "<presence id='JCOM_11' to='lala@j.org' type='available'><status>"
  129. "&quot; &lt;online&amp;dangerous&gt; &quot;</status>meow<a><b c='d'/>"
  130. "</a><test/></presence>";
  131. int main (int argc, char *argv[])
  132. {
  133. document ("<atag></atag>");
  134. string ("<atag/>");
  135. document ("<test>lala<b>bold</b>blablabla<a><c/></a></test>");
  136. tag ("b", 0);
  137. tag ("c", "a", 0);
  138. string ("<test>lala<b>bold</b>blablabla<a><c/></a></test>");
  139. document (buf);
  140. cdata ("\" <online&dangerous> \"", "status", 0);
  141. attrib ("c", "d", "a", "b", 0);
  142. tag ("test", 0);
  143. string (buf);
  144. return 0;
  145. }