yaml_test_helpers.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. //
  5. // This file provides gmock matchers to support testing YAML output.
  6. //
  7. // A YAML document can be converted into a matchable value using
  8. // Yaml::Value::FromText, and then matched with Yaml::Mapping, Yaml::Sequence,
  9. // or Yaml::Scalar. Scalar values can also be matched directly against strings.
  10. //
  11. // Example usage:
  12. //
  13. // namespace Yaml = Carbon::Testing::Yaml;
  14. // using ::testing::ElementsAre;
  15. // using ::testing::Pair;
  16. // Yaml::Value yaml = Yaml::Value::FromText(R"yaml(
  17. // ---
  18. // fruits:
  19. // - apple
  20. // - orange
  21. // - pear
  22. // ...
  23. // ---
  24. // - [foo: "bar"]: "baz"
  25. // )yaml"),
  26. //
  27. // // Exact values can be matched by constructing the desired value.
  28. // EXPECT_THAT(
  29. // yaml,
  30. // Yaml::IsYaml(ElementsAre(
  31. // Yaml::MappingValue{
  32. // {"fruits", Yaml::SequenceValue{"apple", "orange", "pear"}}},
  33. // Yaml::SequenceValue{Yaml::MappingValue{
  34. // {Yaml::SequenceValue{Yaml::MappingValue{{"foo", "bar"}}},
  35. // "baz"}}})));
  36. //
  37. // // Properties can be checked using Yaml::Mapping or Yaml::Sequence to
  38. // // adapt regular gmock container matchers.
  39. // EXPECT_THAT(
  40. // yaml,
  41. // Yaml::IsYaml(Contains(Yaml::Mapping(
  42. // Contains(Pair("fruits", Yaml::Sequence(Contains("orange"))))))));
  43. //
  44. // On match failure, Yaml::Values are printed as C++ code that can be used to
  45. // recreate the value, for easy copy-pasting into test expectations.
  46. #ifndef CARBON_TOOLCHAIN_TESTING_YAML_TEST_HELPERS_H_
  47. #define CARBON_TOOLCHAIN_TESTING_YAML_TEST_HELPERS_H_
  48. #include <gmock/gmock.h>
  49. #include <gtest/gtest.h>
  50. #include <iostream>
  51. #include <variant>
  52. #include "absl/strings/str_replace.h"
  53. #include "common/error.h"
  54. #include "llvm/ADT/StringRef.h"
  55. namespace Carbon::Testing::Yaml {
  56. // Adds the specified indentation before each newline in the given string.
  57. inline auto IndentString(std::string_view str) -> std::string {
  58. return absl::StrReplaceAll(str, {{"\n", "\n "}});
  59. }
  60. struct EmptyComparable {
  61. friend auto operator==(EmptyComparable /*lhs*/, EmptyComparable /*rhs*/)
  62. -> bool {
  63. return true;
  64. }
  65. };
  66. struct Value;
  67. struct NullValue : EmptyComparable {};
  68. using ScalarValue = std::string;
  69. using MappingValue = std::vector<std::pair<Value, Value>>;
  70. using SequenceValue = std::vector<Value>;
  71. struct AliasValue : EmptyComparable {};
  72. // A thin wrapper around a variant of possible YAML value types. This type
  73. // intentionally provides no additional encapsulation or invariants beyond
  74. // those of the variant.
  75. struct Value : std::variant<NullValue, ScalarValue, MappingValue, SequenceValue,
  76. AliasValue> {
  77. using variant::variant;
  78. // Prints the Value in the form of code to recreate the value.
  79. friend auto operator<<(std::ostream& os, const Value& v) -> std::ostream&;
  80. // Parses a sequence of YAML documents from the given YAML text.
  81. static auto FromText(llvm::StringRef text) -> ErrorOr<SequenceValue>;
  82. };
  83. // Used to examine the results of Value::FromText.
  84. // NOLINTNEXTLINE: Expands from GoogleTest.
  85. MATCHER_P(
  86. IsYaml, matcher,
  87. "is yaml root sequence that " +
  88. IndentString(::testing::DescribeMatcher<SequenceValue>(matcher))) {
  89. const ErrorOr<SequenceValue>& yaml = arg;
  90. const ::testing::Matcher<SequenceValue>& typed_matcher = matcher;
  91. if (yaml.ok()) {
  92. // It's hard to intercept printing of the ErrorOr value, so just print it
  93. // here.
  94. *result_listener << "\n which is: " << *yaml << "\n ";
  95. return typed_matcher.MatchAndExplain(*yaml, result_listener);
  96. }
  97. *result_listener << "\n with the error: " << yaml.error() << "\n ";
  98. return false;
  99. }
  100. // Match a Value that is a MappingValue.
  101. // Similar to testing::VariantWith<MappingValue>(matcher), but with better
  102. // descriptions.
  103. // NOLINTNEXTLINE: Expands from GoogleTest.
  104. MATCHER_P(Mapping, matcher,
  105. "is mapping that " +
  106. IndentString(::testing::DescribeMatcher<MappingValue>(matcher))) {
  107. const Value& val = arg;
  108. const ::testing::Matcher<MappingValue>& typed_matcher = matcher;
  109. if (const auto* map = std::get_if<MappingValue>(&val)) {
  110. return typed_matcher.MatchAndExplain(*map, result_listener);
  111. }
  112. *result_listener << "which is not a mapping";
  113. return false;
  114. }
  115. // Match a Value that is a SequenceValue.
  116. // Similar to testing::VariantWith<SequenceValue>(matcher), but with better
  117. // descriptions.
  118. // NOLINTNEXTLINE: Expands from GoogleTest.
  119. MATCHER_P(
  120. Sequence, matcher,
  121. "is sequence that " +
  122. IndentString(::testing::DescribeMatcher<SequenceValue>(matcher))) {
  123. const Value& val = arg;
  124. const ::testing::Matcher<SequenceValue>& typed_matcher = matcher;
  125. if (const auto* map = std::get_if<SequenceValue>(&val)) {
  126. return typed_matcher.MatchAndExplain(*map, result_listener);
  127. }
  128. *result_listener << "which is not a sequence";
  129. return false;
  130. }
  131. // Match a Value that is a ScalarValue.
  132. // Similar to testing::VariantWith<ScalarValue>(matcher), but with better
  133. // descriptions.
  134. // NOLINTNEXTLINE: Expands from GoogleTest.
  135. MATCHER_P(Scalar, matcher,
  136. "has scalar value " +
  137. IndentString(::testing::DescribeMatcher<ScalarValue>(matcher))) {
  138. const Value& val = arg;
  139. const ::testing::Matcher<ScalarValue>& typed_matcher = matcher;
  140. if (const auto* map = std::get_if<ScalarValue>(&val)) {
  141. return typed_matcher.MatchAndExplain(*map, result_listener);
  142. }
  143. *result_listener << "which is not a scalar";
  144. return false;
  145. }
  146. } // namespace Carbon::Testing::Yaml
  147. #endif // CARBON_TOOLCHAIN_TESTING_YAML_TEST_HELPERS_H_