yaml_test_helpers.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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 "common/error.h"
  53. #include "llvm/ADT/StringRef.h"
  54. namespace Carbon::Testing::Yaml {
  55. struct EmptyComparable {
  56. friend auto operator==(EmptyComparable /*lhs*/, EmptyComparable /*rhs*/)
  57. -> bool {
  58. return true;
  59. }
  60. friend auto operator!=(EmptyComparable /*lhs*/, EmptyComparable /*rhs*/)
  61. -> bool {
  62. return false;
  63. }
  64. };
  65. struct Value;
  66. struct NullValue : EmptyComparable {};
  67. using ScalarValue = std::string;
  68. using MappingValue = std::vector<std::pair<Value, Value>>;
  69. using SequenceValue = std::vector<Value>;
  70. struct AliasValue : EmptyComparable {};
  71. // A thin wrapper around a variant of possible YAML value types. This type
  72. // intentionally provides no additional encapsulation or invariants beyond
  73. // those of the variant.
  74. struct Value : std::variant<NullValue, ScalarValue, MappingValue, SequenceValue,
  75. AliasValue> {
  76. using variant::variant;
  77. // Prints the Value in the form of code to recreate the value.
  78. friend auto operator<<(std::ostream& os, const Value& v) -> std::ostream&;
  79. // Parses a sequence of YAML documents from the given YAML text.
  80. static auto FromText(llvm::StringRef text) -> ErrorOr<SequenceValue>;
  81. };
  82. // Used to examine the results of Value::FromText.
  83. // NOLINTNEXTLINE: Expands from GoogleTest.
  84. MATCHER_P(IsYaml, matcher,
  85. "is yaml root sequence that " +
  86. ::testing::DescribeMatcher<SequenceValue>(matcher)) {
  87. const ErrorOr<SequenceValue>& yaml = arg;
  88. const ::testing::Matcher<SequenceValue>& typed_matcher = matcher;
  89. if (yaml.ok()) {
  90. // It's hard to intercept printing of the ErrorOr value, so just print it
  91. // here.
  92. *result_listener << "\n which is: " << *yaml << "\n ";
  93. return typed_matcher.MatchAndExplain(*yaml, result_listener);
  94. }
  95. *result_listener << "\n with the error: " << yaml.error() << "\n ";
  96. return false;
  97. }
  98. // Match a Value that is a MappingValue.
  99. // Similar to testing::VariantWith<MappingValue>(matcher), but with better
  100. // descriptions.
  101. // NOLINTNEXTLINE: Expands from GoogleTest.
  102. MATCHER_P(Mapping, matcher,
  103. "is mapping that " +
  104. ::testing::DescribeMatcher<MappingValue>(matcher)) {
  105. const Value& val = arg;
  106. const ::testing::Matcher<MappingValue>& typed_matcher = matcher;
  107. if (const auto* map = std::get_if<MappingValue>(&val)) {
  108. return typed_matcher.MatchAndExplain(*map, result_listener);
  109. }
  110. *result_listener << "which is not a mapping";
  111. return false;
  112. }
  113. // Match a Value that is a SequenceValue.
  114. // Similar to testing::VariantWith<SequenceValue>(matcher), but with better
  115. // descriptions.
  116. // NOLINTNEXTLINE: Expands from GoogleTest.
  117. MATCHER_P(Sequence, matcher,
  118. "is sequence that " +
  119. ::testing::DescribeMatcher<SequenceValue>(matcher)) {
  120. const Value& val = arg;
  121. const ::testing::Matcher<SequenceValue>& typed_matcher = matcher;
  122. if (const auto* map = std::get_if<SequenceValue>(&val)) {
  123. return typed_matcher.MatchAndExplain(*map, result_listener);
  124. }
  125. *result_listener << "which is not a sequence";
  126. return false;
  127. }
  128. // Match a Value that is a ScalarValue.
  129. // Similar to testing::VariantWith<ScalarValue>(matcher), but with better
  130. // descriptions.
  131. // NOLINTNEXTLINE: Expands from GoogleTest.
  132. MATCHER_P(Scalar, matcher,
  133. "has scalar value " +
  134. ::testing::DescribeMatcher<ScalarValue>(matcher)) {
  135. const Value& val = arg;
  136. const ::testing::Matcher<ScalarValue>& typed_matcher = matcher;
  137. if (const auto* map = std::get_if<ScalarValue>(&val)) {
  138. return typed_matcher.MatchAndExplain(*map, result_listener);
  139. }
  140. *result_listener << "which is not a scalar";
  141. return false;
  142. }
  143. } // namespace Carbon::Testing::Yaml
  144. #endif // CARBON_TOOLCHAIN_TESTING_YAML_TEST_HELPERS_H_