multipart-mixed

Code Snippet: Unit Test Green/Red Bar for ANSI Terminals

If you like writing unit tests but you don't have a fancy "green bar/red bar" unit test window, here's a simple solution you can use in any ANSI terminal window. It looks like this:

Green/red bar screen shot

It's a piece of cake with some ANSI escape codes. Here's the code:

#include <stdio.h>

void printTestResults(int numPass, int numFail)
{
    if (numFail > 0)
        printf("\x1b[37;41m"); /* white on red */
    else
        printf("\x1b[30;42m"); /* black on green */

    printf("\x1b[2K");         /* clear to end of line */

    printf("Test results: %d pass, %d fail (%d total)\n", 
        numPass, numFail, numPass + numFail);

    printf("\x1b[0m");        /* reset colors */
    printf("\x1b[2K");        /* clear to end of line */
}
Spread the word: bookmark it!  |  reddit!  |  digg it!
Recent entries in
Software:
Adding reCAPTCHA to Movable Type (Aug 29)
Boulder/Denver Ruby Group: MapReduce (Apr 19)

Post a comment