tree_test.cpp 5.3 KB

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