sorting_diagnostic_consumer_test.cpp 2.5 KB

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