sorting_diagnostic_consumer_test.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  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. CARBON_DIAGNOSTIC(TestDiagnostic, Error, "{0}", llvm::StringRef);
  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", 2, 1}, TestDiagnostic, "M1");
  27. emitter.Emit({"f", 1, 1}, TestDiagnostic, "M2");
  28. emitter.Emit({"f", 1, 3}, TestDiagnostic, "M3");
  29. emitter.Emit({"f", 3, 4}, TestDiagnostic, "M4");
  30. emitter.Emit({"f", 3, 2}, TestDiagnostic, "M5");
  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, 4, "M4")));
  47. sorting_consumer.Flush();
  48. }
  49. } // namespace
  50. } // namespace Carbon::Testing