yaml_test_helpers.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. // 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. // 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_COMMON_YAML_TEST_HELPERS_H_
  47. #define CARBON_TOOLCHAIN_COMMON_YAML_TEST_HELPERS_H_
  48. #include <gmock/gmock.h>
  49. #include <gtest/gtest.h>
  50. #include <iomanip>
  51. #include <iostream>
  52. #include <sstream>
  53. #include <variant>
  54. #include "common/ostream.h"
  55. namespace Carbon::Testing::Yaml {
  56. struct EmptyComparable {
  57. friend auto operator==(EmptyComparable /*lhs*/, EmptyComparable /*rhs*/)
  58. -> bool {
  59. return true;
  60. }
  61. friend auto operator!=(EmptyComparable /*lhs*/, EmptyComparable /*rhs*/)
  62. -> bool {
  63. return false;
  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. struct ErrorValue : EmptyComparable {};
  73. // A thin wrapper around a variant of possible YAML value types. This type
  74. // intentionally provides no additional encapsulation or invariants beyond
  75. // those of the variant.
  76. struct Value : std::variant<NullValue, ScalarValue, MappingValue, SequenceValue,
  77. AliasValue, ErrorValue> {
  78. using variant::variant;
  79. // Prints the Value in the form of code to recreate the value.
  80. friend auto operator<<(std::ostream& os, const Value& v) -> std::ostream&;
  81. // Parses a sequence of YAML documents from the given YAML text.
  82. static auto FromText(llvm::StringRef text) -> SequenceValue;
  83. };
  84. template <typename T>
  85. auto DescribeMatcher(::testing::Matcher<T> matcher) -> std::string {
  86. std::ostringstream out;
  87. matcher.DescribeTo(&out);
  88. return out.str();
  89. }
  90. // Match a Value that is a MappingValue.
  91. // Same as testing::VariantWith<MappingValue>(contents).
  92. // NOLINTNEXTLINE: Expands from GoogleTest.
  93. MATCHER_P(Mapping, contents,
  94. "is mapping that " + DescribeMatcher<MappingValue>(contents)) {
  95. ::testing::Matcher<MappingValue> contents_matcher = contents;
  96. if (auto* map = std::get_if<MappingValue>(&arg)) {
  97. return contents_matcher.MatchAndExplain(*map, result_listener);
  98. }
  99. *result_listener << "which is not a mapping";
  100. return false;
  101. }
  102. // Match a Value that is a SequenceValue.
  103. // Same as testing::VariantWith<SequenceValue>(contents).
  104. // NOLINTNEXTLINE: Expands from GoogleTest.
  105. MATCHER_P(Sequence, contents,
  106. "is mapping that " + DescribeMatcher<SequenceValue>(contents)) {
  107. ::testing::Matcher<SequenceValue> contents_matcher = contents;
  108. if (auto* map = std::get_if<SequenceValue>(&arg)) {
  109. return contents_matcher.MatchAndExplain(*map, result_listener);
  110. }
  111. *result_listener << "which is not a sequence";
  112. return false;
  113. }
  114. // Match a Value that is a ScalarValue.
  115. // Same as testing::VariantWith<ScalarValue>(contents).
  116. // NOLINTNEXTLINE: Expands from GoogleTest.
  117. MATCHER_P(Scalar, value,
  118. "has scalar value " + ::testing::PrintToString(value)) {
  119. ::testing::Matcher<ScalarValue> value_matcher = value;
  120. if (auto* map = std::get_if<ScalarValue>(&arg)) {
  121. return value_matcher.MatchAndExplain(*map, result_listener);
  122. }
  123. *result_listener << "which is not a scalar";
  124. return false;
  125. }
  126. } // namespace Carbon::Testing::Yaml
  127. #endif // CARBON_TOOLCHAIN_COMMON_YAML_TEST_HELPERS_H_