sorting_diagnostic_consumer_test.cpp 3.5 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/sorting_diagnostic_consumer.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/diagnostic_emitter.h"
  10. #include "toolchain/diagnostics/mocks.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. using ::testing::InSequence;
  14. struct FakeDiagnostic : DiagnosticBase<FakeDiagnostic> {
  15. static constexpr llvm::StringLiteral ShortName = "fake-diagnostic";
  16. // TODO: consider ways to put the Message into `format` to allow dynamic
  17. // selection of the message.
  18. static constexpr llvm::StringLiteral Message = "{0}";
  19. auto Format() -> std::string {
  20. // Work around a bug in Clang's unused const variable warning by marking it
  21. // used here with a no-op.
  22. static_cast<void>(ShortName);
  23. return llvm::formatv(Message.data(), message).str();
  24. }
  25. std::string message;
  26. };
  27. struct FakeDiagnosticLocationTranslator
  28. : DiagnosticLocationTranslator<Diagnostic::Location> {
  29. auto GetLocation(Diagnostic::Location loc) -> Diagnostic::Location override {
  30. return loc;
  31. }
  32. };
  33. // Produces a location for the given line and column.
  34. static auto MakeLoc(int line, int col) -> Diagnostic::Location {
  35. return {.file_name = "test", .line_number = line, .column_number = col};
  36. }
  37. TEST(SortedDiagnosticEmitterTest, SortErrors) {
  38. FakeDiagnosticLocationTranslator translator;
  39. Testing::MockDiagnosticConsumer consumer;
  40. SortingDiagnosticConsumer sorting_consumer(consumer);
  41. DiagnosticEmitter<Diagnostic::Location> emitter(translator, sorting_consumer);
  42. emitter.EmitError<FakeDiagnostic>(MakeLoc(2, 1), {.message = "M1"});
  43. emitter.EmitError<FakeDiagnostic>(MakeLoc(1, 1), {.message = "M2"});
  44. emitter.EmitError<FakeDiagnostic>(MakeLoc(1, 3), {.message = "M3"});
  45. emitter.EmitError<FakeDiagnostic>(MakeLoc(3, 4), {.message = "M4"});
  46. emitter.EmitError<FakeDiagnostic>(MakeLoc(3, 2), {.message = "M5"});
  47. InSequence s;
  48. EXPECT_CALL(consumer, HandleDiagnostic(
  49. AllOf(DiagnosticLevel(Diagnostic::Error),
  50. DiagnosticAt(1, 1), DiagnosticMessage("M2"),
  51. DiagnosticShortName("fake-diagnostic"))));
  52. EXPECT_CALL(consumer, HandleDiagnostic(
  53. AllOf(DiagnosticLevel(Diagnostic::Error),
  54. DiagnosticAt(1, 3), DiagnosticMessage("M3"),
  55. DiagnosticShortName("fake-diagnostic"))));
  56. EXPECT_CALL(consumer, HandleDiagnostic(
  57. AllOf(DiagnosticLevel(Diagnostic::Error),
  58. DiagnosticAt(2, 1), DiagnosticMessage("M1"),
  59. DiagnosticShortName("fake-diagnostic"))));
  60. EXPECT_CALL(consumer, HandleDiagnostic(
  61. AllOf(DiagnosticLevel(Diagnostic::Error),
  62. DiagnosticAt(3, 2), DiagnosticMessage("M5"),
  63. DiagnosticShortName("fake-diagnostic"))));
  64. EXPECT_CALL(consumer, HandleDiagnostic(
  65. AllOf(DiagnosticLevel(Diagnostic::Error),
  66. DiagnosticAt(3, 4), DiagnosticMessage("M4"),
  67. DiagnosticShortName("fake-diagnostic"))));
  68. sorting_consumer.Flush();
  69. }
  70. } // namespace
  71. } // namespace Carbon::Testing