vlog_test.cpp 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. void VLogStream() { CARBON_VLOG() << "Test\n"; }
  23. auto TakeStr() -> std::string { return buffer_.TakeStr(); }
  24. private:
  25. TestRawOstream 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. vlog.VLogStream();
  35. EXPECT_THAT(vlog.TakeStr(), StrEq("Test\n"));
  36. }
  37. TEST(VLogTest, Disabled) {
  38. VLogger vlog(/*enable=*/false);
  39. vlog.VLog();
  40. EXPECT_THAT(vlog.TakeStr(), IsEmpty());
  41. }
  42. } // namespace
  43. } // namespace Carbon::Testing