yaml_test_helpers_test.cpp 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "toolchain/testing/yaml_test_helpers.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include "common/error_test_helpers.h"
  8. namespace Carbon::Testing {
  9. namespace {
  10. using ::testing::_;
  11. using ::testing::ElementsAre;
  12. using ::testing::Not;
  13. TEST(YamlTestHelpersTest, ValidYaml) {
  14. EXPECT_THAT(
  15. Yaml::Value::FromText("[foo, bar]"),
  16. Yaml::IsYaml(ElementsAre(Yaml::Sequence(ElementsAre("foo", "bar")))));
  17. }
  18. TEST(YamlTestHelpersTest, InvalidYaml) {
  19. auto result = Yaml::Value::FromText("- foo\nbar");
  20. // Make sure we've constructed invalid YAML.
  21. EXPECT_FALSE(result.ok());
  22. // Make sure the matcher detects the invalid YAML.
  23. EXPECT_THAT(result, Not(Yaml::IsYaml(_)));
  24. }
  25. TEST(YamlTestHelpersTest, ComposeWithErrorOr) {
  26. auto helper = []() -> ErrorOr<Yaml::Value> {
  27. auto result = Yaml::Value::FromText("[foo, bar]");
  28. if (!result.ok()) {
  29. return std::move(result).error();
  30. }
  31. return {*std::move(result)};
  32. };
  33. // Make sure this works correctly with the generic `ErrorOr` test helper as
  34. // well. Note that `FromText` always produces a sequence of its own, so there
  35. // are two layers of nested sequence here.
  36. EXPECT_THAT(helper(), IsSuccess(Yaml::Sequence(ElementsAre(
  37. Yaml::Sequence(ElementsAre("foo", "bar"))))));
  38. }
  39. } // namespace
  40. } // namespace Carbon::Testing