capture_std_streams.cpp 987 B

12345678910111213141516171819202122232425262728293031323334
  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 "common/ostream.h"
  9. namespace Carbon::Testing::Internal {
  10. // While these are marked as "internal" APIs, they seem to work and be pretty
  11. // widely used for their exact documented behavior.
  12. using ::testing::internal::CaptureStderr;
  13. using ::testing::internal::CaptureStdout;
  14. using ::testing::internal::GetCapturedStderr;
  15. using ::testing::internal::GetCapturedStdout;
  16. auto BeginStdStreamCapture() -> void {
  17. CaptureStderr();
  18. CaptureStdout();
  19. }
  20. auto EndStdStreamCapture(std::string& out, std::string& err) -> void {
  21. // No need to flush stderr.
  22. err = GetCapturedStderr();
  23. llvm::outs().flush();
  24. out = GetCapturedStdout();
  25. }
  26. } // namespace Carbon::Testing::Internal