Change BufferView and BufferLineWalker to return iterator objects for iteration instead of using lambdas for callbacks.

This commit is contained in:
josh committed 2017-09-03 17:05:01 -04:00
1 parent b89f348602
commit 4b3e1ea7e7
6 files changed
+353 -309

No files matched your search

+106 -128
View File
@@ -16,165 +16,143 @@ class MyCharacterWidthDeterminer : public CharacterWidthDeterminer
}
} cld;
TEST(BufferLineWalkerTest, calls_callback_once_for_empty_line)
TEST(BufferLineWalkerTest, iterats_once_for_empty_line)
{
static const uint8_t data[] = "\n";
std::shared_ptr<Buffer> b = std::make_shared<Buffer>(data, 1u);
BufferLineWalker blw(b, cld);
blw.set_width(20);
std::shared_ptr<Buffer::Iterator> start_of_line = b->add_iterator();
int times_called = 0;
blw.walk_line(*start_of_line, [&times_called](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & it) -> bool {
times_called++;
EXPECT_EQ(0, row_offset);
EXPECT_EQ(0, screen_column);
EXPECT_EQ(0, virtual_column);
EXPECT_EQ(0, character_width);
return true;
});
EXPECT_EQ(1, times_called);
BufferLineWalker blw(b, start_of_line, 20, cld);
int iter_count = 0;
for (; blw.is_valid(); blw++)
{
EXPECT_EQ(0, blw.row_offset());
EXPECT_EQ(0, blw.screen_column());
EXPECT_EQ(0, blw.virtual_column());
EXPECT_EQ(0, blw.character_width());
EXPECT_TRUE(blw.is_eol());
iter_count++;
}
EXPECT_EQ(1, iter_count);
}
TEST(BufferLineWalkerTest, walks_correctly_with_tabs_and_no_line_wrapping)
{
static const char data[] = "\t1\t12\t123\t1234\t!";
std::shared_ptr<Buffer> b = std::make_shared<Buffer>((const uint8_t *)data, strlen(data));
BufferLineWalker blw(b, cld);
blw.set_width(100);
std::shared_ptr<Buffer::Iterator> start_of_line = b->add_iterator();
int times_called = 0;
blw.walk_line(*start_of_line, [&times_called](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & it) -> bool {
EXPECT_EQ(0, row_offset);
static int expected_screen_columns[] = {
0, 4, 5, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 24, 25
};
static int expected_character_widths[] = {
4, 1, 3, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 0
};
EXPECT_LT(times_called, (int)(sizeof(expected_screen_columns)/sizeof(expected_screen_columns[0])));
EXPECT_EQ(expected_screen_columns[times_called], screen_column);
EXPECT_EQ(expected_screen_columns[times_called], virtual_column);
EXPECT_EQ(expected_character_widths[times_called], character_width);
times_called++;
return true;
});
EXPECT_EQ((int)strlen(data) + 1, times_called);
BufferLineWalker blw(b, start_of_line, 100, cld);
static int expected_screen_columns[] = {
0, 4, 5, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 24, 25
};
static int expected_character_widths[] = {
4, 1, 3, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 0
};
int iter_count = 0;
for (; blw.is_valid(); blw++)
{
EXPECT_EQ(0, blw.row_offset());
EXPECT_LT(iter_count, (int)(sizeof(expected_screen_columns)/sizeof(expected_screen_columns[0])));
EXPECT_EQ(expected_screen_columns[iter_count], blw.screen_column());
EXPECT_EQ(expected_screen_columns[iter_count], blw.virtual_column());
EXPECT_EQ(expected_character_widths[iter_count], blw.character_width());
iter_count++;
}
EXPECT_EQ((int)strlen(data) + 1, iter_count);
}
TEST(BufferLineWalkerTest, walks_correctly_with_tabs_and_line_wrapping)
{
static const char data[] = "\t1\t12\t123\t1234\t!";
std::shared_ptr<Buffer> b = std::make_shared<Buffer>((const uint8_t *)data, strlen(data));
BufferLineWalker blw(b, cld);
blw.set_width(5);
std::shared_ptr<Buffer::Iterator> start_of_line = b->add_iterator();
int times_called = 0;
blw.walk_line(*start_of_line, [&times_called](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & it) -> bool {
static int expected_screen_columns[] = {
0, 4, 0, 3, 4, 0, 2, 3, 4, 0, 1, 2, 3, 4, 0, 4, 0
};
static int expected_row_offsets[] = {
0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 5
};
static int expected_virtual_columns[] = {
0, 4, 5, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 24, 25
};
static int expected_character_widths[] = {
4, 1, 3, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 0
};
EXPECT_LT(times_called, (int)(sizeof(expected_screen_columns)/sizeof(expected_screen_columns[0])));
EXPECT_EQ(expected_row_offsets[times_called], row_offset);
EXPECT_EQ(expected_screen_columns[times_called], screen_column);
EXPECT_EQ(expected_virtual_columns[times_called], virtual_column);
EXPECT_EQ(expected_character_widths[times_called], character_width);
times_called++;
return true;
});
EXPECT_EQ((int)strlen(data) + 1, times_called);
BufferLineWalker blw(b, start_of_line, 5, cld);
static int expected_screen_columns[] = {
0, 4, 0, 3, 4, 0, 2, 3, 4, 0, 1, 2, 3, 4, 0, 4, 0
};
static int expected_row_offsets[] = {
0, 0, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 5
};
static int expected_virtual_columns[] = {
0, 4, 5, 8, 9, 10, 12, 13, 14, 15, 16, 17, 18, 19, 20, 24, 25
};
static int expected_character_widths[] = {
4, 1, 3, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 4, 1, 0
};
int iter_count = 0;
for (; blw.is_valid(); blw++)
{
EXPECT_LT(iter_count, (int)(sizeof(expected_screen_columns)/sizeof(expected_screen_columns[0])));
EXPECT_EQ(expected_row_offsets[iter_count], blw.row_offset());
EXPECT_EQ(expected_screen_columns[iter_count], blw.screen_column());
EXPECT_EQ(expected_virtual_columns[iter_count], blw.virtual_column());
EXPECT_EQ(expected_character_widths[iter_count], blw.character_width());
iter_count++;
}
EXPECT_EQ((int)strlen(data) + 1, iter_count);
}
TEST(BufferLineWalkerTest, walks_correctly_with_multi_column_characters_and_line_wrapping)
{
static const char data[] = "ab\002xyz\003.";
std::shared_ptr<Buffer> b = std::make_shared<Buffer>((const uint8_t *)data, strlen(data));
BufferLineWalker blw(b, cld);
blw.set_width(4);
std::shared_ptr<Buffer::Iterator> start_of_line = b->add_iterator();
int times_called = 0;
blw.walk_line(*start_of_line, [&times_called](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & it) -> bool {
static int expected_screen_columns[] = {
0, 1, 2, 0, 1, 2, 0, 2, 3
};
static int expected_row_offsets[] = {
0, 0, 0, 1, 1, 1, 2, 2, 2
};
static int expected_virtual_columns[] = {
0, 1, 2, 4, 5, 6, 7, 9, 10
};
static int expected_character_widths[] = {
1, 1, 2, 1, 1, 1, 2, 1, 0
};
EXPECT_LT(times_called, (int)(sizeof(expected_screen_columns)/sizeof(expected_screen_columns[0])));
EXPECT_EQ(expected_row_offsets[times_called], row_offset);
EXPECT_EQ(expected_screen_columns[times_called], screen_column);
EXPECT_EQ(expected_virtual_columns[times_called], virtual_column);
EXPECT_EQ(expected_character_widths[times_called], character_width);
times_called++;
return true;
});
EXPECT_EQ((int)strlen(data) + 1, times_called);
BufferLineWalker blw(b, start_of_line, 4, cld);
static int expected_screen_columns[] = {
0, 1, 2, 0, 1, 2, 0, 2, 3
};
static int expected_row_offsets[] = {
0, 0, 0, 1, 1, 1, 2, 2, 2
};
static int expected_virtual_columns[] = {
0, 1, 2, 4, 5, 6, 7, 9, 10
};
static int expected_character_widths[] = {
1, 1, 2, 1, 1, 1, 2, 1, 0
};
int iter_count = 0;
for (; blw.is_valid(); blw++)
{
EXPECT_LT(iter_count, (int)(sizeof(expected_screen_columns)/sizeof(expected_screen_columns[0])));
EXPECT_EQ(expected_row_offsets[iter_count], blw.row_offset());
EXPECT_EQ(expected_screen_columns[iter_count], blw.screen_column());
EXPECT_EQ(expected_virtual_columns[iter_count], blw.virtual_column());
EXPECT_EQ(expected_character_widths[iter_count], blw.character_width());
iter_count++;
}
EXPECT_EQ((int)strlen(data) + 1, iter_count);
}
TEST(BufferLineWalkerTest, does_not_push_down_a_row_when_a_multi_column_character_is_wider_than_view_width)
{
static const char data[] = "\002\003";
std::shared_ptr<Buffer> b = std::make_shared<Buffer>((const uint8_t *)data, strlen(data));
BufferLineWalker blw(b, cld);
blw.set_width(1);
std::shared_ptr<Buffer::Iterator> start_of_line = b->add_iterator();
int times_called = 0;
blw.walk_line(*start_of_line, [&times_called](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & it) -> bool {
static int expected_screen_columns[] = {
0, 0, 0
};
static int expected_row_offsets[] = {
0, 1, 2
};
static int expected_virtual_columns[] = {
0, 2, 4
};
static int expected_character_widths[] = {
2, 2, 0
};
EXPECT_LT(times_called, (int)(sizeof(expected_screen_columns)/sizeof(expected_screen_columns[0])));
EXPECT_EQ(expected_row_offsets[times_called], row_offset);
EXPECT_EQ(expected_screen_columns[times_called], screen_column);
EXPECT_EQ(expected_virtual_columns[times_called], virtual_column);
EXPECT_EQ(expected_character_widths[times_called], character_width);
times_called++;
return true;
});
EXPECT_EQ((int)strlen(data) + 1, times_called);
}
BufferLineWalker blw(b, start_of_line, 1, cld);
TEST(BufferLineWalkerTest, stops_iterating_when_callback_returns_false)
{
static const char data[] = "abcdef";
std::shared_ptr<Buffer> b = std::make_shared<Buffer>((const uint8_t *)data, strlen(data));
BufferLineWalker blw(b, cld);
blw.set_width(6);
std::shared_ptr<Buffer::Iterator> start_of_line = b->add_iterator();
int times_called = 0;
blw.walk_line(*start_of_line, [&times_called](int row_offset, int screen_column, int virtual_column, int character_width, const Buffer::Iterator & it) -> bool {
times_called++;
if (times_called >= 3)
{
return false;
}
else
{
return true;
}
});
EXPECT_EQ(3, times_called);
static int expected_screen_columns[] = {
0, 0, 0
};
static int expected_row_offsets[] = {
0, 1, 2
};
static int expected_virtual_columns[] = {
0, 2, 4
};
static int expected_character_widths[] = {
2, 2, 0
};
int iter_count = 0;
for (; blw.is_valid(); blw++)
{
EXPECT_LT(iter_count, (int)(sizeof(expected_screen_columns)/sizeof(expected_screen_columns[0])));
EXPECT_EQ(expected_row_offsets[iter_count], blw.row_offset());
EXPECT_EQ(expected_screen_columns[iter_count], blw.screen_column());
EXPECT_EQ(expected_virtual_columns[iter_count], blw.virtual_column());
EXPECT_EQ(expected_character_widths[iter_count], blw.character_width());
iter_count++;
}
EXPECT_EQ((int)strlen(data) + 1, iter_count);
}
+34 -34
View File
@@ -40,34 +40,34 @@ class MyCharacterWidthDeterminer : public CharacterWidthDeterminer
}
} Cwd;
static int LineNumber(const Buffer::Iterator & iterator)
static int LineNumber(std::shared_ptr<Buffer::Iterator> iterator)
{
int line_number = 0;
Buffer::Iterator i = iterator;
auto i = std::make_shared<Buffer::Iterator>(*iterator);
for (;;)
{
uint32_t c = *i;
uint32_t c = **i;
if ((c < '0') || (c > '9'))
break;
line_number *= 10;
line_number += ((int)c - '0');
i.go_forward();
i->go_forward();
}
return line_number;
}
TEST(BufferViewTest, iter_lines_does_not_call_callback_for_empty_buffer)
TEST(BufferViewTest, does_not_iterate_for_empty_buffer)
{
std::shared_ptr<Buffer> b = buffer_empty();
std::shared_ptr<Buffer::Iterator> iterator = b->add_iterator();
BufferView bv(b, iterator, Cwd);
bv.resize(40, 8);
int times_called = 0;
bv.iter_lines([&times_called](int row_offset, const Buffer::Iterator & iterator) -> bool {
times_called++;
return true;
});
EXPECT_EQ(0, times_called);
int iter_count = 0;
for (auto it = bv.vert_iter(); it.is_valid(); it++)
{
iter_count++;
}
EXPECT_EQ(0, iter_count);
EXPECT_EQ(0, bv.cursor_screen_row());
EXPECT_EQ(0, bv.cursor_screen_column());
EXPECT_EQ(0, bv.cursor_virtual_column());
@@ -79,13 +79,13 @@ TEST(BufferViewTest, iterates_through_screen_lines_with_no_wrapping)
std::shared_ptr<Buffer::Iterator> iterator = b->add_iterator();
BufferView bv(b, iterator, Cwd);
bv.resize(40, 8);
int times_called = 0;
bv.iter_lines([&times_called](int row_offset, const Buffer::Iterator & iterator) -> bool {
EXPECT_EQ(times_called, LineNumber(iterator));
times_called++;
return true;
});
EXPECT_EQ(8, times_called);
int iter_count = 0;
for (auto it = bv.vert_iter(); it.is_valid(); it++)
{
EXPECT_EQ(iter_count, LineNumber(it.iterator()));
iter_count++;
}
EXPECT_EQ(8, iter_count);
EXPECT_EQ(0, bv.cursor_screen_row());
EXPECT_EQ(0, bv.cursor_screen_column());
EXPECT_EQ(0, bv.cursor_virtual_column());
@@ -97,15 +97,15 @@ TEST(BufferViewTest, iterates_through_screen_lines_with_wrapping)
std::shared_ptr<Buffer::Iterator> iterator = b->add_iterator();
BufferView bv(b, iterator, Cwd);
bv.resize(3, 10);
int times_called = 0;
int iter_count = 0;
int expected_row_offsets[] = {0, 1, 2, 4, 5, 6, 7};
bv.iter_lines([&times_called, &expected_row_offsets](int row_offset, const Buffer::Iterator & iterator) -> bool {
EXPECT_EQ(times_called, LineNumber(iterator));
EXPECT_EQ(expected_row_offsets[times_called], row_offset);
times_called++;
return true;
});
EXPECT_EQ(7, times_called);
for (auto it = bv.vert_iter(); it.is_valid(); it++)
{
EXPECT_EQ(iter_count, LineNumber(it.iterator()));
EXPECT_EQ(expected_row_offsets[iter_count], it.row_offset());
iter_count++;
}
EXPECT_EQ(7, iter_count);
EXPECT_EQ(0, bv.cursor_screen_row());
EXPECT_EQ(0, bv.cursor_screen_column());
EXPECT_EQ(0, bv.cursor_virtual_column());
@@ -120,15 +120,15 @@ TEST(BufferViewTest, fills_view_with_last_lines_of_buffer_when_cursor_warps_to_l
{
}
bv.resize(40, 3);
int times_called = 0;
int iter_count = 0;
int expected_row_offsets[] = {0, 1, 2};
bv.iter_lines([&times_called, &expected_row_offsets](int row_offset, const Buffer::Iterator & iterator) -> bool {
EXPECT_EQ(10 + times_called, LineNumber(iterator));
EXPECT_EQ(expected_row_offsets[times_called], row_offset);
times_called++;
return true;
});
EXPECT_EQ(3, times_called);
for (auto it = bv.vert_iter(); it.is_valid(); it++)
{
EXPECT_EQ(10 + iter_count, LineNumber(it.iterator()));
EXPECT_EQ(expected_row_offsets[iter_count], it.row_offset());
iter_count++;
}
EXPECT_EQ(3, iter_count);
EXPECT_EQ(2, bv.cursor_screen_row());
EXPECT_EQ(0, bv.cursor_screen_column());
EXPECT_EQ(0, bv.cursor_virtual_column());