|
@@ -27,33 +27,33 @@
|
|
|
// // Exact values can be matched by constructing the desired value.
|
|
// // Exact values can be matched by constructing the desired value.
|
|
|
// EXPECT_THAT(
|
|
// EXPECT_THAT(
|
|
|
// yaml,
|
|
// yaml,
|
|
|
-// ElementsAre(
|
|
|
|
|
|
|
+// Yaml::IsYaml(ElementsAre(
|
|
|
// Yaml::MappingValue{
|
|
// Yaml::MappingValue{
|
|
|
// {"fruits", Yaml::SequenceValue{"apple", "orange", "pear"}}},
|
|
// {"fruits", Yaml::SequenceValue{"apple", "orange", "pear"}}},
|
|
|
// Yaml::SequenceValue{Yaml::MappingValue{
|
|
// Yaml::SequenceValue{Yaml::MappingValue{
|
|
|
// {Yaml::SequenceValue{Yaml::MappingValue{{"foo", "bar"}}},
|
|
// {Yaml::SequenceValue{Yaml::MappingValue{{"foo", "bar"}}},
|
|
|
-// "baz"}}}));
|
|
|
|
|
|
|
+// "baz"}}})));
|
|
|
//
|
|
//
|
|
|
// // Properties can be checked using Yaml::Mapping or Yaml::Sequence to
|
|
// // Properties can be checked using Yaml::Mapping or Yaml::Sequence to
|
|
|
// // adapt regular gmock container matchers.
|
|
// // adapt regular gmock container matchers.
|
|
|
// EXPECT_THAT(
|
|
// EXPECT_THAT(
|
|
|
// yaml,
|
|
// yaml,
|
|
|
-// Contains(Yaml::Mapping(
|
|
|
|
|
-// Contains(Pair("fruits", Yaml::Sequence(Contains("orange")))))));
|
|
|
|
|
|
|
+// Yaml::IsYaml(Contains(Yaml::Mapping(
|
|
|
|
|
+// Contains(Pair("fruits", Yaml::Sequence(Contains("orange"))))))));
|
|
|
//
|
|
//
|
|
|
// On match failure, Yaml::Values are printed as C++ code that can be used to
|
|
// On match failure, Yaml::Values are printed as C++ code that can be used to
|
|
|
// recreate the value, for easy copy-pasting into test expectations.
|
|
// recreate the value, for easy copy-pasting into test expectations.
|
|
|
|
|
|
|
|
-#ifndef CARBON_TOOLCHAIN_BASE_YAML_TEST_HELPERS_H_
|
|
|
|
|
-#define CARBON_TOOLCHAIN_BASE_YAML_TEST_HELPERS_H_
|
|
|
|
|
|
|
+#ifndef CARBON_TOOLCHAIN_TESTING_YAML_TEST_HELPERS_H_
|
|
|
|
|
+#define CARBON_TOOLCHAIN_TESTING_YAML_TEST_HELPERS_H_
|
|
|
|
|
|
|
|
#include <gmock/gmock.h>
|
|
#include <gmock/gmock.h>
|
|
|
#include <gtest/gtest.h>
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
#include <iostream>
|
|
#include <iostream>
|
|
|
-#include <sstream>
|
|
|
|
|
#include <variant>
|
|
#include <variant>
|
|
|
|
|
|
|
|
|
|
+#include "common/error.h"
|
|
|
#include "llvm/ADT/StringRef.h"
|
|
#include "llvm/ADT/StringRef.h"
|
|
|
|
|
|
|
|
namespace Carbon::Testing::Yaml {
|
|
namespace Carbon::Testing::Yaml {
|
|
@@ -75,38 +75,50 @@ using ScalarValue = std::string;
|
|
|
using MappingValue = std::vector<std::pair<Value, Value>>;
|
|
using MappingValue = std::vector<std::pair<Value, Value>>;
|
|
|
using SequenceValue = std::vector<Value>;
|
|
using SequenceValue = std::vector<Value>;
|
|
|
struct AliasValue : EmptyComparable {};
|
|
struct AliasValue : EmptyComparable {};
|
|
|
-struct ErrorValue : EmptyComparable {};
|
|
|
|
|
|
|
|
|
|
// A thin wrapper around a variant of possible YAML value types. This type
|
|
// A thin wrapper around a variant of possible YAML value types. This type
|
|
|
// intentionally provides no additional encapsulation or invariants beyond
|
|
// intentionally provides no additional encapsulation or invariants beyond
|
|
|
// those of the variant.
|
|
// those of the variant.
|
|
|
struct Value : std::variant<NullValue, ScalarValue, MappingValue, SequenceValue,
|
|
struct Value : std::variant<NullValue, ScalarValue, MappingValue, SequenceValue,
|
|
|
- AliasValue, ErrorValue> {
|
|
|
|
|
|
|
+ AliasValue> {
|
|
|
using variant::variant;
|
|
using variant::variant;
|
|
|
|
|
|
|
|
// Prints the Value in the form of code to recreate the value.
|
|
// Prints the Value in the form of code to recreate the value.
|
|
|
friend auto operator<<(std::ostream& os, const Value& v) -> std::ostream&;
|
|
friend auto operator<<(std::ostream& os, const Value& v) -> std::ostream&;
|
|
|
|
|
|
|
|
// Parses a sequence of YAML documents from the given YAML text.
|
|
// Parses a sequence of YAML documents from the given YAML text.
|
|
|
- static auto FromText(llvm::StringRef text) -> SequenceValue;
|
|
|
|
|
|
|
+ static auto FromText(llvm::StringRef text) -> ErrorOr<SequenceValue>;
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
-template <typename T>
|
|
|
|
|
-auto DescribeMatcher(::testing::Matcher<T> matcher) -> std::string {
|
|
|
|
|
- std::ostringstream out;
|
|
|
|
|
- matcher.DescribeTo(&out);
|
|
|
|
|
- return out.str();
|
|
|
|
|
|
|
+// Used to examine the results of Value::FromText.
|
|
|
|
|
+// NOLINTNEXTLINE: Expands from GoogleTest.
|
|
|
|
|
+MATCHER_P(IsYaml, matcher,
|
|
|
|
|
+ "is yaml root sequence that " +
|
|
|
|
|
+ ::testing::DescribeMatcher<SequenceValue>(matcher)) {
|
|
|
|
|
+ const ErrorOr<SequenceValue>& yaml = arg;
|
|
|
|
|
+ const ::testing::Matcher<SequenceValue>& typed_matcher = matcher;
|
|
|
|
|
+ if (yaml.ok()) {
|
|
|
|
|
+ // It's hard to intercept printing of the ErrorOr value, so just print it
|
|
|
|
|
+ // here.
|
|
|
|
|
+ *result_listener << "\n which is: " << *yaml << "\n ";
|
|
|
|
|
+ return typed_matcher.MatchAndExplain(*yaml, result_listener);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ *result_listener << "\n with the error: " << yaml.error() << "\n ";
|
|
|
|
|
+ return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Match a Value that is a MappingValue.
|
|
// Match a Value that is a MappingValue.
|
|
|
-// Same as testing::VariantWith<MappingValue>(contents).
|
|
|
|
|
|
|
+// Similar to testing::VariantWith<MappingValue>(matcher), but with better
|
|
|
|
|
+// descriptions.
|
|
|
// NOLINTNEXTLINE: Expands from GoogleTest.
|
|
// NOLINTNEXTLINE: Expands from GoogleTest.
|
|
|
-MATCHER_P(Mapping, contents,
|
|
|
|
|
- "is mapping that " + DescribeMatcher<MappingValue>(contents)) {
|
|
|
|
|
- ::testing::Matcher<MappingValue> contents_matcher = contents;
|
|
|
|
|
-
|
|
|
|
|
- if (auto* map = std::get_if<MappingValue>(&arg)) {
|
|
|
|
|
- return contents_matcher.MatchAndExplain(*map, result_listener);
|
|
|
|
|
|
|
+MATCHER_P(Mapping, matcher,
|
|
|
|
|
+ "is mapping that " +
|
|
|
|
|
+ ::testing::DescribeMatcher<MappingValue>(matcher)) {
|
|
|
|
|
+ const Value& val = arg;
|
|
|
|
|
+ const ::testing::Matcher<MappingValue>& typed_matcher = matcher;
|
|
|
|
|
+ if (const auto* map = std::get_if<MappingValue>(&val)) {
|
|
|
|
|
+ return typed_matcher.MatchAndExplain(*map, result_listener);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
*result_listener << "which is not a mapping";
|
|
*result_listener << "which is not a mapping";
|
|
@@ -114,14 +126,16 @@ MATCHER_P(Mapping, contents,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Match a Value that is a SequenceValue.
|
|
// Match a Value that is a SequenceValue.
|
|
|
-// Same as testing::VariantWith<SequenceValue>(contents).
|
|
|
|
|
|
|
+// Similar to testing::VariantWith<SequenceValue>(matcher), but with better
|
|
|
|
|
+// descriptions.
|
|
|
// NOLINTNEXTLINE: Expands from GoogleTest.
|
|
// NOLINTNEXTLINE: Expands from GoogleTest.
|
|
|
-MATCHER_P(Sequence, contents,
|
|
|
|
|
- "is mapping that " + DescribeMatcher<SequenceValue>(contents)) {
|
|
|
|
|
- ::testing::Matcher<SequenceValue> contents_matcher = contents;
|
|
|
|
|
-
|
|
|
|
|
- if (auto* map = std::get_if<SequenceValue>(&arg)) {
|
|
|
|
|
- return contents_matcher.MatchAndExplain(*map, result_listener);
|
|
|
|
|
|
|
+MATCHER_P(Sequence, matcher,
|
|
|
|
|
+ "is sequence that " +
|
|
|
|
|
+ ::testing::DescribeMatcher<SequenceValue>(matcher)) {
|
|
|
|
|
+ const Value& val = arg;
|
|
|
|
|
+ const ::testing::Matcher<SequenceValue>& typed_matcher = matcher;
|
|
|
|
|
+ if (const auto* map = std::get_if<SequenceValue>(&val)) {
|
|
|
|
|
+ return typed_matcher.MatchAndExplain(*map, result_listener);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
*result_listener << "which is not a sequence";
|
|
*result_listener << "which is not a sequence";
|
|
@@ -129,14 +143,16 @@ MATCHER_P(Sequence, contents,
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// Match a Value that is a ScalarValue.
|
|
// Match a Value that is a ScalarValue.
|
|
|
-// Same as testing::VariantWith<ScalarValue>(contents).
|
|
|
|
|
|
|
+// Similar to testing::VariantWith<ScalarValue>(matcher), but with better
|
|
|
|
|
+// descriptions.
|
|
|
// NOLINTNEXTLINE: Expands from GoogleTest.
|
|
// NOLINTNEXTLINE: Expands from GoogleTest.
|
|
|
-MATCHER_P(Scalar, value,
|
|
|
|
|
- "has scalar value " + DescribeMatcher<std::string>(value)) {
|
|
|
|
|
- ::testing::Matcher<ScalarValue> value_matcher = value;
|
|
|
|
|
-
|
|
|
|
|
- if (auto* map = std::get_if<ScalarValue>(&arg)) {
|
|
|
|
|
- return value_matcher.MatchAndExplain(*map, result_listener);
|
|
|
|
|
|
|
+MATCHER_P(Scalar, matcher,
|
|
|
|
|
+ "has scalar value " +
|
|
|
|
|
+ ::testing::DescribeMatcher<ScalarValue>(matcher)) {
|
|
|
|
|
+ const Value& val = arg;
|
|
|
|
|
+ const ::testing::Matcher<ScalarValue>& typed_matcher = matcher;
|
|
|
|
|
+ if (const auto* map = std::get_if<ScalarValue>(&val)) {
|
|
|
|
|
+ return typed_matcher.MatchAndExplain(*map, result_listener);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
*result_listener << "which is not a scalar";
|
|
*result_listener << "which is not a scalar";
|
|
@@ -145,4 +161,4 @@ MATCHER_P(Scalar, value,
|
|
|
|
|
|
|
|
} // namespace Carbon::Testing::Yaml
|
|
} // namespace Carbon::Testing::Yaml
|
|
|
|
|
|
|
|
-#endif // CARBON_TOOLCHAIN_BASE_YAML_TEST_HELPERS_H_
|
|
|
|
|
|
|
+#endif // CARBON_TOOLCHAIN_TESTING_YAML_TEST_HELPERS_H_
|