Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading