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
+6 -2
View File
@@ -15,10 +15,12 @@
#include "llvm/Object/Binary.h"
#include "llvm/Support/FormatVariadic.h"
#include "testing/base/test_raw_ostream.h"
#include "toolchain/testing/yaml_test_helpers.h"
namespace Carbon::Testing {
namespace {
using ::testing::_;
using ::testing::ContainsRegex;
using ::testing::HasSubstr;
using ::testing::StrEq;
@@ -125,7 +127,8 @@ TEST_F(DriverTest, DumpTokens) {
driver_.RunCommand({"compile", "--phase=lex", "--dump-tokens", file}));
EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
// Verify there is output without examining it.
EXPECT_FALSE(test_output_stream_.TakeStr().empty());
EXPECT_THAT(Yaml::Value::FromText(test_output_stream_.TakeStr()),
Yaml::IsYaml(_));
}
TEST_F(DriverTest, DumpParseTree) {
@@ -134,7 +137,8 @@ TEST_F(DriverTest, DumpParseTree) {
{"compile", "--phase=parse", "--dump-parse-tree", file}));
EXPECT_THAT(test_error_stream_.TakeStr(), StrEq(""));
// Verify there is output without examining it.
EXPECT_FALSE(test_output_stream_.TakeStr().empty());
EXPECT_THAT(Yaml::Value::FromText(test_output_stream_.TakeStr()),
Yaml::IsYaml(_));
}
TEST_F(DriverTest, StdoutOutput) {