diagnostic_emitter_test.cpp 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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/diagnostic_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 ::Carbon::Testing::IsDiagnostic;
  12. using ::Carbon::Testing::IsSingleDiagnostic;
  13. using testing::ElementsAre;
  14. class FakeDiagnosticEmitter : public DiagnosticEmitter<int> {
  15. public:
  16. using DiagnosticEmitter::DiagnosticEmitter;
  17. protected:
  18. auto ConvertLoc(int n, ContextFnT /*context_fn*/) const
  19. -> ConvertedDiagnosticLoc override {
  20. return {.loc = {.line_number = 1, .column_number = n},
  21. .last_byte_offset = -1};
  22. }
  23. };
  24. class DiagnosticEmitterTest : public ::testing::Test {
  25. public:
  26. DiagnosticEmitterTest() : emitter_(&consumer_) {}
  27. Testing::MockDiagnosticConsumer consumer_;
  28. FakeDiagnosticEmitter emitter_;
  29. };
  30. TEST_F(DiagnosticEmitterTest, EmitSimpleError) {
  31. CARBON_DIAGNOSTIC(TestDiagnostic, Error, "simple error");
  32. EXPECT_CALL(consumer_, HandleDiagnostic(IsSingleDiagnostic(
  33. DiagnosticKind::TestDiagnostic,
  34. DiagnosticLevel::Error, 1, 1, "simple error")));
  35. EXPECT_CALL(consumer_, HandleDiagnostic(IsSingleDiagnostic(
  36. DiagnosticKind::TestDiagnostic,
  37. DiagnosticLevel::Error, 1, 2, "simple error")));
  38. emitter_.Emit(1, TestDiagnostic);
  39. emitter_.Emit(2, TestDiagnostic);
  40. }
  41. TEST_F(DiagnosticEmitterTest, EmitSimpleWarning) {
  42. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  43. EXPECT_CALL(consumer_,
  44. HandleDiagnostic(IsSingleDiagnostic(
  45. DiagnosticKind::TestDiagnostic, DiagnosticLevel::Warning, 1,
  46. 1, "simple warning")));
  47. emitter_.Emit(1, TestDiagnostic);
  48. }
  49. TEST_F(DiagnosticEmitterTest, EmitOneArgDiagnostic) {
  50. CARBON_DIAGNOSTIC(TestDiagnostic, Error, "arg: `{0}`", std::string);
  51. EXPECT_CALL(consumer_, HandleDiagnostic(IsSingleDiagnostic(
  52. DiagnosticKind::TestDiagnostic,
  53. DiagnosticLevel::Error, 1, 1, "arg: `str`")));
  54. emitter_.Emit(1, TestDiagnostic, "str");
  55. }
  56. TEST_F(DiagnosticEmitterTest, EmitNote) {
  57. CARBON_DIAGNOSTIC(TestDiagnostic, Warning, "simple warning");
  58. CARBON_DIAGNOSTIC(TestDiagnosticNote, Note, "note");
  59. EXPECT_CALL(
  60. consumer_,
  61. HandleDiagnostic(IsDiagnostic(
  62. DiagnosticLevel::Warning,
  63. ElementsAre(
  64. IsDiagnosticMessage(DiagnosticKind::TestDiagnostic,
  65. DiagnosticLevel::Warning, 1, 1,
  66. "simple warning"),
  67. IsDiagnosticMessage(DiagnosticKind::TestDiagnosticNote,
  68. DiagnosticLevel::Note, 1, 2, "note")))));
  69. emitter_.Build(1, TestDiagnostic).Note(2, TestDiagnosticNote).Emit();
  70. }
  71. } // namespace
  72. } // namespace Carbon::Testing