jes/test/src/test_BufferView.cc

136 lines
4.0 KiB
C++

#include "gtest/gtest.h"
#include "BufferView.h"
static std::shared_ptr<Buffer> buffer1()
{
static const char data[] =
"0\n"
"1ab\n"
"2abc\n"
"3\n"
"4\n"
"5\n"
"6abcdefg\n"
"7\n"
"8\n"
"9abcdefghijklmno\n"
"10\n"
"11\n"
"12\n";
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)
{
if (character < 32)
{
return 2;
}
else
{
return 1;
}
}
} Cwd;
static int LineNumber(const Buffer::Iterator & iterator)
{
int line_number = 0;
Buffer::Iterator i = iterator;
for (;;)
{
uint32_t c = *i;
if ((c < '0') || (c > '9'))
break;
line_number *= 10;
line_number += ((int)c - '0');
i.go_forward();
}
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();
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);
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());
}