diagnostic_emitter_test.cpp 3.2 KB

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