file_test.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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::Testing {
  13. namespace {
  14. using ::testing::_;
  15. using ::testing::AllOf;
  16. using ::testing::Contains;
  17. using ::testing::Each;
  18. using ::testing::ElementsAre;
  19. using ::testing::IsEmpty;
  20. using ::testing::MatchesRegex;
  21. using ::testing::Pair;
  22. using ::testing::SizeIs;
  23. TEST(SemIRTest, YAML) {
  24. llvm::vfs::InMemoryFileSystem fs;
  25. CARBON_CHECK(fs.addFile(
  26. "test.carbon", /*ModificationTime=*/0,
  27. llvm::MemoryBuffer::getMemBuffer("fn F() { var x: i32 = 0; return; }")));
  28. TestRawOstream print_stream;
  29. Driver d(fs, print_stream, llvm::errs());
  30. d.RunCommand(
  31. {"compile", "--phase=check", "--dump-raw-sem-ir", "test.carbon"});
  32. // Matches the ID of a node. The numbers may change because of builtin
  33. // cross-references, so this code is only doing loose structural checks.
  34. auto node_id = Yaml::Scalar(MatchesRegex(R"(node\+\d+)"));
  35. auto node_builtin = Yaml::Scalar(MatchesRegex(R"(node\w+)"));
  36. auto type_id = Yaml::Scalar(MatchesRegex(R"(type\d+)"));
  37. auto file = Yaml::Sequence(ElementsAre(Yaml::Mapping(ElementsAre(
  38. Pair("cross_reference_irs_size", "1"),
  39. Pair("functions", Yaml::Sequence(SizeIs(1))),
  40. Pair("integer_literals", Yaml::Sequence(ElementsAre("0"))),
  41. Pair("real_literals", Yaml::Sequence(IsEmpty())),
  42. Pair("strings", Yaml::Sequence(ElementsAre("F", "x"))),
  43. Pair("types", Yaml::Sequence(ElementsAre(node_builtin))),
  44. Pair("type_blocks", Yaml::Sequence(IsEmpty())),
  45. Pair("nodes",
  46. Yaml::Sequence(AllOf(
  47. // kind is required, other parts are optional.
  48. Each(Yaml::Mapping(Contains(Pair("kind", _)))),
  49. // A 0-arg node.
  50. Contains(Yaml::Mapping(ElementsAre(Pair("kind", "Return")))),
  51. // A 1-arg node.
  52. Contains(Yaml::Mapping(
  53. ElementsAre(Pair("kind", "IntegerLiteral"),
  54. Pair("arg0", "int0"), Pair("type", type_id)))),
  55. // A 2-arg node.
  56. Contains(Yaml::Mapping(ElementsAre(Pair("kind", "Assign"),
  57. Pair("arg0", node_id),
  58. Pair("arg1", node_id))))))),
  59. // This production has only two node blocks.
  60. Pair("node_blocks",
  61. Yaml::Sequence(ElementsAre(Yaml::Sequence(IsEmpty()),
  62. Yaml::Sequence(Each(node_id)),
  63. Yaml::Sequence(Each(node_id)))))))));
  64. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  65. ElementsAre(Pair("filename", "test.carbon"), Pair("sem_ir", file)))));
  66. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  67. IsYaml(ElementsAre(root)));
  68. }
  69. } // namespace
  70. } // namespace Carbon::Testing