minunit.h — Test Framework
Minimal header-only unit-test framework for 42sh.
Provides shared pass/fail counters and assertion macros used across all test suites. No external libraries are required beyond libc.
- Author
pulgamecanica (arosado-)
Defines
-
MU_ASSERT(msg, expr)
-
MU_ASSERT_INT(expected, actual)
-
MU_ASSERT_STR(msg, expected, actual)
-
MU_RUN(fn)
-
MU_SUMMARY()
Functions
-
static inline void mu_assert(const char *file, int line, const char *msg, int expr)
Generic boolean assertion.
Evaluates expr. Prints a green PASS line on success, or a red FAIL line including the source location on failure. Do not call directly — use the MU_ASSERT macro.
Example:
MU_ASSERT("pointer is not null", ptr != NULL);
- Parameters:
msg – Human-readable description shown in the output line.
expr – Non-zero = pass, zero = fail.
-
static inline void mu_assert_int(const char *file, int line, int expected, int actual)
Integer equality assertion.
Compares expected and actual as
intvalues. On failure the output includes both the expected and the actual values. Do not call directly — use the MU_ASSERT_INT macro.Example:
MU_ASSERT_INT(3, (int)ft_lstsize(list));
- Parameters:
expected – The value the expression should produce.
actual – The expression under test.
-
static inline void mu_assert_str(const char *file, int line, const char *msg, const char *expected, const char *actual)
String equality assertion (NULL-safe).
Compares expected and actual with strcmp(3). A NULL actual is treated as a failure and printed as
"(null)"in the output line. Do not call directly — use the MU_ASSERT_STR macro.Example:
MU_ASSERT_STR("newest entry", "pwd", history_prev(&h));
- Parameters:
msg – Human-readable label shown in the output line.
expected – The expected C string (must not be NULL).
actual – The actual C string returned by the code under test.
-
static inline void mu_run(const char *name, void (*fn)(void))
Execute a test suite function and announce its name.
Prints a cyan header line with the name of fn then calls
fn(). Typically used inmain()of test_runner.c. Do not call directly — use the MU_RUN macro.Example:
MU_RUN(test_list_suite);
- Parameters:
name – Stringified name of the test suite function.
fn – Function pointer to the test suite.
-
static inline int mu_summary(void)
Print the final pass/fail totals and return an exit code.
Must be called as the last statement in
main()via the MU_SUMMARY macro. Returns0if all tests passed or1if any test failed, so thatmakecan propagate the result.Example:
int main(void) { MU_RUN(test_list_suite); MU_SUMMARY(); }
Variables
-
int g_mu_passed
Running total of assertions that passed.
Defined in test_runner.c; all test translation units share this counter through the
externdeclaration in this header.
-
int g_mu_failed
Running total of assertions that failed.
A non-zero value causes MU_SUMMARY() to return exit code 1 so that
makecan detect and report test failures.