#include "gtest/gtest.h" #include "BufferView.h" static std::shared_ptr 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((const uint8_t *)data, strlen(data)); } static std::shared_ptr buffer_empty() { return std::make_shared(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 b = buffer_empty(); std::shared_ptr iterator = b->add_iterator(); BufferView bv(b, iterator, Cwd); bv.resize(40, 8); int times_called = 0; bv.iter_lines([×_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 b = buffer1(); std::shared_ptr iterator = b->add_iterator(); BufferView bv(b, iterator, Cwd); bv.resize(40, 8); int times_called = 0; bv.iter_lines([×_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 b = buffer1(); std::shared_ptr 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([×_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 b = buffer1(); std::shared_ptr 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([×_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()); }