sorting_diagnostic_consumer_test.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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 "toolchain/diagnostics/diagnostic_emitter.h"
  9. #include "toolchain/diagnostics/mocks.h"
  10. namespace Carbon {
  11. namespace {
  12. using ::Carbon::Testing::IsDiagnostic;
  13. using ::testing::InSequence;
  14. CARBON_DIAGNOSTIC(TestDiagnostic, Error, "{0}", llvm::StringLiteral);
  15. struct FakeDiagnosticLocationTranslator
  16. : DiagnosticLocationTranslator<DiagnosticLocation> {
  17. auto GetLocation(DiagnosticLocation loc) -> DiagnosticLocation override {
  18. return loc;
  19. }
  20. };
  21. TEST(SortedDiagnosticEmitterTest, SortErrors) {
  22. FakeDiagnosticLocationTranslator translator;
  23. Testing::MockDiagnosticConsumer consumer;
  24. SortingDiagnosticConsumer sorting_consumer(consumer);
  25. DiagnosticEmitter<DiagnosticLocation> emitter(translator, sorting_consumer);
  26. emitter.Emit({"f", "line", 2, 1}, TestDiagnostic, "M1");
  27. emitter.Emit({"f", "line", 1, 1}, TestDiagnostic, "M2");
  28. emitter.Emit({"f", "line", 1, 3}, TestDiagnostic, "M3");
  29. emitter.Emit({"f", "line", 3, 4}, TestDiagnostic, "M4");
  30. emitter.Emit({"f", "line", 3, 2}, TestDiagnostic, "M5");
  31. emitter.Emit({"f", "line", 3, 2}, TestDiagnostic, "M6");
  32. InSequence s;
  33. EXPECT_CALL(consumer, HandleDiagnostic(
  34. IsDiagnostic(DiagnosticKind::TestDiagnostic,
  35. DiagnosticLevel::Error, 1, 1, "M2")));
  36. EXPECT_CALL(consumer, HandleDiagnostic(
  37. IsDiagnostic(DiagnosticKind::TestDiagnostic,
  38. DiagnosticLevel::Error, 1, 3, "M3")));
  39. EXPECT_CALL(consumer, HandleDiagnostic(
  40. IsDiagnostic(DiagnosticKind::TestDiagnostic,
  41. DiagnosticLevel::Error, 2, 1, "M1")));
  42. EXPECT_CALL(consumer, HandleDiagnostic(
  43. IsDiagnostic(DiagnosticKind::TestDiagnostic,
  44. DiagnosticLevel::Error, 3, 2, "M5")));
  45. EXPECT_CALL(consumer, HandleDiagnostic(
  46. IsDiagnostic(DiagnosticKind::TestDiagnostic,
  47. DiagnosticLevel::Error, 3, 2, "M6")));
  48. EXPECT_CALL(consumer, HandleDiagnostic(
  49. IsDiagnostic(DiagnosticKind::TestDiagnostic,
  50. DiagnosticLevel::Error, 3, 4, "M4")));
  51. sorting_consumer.Flush();
  52. }
  53. } // namespace
  54. } // namespace Carbon