bxparks/AUnit

support assertEqual() on (void*) pointers

bxparks opened this issue · 0 comments

I recently wrote a unit test where I wanted assertEqual() to work on pointers. The workaround was to cast the pointers to uintptr_t from #include <stdint.h>.

I think a cleaner solution could be implemented by adding support for

assertEqual(void*, void*)

which could be accomplished by adding 2 overloads to Assertion.h:

    bool assertion(const char* file, uint16_t line, void* lhs,
        const char *opName, bool (*op)(void* lhs, const void* rhs),
        void* rhs);

    bool assertionVerbose(const char* file, uint16_t line, void* lhs,
        const __FlashStringHelper* lhsString, const char* opName,
        bool (*op)(void* lhs, void* rhs),
        void* rhs, const __FlashStringHelper* rhsString);

I think c-strings (in all its various forms) would match the assertEqual(const char*, const char*) version, while allowing other pointers to bind to the assertEqual(void*, void*) version.

GoogleTest does not have the overload problem because ASSERT_EQ() always compares pointers, even for c-strings. It uses a separate set of macros (ASSERT_STREQ(), ASSERT_STRNE() and their case-insensitive versions) for c-string comparisons.
https://github.com/google/googletest/blob/master/googletest/docs/primer.md#string-comparison

(If I had not aimed for ArduinoUnit compatibility, I probably would have followed the GoogleTest convention.)