Rename SpyReader -> SpyInput

This commit is contained in:
Max Brunsfeld 2015-07-16 17:32:19 -07:00
parent 958ace429f
commit 32e79700bf
4 changed files with 34 additions and 34 deletions

View file

@ -1,4 +1,4 @@
#include "runtime/helpers/spy_reader.h"
#include "runtime/helpers/spy_input.h"
#include <string.h>
#include <algorithm>
#include "utf8proc.h"
@ -23,16 +23,16 @@ static long byte_for_character(const char *str, size_t len, size_t goal_characte
}
static const char * spy_read(void *data, size_t *bytes_read) {
SpyReader *reader = static_cast<SpyReader *>(data);
SpyInput *reader = static_cast<SpyInput *>(data);
return reader->read(bytes_read);
}
static int spy_seek(void *data, TSLength byte_offset) {
SpyReader *reader = static_cast<SpyReader *>(data);
SpyInput *reader = static_cast<SpyInput *>(data);
return reader->seek(byte_offset.bytes);
}
SpyReader::SpyReader(string content, size_t chars_per_chunk) :
SpyInput::SpyInput(string content, size_t chars_per_chunk) :
content(content),
chars_per_chunk(chars_per_chunk),
buffer_size(4 * chars_per_chunk),
@ -40,11 +40,11 @@ SpyReader::SpyReader(string content, size_t chars_per_chunk) :
byte_offset(0),
strings_read({ "" }) {}
SpyReader::~SpyReader() {
SpyInput::~SpyInput() {
delete buffer;
}
const char * SpyReader::read(size_t *bytes_read) {
const char * SpyInput::read(size_t *bytes_read) {
if (byte_offset > content.size()) {
*bytes_read = 0;
return "";
@ -72,13 +72,13 @@ const char * SpyReader::read(size_t *bytes_read) {
return buffer;
}
int SpyReader::seek(size_t pos) {
int SpyInput::seek(size_t pos) {
strings_read.push_back("");
byte_offset = pos;
return 0;
}
TSInput SpyReader::input() {
TSInput SpyInput::input() {
TSInput result;
result.data = this;
result.seek_fn = spy_seek;
@ -87,20 +87,20 @@ TSInput SpyReader::input() {
return result;
}
bool SpyReader::insert(size_t char_index, string text) {
bool SpyInput::insert(size_t char_index, string text) {
long pos = byte_for_character(content.data(), content.size(), char_index);
if (pos < 0) return false;
content.insert(pos, text);
return true;
}
bool SpyReader::erase(size_t char_index, size_t len) {
bool SpyInput::erase(size_t char_index, size_t len) {
long pos = byte_for_character(content.data(), content.size(), char_index);
if (pos < 0) return false;
content.erase(pos, len);
return true;
}
void SpyReader::clear() {
void SpyInput::clear() {
strings_read.clear();
}

View file

@ -1,14 +1,14 @@
#ifndef HELPERS_SPY_READER_H_
#define HELPERS_SPY_READER_H_
#ifndef HELPERS_spy_input_H_
#define HELPERS_spy_input_H_
#include <string>
#include <vector>
#include "tree_sitter/runtime.h"
class SpyReader {
class SpyInput {
public:
SpyReader(std::string content, size_t chars_per_chunk);
~SpyReader();
SpyInput(std::string content, size_t chars_per_chunk);
~SpyInput();
void clear();
TSInput input();
@ -25,4 +25,4 @@ class SpyReader {
std::vector<std::string> strings_read;
};
#endif // HELPERS_SPY_READER_H_
#endif // HELPERS_spy_input_H_

View file

@ -1,6 +1,6 @@
#include "runtime/runtime_spec_helper.h"
#include "runtime/helpers/read_test_entries.h"
#include "runtime/helpers/spy_reader.h"
#include "runtime/helpers/spy_input.h"
#include "runtime/helpers/log_debugger.h"
#include <set>
@ -48,7 +48,7 @@ describe("Languages", [&]() {
continue;
it(("handles random insertions in " + entry.description).c_str(), [&]() {
SpyReader reader(entry.input, 3);
SpyInput reader(entry.input, 3);
ts_document_set_input(doc, reader.input());
string garbage("%^&*");
@ -64,7 +64,7 @@ describe("Languages", [&]() {
});
it(("handles random deletions in " + entry.description).c_str(), [&]() {
SpyReader reader(entry.input, 3);
SpyInput reader(entry.input, 3);
ts_document_set_input(doc, reader.input());
size_t position = entry.input.size() / 2;

View file

@ -1,5 +1,5 @@
#include "runtime/runtime_spec_helper.h"
#include "runtime/helpers/spy_reader.h"
#include "runtime/helpers/spy_input.h"
#include "runtime/helpers/log_debugger.h"
extern "C" const TSLanguage * ts_language_json();
@ -10,33 +10,33 @@ START_TEST
describe("Parser", [&]() {
TSDocument *doc;
SpyReader *reader;
SpyInput *input;
TSNode *root;
size_t chunk_size;
before_each([&]() {
chunk_size = 3;
reader = nullptr;
input = nullptr;
doc = ts_document_make();
});
after_each([&]() {
ts_document_free(doc);
if (reader)
delete reader;
if (input)
delete input;
});
auto set_text = [&](const char *text) {
reader = new SpyReader(text, chunk_size);
ts_document_set_input(doc, reader->input());
input = new SpyInput(text, chunk_size);
ts_document_set_input(doc, input->input());
root = ts_document_root_node(doc);
AssertThat(ts_node_size(root).bytes + ts_node_pos(root).bytes, Equals(strlen(text)));
reader->clear();
input->clear();
};
auto insert_text = [&](size_t position, string text) {
size_t prev_size = ts_node_size(root).bytes + ts_node_pos(root).bytes;
AssertThat(reader->insert(position, text), IsTrue());
AssertThat(input->insert(position, text), IsTrue());
ts_document_edit(doc, { position, text.length(), 0 });
root = ts_document_root_node(doc);
@ -46,7 +46,7 @@ describe("Parser", [&]() {
auto delete_text = [&](size_t position, size_t length) {
size_t prev_size = ts_node_size(root).bytes + ts_node_pos(root).bytes;
AssertThat(reader->erase(position, length), IsTrue());
AssertThat(input->erase(position, length), IsTrue());
ts_document_edit(doc, { position, 0, length });
root = ts_document_root_node(doc);
@ -56,8 +56,8 @@ describe("Parser", [&]() {
auto replace_text = [&](size_t position, size_t length, string new_text) {
size_t prev_size = ts_node_size(root).bytes + ts_node_pos(root).bytes;
AssertThat(reader->erase(position, length), IsTrue());
AssertThat(reader->insert(position, new_text), IsTrue());
AssertThat(input->erase(position, length), IsTrue());
AssertThat(input->insert(position, new_text), IsTrue());
ts_document_edit(doc, { position, new_text.size(), length });
@ -241,7 +241,7 @@ describe("Parser", [&]() {
});
it("re-reads only the changed portion of the input", [&]() {
AssertThat(reader->strings_read, Equals(vector<string>({ " abc * 5)" })));
AssertThat(input->strings_read, Equals(vector<string>({ " abc * 5)" })));
});
});
@ -269,7 +269,7 @@ describe("Parser", [&]() {
});
it("re-reads only the changed portion of the input", [&]() {
AssertThat(reader->strings_read, Equals(vector<string>({ "123 + 5 ", " 4", " ^ (", "" })));
AssertThat(input->strings_read, Equals(vector<string>({ "123 + 5 ", " 4", " ^ (", "" })));
});
});