Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Request for cr_assert_stdout_eq_str and cr_assert_stderr_eq_str to print the actual file contents on failure #556

Open
CamJN opened this issue Sep 15, 2024 · 1 comment

Comments

@CamJN
Copy link

CamJN commented Sep 15, 2024

Currently when cr_assert_stdout_eq_str fails the assertion, it prints a message like: The file contents of __stdoutp does not match the string "foo".

It would be substantially more useful to see both the expected and actual strings, in order to reason about what went wrong.

Something like the following would be awesome:

The file contents of __stdoutp does not match the given string:
- Expected:
This is an example of a multi-line expected string
it is both long and awkward to format, but that's life sometimes
+ Actual:
This was the actual output of the test
it does not match the expected string at all, oops!

I get that it would require reworking cr_file_match_str to stash the file contents somewhere or something and therefore isn't easy, but it would be really great if it could happen.

@CamJN
Copy link
Author

CamJN commented Sep 15, 2024

A somewhat easier approach would be to replace the macros with something like this:

c++:

void cr_assert_stdout_eq_str(char* str) {
  (void)fflush(stdout);
  auto &f_cout = criterion::get_redirected_cout();
  std::string actual(std::istreambuf_iterator<char>(f_cout), {});
  cr_assert(eq(actual, expected));
}

c:

void cr_assert_stdout_eq_str(char* str) {
  (void)fflush(stdout);
  FILE *f_stdout = cr_get_redirected_stdout();
  size_t size = 1024;
  char* buffer = malloc(size);
  char* head = buffer;
  size_t read = 0;
  do {
    if (head - buffer >= size) {
      size *= 2;
      buffer = realloc(buffer, size);
      head = buffer + size / 2;
    }
    read = fread(head, 1, size - (head - buffer), f_stdout);
    head += read;
  } while (read > 0);
  cr_expect_str_eq(buffer, expected_stdout, "expected stdout: '%s', to equal: '%s'", buffer, expected_stdout);
  free(buffer);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant