3
0

bbunit.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * bbunit: Simple unit-testing framework for Busybox.
  4. *
  5. * Copyright (C) 2014 by Bartosz Golaszewski <bartekgola@gmail.com>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. //kbuild:lib-$(CONFIG_UNIT_TEST) += bbunit.o
  10. //applet:IF_UNIT_TEST(APPLET(unit, BB_DIR_USR_BIN, BB_SUID_DROP))
  11. //usage:#define unit_trivial_usage
  12. //usage: ""
  13. //usage:#define unit_full_usage "\n\n"
  14. //usage: "Run the unit-test suite"
  15. #include "libbb.h"
  16. static llist_t *tests = NULL;
  17. static unsigned tests_registered = 0;
  18. static int test_retval;
  19. void bbunit_registertest(struct bbunit_listelem *test)
  20. {
  21. llist_add_to_end(&tests, test);
  22. tests_registered++;
  23. }
  24. void bbunit_settestfailed(void)
  25. {
  26. test_retval = -1;
  27. }
  28. int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) MAIN_EXTERNALLY_VISIBLE;
  29. int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
  30. {
  31. unsigned tests_run = 0;
  32. unsigned tests_failed = 0;
  33. bb_error_msg("Running %d test(s)...", tests_registered);
  34. for (;;) {
  35. struct bbunit_listelem* el = llist_pop(&tests);
  36. if (!el)
  37. break;
  38. bb_error_msg("Case: [%s]", el->name);
  39. test_retval = 0;
  40. el->testfunc();
  41. if (test_retval < 0) {
  42. bb_error_msg("[ERROR] [%s]: TEST FAILED", el->name);
  43. tests_failed++;
  44. }
  45. tests_run++;
  46. }
  47. if (tests_failed > 0) {
  48. bb_error_msg("[ERROR] %u test(s) FAILED", tests_failed);
  49. return EXIT_FAILURE;
  50. }
  51. bb_error_msg("All tests passed");
  52. return EXIT_SUCCESS;
  53. }