Clean up Document spec
This commit is contained in:
parent
83c4c03a70
commit
b833942bb8
1 changed files with 72 additions and 75 deletions
|
|
@ -16,22 +16,22 @@ TSPoint point(size_t row, size_t column) {
|
|||
START_TEST
|
||||
|
||||
describe("Document", [&]() {
|
||||
TSDocument *doc;
|
||||
TSDocument *document;
|
||||
TSNode root;
|
||||
|
||||
before_each([&]() {
|
||||
record_alloc::start();
|
||||
doc = ts_document_new();
|
||||
document = ts_document_new();
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
ts_document_free(doc);
|
||||
ts_document_free(document);
|
||||
record_alloc::stop();
|
||||
AssertThat(record_alloc::outstanding_allocation_indices(), IsEmpty());
|
||||
});
|
||||
|
||||
auto assert_node_string_equals = [&](TSNode node, const string &expected) {
|
||||
char *str = ts_node_string(node, doc);
|
||||
char *str = ts_node_string(node, document);
|
||||
string actual(str);
|
||||
ts_free(str);
|
||||
AssertThat(actual, Equals(expected));
|
||||
|
|
@ -43,11 +43,11 @@ describe("Document", [&]() {
|
|||
before_each([&]() {
|
||||
spy_input = new SpyInput("{\"key\": [null, 2]}", 3);
|
||||
|
||||
ts_document_set_language(doc, get_test_language("json"));
|
||||
ts_document_set_input_string(doc, "{\"key\": [1, 2]}");
|
||||
ts_document_parse(doc);
|
||||
ts_document_set_language(document, get_test_language("json"));
|
||||
ts_document_set_input_string(document, "{\"key\": [1, 2]}");
|
||||
ts_document_parse(document);
|
||||
|
||||
root = ts_document_root_node(doc);
|
||||
root = ts_document_root_node(document);
|
||||
assert_node_string_equals(
|
||||
root,
|
||||
"(object (pair (string) (array (number) (number))))");
|
||||
|
|
@ -62,11 +62,11 @@ describe("Document", [&]() {
|
|||
spy_input->content = string((const char *)content, sizeof(content));
|
||||
spy_input->encoding = TSInputEncodingUTF16;
|
||||
|
||||
ts_document_set_input(doc, spy_input->input());
|
||||
ts_document_invalidate(doc);
|
||||
ts_document_parse(doc);
|
||||
ts_document_set_input(document, spy_input->input());
|
||||
ts_document_invalidate(document);
|
||||
ts_document_parse(document);
|
||||
|
||||
root = ts_document_root_node(doc);
|
||||
root = ts_document_root_node(document);
|
||||
assert_node_string_equals(
|
||||
root,
|
||||
"(array (true) (false))");
|
||||
|
|
@ -78,27 +78,27 @@ describe("Document", [&]() {
|
|||
spy_input->encoding = TSInputEncodingUTF16;
|
||||
// spy_input->measure_columns_in_bytes
|
||||
|
||||
ts_document_set_input(doc, spy_input->input());
|
||||
ts_document_invalidate(doc);
|
||||
ts_document_parse(doc);
|
||||
ts_document_set_input(document, spy_input->input());
|
||||
ts_document_invalidate(document);
|
||||
ts_document_parse(document);
|
||||
});
|
||||
|
||||
it("allows the input to be retrieved later", [&]() {
|
||||
ts_document_set_input(doc, spy_input->input());
|
||||
AssertThat(ts_document_input(doc).payload, Equals<void *>(spy_input));
|
||||
AssertThat(ts_document_input(doc).read, Equals(spy_input->input().read));
|
||||
AssertThat(ts_document_input(doc).seek, Equals(spy_input->input().seek));
|
||||
ts_document_set_input(document, spy_input->input());
|
||||
AssertThat(ts_document_input(document).payload, Equals<void *>(spy_input));
|
||||
AssertThat(ts_document_input(document).read, Equals(spy_input->input().read));
|
||||
AssertThat(ts_document_input(document).seek, Equals(spy_input->input().seek));
|
||||
});
|
||||
|
||||
it("does not assume that the document's text has changed", [&]() {
|
||||
ts_document_set_input(doc, spy_input->input());
|
||||
AssertThat(ts_document_root_node(doc), Equals<TSNode>(root));
|
||||
ts_document_set_input(document, spy_input->input());
|
||||
AssertThat(ts_document_root_node(document), Equals<TSNode>(root));
|
||||
AssertThat(ts_node_has_changes(root), IsFalse());
|
||||
AssertThat(spy_input->strings_read, Equals(vector<string>({ "" })));
|
||||
});
|
||||
|
||||
it("reads text from the new input for future parses", [&]() {
|
||||
ts_document_set_input(doc, spy_input->input());
|
||||
ts_document_set_input(document, spy_input->input());
|
||||
|
||||
// Insert 'null', delete '1'.
|
||||
TSInputEdit edit = {};
|
||||
|
|
@ -106,10 +106,10 @@ describe("Document", [&]() {
|
|||
edit.extent_added.column = edit.bytes_added = 4;
|
||||
edit.extent_removed.column = edit.bytes_removed = 1;
|
||||
|
||||
ts_document_edit(doc, edit);
|
||||
ts_document_parse(doc);
|
||||
ts_document_edit(document, edit);
|
||||
ts_document_parse(document);
|
||||
|
||||
TSNode new_root = ts_document_root_node(doc);
|
||||
TSNode new_root = ts_document_root_node(document);
|
||||
assert_node_string_equals(
|
||||
new_root,
|
||||
"(object (pair (string) (array (null) (number))))");
|
||||
|
|
@ -117,17 +117,17 @@ describe("Document", [&]() {
|
|||
});
|
||||
|
||||
it("reads from the new input correctly when the old input was blank", [&]() {
|
||||
ts_document_set_input_string(doc, "");
|
||||
ts_document_parse(doc);
|
||||
TSNode new_root = ts_document_root_node(doc);
|
||||
ts_document_set_input_string(document, "");
|
||||
ts_document_parse(document);
|
||||
TSNode new_root = ts_document_root_node(document);
|
||||
AssertThat(ts_node_end_char(new_root), Equals<size_t>(0));
|
||||
assert_node_string_equals(
|
||||
new_root,
|
||||
"(ERROR)");
|
||||
|
||||
ts_document_set_input_string(doc, "1");
|
||||
ts_document_parse(doc);
|
||||
new_root = ts_document_root_node(doc);
|
||||
ts_document_set_input_string(document, "1");
|
||||
ts_document_parse(document);
|
||||
new_root = ts_document_root_node(document);
|
||||
AssertThat(ts_node_end_char(new_root), Equals<size_t>(1));
|
||||
assert_node_string_equals(
|
||||
new_root,
|
||||
|
|
@ -137,28 +137,28 @@ describe("Document", [&]() {
|
|||
|
||||
describe("set_language(language)", [&]() {
|
||||
before_each([&]() {
|
||||
ts_document_set_input_string(doc, "{\"key\": [1, 2]}\n");
|
||||
ts_document_set_input_string(document, "{\"key\": [1, 2]}\n");
|
||||
});
|
||||
|
||||
it("uses the given language for future parses", [&]() {
|
||||
ts_document_set_language(doc, get_test_language("json"));
|
||||
ts_document_parse(doc);
|
||||
ts_document_set_language(document, get_test_language("json"));
|
||||
ts_document_parse(document);
|
||||
|
||||
root = ts_document_root_node(doc);
|
||||
root = ts_document_root_node(document);
|
||||
assert_node_string_equals(
|
||||
root,
|
||||
"(object (pair (string) (array (number) (number))))");
|
||||
});
|
||||
|
||||
it("clears out any previous tree", [&]() {
|
||||
ts_document_set_language(doc, get_test_language("json"));
|
||||
ts_document_parse(doc);
|
||||
ts_document_set_language(document, get_test_language("json"));
|
||||
ts_document_parse(document);
|
||||
|
||||
ts_document_set_language(doc, get_test_language("javascript"));
|
||||
AssertThat(ts_document_root_node(doc).data, Equals<void *>(nullptr));
|
||||
ts_document_set_language(document, get_test_language("javascript"));
|
||||
AssertThat(ts_document_root_node(document).data, Equals<void *>(nullptr));
|
||||
|
||||
ts_document_parse(doc);
|
||||
root = ts_document_root_node(doc);
|
||||
ts_document_parse(document);
|
||||
root = ts_document_root_node(document);
|
||||
assert_node_string_equals(
|
||||
root,
|
||||
"(program (expression_statement "
|
||||
|
|
@ -171,8 +171,8 @@ describe("Document", [&]() {
|
|||
|
||||
before_each([&]() {
|
||||
logger = new SpyLogger();
|
||||
ts_document_set_language(doc, get_test_language("json"));
|
||||
ts_document_set_input_string(doc, "[1, 2]");
|
||||
ts_document_set_language(document, get_test_language("json"));
|
||||
ts_document_set_input_string(document, "[1, 2]");
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
|
|
@ -180,8 +180,8 @@ describe("Document", [&]() {
|
|||
});
|
||||
|
||||
it("calls the debugger with a message for each parse action", [&]() {
|
||||
ts_document_set_logger(doc, logger->logger());
|
||||
ts_document_parse(doc);
|
||||
ts_document_set_logger(document, logger->logger());
|
||||
ts_document_parse(document);
|
||||
|
||||
AssertThat(logger->messages, Contains("new_parse"));
|
||||
AssertThat(logger->messages, Contains("skip character:' '"));
|
||||
|
|
@ -192,18 +192,18 @@ describe("Document", [&]() {
|
|||
});
|
||||
|
||||
it("allows the debugger to be retrieved later", [&]() {
|
||||
ts_document_set_logger(doc, logger->logger());
|
||||
AssertThat(ts_document_logger(doc).payload, Equals(logger));
|
||||
ts_document_set_logger(document, logger->logger());
|
||||
AssertThat(ts_document_logger(document).payload, Equals(logger));
|
||||
});
|
||||
|
||||
describe("disabling debugging", [&]() {
|
||||
before_each([&]() {
|
||||
ts_document_set_logger(doc, logger->logger());
|
||||
ts_document_set_logger(doc, {NULL, NULL});
|
||||
ts_document_set_logger(document, logger->logger());
|
||||
ts_document_set_logger(document, {NULL, NULL});
|
||||
});
|
||||
|
||||
it("does not call the debugger any more", [&]() {
|
||||
ts_document_parse(doc);
|
||||
ts_document_parse(document);
|
||||
AssertThat(logger->messages, IsEmpty());
|
||||
});
|
||||
});
|
||||
|
|
@ -213,12 +213,12 @@ describe("Document", [&]() {
|
|||
SpyInput *input;
|
||||
|
||||
before_each([&]() {
|
||||
ts_document_set_language(doc, get_test_language("javascript"));
|
||||
ts_document_set_language(document, get_test_language("javascript"));
|
||||
input = new SpyInput("{a: null};", 3);
|
||||
ts_document_set_input(doc, input->input());
|
||||
ts_document_parse(doc);
|
||||
ts_document_set_input(document, input->input());
|
||||
ts_document_parse(document);
|
||||
assert_node_string_equals(
|
||||
ts_document_root_node(doc),
|
||||
ts_document_root_node(document),
|
||||
"(program (expression_statement (object (pair (identifier) (null)))))");
|
||||
});
|
||||
|
||||
|
|
@ -226,26 +226,25 @@ describe("Document", [&]() {
|
|||
delete input;
|
||||
});
|
||||
|
||||
auto get_ranges = [&](std::function<TSInputEdit()> callback) -> vector<TSRange> {
|
||||
auto get_invalidated_ranges_for_edit = [&](std::function<TSInputEdit()> callback) -> vector<TSRange> {
|
||||
TSInputEdit edit = callback();
|
||||
ts_document_edit(doc, edit);
|
||||
ts_document_edit(document, edit);
|
||||
|
||||
TSRange *ranges;
|
||||
uint32_t range_count = 0;
|
||||
|
||||
ts_document_parse_and_get_changed_ranges(doc, &ranges, &range_count);
|
||||
ts_document_parse_and_get_changed_ranges(document, &ranges, &range_count);
|
||||
|
||||
vector<TSRange> result;
|
||||
for (size_t i = 0; i < range_count; i++)
|
||||
for (size_t i = 0; i < range_count; i++) {
|
||||
result.push_back(ranges[i]);
|
||||
}
|
||||
ts_free(ranges);
|
||||
|
||||
return result;
|
||||
};
|
||||
|
||||
it("reports changes when one token has been updated", [&]() {
|
||||
// Replace `null` with `nothing`
|
||||
auto ranges = get_ranges([&]() {
|
||||
auto ranges = get_invalidated_ranges_for_edit([&]() {
|
||||
return input->replace(input->content.find("ull"), 1, "othing");
|
||||
});
|
||||
|
||||
|
|
@ -257,7 +256,7 @@ describe("Document", [&]() {
|
|||
})));
|
||||
|
||||
// Replace `nothing` with `null` again
|
||||
ranges = get_ranges([&]() {
|
||||
ranges = get_invalidated_ranges_for_edit([&]() {
|
||||
return input->undo();
|
||||
});
|
||||
|
||||
|
|
@ -271,7 +270,7 @@ describe("Document", [&]() {
|
|||
|
||||
it("reports changes when tokens have been appended", [&]() {
|
||||
// Add a second key-value pair
|
||||
auto ranges = get_ranges([&]() {
|
||||
auto ranges = get_invalidated_ranges_for_edit([&]() {
|
||||
return input->replace(input->content.find("}"), 0, ", b: false");
|
||||
});
|
||||
|
||||
|
|
@ -283,12 +282,12 @@ describe("Document", [&]() {
|
|||
})));
|
||||
|
||||
// Add a third key-value pair in between the first two
|
||||
ranges = get_ranges([&]() {
|
||||
ranges = get_invalidated_ranges_for_edit([&]() {
|
||||
return input->replace(input->content.find(", b"), 0, ", c: 1");
|
||||
});
|
||||
|
||||
assert_node_string_equals(
|
||||
ts_document_root_node(doc),
|
||||
ts_document_root_node(document),
|
||||
"(program (expression_statement (object "
|
||||
"(pair (identifier) (null)) "
|
||||
"(pair (identifier) (number)) "
|
||||
|
|
@ -302,41 +301,39 @@ describe("Document", [&]() {
|
|||
})));
|
||||
|
||||
// Delete the middle pair.
|
||||
ranges = get_ranges([&]() {
|
||||
ranges = get_invalidated_ranges_for_edit([&]() {
|
||||
return input->undo();
|
||||
});
|
||||
|
||||
assert_node_string_equals(
|
||||
ts_document_root_node(doc),
|
||||
ts_document_root_node(document),
|
||||
"(program (expression_statement (object "
|
||||
"(pair (identifier) (null)) "
|
||||
"(pair (identifier) (false)))))");
|
||||
|
||||
AssertThat(ranges, Equals(vector<TSRange>({
|
||||
})));
|
||||
AssertThat(ranges, IsEmpty());
|
||||
|
||||
// Delete the second pair.
|
||||
ranges = get_ranges([&]() {
|
||||
ranges = get_invalidated_ranges_for_edit([&]() {
|
||||
return input->undo();
|
||||
});
|
||||
|
||||
assert_node_string_equals(
|
||||
ts_document_root_node(doc),
|
||||
ts_document_root_node(document),
|
||||
"(program (expression_statement (object "
|
||||
"(pair (identifier) (null)))))");
|
||||
|
||||
AssertThat(ranges, Equals(vector<TSRange>({
|
||||
})));
|
||||
AssertThat(ranges, IsEmpty());
|
||||
});
|
||||
|
||||
it("reports changes when trees have been wrapped", [&]() {
|
||||
// Wrap the object in an assignment expression.
|
||||
auto ranges = get_ranges([&]() {
|
||||
auto ranges = get_invalidated_ranges_for_edit([&]() {
|
||||
return input->replace(input->content.find("null"), 0, "b === ");
|
||||
});
|
||||
|
||||
assert_node_string_equals(
|
||||
ts_document_root_node(doc),
|
||||
ts_document_root_node(document),
|
||||
"(program (expression_statement (object "
|
||||
"(pair (identifier) (rel_op (identifier) (null))))))");
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue