-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[testing] Extract StreamCapture test utility (#47774)
Factors out an RAII-based class that can be used to capture std::cout, std::cerr, or technically any other std::ostream, though that's unlikely to be useful. This makes the logic reusable but more importantly, ensures the capture is cleaned up at the end of the test. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style
- Loading branch information
Showing
5 changed files
with
91 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#include "flutter/testing/stream_capture.h" | ||
|
||
namespace flutter { | ||
namespace testing { | ||
|
||
StreamCapture::StreamCapture(std::ostream* ostream) | ||
: ostream_(ostream), old_buffer_(ostream_->rdbuf()) { | ||
ostream_->rdbuf(buffer_.rdbuf()); | ||
} | ||
|
||
StreamCapture::~StreamCapture() { | ||
Stop(); | ||
} | ||
|
||
void StreamCapture::Stop() { | ||
if (old_buffer_) { | ||
ostream_->rdbuf(old_buffer_); | ||
old_buffer_ = nullptr; | ||
} | ||
} | ||
|
||
std::string StreamCapture::GetOutput() const { | ||
return buffer_.str(); | ||
} | ||
|
||
} // namespace testing | ||
} // namespace flutter |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
#ifndef FLUTTER_TESTING_STREAM_CAPTURE_H_ | ||
#define FLUTTER_TESTING_STREAM_CAPTURE_H_ | ||
|
||
#include <ostream> | ||
#include <sstream> | ||
#include <string> | ||
|
||
namespace flutter { | ||
namespace testing { | ||
|
||
// Temporarily replaces the specified stream's output buffer to capture output. | ||
// | ||
// Example: | ||
// StreamCapture captured_stdout(&std::cout); | ||
// ... code that writest to std::cout ... | ||
// std::string output = captured_stdout.GetCapturedOutput(); | ||
class StreamCapture { | ||
public: | ||
// Begins capturing output to the specified stream. | ||
StreamCapture(std::ostream* ostream); | ||
|
||
// Stops capturing output to the specified stream, and restores the original | ||
// output buffer, if |Stop| has not already been called. | ||
~StreamCapture(); | ||
|
||
// Stops capturing output to the specified stream, and restores the original | ||
// output buffer. | ||
void Stop(); | ||
|
||
// Returns any output written to the captured stream between construction and | ||
// the first call to |Stop|, if any, or now. | ||
std::string GetOutput() const; | ||
|
||
private: | ||
std::ostream* ostream_; | ||
std::stringstream buffer_; | ||
std::streambuf* old_buffer_; | ||
}; | ||
|
||
} // namespace testing | ||
} // namespace flutter | ||
|
||
#endif // FLUTTER_TESTING_STREAM_CAPTURE_H_ |