Fix size calculation for error nodes
This commit is contained in:
parent
604b149c4b
commit
c5ac02c571
3 changed files with 107 additions and 54 deletions
|
|
@ -1,6 +1,5 @@
|
|||
#include "runtime/runtime_spec_helper.h"
|
||||
#include "runtime/helpers/spy_reader.h"
|
||||
#include "runtime/tree.h"
|
||||
|
||||
extern "C" const TSLanguage * ts_language_json();
|
||||
|
||||
|
|
@ -17,37 +16,50 @@ describe("Document", [&]() {
|
|||
ts_document_free(doc);
|
||||
});
|
||||
|
||||
describe("when the language is not set", [&]() {
|
||||
describe("setting the input", [&]() {
|
||||
SpyReader *reader;
|
||||
describe("set_input", [&]() {
|
||||
SpyReader *reader;
|
||||
|
||||
before_each([&]() {
|
||||
reader = new SpyReader("{ \"key\": [1, 2] }", 5);
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
delete reader;
|
||||
});
|
||||
|
||||
describe("when the language is set", [&]() {
|
||||
before_each([&]() {
|
||||
reader = new SpyReader("{ \"key\": [1, 2] }", 5);
|
||||
ts_document_set_language(doc, ts_language_json());
|
||||
});
|
||||
|
||||
it("parses the document", [&]() {
|
||||
SpyReader *reader = new SpyReader("{ \"key\": [1, 2] }", 5);
|
||||
ts_document_set_input(doc, reader->input);
|
||||
|
||||
AssertThat(string(ts_node_string(ts_document_root_node(doc))), Equals(
|
||||
"(DOCUMENT (object (string) (array (number) (number))))"
|
||||
));
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
delete reader;
|
||||
it("reads the entire input", [&]() {
|
||||
SpyReader *reader = new SpyReader("{ \"key\": [1, 2] }", 5);
|
||||
ts_document_set_input(doc, reader->input);
|
||||
AssertThat(reader->strings_read, Equals(vector<string>({
|
||||
"{ \"key\": [1, 2] }"
|
||||
})));
|
||||
});
|
||||
});
|
||||
|
||||
describe("when the language is not set", [&]() {
|
||||
it("does not try to parse the document", [&]() {
|
||||
ts_document_set_input(doc, reader->input);
|
||||
|
||||
AssertThat(ts_document_root_node(doc), Equals<TSNode *>(nullptr));
|
||||
});
|
||||
|
||||
describe("setting the language", [&]() {
|
||||
before_each([&]() {
|
||||
ts_document_set_language(doc, ts_language_json());
|
||||
});
|
||||
|
||||
it("parses the document", [&]() {
|
||||
AssertThat(string(ts_node_string(ts_document_root_node(doc))), Equals(
|
||||
"(DOCUMENT (object (string) (array (number) (number))))"));
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("setting the language and input", [&]() {
|
||||
describe("edit", [&]() {
|
||||
SpyReader *reader;
|
||||
|
||||
before_each([&]() {
|
||||
|
|
@ -60,17 +72,6 @@ describe("Document", [&]() {
|
|||
delete reader;
|
||||
});
|
||||
|
||||
it("parses the input", [&]() {
|
||||
AssertThat(string(ts_node_string(ts_document_root_node(doc))), Equals(
|
||||
"(DOCUMENT (object (string) (array (number) (number))))"));
|
||||
});
|
||||
|
||||
it("reads the entire input", [&]() {
|
||||
AssertThat(reader->strings_read, Equals(vector<string>({
|
||||
"{ \"key\": [1, 2] }"
|
||||
})));
|
||||
});
|
||||
|
||||
describe("modifying the end of the input", [&]() {
|
||||
before_each([&]() {
|
||||
size_t position(string("{ \"key\": [1, 2]").length());
|
||||
|
|
@ -111,6 +112,76 @@ describe("Document", [&]() {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("parsing", [&]() {
|
||||
TSNode *root;
|
||||
|
||||
describe("error handling", [&]() {
|
||||
before_each([&]() {
|
||||
ts_document_set_language(doc, ts_language_json());
|
||||
});
|
||||
|
||||
describe("when the error occurs at the beginning of a token", [&]() {
|
||||
it("computes the error node's size and position correctly", [&]() {
|
||||
ts_document_set_input_string(doc, " [123, @@@@@, true]");
|
||||
AssertThat(ts_node_string(ts_document_root_node(doc)), Equals(
|
||||
"(DOCUMENT (array (number) (ERROR '@') (true)))"));
|
||||
|
||||
root = ts_document_root_node(doc);
|
||||
TSNode *array = ts_node_child(root, 0);
|
||||
TSNode *error = ts_node_child(array, 1);
|
||||
|
||||
AssertThat(ts_node_name(error), Equals("error"));
|
||||
AssertThat(ts_node_pos(error), Equals(string(" [123,").length()))
|
||||
AssertThat(ts_node_size(error), Equals(string(" @@@@@").length()))
|
||||
|
||||
ts_node_release(error);
|
||||
ts_node_release(array);
|
||||
ts_node_release(error);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when the error occurs in the middle of a token", [&]() {
|
||||
it("computes the error node's size and position correctly", [&]() {
|
||||
ts_document_set_input_string(doc, " [123, total nonsense, true]");
|
||||
AssertThat(ts_node_string(ts_document_root_node(doc)), Equals(
|
||||
"(DOCUMENT (array (number) (ERROR 'o') (true)))"));
|
||||
|
||||
root = ts_document_root_node(doc);
|
||||
TSNode *array = ts_node_child(root, 0);
|
||||
TSNode *error = ts_node_child(array, 1);
|
||||
|
||||
AssertThat(ts_node_name(error), Equals("error"));
|
||||
AssertThat(ts_node_pos(error), Equals(string(" [123,").length()))
|
||||
AssertThat(ts_node_size(error), Equals(string(" total nonsense").length()))
|
||||
|
||||
ts_node_release(error);
|
||||
ts_node_release(array);
|
||||
ts_node_release(error);
|
||||
});
|
||||
});
|
||||
|
||||
describe("when the error occurs after one or more tokens", [&]() {
|
||||
it("computes the error node's size and position correctly", [&]() {
|
||||
ts_document_set_input_string(doc, " [123, true false, true]");
|
||||
AssertThat(ts_node_string(ts_document_root_node(doc)), Equals(
|
||||
"(DOCUMENT (array (number) (ERROR 'f') (true)))"));
|
||||
|
||||
root = ts_document_root_node(doc);
|
||||
TSNode *array = ts_node_child(root, 0);
|
||||
TSNode *error = ts_node_child(array, 1);
|
||||
|
||||
AssertThat(ts_node_name(error), Equals("error"));
|
||||
AssertThat(ts_node_pos(error), Equals(string(" [123,").length()))
|
||||
AssertThat(ts_node_size(error), Equals(string(" true false").length()))
|
||||
|
||||
ts_node_release(error);
|
||||
ts_node_release(array);
|
||||
ts_node_release(error);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
END_TEST
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue