mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 20:10:13 +01:00
The YAML test helpers didn't use the `Printable` abstraction in one place and instead directly used `<<` with a `std::ostream`. This matches the `require`s expression in the `error_test_helpers.h` printing logic for `ErrorOr`, but fails to provide the necessary implementation for `llvm::formatv` to succeed with the `Yaml::Value` type. The main fix is to use `Printable` and to define the `Print` method in terms of `llvm::raw_ostream`. We already have all the mapping hooks in place to also support `std::ostream` when needed based on that definition. This also adds some constraints to the printing in `error_test_helpers.h` so it is a bit less under-constrained and more understandable when it is correctly being used. These are just tidying though, they aren't what makes these headers work together. I've added a test to try and make sure these test helpers compose as well.
51 lines
1.5 KiB
C++
51 lines
1.5 KiB
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>
|
|
|
|
#include "common/error_test_helpers.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(_)));
|
|
}
|
|
|
|
TEST(YamlTestHelpersTest, ComposeWithErrorOr) {
|
|
auto helper = []() -> ErrorOr<Yaml::Value> {
|
|
auto result = Yaml::Value::FromText("[foo, bar]");
|
|
if (!result.ok()) {
|
|
return std::move(result).error();
|
|
}
|
|
return {*std::move(result)};
|
|
};
|
|
|
|
// Make sure this works correctly with the generic `ErrorOr` test helper as
|
|
// well. Note that `FromText` always produces a sequence of its own, so there
|
|
// are two layers of nested sequence here.
|
|
EXPECT_THAT(helper(), IsSuccess(Yaml::Sequence(ElementsAre(
|
|
Yaml::Sequence(ElementsAre("foo", "bar"))))));
|
|
}
|
|
|
|
} // namespace
|
|
} // namespace Carbon::Testing
|