yaml_test_helpers.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 TOOLCHAIN_COMMON_YAML_TEST_HELPERS_H_
  47. #define 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, EmptyComparable) -> bool {
  58. return true;
  59. }
  60. friend auto operator!=(EmptyComparable, EmptyComparable) -> bool {
  61. return false;
  62. }
  63. };
  64. struct Value;
  65. struct NullValue : EmptyComparable {};
  66. using ScalarValue = std::string;
  67. using MappingValue = std::vector<std::pair<Value, Value>>;
  68. using SequenceValue = std::vector<Value>;
  69. struct AliasValue : EmptyComparable {};
  70. struct ErrorValue : 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, ErrorValue> {
  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) -> SequenceValue;
  81. };
  82. template <typename T>
  83. auto DescribeMatcher(::testing::Matcher<T> matcher) -> std::string {
  84. std::ostringstream out;
  85. matcher.DescribeTo(&out);
  86. return out.str();
  87. }
  88. // Match a Value that is a MappingValue.
  89. // Same as testing::VariantWith<MappingValue>(contents).
  90. // NOLINTNEXTLINE: Expands from GoogleTest.
  91. MATCHER_P(Mapping, contents,
  92. "is mapping that " + DescribeMatcher<MappingValue>(contents)) {
  93. ::testing::Matcher<MappingValue> contents_matcher = contents;
  94. if (auto* map = std::get_if<MappingValue>(&arg)) {
  95. return contents_matcher.MatchAndExplain(*map, result_listener);
  96. }
  97. *result_listener << "which is not a mapping";
  98. return false;
  99. }
  100. // Match a Value that is a SequenceValue.
  101. // Same as testing::VariantWith<SequenceValue>(contents).
  102. // NOLINTNEXTLINE: Expands from GoogleTest.
  103. MATCHER_P(Sequence, contents,
  104. "is mapping that " + DescribeMatcher<SequenceValue>(contents)) {
  105. ::testing::Matcher<SequenceValue> contents_matcher = contents;
  106. if (auto* map = std::get_if<SequenceValue>(&arg)) {
  107. return contents_matcher.MatchAndExplain(*map, result_listener);
  108. }
  109. *result_listener << "which is not a sequence";
  110. return false;
  111. }
  112. // Match a Value that is a ScalarValue.
  113. // Same as testing::VariantWith<ScalarValue>(contents).
  114. // NOLINTNEXTLINE: Expands from GoogleTest.
  115. MATCHER_P(Scalar, value,
  116. "has scalar value " + ::testing::PrintToString(value)) {
  117. ::testing::Matcher<ScalarValue> value_matcher = value;
  118. if (auto* map = std::get_if<ScalarValue>(&arg)) {
  119. return value_matcher.MatchAndExplain(*map, result_listener);
  120. }
  121. *result_listener << "which is not a scalar";
  122. return false;
  123. }
  124. } // namespace Carbon::Testing::Yaml
  125. #endif // TOOLCHAIN_COMMON_YAML_TEST_HELPERS_H_