parse_test_matchers_internal.h 1.8 KB

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