Skip to content

Commit

Permalink
Added stderr capture split into a new file.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tordek committed Aug 10, 2012
1 parent d719e73 commit 5679ac3
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ need this guard:
Tests are defined with the `TEST` macro. You may also use the `GLOBALS`,
`SET_UP` and `TEAR_DOWN` macros to centralize your initializations.

There are additional helpers in the `cheat_helpers.h` file, which provide
stuff to help you test some harder to test stuff.

---

CHEAT is Copyrighted (c) 2012 Guillermo "Tordek" Freschi.
Expand Down
6 changes: 5 additions & 1 deletion cheat.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ static void cheat_suite_init(struct cheat_test_suite *suite)

static void cheat_suite_cleanup(struct cheat_test_suite *suite)
{
(void) suite;
}

static void cheat_suite_summary(struct cheat_test_suite *suite)
Expand Down Expand Up @@ -154,6 +155,9 @@ int main(int argc, char *argv[])

cheat_test **current_test = tests;

(void) argc;
(void) argv;

cheat_suite_init(&suite);

while (*current_test) {
Expand Down Expand Up @@ -189,6 +193,6 @@ int main(int argc, char *argv[])
#define TEAR_DOWN(body) static void cheat_tear_down() body
#define GLOBALS(body) body

#define cheat_assert(assertion) cheat_test_assert(suite, assertion, #assertion, __FILE__, __LINE__);
#define cheat_assert(assertion) cheat_test_assert(suite, assertion, #assertion, __FILE__, __LINE__)

#endif
43 changes: 25 additions & 18 deletions cheat_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,38 @@

#include <unistd.h>

#define TEST_WITH_CAPTURED_STDOUT(name, body) TEST(name, {\
fflush(stdout);\
char filename_pattern[] = "cheat_captured_stdout_" #name "_XXXXXX";\
int original_stdout;\
int fake_stdout;\
original_stdout = dup(STDOUT_FILENO); \
fake_stdout = mkstemp(filename_pattern);\
dup2(fake_stdout, STDOUT_FILENO);\
{ body }\
fflush(stdout);\
dup2(original_stdout, STDOUT_FILENO); \
})

int cheat_stdout_contains(char *contents)
#define CHEAT_WRAP_STREAM(stream, stream_fd, name, body) \
char filename_pattern[] = "cheat_captured_stream_" #name "_XXXXXX";\
int original_stream;\
int fake_stream;\
fflush(stream);\
original_stream = dup(stream_fd); \
fake_stream = mkstemp(filename_pattern);\
dup2(fake_stream, stream_fd);\
setbuf(stream, NULL);\
body\
fflush(stream);\
dup2(original_stream, stream_fd);

#define TEST_WITH_CAPTURED_STDOUT(name, body) TEST(name, { CHEAT_WRAP_STREAM(stdout, STDOUT_FILENO, name, body) } )
#define TEST_WITH_CAPTURED_STDERR(name, body) TEST(name, { CHEAT_WRAP_STREAM(stderr, STDERR_FILENO, name, body) } )
#define TEST_WITH_CAPTURED_OUTPUT(name, body) TEST(name, {\
CHEAT_WRAP_STREAM(stderr, STDERR_FILENO, name,\
CHEAT_WRAP_STREAM(stdout, STDOUT_FILENO, name, body)\
))

int cheat_stream_contains(FILE *stream, char *contents)
{
char *buffer;
int result;
int len;

fflush(stdout);
len = lseek(STDOUT_FILENO, 0, SEEK_CUR);
lseek(STDOUT_FILENO, 0, SEEK_SET);
len = ftell(stream);
fseek(stream, 0, SEEK_SET);

buffer = malloc(len + 1);
fread(buffer, 1, len, stream);

read(STDOUT_FILENO, buffer, len);
result = strstr(buffer, contents) != NULL;

free(buffer);
Expand Down
9 changes: 7 additions & 2 deletions sampletest.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,16 @@ TEST(second_failute, {

TEST_WITH_CAPTURED_STDOUT(output_capture, {
printf("Something stupid");
cheat_assert(cheat_stdout_contains("Something"));
cheat_assert(cheat_stream_contains(stdout, "Something"));
})

TEST_WITH_CAPTURED_STDOUT(large_output_captute, {
printf("%1000s", "Potato");

cheat_assert(cheat_stdout_contains("Potato"));
cheat_assert(cheat_stream_contains(stdout, "Potato"));
})

TEST_WITH_CAPTURED_STDERR(stderr_capture, {
fprintf(stderr, "You can also capture errors!");
cheat_assert(cheat_stream_contains(stderr, "errors"));
})

0 comments on commit 5679ac3

Please sign in to comment.