parse_test_matchers_internal.h 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. #ifndef EXPLORER_SYNTAX_PARSE_TEST_MATCHERS_INTERNAL_H_
  5. #define EXPLORER_SYNTAX_PARSE_TEST_MATCHERS_INTERNAL_H_
  6. #include <gmock/gmock.h>
  7. #include <gtest/gtest.h>
  8. #include <ostream>
  9. #include <variant>
  10. #include "explorer/syntax/parse.h"
  11. #include "llvm/Support/Error.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. namespace Carbon::TestingInternal {
  14. // Implementation of ParsedAs(). See there for detailed documentation.
  15. class ParsedAsMatcher {
  16. public:
  17. // NOLINTNEXTLINE(readability-identifier-naming)
  18. using is_gtest_matcher = void;
  19. explicit ParsedAsMatcher(::testing::Matcher<AST> ast_matcher)
  20. : ast_matcher_(std::move(ast_matcher)) {}
  21. void DescribeTo(std::ostream* out) const {
  22. DescribeToImpl(out, /*negated=*/false);
  23. }
  24. void DescribeNegationTo(std::ostream* out) const {
  25. DescribeToImpl(out, /*negated=*/true);
  26. }
  27. auto MatchAndExplain(const ErrorOr<AST>& result,
  28. ::testing::MatchResultListener* listener) const -> bool {
  29. if (!result.ok()) {
  30. *listener << "is a failed parse with error: " << result.error().message();
  31. return false;
  32. } else {
  33. *listener << "is a successful parse whose ";
  34. return ast_matcher_.MatchAndExplain(*result, listener);
  35. }
  36. }
  37. private:
  38. void DescribeToImpl(std::ostream* out, bool negated) const {
  39. *out << "is " << (negated ? "not " : "")
  40. << "a successful parse result whose ";
  41. ast_matcher_.DescribeTo(out);
  42. }
  43. ::testing::Matcher<AST> ast_matcher_;
  44. };
  45. } // namespace Carbon::TestingInternal
  46. #endif // EXPLORER_SYNTAX_PARSE_TEST_MATCHERS_INTERNAL_H_