function_test_matchers.h 2.4 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. #ifndef CARBON_TOOLCHAIN_SEMANTICS_NODES_FUNCTION_TEST_MATCHERS_H_
  5. #define CARBON_TOOLCHAIN_SEMANTICS_NODES_FUNCTION_TEST_MATCHERS_H_
  6. #include <gmock/gmock.h>
  7. #include <gtest/gtest.h>
  8. #include "llvm/ADT/StringExtras.h"
  9. #include "toolchain/semantics/nodes/function.h"
  10. #include "toolchain/semantics/nodes/pattern_binding.h"
  11. #include "toolchain/semantics/semantics_ir_for_test.h"
  12. namespace Carbon::Testing {
  13. MATCHER_P(FunctionName, name_matcher,
  14. llvm::formatv("fn `{0}`", ::testing::DescribeMatcher<llvm::StringRef>(
  15. name_matcher))) {
  16. const Semantics::Declaration& decl = arg;
  17. if (auto function =
  18. SemanticsIRForTest::GetDeclaration<Semantics::Function>(decl)) {
  19. return ExplainMatchResult(
  20. name_matcher, SemanticsIRForTest::GetNodeText(function->name().node()),
  21. result_listener);
  22. } else {
  23. *result_listener << "node is not a function";
  24. return result_listener;
  25. }
  26. }
  27. MATCHER_P4(
  28. Function, name_matcher, param_matcher, return_matcher, body_matcher,
  29. llvm::formatv(
  30. "fn `{0}` params `{1}` returns `{2}` body `{3}`",
  31. ::testing::DescribeMatcher<llvm::StringRef>(name_matcher),
  32. ::testing::DescribeMatcher<llvm::ArrayRef<Semantics::PatternBinding>>(
  33. param_matcher),
  34. ::testing::DescribeMatcher<llvm::Optional<Semantics::Expression>>(
  35. return_matcher),
  36. ::testing::DescribeMatcher<Semantics::StatementBlock>(body_matcher))) {
  37. const Semantics::Declaration& decl = arg;
  38. if (auto function =
  39. SemanticsIRForTest::GetDeclaration<Semantics::Function>(decl)) {
  40. return ExplainMatchResult(
  41. name_matcher,
  42. SemanticsIRForTest::GetNodeText(function->name().node()),
  43. result_listener) &&
  44. ExplainMatchResult(param_matcher, function->params(),
  45. result_listener) &&
  46. ExplainMatchResult(return_matcher, function->return_expr(),
  47. result_listener) &&
  48. ExplainMatchResult(body_matcher, function->body(), result_listener);
  49. } else {
  50. *result_listener << "node is not a function";
  51. return result_listener;
  52. }
  53. }
  54. } // namespace Carbon::Testing
  55. #endif // CARBON_TOOLCHAIN_SEMANTICS_NODES_FUNCTION_TEST_MATCHERS_H_