tree_test.cpp 4.3 KB

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