tree_test.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 "toolchain/parse/tree.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <forward_list>
  8. #include "testing/base/test_raw_ostream.h"
  9. #include "toolchain/diagnostics/diagnostic_emitter.h"
  10. #include "toolchain/diagnostics/mocks.h"
  11. #include "toolchain/lex/tokenized_buffer.h"
  12. #include "toolchain/testing/yaml_test_helpers.h"
  13. namespace Carbon::Testing {
  14. namespace {
  15. using Parse::Tree;
  16. using ::testing::ElementsAre;
  17. using ::testing::Pair;
  18. class TreeTest : public ::testing::Test {
  19. protected:
  20. auto GetSourceBuffer(llvm::StringRef t) -> SourceBuffer& {
  21. CARBON_CHECK(fs.addFile("test.carbon", /*ModificationTime=*/0,
  22. llvm::MemoryBuffer::getMemBuffer(t)));
  23. source_storage.push_front(
  24. std::move(*SourceBuffer::CreateFromFile(fs, "test.carbon", consumer)));
  25. return source_storage.front();
  26. }
  27. auto GetTokenizedBuffer(llvm::StringRef t) -> Lex::TokenizedBuffer& {
  28. token_storage.push_front(
  29. Lex::TokenizedBuffer::Lex(GetSourceBuffer(t), consumer));
  30. return token_storage.front();
  31. }
  32. llvm::vfs::InMemoryFileSystem fs;
  33. std::forward_list<SourceBuffer> source_storage;
  34. std::forward_list<Lex::TokenizedBuffer> token_storage;
  35. DiagnosticConsumer& consumer = ConsoleDiagnosticConsumer();
  36. };
  37. TEST_F(TreeTest, IsValid) {
  38. Lex::TokenizedBuffer tokens = GetTokenizedBuffer("");
  39. Tree tree = Tree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  40. EXPECT_TRUE((*tree.postorder().begin()).is_valid());
  41. }
  42. TEST_F(TreeTest, PrintPostorderAsYAML) {
  43. Lex::TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  44. Tree tree = Tree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  45. EXPECT_FALSE(tree.has_errors());
  46. TestRawOstream print_stream;
  47. tree.Print(print_stream);
  48. auto file = Yaml::Sequence(ElementsAre(
  49. Yaml::Mapping(
  50. ElementsAre(Pair("kind", "FunctionIntroducer"), Pair("text", "fn"))),
  51. Yaml::Mapping(ElementsAre(Pair("kind", "Name"), Pair("text", "F"))),
  52. Yaml::Mapping(
  53. ElementsAre(Pair("kind", "ParameterListStart"), Pair("text", "("))),
  54. Yaml::Mapping(ElementsAre(Pair("kind", "ParameterList"),
  55. Pair("text", ")"), Pair("subtree_size", "2"))),
  56. Yaml::Mapping(ElementsAre(Pair("kind", "FunctionDeclaration"),
  57. Pair("text", ";"), Pair("subtree_size", "5"))),
  58. Yaml::Mapping(ElementsAre(Pair("kind", "FileEnd"), Pair("text", "")))));
  59. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  60. ElementsAre(Pair("filename", "test.carbon"), Pair("parse_tree", file)))));
  61. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  62. IsYaml(ElementsAre(root)));
  63. }
  64. TEST_F(TreeTest, PrintPreorderAsYAML) {
  65. Lex::TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  66. Tree tree = Tree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  67. EXPECT_FALSE(tree.has_errors());
  68. TestRawOstream print_stream;
  69. tree.Print(print_stream, /*preorder=*/true);
  70. auto parameter_list = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  71. ElementsAre(Pair("node_index", "2"), Pair("kind", "ParameterListStart"),
  72. Pair("text", "(")))));
  73. auto function_decl = Yaml::Sequence(ElementsAre(
  74. Yaml::Mapping(ElementsAre(Pair("node_index", "0"),
  75. Pair("kind", "FunctionIntroducer"),
  76. Pair("text", "fn"))),
  77. Yaml::Mapping(ElementsAre(Pair("node_index", "1"), Pair("kind", "Name"),
  78. Pair("text", "F"))),
  79. Yaml::Mapping(ElementsAre(Pair("node_index", "3"),
  80. Pair("kind", "ParameterList"),
  81. Pair("text", ")"), Pair("subtree_size", "2"),
  82. Pair("children", parameter_list)))));
  83. auto file = Yaml::Sequence(ElementsAre(
  84. Yaml::Mapping(ElementsAre(Pair("node_index", "4"),
  85. Pair("kind", "FunctionDeclaration"),
  86. Pair("text", ";"), Pair("subtree_size", "5"),
  87. Pair("children", function_decl))),
  88. Yaml::Mapping(ElementsAre(Pair("node_index", "5"),
  89. Pair("kind", "FileEnd"), Pair("text", "")))));
  90. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  91. ElementsAre(Pair("filename", "test.carbon"), Pair("parse_tree", file)))));
  92. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  93. IsYaml(ElementsAre(root)));
  94. }
  95. TEST_F(TreeTest, HighRecursion) {
  96. std::string code = "fn Foo() { return ";
  97. code.append(10000, '(');
  98. code.append(10000, ')');
  99. code += "; }";
  100. Lex::TokenizedBuffer tokens = GetTokenizedBuffer(code);
  101. ASSERT_FALSE(tokens.has_errors());
  102. Testing::MockDiagnosticConsumer consumer;
  103. Tree tree = Tree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  104. EXPECT_FALSE(tree.has_errors());
  105. }
  106. } // namespace
  107. } // namespace Carbon::Testing