yaml_test.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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"(type_block\d+)"));
  43. auto inst_id = Yaml::Scalar(MatchesRegex(R"(inst\+\d+)"));
  44. auto constant_id =
  45. Yaml::Scalar(MatchesRegex(R"(templateConstant\(inst(\w+|\+\d+)\))"));
  46. auto inst_builtin = Yaml::Scalar(MatchesRegex(R"(inst\w+)"));
  47. auto type_id = Yaml::Scalar(MatchesRegex(R"(type(\w+|\(inst(\w+|\+\d+)\)))"));
  48. auto type_builtin = Pair(type_id, Yaml::Mapping(_));
  49. auto file = Yaml::Mapping(ElementsAre(
  50. Pair("import_irs", Yaml::Mapping(SizeIs(1))),
  51. Pair("import_ir_insts", Yaml::Mapping(SizeIs(0))),
  52. Pair("name_scopes", Yaml::Mapping(SizeIs(1))),
  53. Pair("entity_names", Yaml::Mapping(SizeIs(1))),
  54. Pair("functions", Yaml::Mapping(SizeIs(1))),
  55. Pair("classes", Yaml::Mapping(SizeIs(0))),
  56. Pair("generics", Yaml::Mapping(SizeIs(0))),
  57. Pair("specifics", Yaml::Mapping(SizeIs(0))),
  58. Pair("types", Yaml::Mapping(Each(type_builtin))),
  59. Pair("type_blocks", Yaml::Mapping(SizeIs(Ge(1)))),
  60. Pair("insts",
  61. Yaml::Mapping(AllOf(
  62. Each(Key(inst_id)),
  63. // kind is required, other parts are optional.
  64. Each(Pair(_, Yaml::Mapping(Contains(Pair("kind", _))))),
  65. // A 0-arg instruction.
  66. Contains(
  67. Pair(_, Yaml::Mapping(ElementsAre(Pair("kind", "Return"))))),
  68. // A 1-arg instruction.
  69. Contains(Pair(_, Yaml::Mapping(ElementsAre(
  70. Pair("kind", "TupleType"),
  71. Pair("arg0", type_block_id),
  72. Pair("type", "typeTypeType"))))),
  73. // A 2-arg instruction.
  74. Contains(Pair(
  75. _, Yaml::Mapping(ElementsAre(Pair("kind", "Assign"),
  76. Pair("arg0", inst_id),
  77. Pair("arg1", inst_id)))))))),
  78. Pair("constant_values",
  79. Yaml::Mapping(AllOf(Each(Pair(inst_id, constant_id))))),
  80. Pair("symbolic_constants", Yaml::Mapping(SizeIs(0))),
  81. // This production has only two instruction blocks.
  82. Pair("inst_blocks",
  83. Yaml::Mapping(ElementsAre(
  84. Pair("empty", Yaml::Mapping(IsEmpty())),
  85. Pair("exports", Yaml::Mapping(Each(Pair(_, inst_id)))),
  86. Pair("import_refs", Yaml::Mapping(IsEmpty())),
  87. Pair("global_init", Yaml::Mapping(IsEmpty())),
  88. Pair("block4", Yaml::Mapping(Each(Pair(_, inst_id)))),
  89. Pair("block5", Yaml::Mapping(Each(Pair(_, inst_id)))),
  90. Pair("block6", Yaml::Mapping(Each(Pair(_, inst_id)))))))));
  91. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  92. ElementsAre(Pair("filename", "test.carbon"), Pair("sem_ir", file)))));
  93. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()), IsYaml(root));
  94. }
  95. } // namespace
  96. } // namespace Carbon::SemIR