Files
carbon-lang/testing/util/test_raw_ostream.h
T
Jon Ross-Perkins 8e940d9724 Migrate //common test libraries to //testing/util. (#2890)
This is just a cleanup. Since we now have a testing directory, I think this is a better home for testonly libraries than //common. (I was thinking about this when I was considering adding more test_raw_ostream deps)
2023-06-13 16:38:08 -07:00

42 lines
1.0 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TESTING_UTIL_TEST_RAW_OSTREAM_H_
#define CARBON_TESTING_UTIL_TEST_RAW_OSTREAM_H_
#include <gtest/gtest.h>
#include <string>
#include "common/ostream.h"
namespace Carbon::Testing {
// A raw_ostream that makes it easy to repeatedly check streamed output.
class TestRawOstream : public llvm::raw_string_ostream {
public:
explicit TestRawOstream() : llvm::raw_string_ostream(buffer_) {}
~TestRawOstream() override {
if (!buffer_.empty()) {
ADD_FAILURE() << "Unchecked output:\n" << buffer_;
}
}
// Flushes the stream and returns the contents so far, clearing the stream
// back to empty.
auto TakeStr() -> std::string {
std::string result = std::move(buffer_);
buffer_.clear();
return result;
}
private:
std::string buffer_;
};
} // namespace Carbon::Testing
#endif // CARBON_TESTING_UTIL_TEST_RAW_OSTREAM_H_