yaml_test.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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::IsEmpty;
  21. using ::testing::MatchesRegex;
  22. using ::testing::Pair;
  23. using ::testing::SizeIs;
  24. namespace Yaml = ::Carbon::Testing::Yaml;
  25. TEST(SemIRTest, YAML) {
  26. llvm::vfs::InMemoryFileSystem fs;
  27. CARBON_CHECK(fs.addFile(
  28. "test.carbon", /*ModificationTime=*/0,
  29. llvm::MemoryBuffer::getMemBuffer("fn F() { var x: i32 = 0; return; }")));
  30. TestRawOstream print_stream;
  31. Driver d(fs, print_stream, llvm::errs());
  32. d.RunCommand(
  33. {"compile", "--phase=check", "--dump-raw-sem-ir", "test.carbon"});
  34. // Matches the ID of an instruction. The numbers may change because of builtin
  35. // cross-references, so this code is only doing loose structural checks.
  36. auto int_id = Yaml::Scalar(MatchesRegex(R"(int\d+)"));
  37. auto inst_id = Yaml::Scalar(MatchesRegex(R"(inst\+\d+)"));
  38. auto constant_id =
  39. Yaml::Scalar(MatchesRegex(R"((template|symbolic) inst\+\d+)"));
  40. auto inst_builtin = Yaml::Scalar(MatchesRegex(R"(inst\w+)"));
  41. auto type_id = Yaml::Scalar(MatchesRegex(R"(type\d+)"));
  42. auto type_builtin = Pair(
  43. type_id, Yaml::Mapping(ElementsAre(Pair("inst", inst_builtin),
  44. Pair("value_rep", Yaml::Mapping(_)))));
  45. auto file = Yaml::Mapping(ElementsAre(
  46. Pair("cross_ref_irs_size", "1"),
  47. Pair("name_scopes", Yaml::Mapping(SizeIs(1))),
  48. Pair("bind_names", Yaml::Mapping(SizeIs(1))),
  49. Pair("functions", Yaml::Mapping(SizeIs(1))),
  50. Pair("classes", Yaml::Mapping(SizeIs(0))),
  51. Pair("types", Yaml::Mapping(Each(type_builtin))),
  52. Pair("type_blocks", Yaml::Mapping(IsEmpty())),
  53. Pair("insts",
  54. Yaml::Mapping(AllOf(
  55. Each(Key(inst_id)),
  56. // kind is required, other parts are optional.
  57. Each(Pair(_, Yaml::Mapping(Contains(Pair("kind", _))))),
  58. // A 0-arg instruction.
  59. Contains(
  60. Pair(_, Yaml::Mapping(ElementsAre(Pair("kind", "Return"))))),
  61. // A 1-arg instruction.
  62. Contains(
  63. Pair(_, Yaml::Mapping(ElementsAre(Pair("kind", "IntLiteral"),
  64. Pair("arg0", int_id),
  65. Pair("type", type_id))))),
  66. // A 2-arg instruction.
  67. Contains(Pair(
  68. _, Yaml::Mapping(ElementsAre(Pair("kind", "Assign"),
  69. Pair("arg0", inst_id),
  70. Pair("arg1", inst_id)))))))),
  71. Pair("constant_values",
  72. Yaml::Mapping(AllOf(Each(Pair(inst_id, constant_id))))),
  73. // This production has only two instruction blocks.
  74. Pair("inst_blocks",
  75. Yaml::Mapping(ElementsAre(
  76. Pair("empty", Yaml::Mapping(IsEmpty())),
  77. Pair("exports", Yaml::Mapping(Each(Pair(_, inst_id)))),
  78. Pair("block2", Yaml::Mapping(Each(Pair(_, inst_id)))),
  79. Pair("block3", Yaml::Mapping(Each(Pair(_, inst_id)))))))));
  80. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  81. ElementsAre(Pair("filename", "test.carbon"), Pair("sem_ir", file)))));
  82. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()), IsYaml(root));
  83. }
  84. } // namespace
  85. } // namespace Carbon::SemIR