diff --git a/toolchain/testing/BUILD b/toolchain/testing/BUILD index b16e74c9fd985..4df14fc334a91 100644 --- a/toolchain/testing/BUILD +++ b/toolchain/testing/BUILD @@ -37,6 +37,7 @@ cc_library( deps = [ "//common:error", "//common:ostream", + "@abseil-cpp//absl/strings", "@googletest//:gtest", "@llvm-project//llvm:Support", ], diff --git a/toolchain/testing/yaml_test_helpers.h b/toolchain/testing/yaml_test_helpers.h index a3bfc55771d22..5249b35bac561 100644 --- a/toolchain/testing/yaml_test_helpers.h +++ b/toolchain/testing/yaml_test_helpers.h @@ -53,11 +53,17 @@ #include #include +#include "absl/strings/str_replace.h" #include "common/error.h" #include "llvm/ADT/StringRef.h" namespace Carbon::Testing::Yaml { +// Adds the specified indentation before each newline in the given string. +inline auto IndentString(std::string_view str) -> std::string { + return absl::StrReplaceAll(str, {{"\n", "\n "}}); +} + struct EmptyComparable { friend auto operator==(EmptyComparable /*lhs*/, EmptyComparable /*rhs*/) -> bool { @@ -88,9 +94,10 @@ struct Value : std::variant(matcher)) { +MATCHER_P( + IsYaml, matcher, + "is yaml root sequence that " + + IndentString(::testing::DescribeMatcher(matcher))) { const ErrorOr& yaml = arg; const ::testing::Matcher& typed_matcher = matcher; if (yaml.ok()) { @@ -110,7 +117,7 @@ MATCHER_P(IsYaml, matcher, // NOLINTNEXTLINE: Expands from GoogleTest. MATCHER_P(Mapping, matcher, "is mapping that " + - ::testing::DescribeMatcher(matcher)) { + IndentString(::testing::DescribeMatcher(matcher))) { const Value& val = arg; const ::testing::Matcher& typed_matcher = matcher; if (const auto* map = std::get_if(&val)) { @@ -125,9 +132,10 @@ MATCHER_P(Mapping, matcher, // Similar to testing::VariantWith(matcher), but with better // descriptions. // NOLINTNEXTLINE: Expands from GoogleTest. -MATCHER_P(Sequence, matcher, - "is sequence that " + - ::testing::DescribeMatcher(matcher)) { +MATCHER_P( + Sequence, matcher, + "is sequence that " + + IndentString(::testing::DescribeMatcher(matcher))) { const Value& val = arg; const ::testing::Matcher& typed_matcher = matcher; if (const auto* map = std::get_if(&val)) { @@ -144,7 +152,7 @@ MATCHER_P(Sequence, matcher, // NOLINTNEXTLINE: Expands from GoogleTest. MATCHER_P(Scalar, matcher, "has scalar value " + - ::testing::DescribeMatcher(matcher)) { + IndentString(::testing::DescribeMatcher(matcher))) { const Value& val = arg; const ::testing::Matcher& typed_matcher = matcher; if (const auto* map = std::get_if(&val)) {