vlog_test.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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 "common/vlog.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. namespace Carbon::Testing {
  8. namespace {
  9. using ::testing::IsEmpty;
  10. using ::testing::StrEq;
  11. // Helper class with a vlog_stream_ member for CARBON_VLOG.
  12. class VLogger {
  13. public:
  14. explicit VLogger(bool enable) : buffer_(buffer_str_) {
  15. if (enable) {
  16. vlog_stream_ = &buffer_;
  17. }
  18. }
  19. void VLog() { CARBON_VLOG() << "Test\n"; }
  20. auto buffer() -> llvm::StringRef { return buffer_str_; }
  21. private:
  22. std::string buffer_str_;
  23. llvm::raw_string_ostream buffer_;
  24. llvm::raw_ostream* vlog_stream_ = nullptr;
  25. };
  26. TEST(VLogTest, Enabled) {
  27. VLogger vlog(/*enable=*/true);
  28. vlog.VLog();
  29. EXPECT_THAT(vlog.buffer(), StrEq("Test\n"));
  30. }
  31. TEST(VLogTest, Disabled) {
  32. VLogger vlog(/*enable=*/false);
  33. vlog.VLog();
  34. EXPECT_THAT(vlog.buffer(), IsEmpty());
  35. }
  36. } // namespace
  37. } // namespace Carbon::Testing