sorting_consumer_test.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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_consumer.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "llvm/ADT/StringRef.h"
  8. #include "toolchain/diagnostics/emitter.h"
  9. #include "toolchain/diagnostics/mocks.h"
  10. namespace Carbon::Diagnostics {
  11. namespace {
  12. using ::Carbon::Testing::IsSingleDiagnostic;
  13. using ::testing::_;
  14. using ::testing::InSequence;
  15. CARBON_DIAGNOSTIC(TestDiagnostic, Error, "M{0}", int);
  16. class FakeEmitter : public Emitter<int32_t> {
  17. public:
  18. using Emitter::Emitter;
  19. protected:
  20. auto ConvertLoc(int32_t last_byte_offset, ContextFnT /*context_fn*/) const
  21. -> ConvertedLoc override {
  22. return {.loc = {}, .last_byte_offset = last_byte_offset};
  23. }
  24. };
  25. TEST(SortedEmitterTest, SortErrors) {
  26. Testing::MockDiagnosticConsumer consumer;
  27. SortingConsumer sorting_consumer(consumer);
  28. FakeEmitter 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. Kind::TestDiagnostic, Level::Error, _, _, "M2")));
  38. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  39. Kind::TestDiagnostic, Level::Error, _, _, "M3")));
  40. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  41. Kind::TestDiagnostic, Level::Error, _, _, "M1")));
  42. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  43. Kind::TestDiagnostic, Level::Error, _, _, "M5")));
  44. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  45. Kind::TestDiagnostic, Level::Error, _, _, "M6")));
  46. EXPECT_CALL(consumer, HandleDiagnostic(IsSingleDiagnostic(
  47. Kind::TestDiagnostic, Level::Error, _, _, "M4")));
  48. sorting_consumer.Flush();
  49. }
  50. } // namespace
  51. } // namespace Carbon::Diagnostics