vlog_test.cpp 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  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 "testing/base/test_raw_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. void VLog() { CARBON_VLOG("Test\n"); }
  21. void VLogFormatArgs() { CARBON_VLOG("Test {0} {1} {2}\n", 1, 2, 3); }
  22. auto TakeStr() -> std::string { return buffer_.TakeStr(); }
  23. private:
  24. TestRawOstream 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. } // namespace
  40. } // namespace Carbon::Testing