Files
carbon-lang/toolchain/testing/yaml_test_helpers_test.cpp
T
Jon Ross-Perkins 32a1be3690 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
2023-09-15 21:28:15 +00:00

33 lines
904 B
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/testing/yaml_test_helpers.h"
#include <gmock/gmock.h>
#include <gtest/gtest.h>
namespace Carbon::Testing {
namespace {
using ::testing::_;
using ::testing::ElementsAre;
using ::testing::Not;
TEST(YamlTestHelpersTest, ValidYaml) {
EXPECT_THAT(
Yaml::Value::FromText("[foo, bar]"),
Yaml::IsYaml(ElementsAre(Yaml::Sequence(ElementsAre("foo", "bar")))));
}
TEST(YamlTestHelpersTest, InvalidYaml) {
auto result = Yaml::Value::FromText("- foo\nbar");
// Make sure we've constructed invalid YAML.
EXPECT_FALSE(result.ok());
// Make sure the matcher detects the invalid YAML.
EXPECT_THAT(result, Not(Yaml::IsYaml(_)));
}
} // namespace
} // namespace Carbon::Testing