yaml_test_helpers.h 5.4 KB

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