Kaynağa Gözat

Add indentation to yaml test failure output to make it easier to read. (#4201)

Richard Smith 1 yıl önce
ebeveyn
işleme
cfed18a6a5
2 değiştirilmiş dosya ile 17 ekleme ve 8 silme
  1. 1 0
      toolchain/testing/BUILD
  2. 16 8
      toolchain/testing/yaml_test_helpers.h

+ 1 - 0
toolchain/testing/BUILD

@@ -37,6 +37,7 @@ cc_library(
     deps = [
         "//common:error",
         "//common:ostream",
+        "@abseil-cpp//absl/strings",
         "@googletest//:gtest",
         "@llvm-project//llvm:Support",
     ],

+ 16 - 8
toolchain/testing/yaml_test_helpers.h

@@ -53,11 +53,17 @@
 #include <iostream>
 #include <variant>
 
+#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<NullValue, ScalarValue, MappingValue, SequenceValue,
 
 // 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)) {
+MATCHER_P(
+    IsYaml, matcher,
+    "is yaml root sequence that " +
+        IndentString(::testing::DescribeMatcher<SequenceValue>(matcher))) {
   const ErrorOr<SequenceValue>& yaml = arg;
   const ::testing::Matcher<SequenceValue>& 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<MappingValue>(matcher)) {
+              IndentString(::testing::DescribeMatcher<MappingValue>(matcher))) {
   const Value& val = arg;
   const ::testing::Matcher<MappingValue>& typed_matcher = matcher;
   if (const auto* map = std::get_if<MappingValue>(&val)) {
@@ -125,9 +132,10 @@ MATCHER_P(Mapping, matcher,
 // Similar to testing::VariantWith<SequenceValue>(matcher), but with better
 // descriptions.
 // NOLINTNEXTLINE: Expands from GoogleTest.
-MATCHER_P(Sequence, matcher,
-          "is sequence that " +
-              ::testing::DescribeMatcher<SequenceValue>(matcher)) {
+MATCHER_P(
+    Sequence, matcher,
+    "is sequence that " +
+        IndentString(::testing::DescribeMatcher<SequenceValue>(matcher))) {
   const Value& val = arg;
   const ::testing::Matcher<SequenceValue>& typed_matcher = matcher;
   if (const auto* map = std::get_if<SequenceValue>(&val)) {
@@ -144,7 +152,7 @@ MATCHER_P(Sequence, matcher,
 // NOLINTNEXTLINE: Expands from GoogleTest.
 MATCHER_P(Scalar, matcher,
           "has scalar value " +
-              ::testing::DescribeMatcher<ScalarValue>(matcher)) {
+              IndentString(::testing::DescribeMatcher<ScalarValue>(matcher))) {
   const Value& val = arg;
   const ::testing::Matcher<ScalarValue>& typed_matcher = matcher;
   if (const auto* map = std::get_if<ScalarValue>(&val)) {