ex_output.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Check: a unit test framework for C
  3. * Copyright (C) 2001, 2002 Arien Malec
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the
  17. * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
  18. * MA 02110-1301, USA.
  19. */
  20. #include <stdlib.h>
  21. #include <stdio.h>
  22. #include <string.h>
  23. #include <check.h>
  24. #include "config.h"
  25. START_TEST(test_pass)
  26. {
  27. ck_assert_msg(1 == 1, "Shouldn't see this");
  28. }
  29. END_TEST
  30. START_TEST(test_fail)
  31. {
  32. ck_abort_msg("Failure");
  33. }
  34. END_TEST
  35. /*
  36. * This test will fail without fork, as it will result in the
  37. * unit test runniner exiting early.
  38. */
  39. #if defined(HAVE_FORK) && HAVE_FORK==1
  40. START_TEST(test_exit)
  41. {
  42. exit(1);
  43. }
  44. END_TEST
  45. #endif /* HAVE_FORK */
  46. /*
  47. * This test will intentionally mess up the unit testing program
  48. * when fork is unavailable. The purpose of including it is to
  49. * ensure that the tap output is correct when a test crashes.
  50. */
  51. START_TEST(test_abort)
  52. {
  53. exit(1);
  54. }
  55. END_TEST
  56. START_TEST(test_pass2)
  57. {
  58. ck_assert_msg(1 == 1, "Shouldn't see this");
  59. }
  60. END_TEST
  61. START_TEST(test_loop)
  62. {
  63. ck_assert_msg(_i == 1, "Iteration %d failed", _i);
  64. }
  65. END_TEST
  66. START_TEST(test_xml_esc_fail_msg)
  67. {
  68. ck_abort_msg("fail \" ' < > & \x9 \xA" "X""\x08"" message"); /* backspace char \x08 deletes the X */
  69. }
  70. END_TEST
  71. static Suite *make_log1_suite(void)
  72. {
  73. Suite *s;
  74. TCase *tc;
  75. s = suite_create("S1");
  76. tc = tcase_create("Core");
  77. suite_add_tcase(s, tc);
  78. tcase_add_test(tc, test_pass);
  79. tcase_add_test(tc, test_fail);
  80. #if defined(HAVE_FORK) && HAVE_FORK==1
  81. tcase_add_test(tc, test_exit);
  82. #endif /* HAVE_FORK */
  83. return s;
  84. }
  85. static Suite *make_log2_suite(int include_exit_test)
  86. {
  87. Suite *s;
  88. TCase *tc;
  89. s = suite_create("S2");
  90. tc = tcase_create("Core");
  91. suite_add_tcase(s, tc);
  92. if(include_exit_test == 1)
  93. {
  94. tcase_add_test(tc, test_abort);
  95. }
  96. tcase_add_test(tc, test_pass2);
  97. tcase_add_loop_test(tc, test_loop, 0, 3);
  98. return s;
  99. }
  100. /* check that XML special characters are properly escaped in XML log file */
  101. static Suite *make_xml_esc_suite(void)
  102. {
  103. Suite *s;
  104. TCase *tc;
  105. s = suite_create("XML escape \" ' < > & \x9 \xA" "X""\x08"" tests"); /* backspace char \x08 deletes the X */
  106. tc = tcase_create("description \" ' < > & \x9 \xA" "X""\x08"" end"); /* backspace char \x08 deletes the X */
  107. suite_add_tcase(s, tc);
  108. tcase_add_test(tc, test_xml_esc_fail_msg);
  109. return s;
  110. }
  111. static void print_usage(void)
  112. {
  113. printf("Usage: ex_output (CK_SILENT | CK_MINIMAL | CK_NORMAL | CK_VERBOSE | CK_ENV");
  114. #if ENABLE_SUBUNIT
  115. printf(" | CK_SUBUNIT");
  116. #endif
  117. printf(")\n");
  118. printf(" (STDOUT | STDOUT_DUMP | LOG | LOG_STDOUT | TAP | TAP_STDOUT | XML | XML_STDOUT)\n");
  119. printf(" (NORMAL | EXIT_TEST)\n");
  120. printf(" If CK_ENV is used, the environment variable CK_VERBOSITY can be set to\n");
  121. printf(" one of these: silent, minimal, or verbose. If it is not set to these, or\n");
  122. printf(" if CK_VERBOSITY is not set, then CK_NORMAL will be used\n");
  123. printf(" If testing the CK_[LOG|TAP_LOG|XML_LOG]_FILE_NAME env var and setting it to '-',\n");
  124. printf(" then use the following mode: CK_SILENT STDOUT [NORMAL|EXIT_TEST].\n");
  125. }
  126. static void run_tests(enum print_output printmode, char *log_type, int include_exit_test)
  127. {
  128. SRunner *sr;
  129. int dump_everything_to_stdout = 0;
  130. sr = srunner_create(make_log1_suite());
  131. srunner_add_suite(sr, make_log2_suite(include_exit_test));
  132. srunner_add_suite(sr, make_xml_esc_suite());
  133. if(strcmp(log_type, "STDOUT") == 0)
  134. {
  135. /* Nothing else to do here */
  136. }
  137. else if(strcmp(log_type, "STDOUT_DUMP") == 0)
  138. {
  139. /*
  140. * Dump each type to stdout, in addition to printing out
  141. * the configured print level.
  142. */
  143. dump_everything_to_stdout = 1;
  144. }
  145. else if(strcmp(log_type, "LOG") == 0)
  146. {
  147. srunner_set_log(sr, "test.log");
  148. }
  149. else if(strcmp(log_type, "LOG_STDOUT") == 0)
  150. {
  151. srunner_set_log(sr, "-");
  152. }
  153. else if(strcmp(log_type, "TAP") == 0)
  154. {
  155. srunner_set_tap(sr, "test.tap");
  156. }
  157. else if(strcmp(log_type, "TAP_STDOUT") == 0)
  158. {
  159. srunner_set_tap(sr, "-");
  160. }
  161. else if(strcmp(log_type, "XML") == 0)
  162. {
  163. srunner_set_xml(sr, "test.xml");
  164. }
  165. else if(strcmp(log_type, "XML_STDOUT") == 0)
  166. {
  167. srunner_set_xml(sr, "-");
  168. }
  169. else
  170. {
  171. print_usage();
  172. exit(EXIT_FAILURE);
  173. }
  174. srunner_run_all(sr, printmode);
  175. if(dump_everything_to_stdout)
  176. {
  177. srunner_print(sr, CK_SILENT);
  178. srunner_print(sr, CK_MINIMAL);
  179. srunner_print(sr, CK_NORMAL);
  180. srunner_print(sr, CK_VERBOSE);
  181. srunner_print(sr, CK_ENV);
  182. #if ENABLE_SUBUNIT
  183. /*
  184. * Note that this call does not contribute anything, as
  185. * subunit is not fully considered an 'output mode'.
  186. */
  187. srunner_print(sr, CK_SUBUNIT);
  188. #endif
  189. }
  190. srunner_free(sr);
  191. }
  192. #define OUTPUT_TYPE_ARG 1
  193. #define LOG_TYPE_ARG 2
  194. #define INCLUDE_EXIT_TEST_ARG 3
  195. int main(int argc, char **argv)
  196. {
  197. enum print_output printmode;
  198. int include_exit_test;
  199. if(argc != 4)
  200. {
  201. print_usage();
  202. return EXIT_FAILURE;
  203. }
  204. if(strcmp(argv[OUTPUT_TYPE_ARG], "CK_SILENT") == 0)
  205. {
  206. printmode = CK_SILENT;
  207. }
  208. else if(strcmp(argv[OUTPUT_TYPE_ARG], "CK_MINIMAL") == 0)
  209. {
  210. printmode = CK_MINIMAL;
  211. }
  212. else if(strcmp(argv[OUTPUT_TYPE_ARG], "CK_NORMAL") == 0)
  213. {
  214. printmode = CK_NORMAL;
  215. }
  216. else if(strcmp(argv[OUTPUT_TYPE_ARG], "CK_VERBOSE") == 0)
  217. {
  218. printmode = CK_VERBOSE;
  219. }
  220. else if(strcmp(argv[OUTPUT_TYPE_ARG], "CK_ENV") == 0)
  221. {
  222. printmode = CK_ENV;
  223. }
  224. #if ENABLE_SUBUNIT
  225. else if(strcmp(argv[OUTPUT_TYPE_ARG], "CK_SUBUNIT") == 0)
  226. {
  227. printmode = CK_SUBUNIT;
  228. }
  229. #endif
  230. else
  231. {
  232. print_usage();
  233. return EXIT_FAILURE;
  234. }
  235. if(strcmp(argv[INCLUDE_EXIT_TEST_ARG], "NORMAL") == 0)
  236. {
  237. include_exit_test = 0;
  238. }
  239. else if(strcmp(argv[INCLUDE_EXIT_TEST_ARG], "EXIT_TEST") == 0)
  240. {
  241. include_exit_test = 1;
  242. }
  243. else
  244. {
  245. print_usage();
  246. return EXIT_FAILURE;
  247. }
  248. run_tests(printmode, argv[LOG_TYPE_ARG], include_exit_test);
  249. return EXIT_SUCCESS;
  250. }