From b89f3486024c3d2c79d06f5f840d2033c5aa691a Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Tue, 29 Aug 2017 23:20:42 -0400 Subject: [PATCH] add some initial BufferView unit tests --- src/core/BufferView.cc | 6 ++++ test/src/test_BufferView.cc | 68 +++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+) diff --git a/src/core/BufferView.cc b/src/core/BufferView.cc index 32bc785..8464d01 100644 --- a/src/core/BufferView.cc +++ b/src/core/BufferView.cc @@ -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; + } } /************************************************************************** diff --git a/test/src/test_BufferView.cc b/test/src/test_BufferView.cc index 4375adf..d43dd72 100644 --- a/test/src/test_BufferView.cc +++ b/test/src/test_BufferView.cc @@ -20,6 +20,11 @@ static std::shared_ptr buffer1() 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) @@ -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 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(); @@ -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 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()); }