tree-sitter/spec/runtime/tree_spec.cc

344 lines
11 KiB
C++
Raw Normal View History

#include "spec_helper.h"
#include "helpers/tree_helpers.h"
#include "runtime/tree.h"
#include "runtime/length.h"
2014-01-07 21:50:32 -08:00
START_TEST
enum {
2015-09-15 16:00:16 -07:00
cat = ts_builtin_sym_start,
dog,
eel,
fox,
goat,
hog,
};
describe("Tree", []() {
2014-08-06 13:00:35 -07:00
TSTree *tree1, *tree2, *parent1;
TSSymbolMetadata visible = {true, true, false, true};
TSSymbolMetadata invisible = {false, false, false, true};
2014-08-06 13:00:35 -07:00
before_each([&]() {
tree1 = ts_tree_make_leaf(cat, {2, 1, 0, 1}, {5, 4, 0, 4}, visible);
tree2 = ts_tree_make_leaf(cat, {1, 1, 0, 1}, {3, 3, 0, 3}, visible);
2016-01-28 23:15:22 -08:00
ts_tree_retain(tree1);
ts_tree_retain(tree2);
2015-09-15 16:00:16 -07:00
parent1 = ts_tree_make_node(dog, 2, tree_array({
tree1,
tree2,
}), visible);
2014-08-06 13:00:35 -07:00
});
after_each([&]() {
ts_tree_release(tree1);
ts_tree_release(tree2);
ts_tree_release(parent1);
});
describe("make_leaf(sym, size, padding, is_hidden)", [&]() {
it("does not record that it is fragile", [&]() {
2015-12-22 14:20:58 -08:00
AssertThat(tree1->fragile_left, IsFalse());
AssertThat(tree1->fragile_right, IsFalse());
});
});
describe("make_error(size, padding, lookahead_char)", [&]() {
it("records that it is fragile", [&]() {
TSTree *error_tree = ts_tree_make_error(
ts_length_zero(),
ts_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);
});
});
describe("make_node(symbol, child_count, children, is_hidden)", [&]() {
2014-08-06 13:00:35 -07:00
it("computes its size based on its child nodes", [&]() {
AssertThat(parent1->size.bytes, Equals<size_t>(
tree1->size.bytes + + tree2->padding.bytes + tree2->size.bytes));
AssertThat(parent1->size.chars, Equals<size_t>(
tree1->size.chars + + tree2->padding.chars + tree2->size.chars));
});
it("computes its padding based on its first child", [&]() {
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", [&]() {
TSTree *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);
2015-09-15 16:00:16 -07:00
parent = ts_tree_make_node(eel, 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", [&]() {
TSTree *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);
2015-09-15 16:00:16 -07:00
parent = ts_tree_make_node(eel, 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", [&]() {
TSTree *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);
2015-09-15 16:00:16 -07:00
parent = ts_tree_make_node(eel, 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
});
2015-09-15 16:00:16 -07:00
describe("edit(InputEdit)", [&]() {
TSTree *tree = nullptr;
before_each([&]() {
tree = ts_tree_make_node(cat, 3, tree_array({
ts_tree_make_leaf(dog, {2, 2, 0, 2}, {3, 3, 0, 3}, visible),
ts_tree_make_leaf(eel, {2, 2, 0, 2}, {3, 3, 0, 3}, visible),
ts_tree_make_leaf(fox, {2, 2, 0, 2}, {3, 3, 0, 3}, visible),
}), visible);
2015-09-15 16:00:16 -07:00
AssertThat(tree->padding, Equals<TSLength>({2, 2, 0, 2}));
AssertThat(tree->size, Equals<TSLength>({13, 13, 0, 13}));
2015-09-15 16:00:16 -07:00
});
after_each([&]() {
ts_tree_release(tree);
});
auto assert_consistent = [&](const TSTree *tree) {
AssertThat(tree->children[0]->padding, Equals<TSLength>(tree->padding));
TSLength total_children_size = ts_length_zero();
for (size_t i = 0; i < tree->child_count; i++)
total_children_size = ts_length_add(total_children_size, ts_tree_total_size(tree->children[i]));
AssertThat(total_children_size, Equals<TSLength>(ts_tree_total_size(tree)));
};
describe("edits within a tree's padding", [&]() {
it("resizes the padding of the tree and its leftmost descendants", [&]() {
ts_tree_edit(tree, {1, 1, 0});
assert_consistent(tree);
2015-12-22 14:20:58 -08:00
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<TSLength>({0, 3, 0, 0}));
AssertThat(tree->size, Equals<TSLength>({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<TSLength>({0, 3, 0, 0}));
AssertThat(tree->children[0]->size, Equals<TSLength>({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<TSLength>({2, 2, 0, 2}));
AssertThat(tree->children[1]->size, Equals<TSLength>({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", [&]() {
ts_tree_edit(tree, {1, 4, 3});
assert_consistent(tree);
2015-12-22 14:20:58 -08:00
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<TSLength>({0, 5, 0, 0}));
AssertThat(tree->size, Equals<TSLength>({0, 11, 0, 0}));
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<TSLength>({0, 5, 0, 0}));
AssertThat(tree->children[0]->size, Equals<TSLength>({0, 1, 0, 0}));
2015-09-15 16:00:16 -07:00
});
});
describe("insertions at the edge of a tree's padding", [&]() {
it("expands the tree's padding", [&]() {
ts_tree_edit(tree, {2, 2, 0});
assert_consistent(tree);
2015-12-22 14:20:58 -08:00
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<TSLength>({0, 4, 0, 0}));
AssertThat(tree->size, Equals<TSLength>({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<TSLength>({0, 4, 0, 0}));
AssertThat(tree->children[0]->size, Equals<TSLength>({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", [&]() {
ts_tree_edit(tree, {2, 5, 2});
assert_consistent(tree);
2015-12-22 14:20:58 -08:00
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<TSLength>({2, 2, 0, 2}));
AssertThat(tree->size, Equals<TSLength>({0, 16, 0, 0}));
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<TSLength>({2, 2, 0, 2}));
AssertThat(tree->children[0]->size, Equals<TSLength>({0, 6, 0, 0}));
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", [&]() {
ts_tree_edit(tree, {1, 3, 10});
assert_consistent(tree);
2015-12-22 14:20:58 -08:00
AssertThat(tree->has_changes, IsTrue());
AssertThat(tree->padding, Equals<TSLength>({0, 4, 0, 0}));
AssertThat(tree->size, Equals<TSLength>({0, 4, 0, 0}));
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<TSLength>({0, 4, 0, 0}));
AssertThat(tree->children[0]->size, Equals<TSLength>({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<TSLength>({0, 0, 0, 0}));
AssertThat(tree->children[1]->size, Equals<TSLength>({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<TSLength>({0, 1, 0, 0}));
AssertThat(tree->children[2]->size, Equals<TSLength>({3, 3, 0, 3}));
2015-09-15 16:00:16 -07:00
});
});
});
2014-08-06 13:00:35 -07:00
describe("equality", [&]() {
it("returns true for identical trees", [&]() {
TSTree *tree1_copy = ts_tree_make_leaf(cat, {2, 1, 1, 1}, {5, 4, 1, 4}, visible);
2014-10-03 16:06:08 -07:00
AssertThat(ts_tree_eq(tree1, tree1_copy), IsTrue());
TSTree *tree2_copy = ts_tree_make_leaf(cat, {1, 1, 0, 1}, {3, 3, 0, 3}, visible);
2014-10-03 16:06:08 -07:00
AssertThat(ts_tree_eq(tree2, tree2_copy), IsTrue());
2014-08-06 13:00:35 -07:00
TSTree *parent2 = ts_tree_make_node(dog, 2, tree_array({
2015-09-15 16:00:16 -07:00
tree1_copy,
tree2_copy,
}), visible);
2014-10-03 16:06:08 -07:00
AssertThat(ts_tree_eq(parent1, parent2), IsTrue());
2014-08-06 13:00:35 -07:00
ts_tree_release(parent2);
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", [&]() {
TSTree *different_tree = ts_tree_make_leaf(
tree1->symbol + 1,
tree1->padding,
2015-09-15 16:00:16 -07:00
tree1->size,
visible);
2014-10-03 16:06:08 -07:00
AssertThat(ts_tree_eq(tree1, different_tree), IsFalse());
2014-08-06 13:00:35 -07:00
ts_tree_release(different_tree);
});
2014-01-07 21:50:32 -08:00
it("returns false for trees with different options", [&]() {
TSTree *tree1_copy = ts_tree_make_leaf(cat, tree1->padding, tree1->size, invisible);
AssertThat(ts_tree_eq(tree1, tree1_copy), IsFalse());
ts_tree_release(tree1_copy);
});
it("returns false for trees with different sizes", [&]() {
TSTree *tree1_copy = ts_tree_make_leaf(cat, {2, 1, 0, 1}, tree1->size, invisible);
AssertThat(ts_tree_eq(tree1, tree1_copy), IsFalse());
ts_tree_release(tree1_copy);
tree1_copy = ts_tree_make_leaf(cat, tree1->padding, {5, 4, 1, 10}, invisible);
AssertThat(ts_tree_eq(tree1, tree1_copy), IsFalse());
ts_tree_release(tree1_copy);
});
2014-08-06 13:00:35 -07:00
it("returns false for trees with different children", [&]() {
TSTree *different_tree = ts_tree_make_leaf(
tree1->symbol + 1,
tree1->padding,
2015-09-15 16:00:16 -07:00
tree1->size,
visible);
2016-01-28 23:15:22 -08:00
ts_tree_retain(different_tree);
ts_tree_retain(tree2);
2014-08-06 13:00:35 -07:00
TSTree *different_parent = ts_tree_make_node(dog, 2, tree_array({
2016-01-28 23:15:22 -08:00
different_tree, tree2,
}), visible);
2014-08-06 13:00:35 -07:00
2014-10-03 16:06:08 -07:00
AssertThat(ts_tree_eq(different_parent, parent1), IsFalse());
AssertThat(ts_tree_eq(parent1, different_parent), IsFalse());
2014-08-06 13:00:35 -07:00
ts_tree_release(different_tree);
ts_tree_release(different_parent);
2014-01-07 21:50:32 -08:00
});
2014-08-06 13:00:35 -07:00
});
2014-01-07 21:50:32 -08:00
});
END_TEST
2015-08-14 14:48:29 -07:00
bool operator==(TSLength left, TSLength right) {
return ts_length_eq(left, right);
}