Rename spec -> test
'Test' is a lot more straightforward of a name.
This commit is contained in:
parent
7d8daf573e
commit
6dc0ff359d
109 changed files with 44 additions and 44 deletions
181
test/integration/real_grammars.cc
Normal file
181
test/integration/real_grammars.cc
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
#include "test_helper.h"
|
||||
#include "runtime/alloc.h"
|
||||
#include "helpers/load_language.h"
|
||||
#include "helpers/read_test_entries.h"
|
||||
#include "helpers/spy_input.h"
|
||||
#include "helpers/stderr_logger.h"
|
||||
#include "helpers/point_helpers.h"
|
||||
#include "helpers/encoding_helpers.h"
|
||||
#include "helpers/record_alloc.h"
|
||||
#include "helpers/random_helpers.h"
|
||||
#include "helpers/scope_sequence.h"
|
||||
#include <set>
|
||||
|
||||
static void assert_consistent_sizes(TSNode node) {
|
||||
size_t child_count = ts_node_child_count(node);
|
||||
size_t start_byte = ts_node_start_byte(node);
|
||||
size_t end_byte = ts_node_end_byte(node);
|
||||
TSPoint start_point = ts_node_start_point(node);
|
||||
TSPoint end_point = ts_node_end_point(node);
|
||||
bool some_child_has_changes = false;
|
||||
|
||||
AssertThat(start_byte, !IsGreaterThan(end_byte));
|
||||
AssertThat(start_point, !IsGreaterThan(end_point));
|
||||
|
||||
size_t last_child_end_byte = start_byte;
|
||||
TSPoint last_child_end_point = start_point;
|
||||
|
||||
for (size_t i = 0; i < child_count; i++) {
|
||||
TSNode child = ts_node_child(node, i);
|
||||
size_t child_start_byte = ts_node_start_byte(child);
|
||||
TSPoint child_start_point = ts_node_start_point(child);
|
||||
|
||||
AssertThat(child_start_byte, !IsLessThan(last_child_end_byte));
|
||||
AssertThat(child_start_point, !IsLessThan(last_child_end_point));
|
||||
assert_consistent_sizes(child);
|
||||
if (ts_node_has_changes(child))
|
||||
some_child_has_changes = true;
|
||||
|
||||
last_child_end_byte = ts_node_end_byte(child);
|
||||
last_child_end_point = ts_node_end_point(child);
|
||||
}
|
||||
|
||||
if (child_count > 0) {
|
||||
AssertThat(end_byte, !IsLessThan(last_child_end_byte));
|
||||
AssertThat(end_point, !IsLessThan(last_child_end_point));
|
||||
}
|
||||
|
||||
if (some_child_has_changes) {
|
||||
AssertThat(ts_node_has_changes(node), IsTrue());
|
||||
}
|
||||
}
|
||||
|
||||
static void assert_correct_tree_size(TSDocument *document, string content) {
|
||||
TSNode root_node = ts_document_root_node(document);
|
||||
size_t expected_size = content.size();
|
||||
|
||||
// In the JSON grammar, the start rule (`_value`) is hidden, so the node
|
||||
// returned from `ts_document_root_node` (e.g. an `object` node), does not
|
||||
// actually point to the root of the tree. In this weird case, trailing
|
||||
// whitespace is not included in the root node's size.
|
||||
//
|
||||
// TODO: Fix this inconsistency. Maybe disallow the start rule being hidden?
|
||||
if (ts_document_language(document) == load_real_language("json") &&
|
||||
string(ts_node_type(root_node, document)) != "ERROR")
|
||||
expected_size = content.find_last_not_of("\n ") + 1;
|
||||
|
||||
AssertThat(ts_node_end_byte(root_node), Equals(expected_size));
|
||||
assert_consistent_sizes(root_node);
|
||||
}
|
||||
|
||||
START_TEST
|
||||
|
||||
vector<string> test_languages({
|
||||
"javascript",
|
||||
"json",
|
||||
"c",
|
||||
"cpp",
|
||||
"python",
|
||||
});
|
||||
|
||||
for (auto &language_name : test_languages) {
|
||||
describe(("the " + language_name + " language").c_str(), [&]() {
|
||||
TSDocument *document;
|
||||
|
||||
before_each([&]() {
|
||||
record_alloc::start();
|
||||
document = ts_document_new();
|
||||
ts_document_set_language(document, load_real_language(language_name));
|
||||
|
||||
// ts_document_set_logger(document, stderr_logger_new(true));
|
||||
// ts_document_print_debugging_graphs(document, true);
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
ts_document_free(document);
|
||||
AssertThat(record_alloc::outstanding_allocation_indices(), IsEmpty());
|
||||
});
|
||||
|
||||
for (auto &entry : read_real_language_corpus(language_name)) {
|
||||
SpyInput *input;
|
||||
|
||||
auto it_handles_edit_sequence = [&](string name, std::function<void()> edit_sequence){
|
||||
it(("parses " + entry.description + ": " + name).c_str(), [&]() {
|
||||
input = new SpyInput(entry.input, 3);
|
||||
ts_document_set_input(document, input->input());
|
||||
edit_sequence();
|
||||
|
||||
TSNode root_node = ts_document_root_node(document);
|
||||
const char *node_string = ts_node_string(root_node, document);
|
||||
string result(node_string);
|
||||
ts_free((void *)node_string);
|
||||
AssertThat(result, Equals(entry.tree_string));
|
||||
|
||||
assert_correct_tree_size(document, input->content);
|
||||
delete input;
|
||||
});
|
||||
};
|
||||
|
||||
it_handles_edit_sequence("initial parse", [&]() {
|
||||
ts_document_parse(document);
|
||||
});
|
||||
|
||||
std::set<std::pair<size_t, size_t>> deletions;
|
||||
std::set<std::pair<size_t, string>> insertions;
|
||||
|
||||
for (size_t i = 0; i < 60; i++) {
|
||||
size_t edit_position = random() % utf8_char_count(entry.input);
|
||||
size_t deletion_size = random() % (utf8_char_count(entry.input) - edit_position);
|
||||
string inserted_text = random_words(random() % 4 + 1);
|
||||
|
||||
if (insertions.insert({edit_position, inserted_text}).second) {
|
||||
string description = "\"" + inserted_text + "\" at " + to_string(edit_position);
|
||||
|
||||
it_handles_edit_sequence("repairing an insertion of " + description, [&]() {
|
||||
ts_document_edit(document, input->replace(edit_position, 0, inserted_text));
|
||||
ts_document_parse(document);
|
||||
assert_correct_tree_size(document, input->content);
|
||||
|
||||
ts_document_edit(document, input->undo());
|
||||
assert_correct_tree_size(document, input->content);
|
||||
|
||||
TSRange *ranges;
|
||||
uint32_t range_count;
|
||||
ScopeSequence old_scope_sequence = build_scope_sequence(document, input->content);
|
||||
ts_document_parse_and_get_changed_ranges(document, &ranges, &range_count);
|
||||
|
||||
ScopeSequence new_scope_sequence = build_scope_sequence(document, input->content);
|
||||
verify_changed_ranges(old_scope_sequence, new_scope_sequence,
|
||||
input->content, ranges, range_count);
|
||||
ts_free(ranges);
|
||||
});
|
||||
}
|
||||
|
||||
if (deletions.insert({edit_position, deletion_size}).second) {
|
||||
string desription = to_string(edit_position) + "-" + to_string(edit_position + deletion_size);
|
||||
|
||||
it_handles_edit_sequence("repairing a deletion of " + desription, [&]() {
|
||||
ts_document_edit(document, input->replace(edit_position, deletion_size, ""));
|
||||
ts_document_parse(document);
|
||||
assert_correct_tree_size(document, input->content);
|
||||
|
||||
ts_document_edit(document, input->undo());
|
||||
assert_correct_tree_size(document, input->content);
|
||||
|
||||
TSRange *ranges;
|
||||
uint32_t range_count;
|
||||
ScopeSequence old_scope_sequence = build_scope_sequence(document, input->content);
|
||||
ts_document_parse_and_get_changed_ranges(document, &ranges, &range_count);
|
||||
|
||||
ScopeSequence new_scope_sequence = build_scope_sequence(document, input->content);
|
||||
verify_changed_ranges(old_scope_sequence, new_scope_sequence,
|
||||
input->content, ranges, range_count);
|
||||
ts_free(ranges);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
END_TEST
|
||||
78
test/integration/test_grammars.cc
Normal file
78
test/integration/test_grammars.cc
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
#include "test_helper.h"
|
||||
#include "helpers/read_test_entries.h"
|
||||
#include "helpers/load_language.h"
|
||||
#include "helpers/stderr_logger.h"
|
||||
#include "helpers/file_helpers.h"
|
||||
#include "runtime/alloc.h"
|
||||
|
||||
START_TEST
|
||||
|
||||
string grammars_dir_path = "test/fixtures/test_grammars";
|
||||
vector<string> test_languages = list_directory(grammars_dir_path);
|
||||
|
||||
for (auto &language_name : test_languages) {
|
||||
if (language_name == "readme.md") continue;
|
||||
|
||||
describe(("test language: " + language_name).c_str(), [&]() {
|
||||
string directory_path = grammars_dir_path + "/" + language_name;
|
||||
string grammar_path = directory_path + "/grammar.json";
|
||||
string external_scanner_path = directory_path + "/scanner.c";
|
||||
string expected_error_path = directory_path + "/expected_error.txt";
|
||||
string corpus_path = directory_path + "/corpus.txt";
|
||||
|
||||
if (!file_exists(external_scanner_path)) {
|
||||
external_scanner_path = "";
|
||||
}
|
||||
|
||||
string grammar_json = read_file(grammar_path);
|
||||
TSCompileResult compile_result = ts_compile_grammar(grammar_json.c_str());
|
||||
|
||||
if (file_exists(expected_error_path)) {
|
||||
it("fails with the correct error message", [&]() {
|
||||
string expected_error = read_file(expected_error_path);
|
||||
AssertThat((void *)compile_result.error_message, !IsNull());
|
||||
AssertThat(compile_result.error_message, Equals(expected_error));
|
||||
});
|
||||
|
||||
return;
|
||||
} else {
|
||||
TSDocument *document = nullptr;
|
||||
const TSLanguage *language = nullptr;
|
||||
|
||||
before_each([&]() {
|
||||
if (!language) {
|
||||
language = load_test_language(
|
||||
language_name,
|
||||
compile_result,
|
||||
external_scanner_path
|
||||
);
|
||||
}
|
||||
|
||||
document = ts_document_new();
|
||||
ts_document_set_language(document, language);
|
||||
|
||||
// ts_document_set_logger(document, stderr_logger_new(true));
|
||||
// ts_document_print_debugging_graphs(document, true);
|
||||
});
|
||||
|
||||
after_each([&]() {
|
||||
if (document) ts_document_free(document);
|
||||
});
|
||||
|
||||
for (auto &entry : read_test_language_corpus(language_name)) {
|
||||
it(("parses " + entry.description).c_str(), [&]() {
|
||||
ts_document_set_input_string_with_length(document, entry.input.c_str(), entry.input.size());
|
||||
ts_document_parse(document);
|
||||
|
||||
TSNode root_node = ts_document_root_node(document);
|
||||
const char *node_string = ts_node_string(root_node, document);
|
||||
string result(node_string);
|
||||
ts_free((void *)node_string);
|
||||
AssertThat(result, Equals(entry.tree_string));
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
END_TEST
|
||||
Loading…
Add table
Add a link
Reference in a new issue