extract helper fn for testing edit sequences against the corpus

This commit is contained in:
Max Brunsfeld 2015-09-20 13:38:46 -07:00
parent 010a606b76
commit b7b082d9b1

View file

@ -1,4 +1,5 @@
#include "runtime/runtime_spec_helper.h"
#include <functional>
#include "runtime/helpers/read_test_entries.h"
#include "runtime/helpers/spy_input.h"
#include "runtime/helpers/log_debugger.h"
@ -11,6 +12,14 @@ extern "C" const TSLanguage *ts_language_c();
START_TEST
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()},
});
describe("Languages", [&]() {
TSDocument *doc;
@ -22,68 +31,54 @@ describe("Languages", [&]() {
ts_document_free(doc);
});
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()},
});
auto expect_the_correct_tree = [&](string tree_string) {
const char *node_string = ts_node_string(ts_document_root_node(doc), doc);
AssertThat(node_string, Equals(tree_string));
free((void *)node_string);
};
for (const auto &pair : languages) {
string language_name = pair.first;
const TSLanguage *language = pair.second;
describe(("The " + language_name + " parser").c_str(), [&]() {
describe(("The " + pair.first + " parser").c_str(), [&]() {
before_each([&]() {
ts_document_set_language(doc, language);
ts_document_set_language(doc, pair.second);
// ts_document_set_debugger(doc, log_debugger_make());
});
for (auto &entry : test_entries_for_language(language_name)) {
it(("parses " + entry.description).c_str(), [&]() {
ts_document_set_input_string(doc, entry.input.c_str());
for (auto &entry : test_entries_for_language(pair.first)) {
SpyInput *input;
auto expect_the_correct_tree = [&](string tree_string) {
const char *node_string = ts_node_string(ts_document_root_node(doc), doc);
AssertThat(node_string, Equals(tree_string));
free((void *)node_string);
};
auto it_handles_edit_sequence = [&](string name, std::function<void()> edit_sequence){
it(("handles " + entry.description + ": " + name).c_str(), [&]() {
input = new SpyInput(entry.input, 3);
ts_document_set_input(doc, input->input());
edit_sequence();
expect_the_correct_tree(entry.tree_string);
delete input;
});
};
it_handles_edit_sequence("initial parse", [&]() {
ts_document_parse(doc);
expect_the_correct_tree(entry.tree_string);
});
it(("handles random insertions in " + entry.description).c_str(), [&]() {
SpyInput input(entry.input, 3);
ts_document_set_input(doc, input.input());
it_handles_edit_sequence("one insertion, undo", [&]() {
ts_document_parse(doc);
string garbage("%^&*");
size_t position = entry.input.size() / 2;
ts_document_edit(doc, input.replace(position, 0, garbage));
ts_document_edit(doc, input->replace(entry.input.size() / 2, 0, "%^&*"));
ts_document_parse(doc);
ts_document_edit(doc, input.undo());
ts_document_edit(doc, input->undo());
ts_document_parse(doc);
expect_the_correct_tree(entry.tree_string);
});
it(("handles random deletions in " + entry.description).c_str(), [&]() {
SpyInput input(entry.input, 3);
ts_document_set_input(doc, input.input());
it_handles_edit_sequence("one deletion, undo", [&]() {
ts_document_parse(doc);
size_t position = entry.input.size() / 2;
ts_document_edit(doc, input.replace(position, 5, ""));
ts_document_edit(doc, input->replace(entry.input.size() / 2, 5, ""));
ts_document_parse(doc);
ts_document_edit(doc, input.undo());
ts_document_edit(doc, input->undo());
ts_document_parse(doc);
expect_the_correct_tree(entry.tree_string);
});
}
});