tree_test.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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(
  51. ElementsAre(Pair("kind", "FunctionIntroducer"), Pair("text", "fn"))),
  52. Yaml::Mapping(ElementsAre(Pair("kind", "Name"), Pair("text", "F"))),
  53. Yaml::Mapping(
  54. ElementsAre(Pair("kind", "ParameterListStart"), Pair("text", "("))),
  55. Yaml::Mapping(ElementsAre(Pair("kind", "ParameterList"),
  56. Pair("text", ")"), Pair("subtree_size", "2"))),
  57. Yaml::Mapping(ElementsAre(Pair("kind", "FunctionDeclaration"),
  58. Pair("text", ";"), Pair("subtree_size", "5"))),
  59. Yaml::Mapping(ElementsAre(Pair("kind", "FileEnd"), Pair("text", "")))));
  60. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  61. ElementsAre(Pair("filename", "test.carbon"), Pair("parse_tree", file)))));
  62. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  63. IsYaml(ElementsAre(root)));
  64. }
  65. TEST_F(TreeTest, PrintPreorderAsYAML) {
  66. Lex::TokenizedBuffer tokens = GetTokenizedBuffer("fn F();");
  67. Tree tree = Tree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  68. EXPECT_FALSE(tree.has_errors());
  69. TestRawOstream print_stream;
  70. tree.Print(print_stream, /*preorder=*/true);
  71. auto parameter_list = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  72. ElementsAre(Pair("node_index", "2"), Pair("kind", "ParameterListStart"),
  73. Pair("text", "(")))));
  74. auto function_decl = Yaml::Sequence(ElementsAre(
  75. Yaml::Mapping(ElementsAre(Pair("node_index", "0"),
  76. Pair("kind", "FunctionIntroducer"),
  77. Pair("text", "fn"))),
  78. Yaml::Mapping(ElementsAre(Pair("node_index", "1"), Pair("kind", "Name"),
  79. Pair("text", "F"))),
  80. Yaml::Mapping(ElementsAre(Pair("node_index", "3"),
  81. Pair("kind", "ParameterList"),
  82. Pair("text", ")"), Pair("subtree_size", "2"),
  83. Pair("children", parameter_list)))));
  84. auto file = Yaml::Sequence(ElementsAre(
  85. Yaml::Mapping(ElementsAre(Pair("node_index", "4"),
  86. Pair("kind", "FunctionDeclaration"),
  87. Pair("text", ";"), Pair("subtree_size", "5"),
  88. Pair("children", function_decl))),
  89. Yaml::Mapping(ElementsAre(Pair("node_index", "5"),
  90. Pair("kind", "FileEnd"), Pair("text", "")))));
  91. auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
  92. ElementsAre(Pair("filename", "test.carbon"), Pair("parse_tree", file)))));
  93. EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
  94. IsYaml(ElementsAre(root)));
  95. }
  96. TEST_F(TreeTest, HighRecursion) {
  97. std::string code = "fn Foo() { return ";
  98. code.append(10000, '(');
  99. code.append(10000, ')');
  100. code += "; }";
  101. Lex::TokenizedBuffer tokens = GetTokenizedBuffer(code);
  102. ASSERT_FALSE(tokens.has_errors());
  103. Testing::MockDiagnosticConsumer consumer;
  104. Tree tree = Tree::Parse(tokens, consumer, /*vlog_stream=*/nullptr);
  105. EXPECT_FALSE(tree.has_errors());
  106. }
  107. } // namespace
  108. } // namespace Carbon::Parse