diagnostic_emitter_test.cpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 "llvm/Support/FormatVariadic.h"
  9. #include "toolchain/diagnostics/mocks.h"
  10. namespace Carbon {
  11. namespace {
  12. using Testing::DiagnosticAt;
  13. using Testing::DiagnosticLevel;
  14. using Testing::DiagnosticMessage;
  15. using Testing::DiagnosticShortName;
  16. using ::testing::ElementsAre;
  17. using ::testing::Eq;
  18. struct FakeDiagnostic {
  19. static constexpr llvm::StringLiteral ShortName = "fake-diagnostic";
  20. // TODO: consider ways to put the Message into `format` to allow dynamic
  21. // selection of the message.
  22. static constexpr llvm::StringLiteral Message = "{0}";
  23. std::string message;
  24. auto Format() -> std::string {
  25. // Work around a bug in Clang's unused const variable warning by marking it
  26. // used here with a no-op.
  27. static_cast<void>(ShortName);
  28. return llvm::formatv(Message.data(), message).str();
  29. }
  30. };
  31. struct FakeDiagnosticLocationTranslator : DiagnosticLocationTranslator<int> {
  32. auto GetLocation(int n) -> Diagnostic::Location override {
  33. return {.file_name = "test", .line_number = 1, .column_number = n};
  34. }
  35. };
  36. TEST(DiagTest, EmitErrors) {
  37. FakeDiagnosticLocationTranslator translator;
  38. Testing::MockDiagnosticConsumer consumer;
  39. DiagnosticEmitter<int> emitter(translator, consumer);
  40. EXPECT_CALL(consumer, HandleDiagnostic(
  41. AllOf(DiagnosticLevel(Diagnostic::Error),
  42. DiagnosticAt(1, 1), DiagnosticMessage("M1"),
  43. DiagnosticShortName("fake-diagnostic"))));
  44. EXPECT_CALL(consumer, HandleDiagnostic(
  45. AllOf(DiagnosticLevel(Diagnostic::Error),
  46. DiagnosticAt(1, 2), DiagnosticMessage("M2"),
  47. DiagnosticShortName("fake-diagnostic"))));
  48. emitter.EmitError<FakeDiagnostic>(1, {.message = "M1"});
  49. emitter.EmitError<FakeDiagnostic>(2, {.message = "M2"});
  50. }
  51. TEST(DiagTest, EmitWarnings) {
  52. std::vector<std::string> reported;
  53. FakeDiagnosticLocationTranslator translator;
  54. Testing::MockDiagnosticConsumer consumer;
  55. DiagnosticEmitter<int> emitter(translator, consumer);
  56. EXPECT_CALL(consumer, HandleDiagnostic(
  57. AllOf(DiagnosticLevel(Diagnostic::Warning),
  58. DiagnosticAt(1, 3), DiagnosticMessage("M1"),
  59. DiagnosticShortName("fake-diagnostic"))));
  60. EXPECT_CALL(consumer, HandleDiagnostic(
  61. AllOf(DiagnosticLevel(Diagnostic::Warning),
  62. DiagnosticAt(1, 5), DiagnosticMessage("M3"),
  63. DiagnosticShortName("fake-diagnostic"))));
  64. emitter.EmitWarningIf<FakeDiagnostic>(3, [](FakeDiagnostic& diagnostic) {
  65. diagnostic.message = "M1";
  66. return true;
  67. });
  68. emitter.EmitWarningIf<FakeDiagnostic>(4, [](FakeDiagnostic& diagnostic) {
  69. diagnostic.message = "M2";
  70. return false;
  71. });
  72. emitter.EmitWarningIf<FakeDiagnostic>(5, [](FakeDiagnostic& diagnostic) {
  73. diagnostic.message = "M3";
  74. return true;
  75. });
  76. }
  77. } // namespace
  78. } // namespace Carbon