diagnostic_emitter_test.cpp 3.1 KB

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