assert.h

Single-file assertion library for C
git clone git://git.finwo.net/lib/assert.h
Log | Files | Refs | README

commit 0268274f362c1090ebd01161e8723dcf63cd4543
parent aa307ecd5203fb8ab2546d8378b37d3f9f37794f
Author: Yersa Nordman <finwo@pm.me>
Date:   Mon, 11 Mar 2024 23:30:13 +0100

Moved whole readme into headerfile as well

Diffstat:
A.gitignore | 3+++
DLICENSE | 7-------
AMakefile | 2++
MREADME.md | 111+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
Minclude/test.h | 208+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------
5 files changed, 268 insertions(+), 63 deletions(-)

diff --git a/.gitignore b/.gitignore @@ -0,0 +1,3 @@ +/lib/ +.DS_Store +._.DS_Store diff --git a/LICENSE b/LICENSE @@ -1,7 +0,0 @@ -Copyright 2023 finwo - -Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/Makefile b/Makefile @@ -0,0 +1,2 @@ +README.md: include/test.h + stddoc < $^ > $@ diff --git a/README.md b/README.md @@ -1,4 +1,4 @@ -C-Assert +assert.h ======== Single-file unit-testing library for C @@ -24,8 +24,8 @@ Todo Example Usage ------------- -```c -#include "test.h" +```C +#include "finwo/assert.h" #include "mylib.h" void test_sheep() { @@ -47,11 +47,106 @@ int main() { To run the tests, compile the tests as a binary and run it. +API +--- + + +### Macros + +<details> + <summary>ASSERT(msg, expression)</summary> + + Perform an assertion + +```C +#define ASSERT(msg, expression) if (!tap_assert(__FILE__, __LINE__, (msg), (#expression), (expression) ? 1 : 0)) return +``` + +</details> +<details> + <summary>ASSERT_EQUALS(expected, actual)</summary> + + Perform an equal assertion + +```C +/* Convenient assertion methods */ +/* TODO: Generate readable error messages for assert_equals or assert_str_equals */ +#define ASSERT_EQUALS(expected, actual) ASSERT((#actual), (expected) == (actual)) +``` + +</details> +<details> + <summary>ASSERT_STRING_EQUALS(expected, actual)</summary> + + Perform an equal string assertion + +```C +#define ASSERT_STRING_EQUALS(expected, actual) ASSERT((#actual), strcmp((expected),(actual)) == 0) +``` + +</details> +<details> + <summary>RUN(fn)</summary> + + Run a test suite/function containing assertions + +```C +#define RUN(test_function) tap_execute((#test_function), (test_function)) +``` + +</details> +<details> + <summary>TEST_REPORT()</summary> + + Report on the tests that have been run + +```C +#define TEST_REPORT() tap_report() +``` + +</details> + +Extras +------ + +### Disable color + +If you want to disable color during the assertions, because you want to +interpret the output for example (it is "tap" format after all), you can +define `NO_COLOR` during compilation to disable color output. + +```sh +cc -D NO_COLOR source.c -o test +``` + +### Silent assertions + +You can also fully disable output for assertions by defining the +`ASSERT_SILENT` macro. This will fully disable the printf performed after +the assertion is performed. + +```sh +cc -D ASSERT_SILENT source.c -o test +``` + +### Silent reporting + +If you do not want the report to be displayed at the end, you can define the +`REPORT_SILENT` macro. This will disable the printf during reporting and +only keep the return code. + +```sh +cc -D REPORT_SILENT source.c -o test +``` + + Credits ------- -This library was heavily based on the -[tinytest](https://github.com/joewalnes/tinytest) library by -[Joe Walnes](https://github.com/joewalnes). A license reference to his library -could not be found, which is why this reference is in this file. Should I be -contacted about licensing issues, I'll investigate further. +This library was heavily based on the [tinytest][tinytest] library by +[Joe Walnes][joewalnes]. A license reference to his library could not be +found, which is why this reference is in this file. Should I be contacted +about licensing issues, I'll investigate further. + +[joewalnes]: https://github.com/joewalnes +[tinytest]: https://github.com/joewalnes/tinytest diff --git a/include/test.h b/include/test.h @@ -1,44 +1,51 @@ -/* Based on http://github.com/joewalnes/tinytest - * - * TinyTest: A really really really tiny and simple no-hassle C unit-testing framework. - * - * Features: - * - No library dependencies. Not even itself. Just a header file. - * - Simple ANSI C. Should work with virtually every C or C++ compiler on - * virtually any platform. - * - Reports assertion failures, including expressions and line numbers. - * - Stops test on first failed assertion. - * - ANSI color output for maximum visibility. - * - Easy to embed in apps for runtime tests (e.g. environment tests). - * - * Example Usage: - * - * #include "tinytest.h" - * #include "mylib.h" - * - * void test_sheep() - * { - * ASSERT("Sheep are cool", are_sheep_cool()); - * ASSERT_EQUALS(4, sheep.legs); - * } - * - * void test_cheese() - * { - * ASSERT("Cheese is tangy", cheese.tanginess > 0); - * ASSERT_STRING_EQUALS("Wensleydale", cheese.name); - * } - * - * int main() - * { - * RUN(test_sheep); - * RUN(test_cheese); - * return TEST_REPORT(); - * } - * - * To run the tests, compile the tests as a binary and run it. - * - * Project home page: http://github.com/finwo/c-assert - */ +/// assert.h +/// ======== +/// +/// Single-file unit-testing library for C +/// +/// Features +/// -------- +/// +/// - Single header file, no other library dependencies +/// - Simple ANSI C. The library should work with virtually every C(++) compiler on +/// virtually any playform +/// - Reporting of assertion failures, including the expression and location of the +/// failure +/// - Stops test on first failed assertion +/// - ANSI color output for maximum visibility +/// - Easily embeddable in applications for runtime tests or separate testing +/// applications +/// +/// Todo +/// ---- +/// +/// - Disable assertions on definition, to allow production build without source modifications +/// +/// Example Usage +/// ------------- +/// +/// ```C +/// #include "finwo/assert.h" +/// #include "mylib.h" +/// +/// void test_sheep() { +/// ASSERT("Sheep are cool", are_sheep_cool()); +/// ASSERT_EQUALS(4, sheep.legs); +/// } +/// +/// void test_cheese() { +/// ASSERT("Cheese is tangy", cheese.tanginess > 0); +/// ASSERT_STRING_EQUALS("Wensleydale", cheese.name); +/// } +/// +/// int main() { +/// RUN(test_sheep); +/// RUN(test_cheese); +/// return TEST_REPORT(); +/// } +/// ``` +/// +/// To run the tests, compile the tests as a binary and run it. #ifndef __TINYTEST_INCLUDED_H__ #define __TINYTEST_INCLUDED_H__ @@ -47,22 +54,110 @@ #include <stdlib.h> #include <string.h> -/* Main assertion method */ +/// +/// API +/// --- +/// + +/// +/// ### Macros +/// + + +/// <details> +/// <summary>ASSERT(msg, expression)</summary> +/// +/// Perform an assertion +///<C #define ASSERT(msg, expression) if (!tap_assert(__FILE__, __LINE__, (msg), (#expression), (expression) ? 1 : 0)) return +///> +/// </details> + +/// <details> +/// <summary>ASSERT_EQUALS(expected, actual)</summary> +/// +/// Perform an equal assertion +///<C /* Convenient assertion methods */ /* TODO: Generate readable error messages for assert_equals or assert_str_equals */ #define ASSERT_EQUALS(expected, actual) ASSERT((#actual), (expected) == (actual)) +///> +/// </details> + +/// <details> +/// <summary>ASSERT_STRING_EQUALS(expected, actual)</summary> +/// +/// Perform an equal string assertion +///<C #define ASSERT_STRING_EQUALS(expected, actual) ASSERT((#actual), strcmp((expected),(actual)) == 0) +///> +/// </details> -/* Run a test() function */ +/// <details> +/// <summary>RUN(fn)</summary> +/// +/// Run a test suite/function containing assertions +///<C #define RUN(test_function) tap_execute((#test_function), (test_function)) +///> +/// </details> + +/// <details> +/// <summary>TEST_REPORT()</summary> +/// +/// Report on the tests that have been run +///<C #define TEST_REPORT() tap_report() +///> +/// </details> + +/// +/// Extras +/// ------ +/// +/// ### Disable color +/// +/// If you want to disable color during the assertions, because you want to +/// interpret the output for example (it is "tap" format after all), you can +/// define `NO_COLOR` during compilation to disable color output. +/// +/// ```sh +/// cc -D NO_COLOR source.c -o test +/// ``` +/// +/// ### Silent assertions +/// +/// You can also fully disable output for assertions by defining the +/// `ASSERT_SILENT` macro. This will fully disable the printf performed after +/// the assertion is performed. +/// +/// ```sh +/// cc -D ASSERT_SILENT source.c -o test +/// ``` +/// +/// ### Silent reporting +/// +/// If you do not want the report to be displayed at the end, you can define the +/// `REPORT_SILENT` macro. This will disable the printf during reporting and +/// only keep the return code. +/// +/// ```sh +/// cc -D REPORT_SILENT source.c -o test +/// ``` +/// -#define TAP_COLOR_CODE 0x1B -#define TAP_COLOR_RED "[1;31m" +#ifdef NO_COLOR +#define TAP_COLOR_CODE "" +#define TAP_COLOR_RED "" +#define TAP_COLOR_GREEN "" +#define TAP_COLOR_RESET "" +#else +#define TAP_COLOR_CODE "\x1B" +#define TAP_COLOR_RED "[1;31m" #define TAP_COLOR_GREEN "[1;32m" #define TAP_COLOR_RESET "[0m" +#endif int tap_asserts = 0; int tap_passes = 0; @@ -80,16 +175,19 @@ int tap_assert(const char* file, int line, const char* msg, const char* expressi if (pass) { tap_passes++; - printf("%c%sok%c%s %d - %s\n", +#ifndef ASSERT_SILENT + printf("%s%sok%s%s %d - %s\n", TAP_COLOR_CODE, TAP_COLOR_GREEN, TAP_COLOR_CODE, TAP_COLOR_RESET, tap_asserts, msg ); +#endif } else { tap_fails++; +#ifndef ASSERT_SILENT printf( - "%c%snot ok%c%s %d - %s\n" + "%s%snot ok%s%s %d - %s\n" " On %s:%d, in test %s()\n" " %s\n" , @@ -100,11 +198,12 @@ int tap_assert(const char* file, int line, const char* msg, const char* expressi expression ); } - +#endif return pass; } int tap_report(void) { +#ifndef REPORT_SILENT printf( "1..%d\n" "# tests %d\n" @@ -115,7 +214,20 @@ int tap_report(void) { tap_passes, tap_fails ); +#endif return tap_fails ? 2 : 0; } #endif // __TINYTEST_INCLUDED_H__ + +/// +/// Credits +/// ------- +/// +/// This library was heavily based on the [tinytest][tinytest] library by +/// [Joe Walnes][joewalnes]. A license reference to his library could not be +/// found, which is why this reference is in this file. Should I be contacted +/// about licensing issues, I'll investigate further. +/// +/// [joewalnes]: https://github.com/joewalnes +/// [tinytest]: https://github.com/joewalnes/tinytest