add some initial BufferView unit tests

This commit is contained in:
Josh Holtrop 2017-08-29 23:20:42 -04:00
parent 8e0796decf
commit b89f348602
2 changed files with 74 additions and 0 deletions

View File

@ -43,6 +43,12 @@ void BufferView::iter_lines(
break;
}
}
else
{
m_cursor_screen_row = 0;
m_cursor_screen_column = 0;
m_cursor_virtual_column = 0;
}
}
/**************************************************************************

View File

@ -20,6 +20,11 @@ static std::shared_ptr<Buffer> buffer1()
return std::make_shared<Buffer>((const uint8_t *)data, strlen(data));
}
static std::shared_ptr<Buffer> buffer_empty()
{
return std::make_shared<Buffer>(nullptr, 0u);
}
class MyCharacterWidthDeterminer : public CharacterWidthDeterminer
{
int operator()(uint32_t character)
@ -51,6 +56,23 @@ static int LineNumber(const Buffer::Iterator & iterator)
return line_number;
}
TEST(BufferViewTest, iter_lines_does_not_call_callback_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);
EXPECT_EQ(0, bv.cursor_screen_row());
EXPECT_EQ(0, bv.cursor_screen_column());
EXPECT_EQ(0, bv.cursor_virtual_column());
}
TEST(BufferViewTest, iterates_through_screen_lines_with_no_wrapping)
{
std::shared_ptr<Buffer> b = buffer1();
@ -64,4 +86,50 @@ TEST(BufferViewTest, iterates_through_screen_lines_with_no_wrapping)
return true;
});
EXPECT_EQ(8, times_called);
EXPECT_EQ(0, bv.cursor_screen_row());
EXPECT_EQ(0, bv.cursor_screen_column());
EXPECT_EQ(0, bv.cursor_virtual_column());
}
TEST(BufferViewTest, iterates_through_screen_lines_with_wrapping)
{
std::shared_ptr<Buffer> b = buffer1();
std::shared_ptr<Buffer::Iterator> iterator = b->add_iterator();
BufferView bv(b, iterator, Cwd);
bv.resize(3, 10);
int times_called = 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);
EXPECT_EQ(0, bv.cursor_screen_row());
EXPECT_EQ(0, bv.cursor_screen_column());
EXPECT_EQ(0, bv.cursor_virtual_column());
}
TEST(BufferViewTest, fills_view_with_last_lines_of_buffer_when_cursor_warps_to_last_line)
{
std::shared_ptr<Buffer> b = buffer1();
std::shared_ptr<Buffer::Iterator> iterator = b->add_iterator();
BufferView bv(b, iterator, Cwd);
while (iterator->go_next_line())
{
}
bv.resize(40, 3);
int times_called = 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);
EXPECT_EQ(2, bv.cursor_screen_row());
EXPECT_EQ(0, bv.cursor_screen_column());
EXPECT_EQ(0, bv.cursor_virtual_column());
}