yaml_test.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 integer_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("functions", Yaml::Mapping(SizeIs(1))),
  46. Pair("classes", Yaml::Mapping(SizeIs(0))),
  47. Pair("types", Yaml::Mapping(Each(type_builtin))),
  48. Pair("type_blocks", Yaml::Mapping(IsEmpty())),
  49. Pair("insts",
  50. Yaml::Mapping(AllOf(
  51. Each(Key(inst_id)),
  52. // kind is required, other parts are optional.
  53. Each(Pair(_, Yaml::Mapping(Contains(Pair("kind", _))))),
  54. // A 0-arg instruction.
  55. Contains(
  56. Pair(_, Yaml::Mapping(ElementsAre(Pair("kind", "Return"))))),
  57. // A 1-arg instruction.
  58. Contains(Pair(
  59. _, Yaml::Mapping(ElementsAre(Pair("kind", "IntegerLiteral"),
  60. Pair("arg0", integer_id),
  61. Pair("type", type_id))))),
  62. // A 2-arg instruction.
  63. Contains(Pair(
  64. _, Yaml::Mapping(ElementsAre(Pair("kind", "Assign"),
  65. Pair("arg0", inst_id),
  66. Pair("arg1", inst_id)))))))),
  67. // This production has only two instruction blocks.
  68. Pair("inst_blocks",
  69. Yaml::Mapping(ElementsAre(
  70. Pair("block0", Yaml::Mapping(IsEmpty())),
  71. Pair("block1", Yaml::Mapping(Each(Pair(_, inst_id)))),
  72. Pair("block2", Yaml::Mapping(Each(Pair(_, inst_id)))))))));
  73. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  74. ElementsAre(Pair("filename", "test.carbon"), Pair("sem_ir", file)))));
  75. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()), IsYaml(root));
  76. }
  77. } // namespace
  78. } // namespace Carbon::SemIR