vlog_test.cpp 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. #include "common/raw_string_ostream.h"
  8. namespace Carbon::Testing {
  9. namespace {
  10. using ::testing::IsEmpty;
  11. using ::testing::StrEq;
  12. // Helper class with a vlog_stream_ member for CARBON_VLOG.
  13. class VLogger {
  14. public:
  15. explicit VLogger(bool enable) {
  16. if (enable) {
  17. vlog_stream_ = &buffer_;
  18. }
  19. }
  20. auto VLog() -> void { CARBON_VLOG("Test\n"); }
  21. auto VLogFormatArgs() -> void { CARBON_VLOG("Test {0} {1} {2}\n", 1, 2, 3); }
  22. auto TakeStr() -> std::string { return buffer_.TakeStr(); }
  23. private:
  24. RawStringOstream buffer_;
  25. llvm::raw_ostream* vlog_stream_ = nullptr;
  26. };
  27. TEST(VLogTest, Enabled) {
  28. VLogger vlog(/*enable=*/true);
  29. vlog.VLog();
  30. EXPECT_THAT(vlog.TakeStr(), StrEq("Test\n"));
  31. vlog.VLogFormatArgs();
  32. EXPECT_THAT(vlog.TakeStr(), StrEq("Test 1 2 3\n"));
  33. }
  34. TEST(VLogTest, Disabled) {
  35. VLogger vlog(/*enable=*/false);
  36. vlog.VLog();
  37. EXPECT_THAT(vlog.TakeStr(), IsEmpty());
  38. }
  39. TEST(VLogTest, To) {
  40. RawStringOstream buffer;
  41. CARBON_VLOG_TO(&buffer, "Test");
  42. EXPECT_THAT(buffer.TakeStr(), "Test");
  43. }
  44. TEST(VLogTest, ToNull) { CARBON_VLOG_TO(nullptr, "Unused"); }
  45. } // namespace
  46. } // namespace Carbon::Testing