yaml_test.cpp 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 inst_builtin = Yaml::Scalar(MatchesRegex(R"(inst\w+)"));
  39. auto type_id = Yaml::Scalar(MatchesRegex(R"(type\d+)"));
  40. auto type_builtin = Pair(
  41. type_id, Yaml::Mapping(ElementsAre(Pair("inst", inst_builtin),
  42. Pair("value_rep", Yaml::Mapping(_)))));
  43. auto file = Yaml::Mapping(ElementsAre(
  44. Pair("cross_ref_irs_size", "1"),
  45. Pair("bind_names", Yaml::Mapping(SizeIs(1))),
  46. Pair("functions", Yaml::Mapping(SizeIs(1))),
  47. Pair("classes", Yaml::Mapping(SizeIs(0))),
  48. Pair("types", Yaml::Mapping(Each(type_builtin))),
  49. Pair("type_blocks", Yaml::Mapping(IsEmpty())),
  50. Pair("insts",
  51. Yaml::Mapping(AllOf(
  52. Each(Key(inst_id)),
  53. // kind is required, other parts are optional.
  54. Each(Pair(_, Yaml::Mapping(Contains(Pair("kind", _))))),
  55. // A 0-arg instruction.
  56. Contains(
  57. Pair(_, Yaml::Mapping(ElementsAre(Pair("kind", "Return"))))),
  58. // A 1-arg instruction.
  59. Contains(
  60. Pair(_, Yaml::Mapping(ElementsAre(Pair("kind", "IntLiteral"),
  61. Pair("arg0", int_id),
  62. Pair("type", type_id))))),
  63. // A 2-arg instruction.
  64. Contains(Pair(
  65. _, Yaml::Mapping(ElementsAre(Pair("kind", "Assign"),
  66. Pair("arg0", inst_id),
  67. Pair("arg1", inst_id)))))))),
  68. // This production has only two instruction blocks.
  69. Pair("inst_blocks",
  70. Yaml::Mapping(ElementsAre(
  71. Pair("empty", Yaml::Mapping(IsEmpty())),
  72. Pair("exports", Yaml::Mapping(Each(Pair(_, inst_id)))),
  73. Pair("block2", Yaml::Mapping(Each(Pair(_, inst_id)))),
  74. Pair("block3", Yaml::Mapping(Each(Pair(_, inst_id)))))))));
  75. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  76. ElementsAre(Pair("filename", "test.carbon"), Pair("sem_ir", file)))));
  77. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()), IsYaml(root));
  78. }
  79. } // namespace
  80. } // namespace Carbon::SemIR