vlog_test.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. auto TakeStr() -> std::string { return buffer_.TakeStr(); }
  22. private:
  23. TestRawOstream 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.TakeStr(), StrEq("Test\n"));
  30. }
  31. TEST(VLogTest, Disabled) {
  32. VLogger vlog(/*enable=*/false);
  33. vlog.VLog();
  34. EXPECT_THAT(vlog.TakeStr(), IsEmpty());
  35. }
  36. } // namespace
  37. } // namespace Carbon::Testing