sorting_diagnostic_consumer_test.cpp 2.6 KB

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