blob: 5f8d980a3a9bc70d03b3b63f702e682ce888e32f [file] [log] [blame]
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +02001/* 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 */
Denys Vlasenkoebe6d9d2017-10-05 14:40:24 +02009//applet:IF_UNIT_TEST(APPLET(unit, BB_DIR_USR_BIN, BB_SUID_DROP))
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020010
11//kbuild:lib-$(CONFIG_UNIT_TEST) += bbunit.o
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020012
13//usage:#define unit_trivial_usage
14//usage: ""
15//usage:#define unit_full_usage "\n\n"
16//usage: "Run the unit-test suite"
17
18#include "libbb.h"
19
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020020static llist_t *tests = NULL;
21static unsigned tests_registered = 0;
22static int test_retval;
23
24void bbunit_registertest(struct bbunit_listelem *test)
25{
26 llist_add_to_end(&tests, test);
27 tests_registered++;
28}
29
30void bbunit_settestfailed(void)
31{
32 test_retval = -1;
33}
34
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020035int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM) MAIN_EXTERNALLY_VISIBLE;
36int unit_main(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
37{
38 unsigned tests_run = 0;
39 unsigned tests_failed = 0;
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020040
41 bb_error_msg("Running %d test(s)...", tests_registered);
42 for (;;) {
43 struct bbunit_listelem* el = llist_pop(&tests);
44 if (!el)
45 break;
Bartosz Golaszewski718e2282015-08-13 15:57:22 +020046
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020047 bb_error_msg("Case: [%s]", el->name);
48 test_retval = 0;
49 el->testfunc();
Bartosz Golaszewski718e2282015-08-13 15:57:22 +020050
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020051 if (test_retval < 0) {
52 bb_error_msg("[ERROR] [%s]: TEST FAILED", el->name);
53 tests_failed++;
54 }
55 tests_run++;
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020056 }
57
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020058 if (tests_failed > 0) {
59 bb_error_msg("[ERROR] %u test(s) FAILED", tests_failed);
60 return EXIT_FAILURE;
61 }
Bartosz Golaszewski718e2282015-08-13 15:57:22 +020062
Bartosz Golaszewski3ed81cf2014-06-22 16:30:41 +020063 bb_error_msg("All tests passed");
64 return EXIT_SUCCESS;
65}