1
0
mirror of https://github.com/ThrowTheSwitch/Unity.git synced 2026-01-23 00:15:58 +01:00

Merge pull request #488 from jvcdk/feature/test_printf

Feature/test printf
This commit is contained in:
Mark VanderVoord
2020-02-18 10:02:41 -05:00
committed by GitHub
5 changed files with 145 additions and 130 deletions

View File

@@ -282,18 +282,18 @@ _Example:_
#define UNITY_INCLUDE_PRINT_FORMATTED
int a = 0xfab1;
UnityPrintFormatted("Decimal %d\n", -7);
UnityPrintFormatted("Unsigned %u\n", 987);
UnityPrintFormatted("Float %f\n", 3.1415926535897932384);
UnityPrintFormatted("Binary %b\n", 0xA);
UnityPrintFormatted("Hex %X\n", 0xFAB);
UnityPrintFormatted("Pointer %p\n", &a);
UnityPrintFormatted("Character %c\n", 'F');
UnityPrintFormatted("String %s\n", "My string");
UnityPrintFormatted("Percent %%\n");
UnityPrintFormatted("Color Red \033[41mFAIL\033[00m\n");
UnityPrintFormatted("\n");
UnityPrintFormatted("Multiple (%d) (%i) (%u) (%x)\n", -100, 0, 200, 0x12345);
TEST_PRINTF("Decimal %d\n", -7);
TEST_PRINTF("Unsigned %u\n", 987);
TEST_PRINTF("Float %f\n", 3.1415926535897932384);
TEST_PRINTF("Binary %b\n", 0xA);
TEST_PRINTF("Hex %X\n", 0xFAB);
TEST_PRINTF("Pointer %p\n", &a);
TEST_PRINTF("Character %c\n", 'F');
TEST_PRINTF("String %s\n", "My string");
TEST_PRINTF("Percent %%\n");
TEST_PRINTF("Color Red \033[41mFAIL\033[00m\n");
TEST_PRINTF("\n");
TEST_PRINTF("Multiple (%d) (%i) (%u) (%x)\n", -100, 0, 200, 0x12345);
```

View File

@@ -186,8 +186,8 @@
*/
/* #define UNITY_EXCLUDE_STDDEF_H */
/* Define this to enable the unity formatted print function:
* "UnityPrintFormatted"
/* Define this to enable the unity formatted print macro:
* "TEST_PRINTF"
*/
/* #define UNITY_INCLUDE_PRINT_FORMATTED */

View File

@@ -146,121 +146,6 @@ void UnityPrint(const char* string)
}
}
}
/*-----------------------------------------------*/
#ifdef UNITY_INCLUDE_PRINT_FORMATTED
void UnityPrintFormatted(const char* format, ...)
{
const char* pch = format;
va_list va;
va_start(va, format);
if (pch != NULL)
{
while (*pch)
{
/* format identification character */
if (*pch == '%')
{
pch++;
if (pch != NULL)
{
switch (*pch)
{
case 'd':
case 'i':
{
const int number = va_arg(va, int);
UnityPrintNumber((UNITY_INT)number);
break;
}
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
case 'f':
case 'g':
{
const double number = va_arg(va, double);
UnityPrintFloat((UNITY_DOUBLE)number);
break;
}
#endif
case 'u':
{
const unsigned int number = va_arg(va, unsigned int);
UnityPrintNumberUnsigned((UNITY_UINT)number);
break;
}
case 'b':
{
const unsigned int number = va_arg(va, unsigned int);
const UNITY_UINT mask = (UNITY_UINT)0 - (UNITY_UINT)1;
UNITY_OUTPUT_CHAR('0');
UNITY_OUTPUT_CHAR('b');
UnityPrintMask(mask, (UNITY_UINT)number);
break;
}
case 'x':
case 'X':
case 'p':
{
const unsigned int number = va_arg(va, unsigned int);
UNITY_OUTPUT_CHAR('0');
UNITY_OUTPUT_CHAR('x');
UnityPrintNumberHex((UNITY_UINT)number, 8);
break;
}
case 'c':
{
const int ch = va_arg(va, int);
UnityPrintChar((const char *)&ch);
break;
}
case 's':
{
const char * string = va_arg(va, const char *);
UnityPrint(string);
break;
}
case '%':
{
UnityPrintChar(pch);
break;
}
default:
{
/* print the unknown format character */
UNITY_OUTPUT_CHAR('%');
UnityPrintChar(pch);
break;
}
}
}
}
#ifdef UNITY_OUTPUT_COLOR
/* print ANSI escape code */
else if ((*pch == 27) && (*(pch + 1) == '['))
{
pch += UnityPrintAnsiEscapeString(pch);
continue;
}
#endif
else if (*pch == '\n')
{
UNITY_PRINT_EOL();
}
else
{
UnityPrintChar(pch);
}
pch++;
}
}
va_end(va);
}
#endif /* ! UNITY_INCLUDE_PRINT_FORMATTED */
/*-----------------------------------------------*/
void UnityPrintLen(const char* string, const UNITY_UINT32 length)
{
@@ -1731,6 +1616,133 @@ UNITY_INTERNAL_PTR UnityDoubleToPtr(const double num)
}
#endif
/*-----------------------------------------------
* printf helper function
*-----------------------------------------------*/
#ifdef UNITY_INCLUDE_PRINT_FORMATTED
static void UnityPrintFVA(const char* format, va_list va)
{
const char* pch = format;
if (pch != NULL)
{
while (*pch)
{
/* format identification character */
if (*pch == '%')
{
pch++;
if (pch != NULL)
{
switch (*pch)
{
case 'd':
case 'i':
{
const int number = va_arg(va, int);
UnityPrintNumber((UNITY_INT)number);
break;
}
#ifndef UNITY_EXCLUDE_FLOAT_PRINT
case 'f':
case 'g':
{
const double number = va_arg(va, double);
UnityPrintFloat((UNITY_DOUBLE)number);
break;
}
#endif
case 'u':
{
const unsigned int number = va_arg(va, unsigned int);
UnityPrintNumberUnsigned((UNITY_UINT)number);
break;
}
case 'b':
{
const unsigned int number = va_arg(va, unsigned int);
const UNITY_UINT mask = (UNITY_UINT)0 - (UNITY_UINT)1;
UNITY_OUTPUT_CHAR('0');
UNITY_OUTPUT_CHAR('b');
UnityPrintMask(mask, (UNITY_UINT)number);
break;
}
case 'x':
case 'X':
case 'p':
{
const unsigned int number = va_arg(va, unsigned int);
UNITY_OUTPUT_CHAR('0');
UNITY_OUTPUT_CHAR('x');
UnityPrintNumberHex((UNITY_UINT)number, 8);
break;
}
case 'c':
{
const int ch = va_arg(va, int);
UnityPrintChar((const char *)&ch);
break;
}
case 's':
{
const char * string = va_arg(va, const char *);
UnityPrint(string);
break;
}
case '%':
{
UnityPrintChar(pch);
break;
}
default:
{
/* print the unknown format character */
UNITY_OUTPUT_CHAR('%');
UnityPrintChar(pch);
break;
}
}
}
}
#ifdef UNITY_OUTPUT_COLOR
/* print ANSI escape code */
else if ((*pch == 27) && (*(pch + 1) == '['))
{
pch += UnityPrintAnsiEscapeString(pch);
continue;
}
#endif
else if (*pch == '\n')
{
UNITY_PRINT_EOL();
}
else
{
UnityPrintChar(pch);
}
pch++;
}
}
}
void UnityPrintF(const UNITY_LINE_TYPE line, const char* format, ...)
{
UnityTestResultsBegin(Unity.TestFile, line);
UnityPrint("INFO");
if(format != NULL)
{
UnityPrint(": ");
va_list va;
va_start(va, format);
UnityPrintFVA(format, va);
va_end(va);
}
UNITY_PRINT_EOL();
}
#endif /* ! UNITY_INCLUDE_PRINT_FORMATTED */
/*-----------------------------------------------
* Control Functions
*-----------------------------------------------*/

View File

@@ -104,6 +104,9 @@ void verifyTest(void);
#define TEST_IGNORE() UNITY_TEST_IGNORE(__LINE__, NULL)
#define TEST_MESSAGE(message) UnityMessage((message), __LINE__)
#define TEST_ONLY()
#ifdef UNITY_INCLUDE_PRINT_FORMATTED
#define TEST_PRINTF(message, ...) UnityPrintF(__LINE__, (message), __VA_ARGS__)
#endif
/* It is not necessary for you to call PASS. A PASS condition is assumed if nothing fails.
* This method allows you to abort a test immediately with a PASS state, ignoring the remainder of the test. */

View File

@@ -515,7 +515,7 @@ void UnityDefaultTestRun(UnityTestFunction Func, const char* FuncName, const int
void UnityPrint(const char* string);
#ifdef UNITY_INCLUDE_PRINT_FORMATTED
void UnityPrintFormatted(const char* format, ...);
void UnityPrintF(const UNITY_LINE_TYPE line, const char* format, ...);
#endif
void UnityPrintLen(const char* string, const UNITY_UINT32 length);