yaml_test.cpp 4.2 KB

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