vlog_test.cpp 1.4 KB

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