Fix assorted memory leaks in test code

This commit is contained in:
Max Brunsfeld 2016-02-05 12:23:54 -08:00
parent b1c8b74e9c
commit b80a330a74
7 changed files with 32 additions and 21 deletions

View file

@ -16,8 +16,8 @@ typedef enum {
} TSCompileErrorType;
typedef struct {
const char *code;
const char *error_message;
char *code;
char *error_message;
TSCompileErrorType error_type;
} TSCompileResult;

View file

@ -40,11 +40,13 @@ static std::string run_cmd(const char *cmd, const char *args[]) {
const TSLanguage *load_language(const string &name, const TSCompileResult &compile_result) {
if (compile_result.error_type != TSCompileErrorTypeNone) {
AssertThat(string(compile_result.error_message), IsEmpty());
Assert::Failure(string("Compilation failed ") + compile_result.error_message);
return nullptr;
}
return load_language(name, compile_result.code);
const TSLanguage *language = load_language(name, compile_result.code);
free(compile_result.code);
return language;
}
const TSLanguage *load_language(const string &name, const string &code) {

View file

@ -1,4 +1,5 @@
#include "spec_helper.h"
#include "runtime/alloc.h"
#include "helpers/load_language.h"
START_TEST
@ -14,6 +15,13 @@ describe("compile_grammar", []() {
ts_document_free(document);
});
auto assert_root_node = [&](const string &expected_string) {
TSNode root_node = ts_document_root_node(document);
char *node_string = ts_node_string(root_node, document);
AssertThat(node_string, Equals(expected_string));
ts_free(node_string);
};
describe("when the grammar's start symbol is a token", [&]() {
it("parses the token", [&]() {
TSCompileResult result = ts_compile_grammar(R"JSON(
@ -29,8 +37,7 @@ describe("compile_grammar", []() {
ts_document_set_input_string(document, "the-value");
ts_document_parse(document);
TSNode root_node = ts_document_root_node(document);
AssertThat(ts_node_string(root_node, document), Equals("(first_rule)"));
assert_root_node("(first_rule)");
});
});
@ -49,8 +56,7 @@ describe("compile_grammar", []() {
ts_document_set_input_string(document, "");
ts_document_parse(document);
TSNode root_node = ts_document_root_node(document);
AssertThat(ts_node_string(root_node, document), Equals("(first_rule)"));
assert_root_node("(first_rule)");
});
});
@ -77,18 +83,15 @@ describe("compile_grammar", []() {
ts_document_set_input_string(document, "1234");
ts_document_parse(document);
TSNode root_node = ts_document_root_node(document);
AssertThat(ts_node_string(root_node, document), Equals("(first_rule)"));
assert_root_node("(first_rule)");
ts_document_set_input_string(document, "\n");
ts_document_parse(document);
root_node = ts_document_root_node(document);
AssertThat(ts_node_string(root_node, document), Equals("(first_rule)"));
assert_root_node("(first_rule)");
ts_document_set_input_string(document, "'hello'");
ts_document_parse(document);
root_node = ts_document_root_node(document);
AssertThat(ts_node_string(root_node, document), Equals("(first_rule)"));
assert_root_node("(first_rule)");
});
});
@ -177,13 +180,12 @@ describe("compile_grammar", []() {
ts_document_set_input_string(document, "a + b * c");
ts_document_parse(document);
TSNode root_node = ts_document_root_node(document);
AssertThat(ts_node_string(root_node, document), Equals(
assert_root_node(
"(expression (sum "
"(expression (variable)) "
"(expression (product "
"(expression (variable)) "
"(expression (variable))))))"));
"(expression (variable))))))");
});
});
});

View file

@ -151,6 +151,10 @@ describe("Document", [&]() {
ts_document_set_input_string(doc, "[1, 2]");
});
after_each([&]() {
delete debugger;
});
it("calls the debugger with a message for each lex action", [&]() {
ts_document_set_debugger(doc, debugger->debugger());
ts_document_parse(doc);

View file

@ -64,6 +64,8 @@ describe("Tree", []() {
AssertThat(error_tree->fragile_left, IsTrue());
AssertThat(error_tree->fragile_right, IsTrue());
ts_tree_release(error_tree);
});
});

View file

@ -18,7 +18,7 @@ using std::make_tuple;
extern "C" TSCompileResult ts_compile_grammar(const char *input) {
ParseGrammarResult parse_result = parse_grammar(string(input));
if (!parse_result.error_message.empty()) {
return { "", strdup(parse_result.error_message.c_str()),
return { nullptr, strdup(parse_result.error_message.c_str()),
TSCompileErrorTypeInvalidGrammar };
}
@ -28,7 +28,7 @@ extern "C" TSCompileResult ts_compile_grammar(const char *input) {
const LexicalGrammar &lexical_grammar = get<1>(prepare_grammar_result);
CompileError error = get<2>(prepare_grammar_result);
if (error.type) {
return { "", strdup(error.message.c_str()), error.type };
return { nullptr, strdup(error.message.c_str()), error.type };
}
auto table_build_result =
@ -37,13 +37,13 @@ extern "C" TSCompileResult ts_compile_grammar(const char *input) {
const LexTable &lex_table = get<1>(table_build_result);
error = get<2>(table_build_result);
if (error.type) {
return { "", strdup(error.message.c_str()), error.type };
return { nullptr, strdup(error.message.c_str()), error.type };
}
string code = generate_code::c_code(parse_result.name, parse_table, lex_table,
syntax_grammar, lexical_grammar);
return { strdup(code.c_str()), "", TSCompileErrorTypeNone };
return { strdup(code.c_str()), nullptr, TSCompileErrorTypeNone };
}
pair<string, const CompileError> compile(const Grammar &grammar,

View file

@ -313,6 +313,7 @@ ParseGrammarResult parse_grammar(const string &input) {
}
}
json_value_free(grammar_json);
return { name, grammar, "" };
error: