Detect invalid yaml. (#3239)

Previous code could fail silently, only caught with yaml output
mismatches. This uses ErrorOr to be explicit about the error detection;
recovery isn't supported because we should only print valid yaml (in
tests, at least).

Also moves the test helpers from //toolchain/base to
//toolchain/testing.

Switches tree_test to use the matchers so that, on mismatch, gtest
prints a path to the mismatch (the straight value means it was just
printing "not equal").

Addresses
https://github.com/carbon-language/carbon-lang/pull/3217#discussion_r1323586836
This commit is contained in:
Jon Ross-Perkins
2023-09-15 21:28:15 +00:00
committed by GitHub
parent f8fd8648ec
commit 32a1be3690
13 changed files with 204 additions and 143 deletions
+25 -31
View File
@@ -12,10 +12,10 @@
#include "llvm/ADT/ArrayRef.h"
#include "testing/base/test_raw_ostream.h"
#include "toolchain/base/yaml_test_helpers.h"
#include "toolchain/diagnostics/diagnostic_emitter.h"
#include "toolchain/diagnostics/mocks.h"
#include "toolchain/lex/tokenized_buffer_test_helpers.h"
#include "toolchain/testing/yaml_test_helpers.h"
namespace Carbon::Testing {
namespace {
@@ -27,6 +27,7 @@ using ::testing::_;
using ::testing::ElementsAre;
using ::testing::Eq;
using ::testing::HasSubstr;
using ::testing::Pair;
class LexerTest : public ::testing::Test {
protected:
@@ -1003,36 +1004,29 @@ TEST_F(LexerTest, PrintingAsYaml) {
EXPECT_THAT(
Yaml::Value::FromText(print_stream.TakeStr()),
ElementsAre(Yaml::SequenceValue{Yaml::MappingValue{
{"filename", Yaml::ScalarValue{source_storage_.front().filename()}},
{"tokens", Yaml::SequenceValue{
Yaml::MappingValue{{"index", "0"},
{"kind", "Semi"},
{"line", "2"},
{"column", "2"},
{"indent", "2"},
{"spelling", ";"},
{"has_trailing_space", "true"}},
Yaml::MappingValue{{"index", "1"},
{"kind", "Semi"},
{"line", "5"},
{"column", "1"},
{"indent", "1"},
{"spelling", ";"},
{"has_trailing_space", "true"}},
Yaml::MappingValue{{"index", "2"},
{"kind", "Semi"},
{"line", "5"},
{"column", "3"},
{"indent", "1"},
{"spelling", ";"},
{"has_trailing_space", "true"}},
Yaml::MappingValue{{"index", "3"},
{"kind", "EndOfFile"},
{"line", "15"},
{"column", "1"},
{"indent", "1"},
{"spelling", ""}}}}}}));
IsYaml(ElementsAre(Yaml::Sequence(ElementsAre(Yaml::Mapping(ElementsAre(
Pair("filename", source_storage_.front().filename().str()),
Pair("tokens",
Yaml::Sequence(ElementsAre(
Yaml::Mapping(ElementsAre(
Pair("index", "0"), Pair("kind", "Semi"),
Pair("line", "2"), Pair("column", "2"),
Pair("indent", "2"), Pair("spelling", ";"),
Pair("has_trailing_space", "true"))),
Yaml::Mapping(
ElementsAre(Pair("index", "1"), Pair("kind", "Semi"),
Pair("line", "5"), Pair("column", "1"),
Pair("indent", "1"), Pair("spelling", ";"),
Pair("has_trailing_space", "true"))),
Yaml::Mapping(
ElementsAre(Pair("index", "2"), Pair("kind", "Semi"),
Pair("line", "5"), Pair("column", "3"),
Pair("indent", "1"), Pair("spelling", ";"),
Pair("has_trailing_space", "true"))),
Yaml::Mapping(ElementsAre(
Pair("index", "3"), Pair("kind", "EndOfFile"),
Pair("line", "15"), Pair("column", "1"),
Pair("indent", "1"), Pair("spelling", "")))))))))))));
}
} // namespace