capture_std_streams.cpp 1005 B

1234567891011121314151617181920212223242526272829303132333435
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "testing/base/capture_std_streams.h"
  5. #include <gtest/gtest.h>
  6. #include <fstream>
  7. #include <sstream>
  8. #include <string>
  9. #include "common/ostream.h"
  10. namespace Carbon::Testing::Internal {
  11. // While these are marked as "internal" APIs, they seem to work and be pretty
  12. // widely used for their exact documented behavior.
  13. using ::testing::internal::CaptureStderr;
  14. using ::testing::internal::CaptureStdout;
  15. using ::testing::internal::GetCapturedStderr;
  16. using ::testing::internal::GetCapturedStdout;
  17. auto BeginStdStreamCapture() -> void {
  18. CaptureStderr();
  19. CaptureStdout();
  20. }
  21. auto EndStdStreamCapture(std::string& out, std::string& err) -> void {
  22. // No need to flush stderr.
  23. err = GetCapturedStderr();
  24. llvm::outs().flush();
  25. out = GetCapturedStdout();
  26. }
  27. } // namespace Carbon::Testing::Internal