Coder Social home page Coder Social logo

fdefelici / clove-unit Goto Github PK

View Code? Open in Web Editor NEW
46.0 2.0 7.0 1.12 MB

Single-Header Unit Testing framework for C (interoperable with C++) with test autodiscovery feature

License: MIT License

CMake 0.26% C 99.74% C++ 0.01%
unit-testing clove single-header unit-testing-framework unit-testing-library c-language clove-unit autodiscovery

clove-unit's People

Contributors

doctorwho406 avatar fdefelici avatar rdeioris avatar vonhoff avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

clove-unit's Issues

CmdLine: add report and output options

Add commandline options for choosing:

  • report format: like pretty print and json (options: -r, --report)
  • output stream: stdout or file (output: -o, --output)

CmdLine: Exit with error configurable on test failure

Currently, if some test fail, the process exit with error code 1.

This could be misleading when you want to know if the process ended gracefully, or exit by applicative or system error.

The idea could be to make the behaviour to return error code, in case of test fail configurable using a specific flag on command line.

For example:
<exec> -x will exit with error in case of test failure.
otherwise it will exit with 0, if the process end gracefully.

NOTE: This is a breaking change with vscode-clove-unit extension

fsanitize emits memory leaks errors

Hello,

when compiling with sanitizers (I used memory, leaks, and undefined), I get a memory leak during the call to the following macro call (I guess it returns early and some temporary malloc is not freed)

__CLOVE_VECTOR_FOREACH(&cmd_handlers, __clove_cmdline_handler_f, handler, {
        __clove_cmdline_errno_t result = (*handler)(&cmdline);
        if (result != __CLOVE_CMD_ERRNO_UNMANAGED) {
            cmd_result =  result;
            break;
        }
    });

I tried to fix it by using various free functions but did not manage to make it pass the CI tests. I'm probably missing some understanding in here. In particular I tried adding

__clove_vector_free(&cmd_handlers);
__clove_cmdline_free(&cmdline);

on the other branch of the if but I guess it frees too much (it indeed removes the memory leak but..... then no test is executed anymore in the CI...).

To add the sanitizers you can add the following lines in the CMakeLists.txt files:

add_compile_options(-fsanitize=address,leak,undefined -g)
add_link_options(-fsanitize=address,leak,undefined)

Edit: formatting.

Pretty Report: print file/line info for skipped test

For pretty report, in case of skipped test, print suite file path followed by test case line number.

NOTE:

  • check if for some reason this information could be useful also for csv and json report.
  • update image in readme with new skipped test output

Console Report: on fail test print path-line to work with ide hyperlink

Feature that could be helpful for the one running clove-unit tests from a Terminal within an ide (e.g. vscode).
Basically the idea is to produce a path-line format so that the ide succeed to create an hyperlink, making the failining testcase easyly reachable with a click (or CTRL+click depending on ide).

Currently in pretty print report, on test with failure the path line are printed like this:

<AbsolutePathToTestFile>@<LineNumber>

Convert this format as c/cpp compilers do, so that will allow an ide with terminal hyperlink feature to create the link with the real file. Possible choises:

  • <AbsolutePathToTestFile>(<LineNumber>)
  • <AbsolutePathToTestFile>:<LineNumber>

NOTE: Compilers format is (<LineNumber>,<ColumnNumber>) or <LineNumber>:<ColumnNumber>, but for this usecase, ColumnNumber could be omitted and hyperlink would be created anyway

This an example of the hyperlink created by vscode from its terminal:
image

CmdLine: Add list tests flag to print suites/tests and code location

consider this (not fully complete) implementation:

#define AS_STRING(x) #x

#define __CLOVE_SUITE_CALL_METHOD_AUTO_2(suite, title, param) __clove_sym___##suite##___##title(param)
#define __CLOVE_SUITE_CALL_METHOD_AUTO_1(suite, name, param) __CLOVE_SUITE_CALL_METHOD_AUTO_2(suite, name, param)
#define __CLOVE_SUITE_PRINT_METHOD_AUTO(suite, name) printf("%s %s %s %d\n", AS_STRING(suite), #name, __FILE__, __LINE__)

#define __CLOVE_TEST_AUTO(title) \
	__CLOVE_SUITE_METHOD_AUTO_1( CLOVE_SUITE_NAME, 30_ ## title, __clove_test_t *_this); \
	__CLOVE_SUITE_METHOD_AUTO_1( CLOVE_SUITE_NAME, 20_ ## title, __clove_test_t *_this) { \
		if (print_tests) \
			__CLOVE_SUITE_PRINT_METHOD_AUTO(CLOVE_SUITE_NAME, title); \
		else \
			__CLOVE_SUITE_CALL_METHOD_AUTO_1(CLOVE_SUITE_NAME, 30_ ## title, _this); \
	} \
	__CLOVE_SUITE_METHOD_AUTO_1( CLOVE_SUITE_NAME, 30_ ## title, __clove_test_t *_this)

when print_tests is true we will get print output of all the tests and when print_tests is false the tests will run as usaul.

Console Report: Create Detail Mode where only test failure are listed

Currently, all the executed tests are listes on the console with PASS, SKIP, FAIL status.

Allow to launch test in "Detail Mode" where only tests with FAIL status are listed.
The "Detail Mode" will provide stats like: total tests, passed/skip/fail count, execution time.

"Detail Mode" could be activated on run tests execution using options:

<program> [-d , --run-detail <detail_mode>]

where <detail mode> could be one of this values:

  • 1 : will report only failed tests
  • 2 : will report only failed and skipped tests
  • 3 : will report all tests (passed, failed and skipped). This is also the default.

NOTE: Need also update:

  • README update to expose command line commands
  • list option in help command

Assertion: Add Regex assert for string

Could be useful for integration testing when you have no control over logic preparing the string and exists some parts that are not deterministic.

This also could be used for improving clove-unit integration testing where the non-deterministic part is the test duration.

Api example:

CLOVE_STRING_REGEX(expected_with_regex, actual)

If expected_with_regex doesn't contain any regex, this should behave like CLOVE_STRING_EQ.

Related to #44

Replace static function with IMPLEMENTATION macro

Now all the function are static so are copied for every inclusion of the header file.
To reduce binary size:

  • split function definition from implementation
  • use CLOVE_IMPLEMENTATION macro to let the user to inclue only once the implementation.
    Something like this
#define CLOVE_IMPLEMENTATION
#include "clove-unit.h"

gcc/clang on linux emit warnings

Hello,

first let me say that this library looks really awesome. Thanks for the great work.

I have nevertheless a slight problem when compiling with gcc or clang (with -Wall, -Wextra, and -pedantic). There are a very large amount of warnings (unknown-pragmas only with gcc, unused parameters, defined but not used variables, unused variables, etc).

I tested with gcc 11.4 and clang 14.0 on Ubuntu.

Unfortunately this is problematic for use for using in a testing framwork since it pollutes a lot the possible warnings of the actual library one wants to test.

Most problems are related with using linux I think and could be corrected with a lot of pre-processing directives spread all over the place.

What would you think about the idea of creating a MR about this?

CmdLine: Passing test list to run by file

Make the command line accept list of test to be run by file.

This could help in some automation/plugin scenario, where the test list is very huge (and command line characters are limited. Eg. windows around 8000 char) to be passed directly on the command line using multiple -i (--include) option.

API: Add assertion for CHAR

To improve semantic add CLOVE__CHAR_EQ and CLOVE__CHAR_NE (instead of using INT version), and have a better output message on console in case of failure

API: Implement SetUp / TearDown fixture

Need to introduce test suite concept so that can have:

  • SetUp / TearDown: executed for each test
  • A List of test to be run
  • Nice to have: SetUpOnce / TearDownOnce: executed before/after all the test in the list

Conan: Automate Release

Conan release process foresees to open a pull request agains conan-center-index.

Understand if could be automatized.

Runner: Customizable Runner

the idea is to make possibile to implement a custom runner.

This will allow the user to execute custom logic before/after clove-unit test execution

Assertion: Add extra assertion for string

idea is to provide assertion helper when dealing with strings like: Contains, StartWith, EndWith....

Understand if make sense or not.

NOTE: Breaking change for VSCode Plugin because adding more string assertion type

Configurable floating-point precision

Consider the floating-point precision configuration. Currently, the following assertion returns true with:

CLOVE_FLOAT_EQ(0.00000198765435, 0.00000112345678);

Should it be specific to each suite or should it be global?

Enable Ansi color for Windows console

On Windows cmd console has not Ansi codes enabled so you will obtain something like this:

image

Understand if it possible to check at runtime if console support coloring, or eventually enable Ansi

Run Pretty: Make inclusion / exclusion options accept test index

Currently -i / -e options accepts expressions involving Suite name and Test name.

When running test using pretty report a test index is shown:
image

So eventually could be usefull allow include/exclude options to accept also test indexes, like this:

  • <program> -i 1 will run the test with "01" index
  • <program> -e 2 will run the test with "01" and "03" index

This kind of change will have a huge impact on test filtering process, because filtering happen while retrieving test symbols while test index is assigned after when test run (depending on the number of test executed)

NOTE:

  • to update commandline api documentation
  • to update help command

Suggestion: Remove semicolons in public assertions

In the current implementation, all public assertions end with semicolons, leading to empty statements when assertions are followed by semicolons, as shown in the sample code and screenshot below:

image

To address this issue, one solution would be to remove semicolons from public assertions, requiring developers to use semicolons after assertions. However, this change would have an impact on developers who have not previously used semicolons after their assertions in order to avoid empty statements.

Another potential solution is to update the documentation and sample code to indicate that semicolons are not required after assertions.

Add Release Automation

add support for travis-ci and release automation creation.

Try compiling / executing with different compiler / os

CmdLine: Make args available for tests

The idea could be to make cmdline arguments available within the test case, in order to implement custom logic.

Eventually from this args could be filtered out the one related to clove-unit command line, and just keep the user defined parameters.

Potential use cases:

  • Execute tests assertion on a dynamic collection for performances. So from commandline, one can input the number of elements
  • Dynamically change some assertion value
  • Even skip test execution following custom logic (instead of using -include -exclude params)

Double allocation in __clove_path_basepath

__clove_path_basepath uses strdup and calloc. This results in two separate memory allocations, which is inefficient and may cause memory fragmentation. A better approach would be to use only one allocation as mentioned in the function itself.
See: clove-unit.h#L818

Report CSV/JSON: add test case function line number

Add test function line number in both csv/json test execution report.

This to open better intergration with plugins and automation.

NOTE:

  • For CSV format could have some impact in naming properly column names (already exits 'line' column referred to the assert withing the test)
  • Maybe better to split this issue in 2, one related to the json (without impact) and one related to csv.

Remove Manual Mode

Dropping manual mode feature. Autodiscovery implementation is good enough, so no need to keep the manual one.

RunTest Pretty Print: Align test counter

Currently test counter is like this:

[INFO] 9) UNIT_StringViewTest.Contains.............................[PASS] (0.011 ms)
[INFO] 99) UNIT_StringViewTest.AsString............................[PASS] (0.011 ms)
[INFO] 100) UNIT_StringViewTest.BeginAndEnd........................[PASS] (0.001 ms)

It could become like this:

[INFO] 009) UNIT_StringViewTest.Contains...........................[PASS] (0.011 ms)
[INFO] 099) UNIT_StringViewTest.AsString...........................[PASS] (0.011 ms)
[INFO] 100) UNIT_StringViewTest.BeginAndEnd........................[PASS] (0.001 ms)

So '0' left padding to reach same length of the highest test number.

NOTE: Update reference image (pretty print) in Readme

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.