Example of defining custom error strings for enums / defines

42 views
Skip to first unread message

David Good

unread,
Sep 16, 2024, 6:05:32 PM9/16/24
to throwth...@googlegroups.com
Hi Mad Scientists !

I remember learning how to setup unity to show more meaningful output messages when using things like enums or defines , but I don't remember exactly how it was done . I vaguely remember defining these in a file which might have been in the support directory of the test dir , however I can't remember . Where is the documentation for this feature and where can I find an example ?

Thanks !

--David

David Good

unread,
Sep 19, 2024, 10:52:51 AM9/19/24
to throwth...@googlegroups.com
Bump .

David Good

unread,
Sep 19, 2024, 10:55:59 AM9/19/24
to throwth...@googlegroups.com
To be clear , what I'm trying to do is get error messages which instead of this

At line (311): "Expected 6 Was 0"

say something like this :

At line (311): "Expected ERR_BAD_PARAM Was ERR_OK"

--David

Mike Karlesky

unread,
Sep 19, 2024, 10:59:08 AM9/19/24
to throwth...@googlegroups.com

David: You want TEST_ASSERT_x_MESSAGE(...) [docs



--
You received this message because you are subscribed to the Google Groups "ThrowTheSwitch Forums" group.
To unsubscribe from this group and stop receiving emails from it, send an email to throwtheswitc...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/throwtheswitch/CALMFhSyhmZLxeLsm8DLQxQZT30VSyQCke9KMqCp%2BAjq_RXezjg%40mail.gmail.com.

David Good

unread,
Sep 19, 2024, 11:10:50 AM9/19/24
to throwth...@googlegroups.com
Hi Mike ,

I don't think that's exactly what I was looking for . Those do allow me to customize a pre-canned failure message but wouldn't automatically substitute the values of the enums for me , right ?

I specifically remember a feature (possibly in one of the udemy classes) where I could tell Unity about enums and have Unity substitute the values automatically . To do it , I had to create some kind of typdef file that Unity would use in order to lookup the substitutions .

--David

Mike Karlesky

unread,
Sep 19, 2024, 11:20:08 AM9/19/24
to throwth...@googlegroups.com

Ah. You're looking for a UnityHelper. Unfortunately, I don't believe this is well documented in Unity, but there is an example in the repo.

Essentially, you're just creating new assertion functions with complementary test assertion macros. (Over on the CMock side, with appropriate configuration, CMock can discover these and incorporate them into argument expectations.)


David Good

unread,
Sep 19, 2024, 11:42:08 AM9/19/24
to throwth...@googlegroups.com
Yes , this is it !

Do you happen to remember where this was covered in the udemy classes ? I looked through both classes and couldn't find it , but I swear it was in there ....

--David

Mike Karlesky

unread,
Sep 19, 2024, 12:16:21 PM9/19/24
to throwth...@googlegroups.com

Perhaps you are thinking of Lecture 49 in the second course?

David Good

unread,
Sep 19, 2024, 12:25:40 PM9/19/24
to throwth...@googlegroups.com
That's the one !!!

I knew it was in there , but there was literally no way I could think of to find it . Skimming the lectures wasn't good enough apparently .

--David

David Good

unread,
Sep 19, 2024, 1:32:56 PM9/19/24
to throwth...@googlegroups.com
I got it working based on an amalgamation of the udemy course info and the example you linked . Here's my working code which produced the very nice error message :

At line (11): "Expected PTRESULT_BUSY Was PTRESULT_ERROR"

#ifndef _TESTHELPERPTRESULT_H
#define _TESTHELPERPTRESULT_H

#include <string.h>
#include "header_with_ptresult_t_defined.h"

static char * HelperPTResultNames[] = {
    "PTRESULT_IDLE",
    "PTRESULT_BUSY",
    "PTRESULT_OK",
    "PTRESULT_ERROR",
    "PTRESULT_INVALID"
};

char HelperPTResultFailMsg[100];

static inline void AssertEqualPTResult(const ptresult_t expected, const ptresult_t actual, const UNITY_LINE_TYPE linenum, const char * msg)
{
    if ( expected != actual )
    {
        HelperPTResultFailMsg[0] = 0;
        strcat( HelperPTResultFailMsg, "Expected " );
        if ( expected <= PTRESULT_MAX ) strcat( HelperPTResultFailMsg, HelperPTResultNames[expected] );
        else sprintf( &HelperPTResultFailMsg[strlen( HelperPTResultFailMsg )], "%u", expected );
        strcat( HelperPTResultFailMsg, " Was " );
        if ( expected <= PTRESULT_MAX ) strcat( HelperPTResultFailMsg, HelperPTResultNames[actual] );
        else sprintf( &HelperPTResultFailMsg[strlen( HelperPTResultFailMsg )], "%u", actual );
        UNITY_TEST_FAIL( linenum, HelperPTResultFailMsg );
    }
}

#define UNITY_TEST_ASSERT_EQUAL_PTRESULT_T(e, a, l, m) AssertEqualPTResult(e, a, l, m);
#define TEST_ASSERT_EQUAL_PTRESULT_T(e, a) UNITY_TEST_ASSERT_EQUAL_PTRESULT(e, a, __LINE__, NULL);
#define TEST_ASSERT_EQUAL_PTRESULT_T_MESSAGE( e, a ) UNITY_TEST_ASSERT_EQUAL_PTRESULT(e, a, __LINE__, NULL);

#endif // _TESTHELPERPTRESULT_H

I opted to break these into multiple files for each type (or closely related types) in order to remind myself what was in the file , rather than having one large "UnityHelpers.h" file depend on a lot of outside header files which would start to intermingle the tests in a way that I don't want .

I'll probably refactor out the error message character array into a generic unity_helper.h file .

Finally , as I noted , there was a difference between example 3 and the udemy example . Example 3 used a combination of a .h and a .c source file , while the udemy example used just a .h with static inline functions as above in my code . Can you comment on the differences between these two approaches ?

Thanks again !

--David

David Good

unread,
Sep 27, 2024, 4:59:51 PM9/27/24
to throwth...@googlegroups.com
Hi Mike and Mark !

I just wanted to say thanks again for this tool and help with this feature ! It is very helpful to see clear error messages as I use TDD to write code !

--David
Reply all
Reply to author
Forward
0 new messages