Make clearer assertions about SpyInput's read strings
This commit is contained in:
parent
ca943f09a4
commit
a15e974150
4 changed files with 31 additions and 10 deletions
|
|
@ -6,6 +6,7 @@
|
|||
|
||||
using std::pair;
|
||||
using std::string;
|
||||
using std::vector;
|
||||
|
||||
static const size_t UTF8_MAX_CHAR_SIZE = 4;
|
||||
|
||||
|
|
@ -16,12 +17,25 @@ SpyInput::SpyInput(string content, size_t chars_per_chunk) :
|
|||
byte_offset(0),
|
||||
content(content),
|
||||
encoding(TSInputEncodingUTF8),
|
||||
strings_read({""}) {}
|
||||
ranges_read({}) {}
|
||||
|
||||
SpyInput::~SpyInput() {
|
||||
delete[] buffer;
|
||||
}
|
||||
|
||||
static void add_byte_range(vector<pair<uint32_t, uint32_t>> *ranges,
|
||||
uint32_t start, uint32_t count) {
|
||||
uint32_t end = start + count;
|
||||
for (auto &range : *ranges) {
|
||||
if (range.first <= start && start <= range.second) {
|
||||
if (start < range.first) range.first = start;
|
||||
if (end > range.second) range.second = end;
|
||||
return;
|
||||
}
|
||||
}
|
||||
ranges->push_back({start, end});
|
||||
}
|
||||
|
||||
const char * SpyInput::read(void *payload, uint32_t *bytes_read) {
|
||||
auto spy = static_cast<SpyInput *>(payload);
|
||||
|
||||
|
|
@ -36,7 +50,7 @@ const char * SpyInput::read(void *payload, uint32_t *bytes_read) {
|
|||
|
||||
string result = spy->content.substr(spy->byte_offset, byte_count);
|
||||
*bytes_read = byte_count;
|
||||
spy->strings_read.back() += result;
|
||||
add_byte_range(&spy->ranges_read, spy->byte_offset, byte_count);
|
||||
spy->byte_offset += byte_count;
|
||||
|
||||
/*
|
||||
|
|
@ -54,12 +68,18 @@ const char * SpyInput::read(void *payload, uint32_t *bytes_read) {
|
|||
|
||||
int SpyInput::seek(void *payload, uint32_t character, uint32_t byte) {
|
||||
auto spy = static_cast<SpyInput *>(payload);
|
||||
if (spy->strings_read.size() == 0 || spy->strings_read.back().size() > 0)
|
||||
spy->strings_read.push_back("");
|
||||
spy->byte_offset = byte;
|
||||
return 0;
|
||||
}
|
||||
|
||||
vector<string> SpyInput::strings_read() const {
|
||||
vector<string> result;
|
||||
for (auto &range : ranges_read) {
|
||||
result.push_back(content.substr(range.first, range.second - range.first));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
TSInput SpyInput::input() {
|
||||
TSInput result;
|
||||
result.payload = this;
|
||||
|
|
@ -129,5 +149,5 @@ pair<string, TSPoint> SpyInput::swap_substr(size_t start_byte, size_t bytes_remo
|
|||
}
|
||||
|
||||
void SpyInput::clear() {
|
||||
strings_read.clear();
|
||||
ranges_read.clear();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -30,10 +30,11 @@ class SpyInput {
|
|||
void clear();
|
||||
TSInputEdit replace(size_t start_char, size_t chars_removed, std::string text);
|
||||
TSInputEdit undo();
|
||||
std::vector<std::string> strings_read() const;
|
||||
|
||||
std::string content;
|
||||
TSInputEncoding encoding;
|
||||
std::vector<std::string> strings_read;
|
||||
std::vector<std::pair<uint32_t, uint32_t>> ranges_read;
|
||||
};
|
||||
|
||||
#endif // HELPERS_SPY_INPUT_H_
|
||||
|
|
|
|||
|
|
@ -94,7 +94,7 @@ describe("Document", [&]() {
|
|||
ts_document_set_input(document, spy_input->input());
|
||||
AssertThat(ts_document_root_node(document), Equals<TSNode>(root));
|
||||
AssertThat(ts_node_has_changes(root), IsFalse());
|
||||
AssertThat(spy_input->strings_read, Equals(vector<string>({ "" })));
|
||||
AssertThat(spy_input->strings_read(), IsEmpty());
|
||||
});
|
||||
|
||||
it("reads text from the new input for future parses", [&]() {
|
||||
|
|
@ -113,7 +113,7 @@ describe("Document", [&]() {
|
|||
assert_node_string_equals(
|
||||
new_root,
|
||||
"(object (pair (string) (array (null) (number))))");
|
||||
AssertThat(spy_input->strings_read, Equals(vector<string>({" [null, 2" })));
|
||||
AssertThat(spy_input->strings_read(), Equals(vector<string>({" [null, 2" })));
|
||||
});
|
||||
|
||||
it("allows setting input string with length", [&]() {
|
||||
|
|
|
|||
|
|
@ -254,7 +254,7 @@ describe("Parser", [&]() {
|
|||
"(identifier) "
|
||||
"(math_op (number) (member_access (identifier) (identifier))))))");
|
||||
|
||||
AssertThat(input->strings_read, Equals(vector<string>({ " + abc.d)" })));
|
||||
AssertThat(input->strings_read(), Equals(vector<string>({ " abc.d);" })));
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -279,7 +279,7 @@ describe("Parser", [&]() {
|
|||
"(number) "
|
||||
"(math_op (number) (math_op (number) (identifier)))))))");
|
||||
|
||||
AssertThat(input->strings_read, Equals(vector<string>({ "123 || 5 +" })));
|
||||
AssertThat(input->strings_read(), Equals(vector<string>({"123 || 5 ", ";"})));
|
||||
});
|
||||
});
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue