Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for Asserting Equality of C++ UDTs #671

Open
TrebledJ opened this issue Apr 20, 2023 · 1 comment
Open

Support for Asserting Equality of C++ UDTs #671

TrebledJ opened this issue Apr 20, 2023 · 1 comment

Comments

@TrebledJ
Copy link

TrebledJ commented Apr 20, 2023

I'm using Unity for a PlatformIO project. I would like to test a C++ library I wrote, which contains user-defined types (UDT). However it seems Unity limits macros to ints, strings (const char*), pointers, etc. It would be nice if there were TEST_X_EQUAL macros that were generic, and be able to check equality == (possibly overloaded for our UDTs).

Are there any plans for this?

@TrebledJ TrebledJ changed the title Support for C++ UDTs Support for Asserting Equality of C++ UDTs Apr 20, 2023
@mvandervoord
Copy link
Member

Hi.

Unity already has this feature, it's just not documented well. It's used quite a bit in our classes. I need to make sure some of the documentation from the class migrates its way back to here.

You're looking for the unity_helper feature. You can create as many custom assertion types as you want. You place them in a unity_helper.h and unity_helper.c/cpp file. If you follow the same naming patterns as Unity does already, then you automatically can use the assertions with CMock as well.

For example, let's say I have a type BLAH_T, I'd create a function like the following:

//unity_helper.h
void test_assert_equal_BLAH_T(const BLAH_T expected, const BLAH_T actual, const unsigned int line, const char* msg);

Then I also want to add the following macros:

#define UNITY_TEST_ASSERT_EQUAL_BLAH_T(e, a, l, m) test_assert_equal_BLAH_T(e, a, l, m)
#define TEST_ASSERT_EQUAL_BLAH_T_MESSAGE(e, a, m) test_assert_equal_BLAH_T(e, a, __LINE__, m)
#define TEST_ASSERT_EQUAL_BLAH_T(e, a) test_assert_equal_BLAH_T(e, a, __LINE__, NULL)

In the C/CPP file you'd flesh out WHAT you actually want to compare... checking important fields of structs, etc... you can use the UNITY_ASSERT_EQUAL_* versions of the macros to pass the line and/or message along.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants