yaml_test.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 <gmock/gmock.h>
  5. #include <gtest/gtest.h>
  6. #include "common/ostream.h"
  7. #include "llvm/Support/MemoryBuffer.h"
  8. #include "llvm/Support/VirtualFileSystem.h"
  9. #include "testing/base/gtest_main.h"
  10. #include "testing/base/test_raw_ostream.h"
  11. #include "toolchain/driver/driver.h"
  12. #include "toolchain/testing/yaml_test_helpers.h"
  13. namespace Carbon::SemIR {
  14. namespace {
  15. using ::Carbon::Testing::TestRawOstream;
  16. using ::testing::_;
  17. using ::testing::AllOf;
  18. using ::testing::Contains;
  19. using ::testing::Each;
  20. using ::testing::ElementsAre;
  21. using ::testing::Ge;
  22. using ::testing::IsEmpty;
  23. using ::testing::MatchesRegex;
  24. using ::testing::Pair;
  25. using ::testing::SizeIs;
  26. namespace Yaml = ::Carbon::Testing::Yaml;
  27. TEST(SemIRTest, YAML) {
  28. llvm::vfs::InMemoryFileSystem fs;
  29. CARBON_CHECK(fs.addFile(
  30. "test.carbon", /*ModificationTime=*/0,
  31. llvm::MemoryBuffer::getMemBuffer("fn F() { var x: () = (); return; }")));
  32. const auto install_paths =
  33. InstallPaths::MakeForBazelRunfiles(Testing::GetTestExePath());
  34. TestRawOstream print_stream;
  35. Driver d(fs, &install_paths, print_stream, llvm::errs());
  36. auto run_result =
  37. d.RunCommand({"compile", "--no-prelude-import", "--phase=check",
  38. "--dump-raw-sem-ir", "test.carbon"});
  39. EXPECT_TRUE(run_result.success);
  40. // Matches the ID of an instruction. Instruction counts may change as various
  41. // support changes, so this code is only doing loose structural checks.
  42. auto type_block_id = Yaml::Scalar(MatchesRegex(R"(typeBlock\d+)"));
  43. auto inst_id = Yaml::Scalar(MatchesRegex(R"(inst\+\d+)"));
  44. auto constant_id =
  45. Yaml::Scalar(MatchesRegex(R"((template|symbolic) inst(\w+|\+\d+))"));
  46. auto inst_builtin = Yaml::Scalar(MatchesRegex(R"(inst\w+)"));
  47. auto type_id = Yaml::Scalar(MatchesRegex(R"(type\d+)"));
  48. auto type_builtin = Pair(
  49. type_id, Yaml::Mapping(ElementsAre(Pair("constant", constant_id),
  50. Pair("value_rep", Yaml::Mapping(_)))));
  51. auto file = Yaml::Mapping(ElementsAre(
  52. Pair("import_irs_size", "1"),
  53. Pair("name_scopes", Yaml::Mapping(SizeIs(1))),
  54. Pair("bind_names", Yaml::Mapping(SizeIs(1))),
  55. Pair("functions", Yaml::Mapping(SizeIs(1))),
  56. Pair("classes", Yaml::Mapping(SizeIs(0))),
  57. Pair("types", Yaml::Mapping(Each(type_builtin))),
  58. Pair("type_blocks", Yaml::Mapping(SizeIs(Ge(1)))),
  59. Pair("insts",
  60. Yaml::Mapping(AllOf(
  61. Each(Key(inst_id)),
  62. // kind is required, other parts are optional.
  63. Each(Pair(_, Yaml::Mapping(Contains(Pair("kind", _))))),
  64. // A 0-arg instruction.
  65. Contains(
  66. Pair(_, Yaml::Mapping(ElementsAre(Pair("kind", "Return"))))),
  67. // A 1-arg instruction.
  68. Contains(Pair(_, Yaml::Mapping(ElementsAre(
  69. Pair("kind", "TupleType"),
  70. Pair("arg0", type_block_id),
  71. Pair("type", "typeTypeType"))))),
  72. // A 2-arg instruction.
  73. Contains(Pair(
  74. _, Yaml::Mapping(ElementsAre(Pair("kind", "Assign"),
  75. Pair("arg0", inst_id),
  76. Pair("arg1", inst_id)))))))),
  77. Pair("constant_values",
  78. Yaml::Mapping(AllOf(Each(Pair(inst_id, constant_id))))),
  79. // This production has only two instruction blocks.
  80. Pair("inst_blocks",
  81. Yaml::Mapping(ElementsAre(
  82. Pair("empty", Yaml::Mapping(IsEmpty())),
  83. Pair("exports", Yaml::Mapping(Each(Pair(_, inst_id)))),
  84. Pair("global_init", Yaml::Mapping(IsEmpty())),
  85. Pair("block3", Yaml::Mapping(Each(Pair(_, inst_id)))),
  86. Pair("block4", Yaml::Mapping(Each(Pair(_, inst_id)))),
  87. Pair("block5", Yaml::Mapping(Each(Pair(_, inst_id)))))))));
  88. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  89. ElementsAre(Pair("filename", "test.carbon"), Pair("sem_ir", file)))));
  90. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()), IsYaml(root));
  91. }
  92. } // namespace
  93. } // namespace Carbon::SemIR