|
|
@@ -10,16 +10,17 @@
|
|
|
#include <forward_list>
|
|
|
|
|
|
#include "testing/base/test_raw_ostream.h"
|
|
|
-#include "toolchain/base/yaml_test_helpers.h"
|
|
|
#include "toolchain/diagnostics/diagnostic_emitter.h"
|
|
|
#include "toolchain/diagnostics/mocks.h"
|
|
|
#include "toolchain/lex/tokenized_buffer.h"
|
|
|
+#include "toolchain/testing/yaml_test_helpers.h"
|
|
|
|
|
|
namespace Carbon::Testing {
|
|
|
namespace {
|
|
|
|
|
|
using Parse::Tree;
|
|
|
using ::testing::ElementsAre;
|
|
|
+using ::testing::Pair;
|
|
|
|
|
|
class TreeTest : public ::testing::Test {
|
|
|
protected:
|
|
|
@@ -56,26 +57,23 @@ TEST_F(TreeTest, PrintPostorderAsYAML) {
|
|
|
TestRawOstream print_stream;
|
|
|
tree.Print(print_stream);
|
|
|
|
|
|
- auto file = Yaml::SequenceValue{
|
|
|
- Yaml::MappingValue{{"kind", "FunctionIntroducer"}, {"text", "fn"}},
|
|
|
- Yaml::MappingValue{{"kind", "Name"}, {"text", "F"}},
|
|
|
- Yaml::MappingValue{{"kind", "ParameterListStart"}, {"text", "("}},
|
|
|
- Yaml::MappingValue{
|
|
|
- {"kind", "ParameterList"}, {"text", ")"}, {"subtree_size", "2"}},
|
|
|
- Yaml::MappingValue{{"kind", "FunctionDeclaration"},
|
|
|
- {"text", ";"},
|
|
|
- {"subtree_size", "5"}},
|
|
|
- Yaml::MappingValue{{"kind", "FileEnd"}, {"text", ""}},
|
|
|
- };
|
|
|
-
|
|
|
- auto root = Yaml::SequenceValue{
|
|
|
- Yaml::MappingValue{
|
|
|
- {"filename", "test.carbon"},
|
|
|
- {"parse_tree", file},
|
|
|
- },
|
|
|
- };
|
|
|
-
|
|
|
- EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()), ElementsAre(root));
|
|
|
+ auto file = Yaml::Sequence(ElementsAre(
|
|
|
+ Yaml::Mapping(
|
|
|
+ ElementsAre(Pair("kind", "FunctionIntroducer"), Pair("text", "fn"))),
|
|
|
+ Yaml::Mapping(ElementsAre(Pair("kind", "Name"), Pair("text", "F"))),
|
|
|
+ Yaml::Mapping(
|
|
|
+ ElementsAre(Pair("kind", "ParameterListStart"), Pair("text", "("))),
|
|
|
+ Yaml::Mapping(ElementsAre(Pair("kind", "ParameterList"),
|
|
|
+ Pair("text", ")"), Pair("subtree_size", "2"))),
|
|
|
+ Yaml::Mapping(ElementsAre(Pair("kind", "FunctionDeclaration"),
|
|
|
+ Pair("text", ";"), Pair("subtree_size", "5"))),
|
|
|
+ Yaml::Mapping(ElementsAre(Pair("kind", "FileEnd"), Pair("text", "")))));
|
|
|
+
|
|
|
+ auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
|
|
|
+ ElementsAre(Pair("filename", "test.carbon"), Pair("parse_tree", file)))));
|
|
|
+
|
|
|
+ EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
|
|
|
+ IsYaml(ElementsAre(root)));
|
|
|
}
|
|
|
|
|
|
TEST_F(TreeTest, PrintPreorderAsYAML) {
|
|
|
@@ -85,40 +83,34 @@ TEST_F(TreeTest, PrintPreorderAsYAML) {
|
|
|
TestRawOstream print_stream;
|
|
|
tree.Print(print_stream, /*preorder=*/true);
|
|
|
|
|
|
- auto parameter_list = Yaml::SequenceValue{
|
|
|
- Yaml::MappingValue{
|
|
|
- {"node_index", "2"}, {"kind", "ParameterListStart"}, {"text", "("}},
|
|
|
- };
|
|
|
-
|
|
|
- auto function_decl = Yaml::SequenceValue{
|
|
|
- Yaml::MappingValue{
|
|
|
- {"node_index", "0"}, {"kind", "FunctionIntroducer"}, {"text", "fn"}},
|
|
|
- Yaml::MappingValue{{"node_index", "1"}, {"kind", "Name"}, {"text", "F"}},
|
|
|
- Yaml::MappingValue{{"node_index", "3"},
|
|
|
- {"kind", "ParameterList"},
|
|
|
- {"text", ")"},
|
|
|
- {"subtree_size", "2"},
|
|
|
- {"children", parameter_list}},
|
|
|
- };
|
|
|
-
|
|
|
- auto file = Yaml::SequenceValue{
|
|
|
- Yaml::MappingValue{{"node_index", "4"},
|
|
|
- {"kind", "FunctionDeclaration"},
|
|
|
- {"text", ";"},
|
|
|
- {"subtree_size", "5"},
|
|
|
- {"children", function_decl}},
|
|
|
- Yaml::MappingValue{
|
|
|
- {"node_index", "5"}, {"kind", "FileEnd"}, {"text", ""}},
|
|
|
- };
|
|
|
-
|
|
|
- auto root = Yaml::SequenceValue{
|
|
|
- Yaml::MappingValue{
|
|
|
- {"filename", "test.carbon"},
|
|
|
- {"parse_tree", file},
|
|
|
- },
|
|
|
- };
|
|
|
-
|
|
|
- EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()), ElementsAre(root));
|
|
|
+ auto parameter_list = Yaml::Sequence(ElementsAre(Yaml::Mapping(
|
|
|
+ ElementsAre(Pair("node_index", "2"), Pair("kind", "ParameterListStart"),
|
|
|
+ Pair("text", "(")))));
|
|
|
+
|
|
|
+ auto function_decl = Yaml::Sequence(ElementsAre(
|
|
|
+ Yaml::Mapping(ElementsAre(Pair("node_index", "0"),
|
|
|
+ Pair("kind", "FunctionIntroducer"),
|
|
|
+ Pair("text", "fn"))),
|
|
|
+ Yaml::Mapping(ElementsAre(Pair("node_index", "1"), Pair("kind", "Name"),
|
|
|
+ Pair("text", "F"))),
|
|
|
+ Yaml::Mapping(ElementsAre(Pair("node_index", "3"),
|
|
|
+ Pair("kind", "ParameterList"),
|
|
|
+ Pair("text", ")"), Pair("subtree_size", "2"),
|
|
|
+ Pair("children", parameter_list)))));
|
|
|
+
|
|
|
+ auto file = Yaml::Sequence(ElementsAre(
|
|
|
+ Yaml::Mapping(ElementsAre(Pair("node_index", "4"),
|
|
|
+ Pair("kind", "FunctionDeclaration"),
|
|
|
+ Pair("text", ";"), Pair("subtree_size", "5"),
|
|
|
+ Pair("children", function_decl))),
|
|
|
+ Yaml::Mapping(ElementsAre(Pair("node_index", "5"),
|
|
|
+ Pair("kind", "FileEnd"), Pair("text", "")))));
|
|
|
+
|
|
|
+ auto root = Yaml::Sequence(ElementsAre(Yaml::Mapping(
|
|
|
+ ElementsAre(Pair("filename", "test.carbon"), Pair("parse_tree", file)))));
|
|
|
+
|
|
|
+ EXPECT_THAT(Yaml::Value::FromText(print_stream.TakeStr()),
|
|
|
+ IsYaml(ElementsAre(root)));
|
|
|
}
|
|
|
|
|
|
TEST_F(TreeTest, HighRecursion) {
|