Allow callbacks to be specified for debug output

This commit is contained in:
Max Brunsfeld 2014-10-13 01:02:12 -07:00
parent 71cc7a2dc2
commit c594208ab8
9 changed files with 226 additions and 69 deletions

View file

@ -1,4 +1,5 @@
#include "runtime/runtime_spec_helper.h"
#include "runtime/helpers/spy_debugger.h"
extern "C" const TSLanguage * ts_language_json();
@ -60,6 +61,73 @@ describe("Document", [&]() {
});
});
});
describe("debugging", [&]() {
SpyDebugger *debugger;
before_each([&]() {
ts_document_set_language(doc, ts_language_json());
debugger = new SpyDebugger();
});
describe("debug_lex(TSDebugger)", [&]() {
before_each([&]() {
ts_document_debug_lex(doc, debugger->debugger());
});
it("calls the debugger with a message for each lex action", [&]() {
ts_document_set_input_string(doc, "[1, 2]");
AssertThat(debugger->messages, Contains("lookahead char:'1'"));
AssertThat(debugger->messages, Contains("accept_token sym:number"));
AssertThat(debugger->messages, Contains("advance state:1"));
});
describe("disabling debugging", [&]() {
before_each([&]() {
ts_document_debug_lex(doc, {});
});
it("does not call the debugger any more", [&]() {
ts_document_set_input_string(doc, "[1, 2]");
AssertThat(debugger->messages, IsEmpty());
});
it("releases the old debugger", [&]() {
AssertThat(debugger->release_call_count, Equals<size_t>(1));
});
});
});
describe("debug_parse(TSDebugger)", [&]() {
before_each([&]() {
ts_document_debug_parse(doc, debugger->debugger());
});
it("calls the debugger with a message for each parse action", [&]() {
ts_document_set_input_string(doc, "[1, 2]");
AssertThat(debugger->messages, Contains("lex sym:number"));
AssertThat(debugger->messages, Contains("shift state:1"));
AssertThat(debugger->messages, Contains("reduce sym:value count:1"));
});
describe("disabling debugging", [&]() {
before_each([&]() {
ts_document_debug_parse(doc, {});
});
it("does not call the debugger any more", [&]() {
ts_document_set_input_string(doc, "[1, 2]");
AssertThat(debugger->messages, IsEmpty());
});
it("releases the old debugger", [&]() {
AssertThat(debugger->release_call_count, Equals<size_t>(1));
});
});
});
});
});
END_TEST

View file

@ -0,0 +1,28 @@
#include "runtime/helpers/spy_debugger.h"
#include <string>
#include <vector>
using std::string;
using std::vector;
static void spy_debug(void *data, const char *msg) {
SpyDebugger *debugger = static_cast<SpyDebugger *>(data);
debugger->messages.push_back(msg);
}
static void spy_release(void *data) {
SpyDebugger *debugger = static_cast<SpyDebugger *>(data);
debugger->release_call_count++;
}
TSDebugger SpyDebugger::debugger() {
TSDebugger result;
result.data = (void *)this;
result.debug_fn = spy_debug;
result.release_fn = spy_release;
return result;
}
void SpyDebugger::clear() {
messages.clear();
}

View file

@ -0,0 +1,18 @@
#ifndef HELPERS_SPY_DEBUGGER_H_
#define HELPERS_SPY_DEBUGGER_H_
#include <string>
#include <vector>
#include "tree_sitter/runtime.h"
class SpyDebugger {
public:
void clear();
TSDebugger debugger();
std::vector<std::string> messages;
size_t release_call_count;
};
#endif // HELPERS_SPY_DEBUGGER_H_