tree-sitter/spec/runtime/tree_spec.cc

440 lines
14 KiB
C++
Raw Normal View History

#include "spec_helper.h"
#include "helpers/tree_helpers.h"
2016-09-09 21:11:02 -07:00
#include "helpers/point_helpers.h"
#include "runtime/tree.h"
#include "runtime/length.h"
2014-01-07 21:50:32 -08:00
void assert_consistent(const Tree *tree) {
if (tree->child_count == 0)
return;
AssertThat(tree->children[0]->padding, Equals<Length>(tree->padding));
Length total_children_size = length_zero();
for (size_t i = 0; i < tree->child_count; i++) {
Tree *child = tree->children[i];
AssertThat(child->context.offset, Equals(total_children_size));
assert_consistent(child);
total_children_size = length_add(total_children_size, ts_tree_total_size(child));
}
AssertThat(total_children_size, Equals<Length>(ts_tree_total_size(tree)));
};
2014-01-07 21:50:32 -08:00
START_TEST
describe("Tree", []() {
2016-12-26 13:34:05 -08:00
enum {
symbol1 = 1,
symbol2,
symbol3,
symbol4,
symbol5,
symbol6,
symbol7,
symbol8,
symbol9,
2016-12-26 13:34:05 -08:00
};
TSSymbolMetadata visible = {true, true, false, true};
TSSymbolMetadata invisible = {false, false, false, true};
2014-08-06 13:00:35 -07:00
2016-12-26 13:34:05 -08:00
describe("make_leaf", [&]() {
it("does not mark the tree as fragile", [&]() {
Tree *tree = ts_tree_make_leaf(symbol1, {2, 1, {0, 1}}, {5, 4, {0, 4}}, visible);
AssertThat(tree->fragile_left, IsFalse());
AssertThat(tree->fragile_right, IsFalse());
});
});
2016-12-26 13:34:05 -08:00
describe("make_error", [&]() {
it("marks the tree as fragile", [&]() {
Tree *error_tree = ts_tree_make_error(
length_zero(),
length_zero(),
'z');
2015-12-22 14:20:58 -08:00
AssertThat(error_tree->fragile_left, IsTrue());
AssertThat(error_tree->fragile_right, IsTrue());
2016-02-05 12:23:54 -08:00
ts_tree_release(error_tree);
});
});
2016-12-26 13:34:05 -08:00
describe("make_node", [&]() {
Tree *tree1, *tree2, *parent1;
before_each([&]() {
tree1 = ts_tree_make_leaf(symbol1, {2, 1, {0, 1}}, {5, 4, {0, 4}}, visible);
tree2 = ts_tree_make_leaf(symbol2, {1, 1, {0, 1}}, {3, 3, {0, 3}}, visible);
ts_tree_retain(tree1);
ts_tree_retain(tree2);
parent1 = ts_tree_make_node(symbol3, 2, tree_array({
tree1,
tree2,
}), visible);
});
after_each([&]() {
ts_tree_release(tree1);
ts_tree_release(tree2);
ts_tree_release(parent1);
});
it("computes its size and padding based on its child nodes", [&]() {
AssertThat(parent1->size.bytes, Equals<size_t>(
2016-12-26 13:34:05 -08:00
tree1->size.bytes + tree2->padding.bytes + tree2->size.bytes));
AssertThat(parent1->size.chars, Equals<size_t>(
2016-12-26 13:34:05 -08:00
tree1->size.chars + tree2->padding.chars + tree2->size.chars));
AssertThat(parent1->padding.bytes, Equals<size_t>(tree1->padding.bytes));
AssertThat(parent1->padding.chars, Equals<size_t>(tree1->padding.chars));
});
describe("when the first node is fragile on the left side", [&]() {
Tree *parent;
before_each([&]() {
2015-12-22 14:20:58 -08:00
tree1->fragile_left = true;
tree1->extra = true;
2016-01-28 23:15:22 -08:00
ts_tree_retain(tree1);
ts_tree_retain(tree2);
2016-12-26 13:34:05 -08:00
parent = ts_tree_make_node(symbol3, 2, tree_array({
tree1,
tree2,
}), visible);
});
after_each([&]() {
ts_tree_release(parent);
});
it("records that it is fragile on the left side", [&]() {
2015-12-22 14:20:58 -08:00
AssertThat(parent->fragile_left, IsTrue());
});
});
describe("when the last node is fragile on the right side", [&]() {
Tree *parent;
before_each([&]() {
2015-12-22 14:20:58 -08:00
tree2->fragile_right = true;
tree2->extra = true;
2016-01-28 23:15:22 -08:00
ts_tree_retain(tree1);
ts_tree_retain(tree2);
2016-12-26 13:34:05 -08:00
parent = ts_tree_make_node(symbol3, 2, tree_array({
tree1,
tree2,
}), visible);
});
after_each([&]() {
ts_tree_release(parent);
});
it("records that it is fragile on the right side", [&]() {
2015-12-22 14:20:58 -08:00
AssertThat(parent->fragile_right, IsTrue());
});
});
describe("when the outer nodes aren't fragile on their outer side", [&]() {
Tree *parent;
before_each([&]() {
2015-12-22 14:20:58 -08:00
tree1->fragile_right = true;
tree2->fragile_left = true;
2016-01-28 23:15:22 -08:00
ts_tree_retain(tree1);
ts_tree_retain(tree2);
2016-12-26 13:34:05 -08:00
parent = ts_tree_make_node(symbol3, 2, tree_array({
tree1,
tree2,
}), visible);
});
after_each([&]() {
ts_tree_release(parent);
});
it("records that it is not fragile", [&]() {
2015-12-22 14:20:58 -08:00
AssertThat(parent->fragile_left, IsFalse());
AssertThat(parent->fragile_right, IsFalse());
});
});
2014-08-06 13:00:35 -07:00
});
2016-12-26 13:34:05 -08:00
describe("edit", [&]() {
Tree *tree = nullptr;
2015-09-15 16:00:16 -07:00
before_each([&]() {
2016-12-26 13:34:05 -08:00
tree = ts_tree_make_node(symbol1, 3, tree_array({
ts_tree_make_leaf(symbol2, {2, 2, {0, 2}}, {3, 3, {0, 3}}, visible),
ts_tree_make_leaf(symbol3, {2, 2, {0, 2}}, {3, 3, {0, 3}}, visible),
ts_tree_make_leaf(symbol4, {2, 2, {0, 2}}, {3, 3, {0, 3}}, visible),
}), visible);
2015-09-15 16:00:16 -07:00
AssertThat(tree->padding, Equals<Length>({2, 2, {0, 2}}));
AssertThat(tree->size, Equals<Length>({13, 13, {0, 13}}));
2015-09-15 16:00:16 -07:00
});
after_each([&]() {
ts_tree_release(tree);
});
describe("edits within a tree's padding", [&]() {
it("resizes the padding of the tree and its leftmost descendants", [&]() {
TSInputEdit edit;
edit.start_byte = 1;
edit.bytes_removed = 0;
edit.bytes_added = 1;
edit.start_point = {0, 1};
edit.extent_removed = {0, 0};
edit.extent_added = {0, 1};
2016-09-13 13:08:52 -07:00
ts_tree_edit(tree, &edit);
2015-09-15 16:00:16 -07:00
assert_consistent(tree);
2015-12-22 14:20:58 -08:00
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<Length>({3, 0, {0, 3}}));
AssertThat(tree->size, Equals<Length>({13, 13, {0, 13}}));
2015-09-15 16:00:16 -07:00
2015-12-22 14:20:58 -08:00
AssertThat(tree->children[0]->has_changes, IsTrue());
AssertThat(tree->children[0]->padding, Equals<Length>({3, 0, {0, 3}}));
AssertThat(tree->children[0]->size, Equals<Length>({3, 3, {0, 3}}));
2015-09-15 16:00:16 -07:00
2015-12-22 14:20:58 -08:00
AssertThat(tree->children[1]->has_changes, IsFalse());
AssertThat(tree->children[1]->padding, Equals<Length>({2, 2, {0, 2}}));
AssertThat(tree->children[1]->size, Equals<Length>({3, 3, {0, 3}}));
2015-09-15 16:00:16 -07:00
});
});
describe("edits that start in a tree's padding but extend into its content", [&]() {
it("shrinks the content to compensate for the expanded padding", [&]() {
TSInputEdit edit;
edit.start_byte = 1;
edit.bytes_removed = 3;
edit.bytes_added = 4;
edit.start_point = {0, 1};
edit.extent_removed = {0, 3};
edit.extent_added = {0, 4};
2016-09-13 13:08:52 -07:00
ts_tree_edit(tree, &edit);
2015-09-15 16:00:16 -07:00
assert_consistent(tree);
2015-12-22 14:20:58 -08:00
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<Length>({5, 0, {0, 5}}));
AssertThat(tree->size, Equals<Length>({11, 0, {0, 11}}));
2015-09-15 16:00:16 -07:00
2015-12-22 14:20:58 -08:00
AssertThat(tree->children[0]->has_changes, IsTrue());
AssertThat(tree->children[0]->padding, Equals<Length>({5, 0, {0, 5}}));
AssertThat(tree->children[0]->size, Equals<Length>({1, 0, {0, 1}}));
2015-09-15 16:00:16 -07:00
});
});
describe("insertions at the edge of a tree's padding", [&]() {
it("expands the tree's padding", [&]() {
TSInputEdit edit;
edit.start_byte = 2;
edit.bytes_removed = 0;
edit.bytes_added = 2;
edit.start_point = {0, 2};
edit.extent_removed = {0, 0};
edit.extent_added = {0, 2};
2016-09-13 13:08:52 -07:00
ts_tree_edit(tree, &edit);
assert_consistent(tree);
2015-09-15 16:00:16 -07:00
assert_consistent(tree);
2015-12-22 14:20:58 -08:00
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<Length>({4, 0, {0, 4}}));
AssertThat(tree->size, Equals<Length>({13, 13, {0, 13}}));
2015-09-15 16:00:16 -07:00
2015-12-22 14:20:58 -08:00
AssertThat(tree->children[0]->has_changes, IsTrue());
AssertThat(tree->children[0]->padding, Equals<Length>({4, 0, {0, 4}}));
AssertThat(tree->children[0]->size, Equals<Length>({3, 3, {0, 3}}));
2015-09-15 16:00:16 -07:00
2015-12-22 14:20:58 -08:00
AssertThat(tree->children[1]->has_changes, IsFalse());
2015-09-15 16:00:16 -07:00
});
});
describe("replacements starting at the edge of a tree's padding", [&]() {
it("resizes the content and not the padding", [&]() {
TSInputEdit edit;
edit.start_byte = 2;
edit.bytes_removed = 2;
edit.bytes_added = 5;
edit.start_point = {0, 2};
edit.extent_removed = {0, 2};
edit.extent_added = {0, 5};
2016-09-13 13:08:52 -07:00
ts_tree_edit(tree, &edit);
2015-09-15 16:00:16 -07:00
assert_consistent(tree);
2015-12-22 14:20:58 -08:00
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<Length>({2, 2, {0, 2}}));
AssertThat(tree->size, Equals<Length>({16, 0, {0, 16}}));
2015-09-15 16:00:16 -07:00
2015-12-22 14:20:58 -08:00
AssertThat(tree->children[0]->has_changes, IsTrue());
AssertThat(tree->children[0]->padding, Equals<Length>({2, 2, {0, 2}}));
AssertThat(tree->children[0]->size, Equals<Length>({6, 0, {0, 6}}));
2015-09-15 16:00:16 -07:00
2015-12-22 14:20:58 -08:00
AssertThat(tree->children[1]->has_changes, IsFalse());
2015-09-15 16:00:16 -07:00
});
});
describe("deletions that span more than one child node", [&]() {
it("shrinks subsequent child nodes", [&]() {
TSInputEdit edit;
edit.start_byte = 1;
edit.bytes_removed = 10;
edit.bytes_added = 3;
edit.start_point = {0, 1};
edit.extent_removed = {0, 10};
edit.extent_added = {0, 3};
2016-09-13 13:08:52 -07:00
ts_tree_edit(tree, &edit);
assert_consistent(tree);
2015-09-15 16:00:16 -07:00
assert_consistent(tree);
2015-12-22 14:20:58 -08:00
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<Length>({4, 0, {0, 4}}));
AssertThat(tree->size, Equals<Length>({4, 0, {0, 4}}));
2015-09-15 16:00:16 -07:00
2015-12-22 14:20:58 -08:00
AssertThat(tree->children[0]->has_changes, IsTrue());
AssertThat(tree->children[0]->padding, Equals<Length>({4, 0, {0, 4}}));
AssertThat(tree->children[0]->size, Equals<Length>({0, 0, {0, 0}}));
2015-09-15 16:00:16 -07:00
2015-12-22 14:20:58 -08:00
AssertThat(tree->children[1]->has_changes, IsTrue());
AssertThat(tree->children[1]->padding, Equals<Length>({0, 0, {0, 0}}));
AssertThat(tree->children[1]->size, Equals<Length>({0, 0, {0, 0}}));
2015-09-15 16:00:16 -07:00
2015-12-22 14:20:58 -08:00
AssertThat(tree->children[2]->has_changes, IsTrue());
AssertThat(tree->children[2]->padding, Equals<Length>({1, 0, {0, 1}}));
AssertThat(tree->children[2]->size, Equals<Length>({3, 3, {0, 3}}));
2015-09-15 16:00:16 -07:00
});
});
});
2016-12-26 13:34:05 -08:00
describe("eq", [&]() {
Tree *leaf;
before_each([&]() {
leaf = ts_tree_make_leaf(symbol1, {2, 1, {0, 1}}, {5, 4, {0, 4}}, visible);
});
after_each([&]() {
ts_tree_release(leaf);
});
2014-08-06 13:00:35 -07:00
it("returns true for identical trees", [&]() {
2016-12-26 13:34:05 -08:00
Tree *leaf_copy = ts_tree_make_leaf(symbol1, {2, 1, {1, 1}}, {5, 4, {1, 4}}, visible);
AssertThat(ts_tree_eq(leaf, leaf_copy), IsTrue());
2016-12-26 13:34:05 -08:00
Tree *parent = ts_tree_make_node(symbol2, 2, tree_array({
leaf,
leaf_copy,
}), visible);
ts_tree_retain(leaf);
ts_tree_retain(leaf_copy);
2014-08-06 13:00:35 -07:00
2016-12-26 13:34:05 -08:00
Tree *parent_copy = ts_tree_make_node(symbol2, 2, tree_array({
leaf,
leaf_copy,
}), visible);
2016-12-26 13:34:05 -08:00
ts_tree_retain(leaf);
ts_tree_retain(leaf_copy);
2016-12-26 13:34:05 -08:00
AssertThat(ts_tree_eq(parent, parent_copy), IsTrue());
2014-08-06 13:00:35 -07:00
2016-12-26 13:34:05 -08:00
ts_tree_release(leaf_copy);
ts_tree_release(parent);
ts_tree_release(parent_copy);
2014-01-07 21:50:32 -08:00
});
2014-06-09 21:14:38 -07:00
2014-08-06 13:00:35 -07:00
it("returns false for trees with different symbols", [&]() {
2016-12-26 13:34:05 -08:00
Tree *different_leaf = ts_tree_make_leaf(
leaf->symbol + 1,
leaf->padding,
leaf->size,
visible);
2016-12-26 13:34:05 -08:00
AssertThat(ts_tree_eq(leaf, different_leaf), IsFalse());
ts_tree_release(different_leaf);
});
2014-01-07 21:50:32 -08:00
it("returns false for trees with different options", [&]() {
2016-12-26 13:34:05 -08:00
Tree *different_leaf = ts_tree_make_leaf(symbol1, leaf->padding, leaf->size, invisible);
AssertThat(ts_tree_eq(leaf, different_leaf), IsFalse());
ts_tree_release(different_leaf);
});
it("returns false for trees with different sizes", [&]() {
2016-12-26 13:34:05 -08:00
Tree *different_leaf = ts_tree_make_leaf(symbol1, {2, 1, {0, 1}}, leaf->size, invisible);
AssertThat(ts_tree_eq(leaf, different_leaf), IsFalse());
ts_tree_release(different_leaf);
2016-12-26 13:34:05 -08:00
different_leaf = ts_tree_make_leaf(symbol1, leaf->padding, {5, 4, {1, 10}}, invisible);
AssertThat(ts_tree_eq(leaf, different_leaf), IsFalse());
ts_tree_release(different_leaf);
});
2014-08-06 13:00:35 -07:00
it("returns false for trees with different children", [&]() {
2016-12-26 13:34:05 -08:00
Tree *leaf2 = ts_tree_make_leaf(symbol2, {1, 1, {0, 1}}, {3, 3, {0, 3}}, visible);
2016-12-26 13:34:05 -08:00
Tree *parent = ts_tree_make_node(symbol2, 2, tree_array({
leaf,
leaf2,
}), visible);
ts_tree_retain(leaf);
ts_tree_retain(leaf2);
Tree *different_parent = ts_tree_make_node(symbol2, 2, tree_array({
leaf2,
leaf,
}), visible);
2016-12-26 13:34:05 -08:00
ts_tree_retain(leaf2);
ts_tree_retain(leaf);
2014-08-06 13:00:35 -07:00
2016-12-26 13:34:05 -08:00
AssertThat(ts_tree_eq(different_parent, parent), IsFalse());
AssertThat(ts_tree_eq(parent, different_parent), IsFalse());
2014-08-06 13:00:35 -07:00
2016-12-26 13:34:05 -08:00
ts_tree_release(leaf2);
ts_tree_release(parent);
2014-08-06 13:00:35 -07:00
ts_tree_release(different_parent);
2014-01-07 21:50:32 -08:00
});
2014-08-06 13:00:35 -07:00
});
describe("last_external_token_state", [&]() {
Length padding = {1, 1, {0, 1}};
Length size = {2, 2, {0, 2}};
auto make_external = [](Tree *tree) {
tree->has_external_tokens = true;
tree->has_external_token_state = true;
return tree;
};
it("returns the last serialized external token state in the given tree", [&]() {
Tree *tree1, *tree2, *tree3, *tree4, *tree5, *tree6, *tree7, *tree8, *tree9;
tree1 = ts_tree_make_node(symbol1, 2, tree_array({
(tree2 = ts_tree_make_node(symbol2, 3, tree_array({
(tree3 = make_external(ts_tree_make_leaf(symbol3, padding, size, visible))),
(tree4 = ts_tree_make_leaf(symbol4, padding, size, visible)),
(tree5 = ts_tree_make_leaf(symbol5, padding, size, visible)),
}), visible)),
(tree6 = ts_tree_make_node(symbol6, 2, tree_array({
(tree7 = ts_tree_make_node(symbol7, 1, tree_array({
(tree8 = ts_tree_make_leaf(symbol8, padding, size, visible)),
}), visible)),
(tree9 = ts_tree_make_leaf(symbol9, padding, size, visible)),
}), visible)),
}), visible);
auto state = ts_tree_last_external_token_state(tree1);
AssertThat(state, Equals(&tree3->external_token_state));
});
});
2014-01-07 21:50:32 -08:00
});
END_TEST