emitter_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 "toolchain/diagnostics/emitter.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "llvm/ADT/StringRef.h"
  8. #include "toolchain/diagnostics/mocks.h"
  9. namespace Carbon::Testing {
  10. namespace {
  11. using testing::ElementsAre;
  12. class FakeEmitter : public Diagnostics::Emitter<int> {
  13. public:
  14. using Emitter::Emitter;
  15. protected:
  16. auto ConvertLoc(int n, ContextFnT /*context_fn*/) const
  17. -> Diagnostics::ConvertedLoc override {
  18. return {.loc = {.line_number = 1, .column_number = n},
  19. .last_byte_offset = -1};
  20. }
  21. };
  22. class EmitterTest : public ::testing::Test {
  23. public:
  24. EmitterTest() : emitter_(&consumer_) {}
  25. Testing::MockDiagnosticConsumer consumer_;
  26. FakeEmitter emitter_;
  27. };
  28. TEST_F(EmitterTest, EmitSimpleError) {
  29. CARBON_DIAGNOSTIC(TestDiagnostic, Error, "simple error");
  30. EXPECT_CALL(consumer_, HandleDiagnostic(IsSingleDiagnostic(
  31. Diagnostics::Kind::TestDiagnostic,
  32. Diagnostics::Level::Error, 1, 1, "simple error")));
  33. EXPECT_CALL(consumer_, HandleDiagnostic(IsSingleDiagnostic(
  34. Diagnostics::Kind::TestDiagnostic,
  35. Diagnostics::Level::Error, 1, 2, "simple error")));
  36. emitter_.Emit(1, TestDiagnostic);
  37. emitter_.Emit(2, TestDiagnostic);
  38. }
  39. TEST_F(EmitterTest, EmitSimpleWarning) {
  40. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  41. EXPECT_CALL(consumer_,
  42. HandleDiagnostic(IsSingleDiagnostic(
  43. Diagnostics::Kind::TestDiagnostic,
  44. Diagnostics::Level::Warning, 1, 1, "simple warning")));
  45. emitter_.Emit(1, TestDiagnostic);
  46. }
  47. TEST_F(EmitterTest, EmitOneArgDiagnostic) {
  48. CARBON_DIAGNOSTIC(TestDiagnostic, Error, "arg: `{0}`", std::string);
  49. EXPECT_CALL(consumer_, HandleDiagnostic(IsSingleDiagnostic(
  50. Diagnostics::Kind::TestDiagnostic,
  51. Diagnostics::Level::Error, 1, 1, "arg: `str`")));
  52. emitter_.Emit(1, TestDiagnostic, "str");
  53. }
  54. TEST_F(EmitterTest, EmitNote) {
  55. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  56. CARBON_DIAGNOSTIC(TestDiagnosticNote, Note, "note");
  57. EXPECT_CALL(
  58. consumer_,
  59. HandleDiagnostic(IsDiagnostic(
  60. Diagnostics::Level::Warning,
  61. ElementsAre(
  62. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnostic,
  63. Diagnostics::Level::Warning, 1, 1,
  64. "simple warning"),
  65. IsDiagnosticMessage(Diagnostics::Kind::TestDiagnosticNote,
  66. Diagnostics::Level::Note, 1, 2, "note")))));
  67. emitter_.Build(1, TestDiagnostic).Note(2, TestDiagnosticNote).Emit();
  68. }
  69. TEST_F(EmitterTest, Flush) {
  70. bool flushed = false;
  71. auto flush_fn = [&]() { flushed = true; };
  72. {
  73. FakeEmitter emitter(&consumer_);
  74. emitter.AddFlushFn(flush_fn);
  75. // Registering the function does not flush.
  76. EXPECT_FALSE(flushed);
  77. // Explicit calls to `Flush` should flush.
  78. emitter.Flush();
  79. EXPECT_TRUE(flushed);
  80. flushed = false;
  81. {
  82. Diagnostics::AnnotationScope annot(&emitter, [](auto&) {});
  83. // Registering an annotation scope should flush.
  84. EXPECT_TRUE(flushed);
  85. flushed = false;
  86. }
  87. // Unregistering an annotation scope should flush.
  88. EXPECT_TRUE(flushed);
  89. flushed = false;
  90. }
  91. // Destroying the emitter should flush.
  92. EXPECT_TRUE(flushed);
  93. flushed = false;
  94. }
  95. } // namespace
  96. } // namespace Carbon::Testing