ThrowTheSwitch / Unity

Simple Unit Testing for C
ThrowTheSwitch.org
MIT License
4.11k stars 983 forks source link

Generic assertion #743

Open yairlenga opened 3 months ago

yairlenga commented 3 months ago

Practically (almost) every C compiler provide support for _generic (with gcc/clang having additional features based on extensions). Would it be possible to implement a generic TEST-EQUAL that will get dispatched to the correct type-based test ?

Ideally, will support wide all atomic types, and therecarray variants.

For my test cases, 90% of assertion are for EQUAL, will make test cases more compact, and reduce time to write. Expecting build time errors if applied to unsupported type.

mvandervoord commented 3 months ago

To allow myself to get sidetracked quickly... let's avoid saying things like "Practically (almost) every C compiler provide support for _generic"... particularly since it was only introduced in C11 and its use is sadly not widespread in the embedded space yet.

The argumentative opener aside, this is a topic worth discussing. Do you envision that this generic assertion would attempt to identify the types involved and format the failures with the best formatting, or are you more interested in a more streamlined notation (instead of needing to remember/match all the assertion types)?

If you don't care about the format of the error and only want to compare two values, you can already enable UNITY_SHORTHAND_AS_RAW and then you can use TEST_ASSERT_EQUAL(e,a) just as you've described. If the two types are compatible, the compiler will happily compile it. A failure will simply let you know that this line failed and it's up to the user to determine what the values were.

Or perhaps neither of these options is what you have in mind?

yairlenga commented 3 months ago

I apologize for argumentative tone. I was trying to establish the case that for desktop/server, the _Generic feature is widely available.

Ideally, implementation will be as you described - if the type is known, it should produce output based on the type. My "poor-man" implementation did not have the fallback to "any" type (like you described with UNITY_SHORTHAND). In the applications that I work with, we constantly have 2-5% failures on a large test suite. We will proceed with releasing code without 100% pass rate, based on human review of the failures - good formatting of error messages make a difference.

For my use case (financial app) - If the _Generic version will handle int, bool, float and double (including arrays - int [], double [], ...) - it solve my problem. I assume other users might want to see the minimal implementation include additional types.

Thanks for taking time to look at this issue.

mvandervoord commented 3 months ago

Thank you! This is a good idea and I appreciate you sharing it.

We're wrapping up a release of Ceedling at the moment, but this is exactly the type of feature I'd like to introduce after that.