Skip to content

Commit

Permalink
Add indentation to yaml test failure output to make it easier to read. (
Browse files Browse the repository at this point in the history
  • Loading branch information
zygoloid authored Aug 7, 2024
1 parent 3c4c234 commit cfed18a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
1 change: 1 addition & 0 deletions toolchain/testing/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ cc_library(
deps = [
"//common:error",
"//common:ostream",
"@abseil-cpp//absl/strings",
"@googletest//:gtest",
"@llvm-project//llvm:Support",
],
Expand Down
24 changes: 16 additions & 8 deletions toolchain/testing/yaml_test_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand Down

0 comments on commit cfed18a

Please sign in to comment.