2014-06-23 18:50:03 -07:00
|
|
|
#include "runtime/runtime_spec_helper.h"
|
2015-09-20 13:38:46 -07:00
|
|
|
#include <functional>
|
2015-12-09 12:35:11 -08:00
|
|
|
#include <set>
|
|
|
|
|
#include <utility>
|
2015-10-24 13:45:42 -07:00
|
|
|
#include "runtime/length.h"
|
2014-06-23 18:50:03 -07:00
|
|
|
#include "runtime/helpers/read_test_entries.h"
|
2015-07-16 17:32:19 -07:00
|
|
|
#include "runtime/helpers/spy_input.h"
|
2014-10-22 18:44:52 -07:00
|
|
|
#include "runtime/helpers/log_debugger.h"
|
2015-12-02 17:29:10 -05:00
|
|
|
#include "runtime/helpers/point_helpers.h"
|
2014-03-20 18:15:38 -07:00
|
|
|
|
2014-07-31 13:04:46 -07:00
|
|
|
extern "C" const TSLanguage *ts_language_javascript();
|
|
|
|
|
extern "C" const TSLanguage *ts_language_json();
|
|
|
|
|
extern "C" const TSLanguage *ts_language_arithmetic();
|
|
|
|
|
extern "C" const TSLanguage *ts_language_golang();
|
2015-06-28 16:22:31 -05:00
|
|
|
extern "C" const TSLanguage *ts_language_c();
|
2015-10-13 11:28:53 -07:00
|
|
|
extern "C" const TSLanguage *ts_language_cpp();
|
2014-03-20 18:15:38 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
map<string, const TSLanguage *> languages({
|
|
|
|
|
{"json", ts_language_json()},
|
|
|
|
|
{"arithmetic", ts_language_arithmetic()},
|
|
|
|
|
{"javascript", ts_language_javascript()},
|
|
|
|
|
{"golang", ts_language_golang()},
|
|
|
|
|
{"c", ts_language_c()},
|
|
|
|
|
{"cpp", ts_language_cpp()},
|
|
|
|
|
});
|
|
|
|
|
|
2015-10-24 13:45:42 -07:00
|
|
|
void expect_the_correct_tree(TSNode node, TSDocument *doc, string tree_string) {
|
|
|
|
|
const char *node_string = ts_node_string(node, doc);
|
|
|
|
|
AssertThat(node_string, Equals(tree_string));
|
|
|
|
|
free((void *)node_string);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void expect_a_consistent_tree(TSNode node, TSDocument *doc) {
|
|
|
|
|
size_t child_count = ts_node_child_count(node);
|
2015-12-09 12:35:11 -08:00
|
|
|
size_t start_char = ts_node_start_char(node);
|
|
|
|
|
size_t end_char = ts_node_end_char(node);
|
2015-12-02 17:29:10 -05:00
|
|
|
TSPoint start_point = ts_node_start_point(node);
|
|
|
|
|
TSPoint end_point = ts_node_end_point(node);
|
2015-10-24 13:45:42 -07:00
|
|
|
bool has_changes = ts_node_has_changes(node);
|
|
|
|
|
bool some_child_has_changes = false;
|
|
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
AssertThat(start_char, !IsGreaterThan(end_char));
|
|
|
|
|
AssertThat(start_point, !IsGreaterThan(end_point));
|
|
|
|
|
|
|
|
|
|
size_t last_child_end_char = 0;
|
|
|
|
|
TSPoint last_child_end_point = {0, 0};
|
|
|
|
|
|
2015-10-24 13:45:42 -07:00
|
|
|
for (size_t i = 0; i < child_count; i++) {
|
|
|
|
|
TSNode child = ts_node_child(node, i);
|
2015-12-09 12:35:11 -08:00
|
|
|
size_t child_start_char = ts_node_start_char(child);
|
|
|
|
|
size_t child_end_char = ts_node_end_char(child);
|
2015-12-02 17:29:10 -05:00
|
|
|
TSPoint child_start_point = ts_node_start_point(child);
|
|
|
|
|
TSPoint child_end_point = ts_node_end_point(child);
|
|
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
if (i > 0) {
|
|
|
|
|
AssertThat(child_start_char, !IsLessThan(last_child_end_char));
|
|
|
|
|
AssertThat(child_start_point, !IsLessThan(last_child_end_point));
|
|
|
|
|
last_child_end_char = child_end_char;
|
|
|
|
|
last_child_end_point = child_end_point;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
AssertThat(child_start_char, !IsLessThan(start_char));
|
|
|
|
|
AssertThat(child_end_char, !IsGreaterThan(end_char));
|
|
|
|
|
AssertThat(child_start_point, !IsLessThan(start_point));
|
|
|
|
|
AssertThat(child_end_point, !IsGreaterThan(end_point));
|
|
|
|
|
|
|
|
|
|
expect_a_consistent_tree(child, doc);
|
2015-12-02 17:29:10 -05:00
|
|
|
|
2015-10-24 13:45:42 -07:00
|
|
|
if (ts_node_has_changes(child))
|
|
|
|
|
some_child_has_changes = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (child_count > 0)
|
|
|
|
|
AssertThat(has_changes, Equals(some_child_has_changes));
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
string random_string(char min, char max) {
|
|
|
|
|
string result;
|
2015-12-10 21:23:23 -08:00
|
|
|
size_t length = random() % 12;
|
2015-12-09 12:35:11 -08:00
|
|
|
for (size_t i = 0; i < length; i++) {
|
2015-12-10 21:23:23 -08:00
|
|
|
char inserted_char = min + (random() % (max - min));
|
2015-12-09 12:35:11 -08:00
|
|
|
result += inserted_char;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2014-03-20 18:15:38 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
string random_char(string characters) {
|
2015-12-10 21:23:23 -08:00
|
|
|
size_t index = random() % characters.size();
|
2015-12-09 12:35:11 -08:00
|
|
|
return string() + characters[index];
|
|
|
|
|
}
|
2015-09-20 13:38:46 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
string random_words(size_t count) {
|
|
|
|
|
string result;
|
2015-12-10 16:47:55 -08:00
|
|
|
bool just_inserted_word = false;
|
2015-12-09 12:35:11 -08:00
|
|
|
for (size_t i = 0; i < count; i++) {
|
2015-12-10 21:23:23 -08:00
|
|
|
if (random() % 10 < 6) {
|
2015-12-09 12:35:11 -08:00
|
|
|
result += random_char("!(){}[]<>+-=");
|
2015-12-10 16:47:55 -08:00
|
|
|
} else {
|
|
|
|
|
if (just_inserted_word)
|
|
|
|
|
result += " ";
|
2015-12-09 12:35:11 -08:00
|
|
|
result += random_string('a', 'z');
|
2015-12-10 16:47:55 -08:00
|
|
|
just_inserted_word = true;
|
|
|
|
|
}
|
2015-12-09 12:35:11 -08:00
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
2014-08-06 13:00:35 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
START_TEST
|
2014-08-06 13:00:35 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
describe("Languages", [&]() {
|
2015-09-18 23:36:47 -07:00
|
|
|
for (const auto &pair : languages) {
|
2015-09-20 13:38:46 -07:00
|
|
|
describe(("The " + pair.first + " parser").c_str(), [&]() {
|
2015-12-09 12:35:11 -08:00
|
|
|
TSDocument *doc;
|
|
|
|
|
|
2014-08-06 13:00:35 -07:00
|
|
|
before_each([&]() {
|
2015-12-09 12:35:11 -08:00
|
|
|
doc = ts_document_make();
|
2015-09-20 13:38:46 -07:00
|
|
|
ts_document_set_language(doc, pair.second);
|
2015-12-10 21:01:27 -08:00
|
|
|
// ts_document_set_debugger(doc, log_debugger_make(true));
|
2015-12-09 12:35:11 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
after_each([&]() {
|
|
|
|
|
ts_document_free(doc);
|
2014-08-06 13:00:35 -07:00
|
|
|
});
|
|
|
|
|
|
2015-09-20 13:38:46 -07:00
|
|
|
for (auto &entry : test_entries_for_language(pair.first)) {
|
|
|
|
|
SpyInput *input;
|
|
|
|
|
|
|
|
|
|
auto it_handles_edit_sequence = [&](string name, std::function<void()> edit_sequence){
|
2015-09-22 21:19:19 -07:00
|
|
|
it(("parses " + entry.description + ": " + name).c_str(), [&]() {
|
2015-09-20 13:38:46 -07:00
|
|
|
input = new SpyInput(entry.input, 3);
|
|
|
|
|
ts_document_set_input(doc, input->input());
|
|
|
|
|
edit_sequence();
|
2015-10-24 13:45:42 -07:00
|
|
|
TSNode root_node = ts_document_root_node(doc);
|
|
|
|
|
expect_the_correct_tree(root_node, doc, entry.tree_string);
|
|
|
|
|
expect_a_consistent_tree(root_node, doc);
|
2015-09-20 13:38:46 -07:00
|
|
|
delete input;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
it_handles_edit_sequence("initial parse", [&]() {
|
2015-09-18 23:20:06 -07:00
|
|
|
ts_document_parse(doc);
|
2014-10-22 18:44:52 -07:00
|
|
|
});
|
|
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
std::set<std::pair<size_t, size_t>> deletions;
|
|
|
|
|
std::set<std::pair<size_t, string>> insertions;
|
2015-09-22 21:19:19 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
for (size_t i = 0; i < 50; i++) {
|
2015-12-18 13:35:02 -08:00
|
|
|
size_t edit_position = random() % SpyInput::char_count(entry.input);
|
|
|
|
|
size_t deletion_size = random() % (SpyInput::char_count(entry.input) - edit_position);
|
2015-12-10 21:23:23 -08:00
|
|
|
string inserted_text = random_words(random() % 4 + 1);
|
2015-09-22 21:19:19 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
if (insertions.insert({edit_position, inserted_text}).second) {
|
2015-12-10 16:47:55 -08:00
|
|
|
string description = "\"" + inserted_text + "\" at " + to_string(edit_position);
|
2014-10-22 18:44:52 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
it_handles_edit_sequence("repairing an insertion of " + description, [&]() {
|
|
|
|
|
ts_document_edit(doc, input->replace(edit_position, 0, inserted_text));
|
|
|
|
|
ts_document_parse(doc);
|
2014-10-22 18:44:52 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
ts_document_edit(doc, input->undo());
|
|
|
|
|
ts_document_parse(doc);
|
|
|
|
|
});
|
2015-02-21 01:28:33 -08:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
it_handles_edit_sequence("performing and repairing an insertion of " + description, [&]() {
|
|
|
|
|
ts_document_parse(doc);
|
2015-02-21 01:28:33 -08:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
ts_document_edit(doc, input->replace(edit_position, 0, inserted_text));
|
|
|
|
|
ts_document_parse(doc);
|
2015-02-21 01:28:33 -08:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
ts_document_edit(doc, input->undo());
|
|
|
|
|
ts_document_parse(doc);
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-09-22 21:19:19 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
if (deletions.insert({edit_position, deletion_size}).second) {
|
|
|
|
|
string desription = to_string(edit_position) + "-" + to_string(edit_position + deletion_size);
|
2015-09-22 21:19:19 -07:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
it_handles_edit_sequence("repairing a deletion of " + desription, [&]() {
|
|
|
|
|
ts_document_edit(doc, input->replace(edit_position, deletion_size, ""));
|
|
|
|
|
ts_document_parse(doc);
|
2015-12-02 11:24:13 -08:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
ts_document_edit(doc, input->undo());
|
|
|
|
|
ts_document_parse(doc);
|
|
|
|
|
});
|
2015-12-02 11:24:13 -08:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
it_handles_edit_sequence("performing and repairing a deletion of " + desription, [&]() {
|
|
|
|
|
ts_document_parse(doc);
|
2015-12-02 11:24:13 -08:00
|
|
|
|
2015-12-09 12:35:11 -08:00
|
|
|
ts_document_edit(doc, input->replace(edit_position, deletion_size, ""));
|
|
|
|
|
ts_document_parse(doc);
|
|
|
|
|
|
|
|
|
|
ts_document_edit(doc, input->undo());
|
|
|
|
|
ts_document_parse(doc);
|
|
|
|
|
});
|
|
|
|
|
}
|
2015-12-02 11:24:13 -08:00
|
|
|
}
|
2014-08-06 13:00:35 -07:00
|
|
|
}
|
|
|
|
|
});
|
2015-09-18 23:36:47 -07:00
|
|
|
}
|
2014-03-20 18:15:38 -07:00
|
|
|
});
|
|
|
|
|
|
2014-06-23 18:50:03 -07:00
|
|
|
END_TEST
|