sorting_diagnostic_consumer_test.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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::IsSingleDiagnostic;
  13. using ::testing::_;
  14. using ::testing::InSequence;
  15. CARBON_DIAGNOSTIC(TestDiagnostic, Error, "M{0}", int);
  16. struct FakeDiagnosticConverter : DiagnosticConverter<int32_t> {
  17. auto ConvertLoc(int32_t last_byte_offset, ContextFnT /*context_fn*/) const
  18. -> ConvertedDiagnosticLoc override {
  19. return {.loc = {}, .last_byte_offset = last_byte_offset};
  20. }
  21. };
  22. TEST(SortedDiagnosticEmitterTest, SortErrors) {
  23. FakeDiagnosticConverter converter;
  24. Testing::MockDiagnosticConsumer consumer;
  25. SortingDiagnosticConsumer sorting_consumer(consumer);
  26. DiagnosticEmitter<int32_t> emitter(converter, sorting_consumer);
  27. emitter.Emit(1, TestDiagnostic, 1);
  28. emitter.Emit(-1, TestDiagnostic, 2);
  29. emitter.Emit(0, TestDiagnostic, 3);
  30. emitter.Emit(4, TestDiagnostic, 4);
  31. emitter.Emit(3, TestDiagnostic, 5);
  32. emitter.Emit(3, TestDiagnostic, 6);
  33. InSequence s;
  34. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  35. DiagnosticKind::TestDiagnostic,
  36. DiagnosticLevel::Error, _, _, "M2")));
  37. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  38. DiagnosticKind::TestDiagnostic,
  39. DiagnosticLevel::Error, _, _, "M3")));
  40. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  41. DiagnosticKind::TestDiagnostic,
  42. DiagnosticLevel::Error, _, _, "M1")));
  43. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  44. DiagnosticKind::TestDiagnostic,
  45. DiagnosticLevel::Error, _, _, "M5")));
  46. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  47. DiagnosticKind::TestDiagnostic,
  48. DiagnosticLevel::Error, _, _, "M6")));
  49. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  50. DiagnosticKind::TestDiagnostic,
  51. DiagnosticLevel::Error, _, _, "M4")));
  52. sorting_consumer.Flush();
  53. }
  54. } // namespace
  55. } // namespace Carbon