Skip to content

Commit

Permalink
Added some more tests for newly added TextArea code
Browse files Browse the repository at this point in the history
  • Loading branch information
texus committed Aug 31, 2023
1 parent c5c6d96 commit 58ece04
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
8 changes: 6 additions & 2 deletions include/TGUI/Widgets/TextArea.hpp
Expand Up @@ -247,10 +247,12 @@ TGUI_MODULE_EXPORT namespace tgui
/// This function will take word-wrap into account. So, if a caret is on a line that is
/// currently wrapping, the caret will still register as being on the same line even if
/// that line spans many lines in the TextArea.
///
/// If the value is at the top, the value will be 1.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD std::size_t getCaretLine() const;


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/// @brief Returns which column the blinking cursor is currently located on
///
Expand All @@ -259,6 +261,8 @@ TGUI_MODULE_EXPORT namespace tgui
/// This function will take word-wrap into account. So, if a caret is on a line that is
/// currently wrapping, the caret will still register as being on the same line even if
/// that line spans many lines in the TextArea.
///
/// If the caret is at the beginning of a line, the value will be 1.
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
TGUI_NODISCARD std::size_t getCaretColumn() const;

Expand Down
6 changes: 5 additions & 1 deletion src/String.cpp
Expand Up @@ -2303,7 +2303,11 @@ namespace tgui
std::size_t counter = 0;
const std::size_t end = m_string.size();
for (std::size_t c = pos; c < end; ++c)
if (m_string[c] == ch) ++counter;
{
if (m_string[c] == ch)
++counter;
}

return counter;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/CompareFiles.cpp
Expand Up @@ -62,7 +62,7 @@ void compareImageFiles(const tgui::String& filename1, const tgui::String& filena
const double diffPercentage = (totalDiff * 100) / (imageSize1.x * imageSize1.y * 3);
INFO("Filename: " + filename1.toStdString());

REQUIRE(diffPercentage < 0.05);
REQUIRE(diffPercentage < 0.06);
}

// The compareFiles can't be used to compare empty files because it uses readFileToMemory which
Expand Down
41 changes: 41 additions & 0 deletions tests/Widgets/TextArea.cpp
Expand Up @@ -53,7 +53,10 @@ TEST_CASE("[TextArea]")
textArea->onTextChange([](){});
textArea->onTextChange([](const tgui::String&){});

textArea->onCaretPositionChange([](){});

REQUIRE_NOTHROW(tgui::Widget::Ptr(textArea)->getSignal("TextChanged").connect([]{}));
REQUIRE_NOTHROW(tgui::Widget::Ptr(textArea)->getSignal("CaretPositionChanged").connect([]{}));
}

SECTION("WidgetType")
Expand Down Expand Up @@ -118,6 +121,16 @@ TEST_CASE("[TextArea]")
REQUIRE(textArea->getSelectedText() == "");
REQUIRE(textArea->getSelectionStart() == 2);
REQUIRE(textArea->getSelectionEnd() == 2);

textArea->setSelectedText(6, 10);
REQUIRE(textArea->getSelectedText() == "xt");
REQUIRE(textArea->getSelectionStart() == 6);
REQUIRE(textArea->getSelectionEnd() == 8);

textArea->setSelectedText(10, 20);
REQUIRE(textArea->getSelectedText() == "");
REQUIRE(textArea->getSelectionStart() == 8);
REQUIRE(textArea->getSelectionEnd() == 8);
}

SECTION("TextSize")
Expand Down Expand Up @@ -224,6 +237,34 @@ TEST_CASE("[TextArea]")

textArea->setCaretPosition(25);
REQUIRE(textArea->getCaretPosition() == 9);

textArea->setSize({50, 50});
textArea->setText("A long text that wouldn't fit on a single line.\nFollowed by more\nand more...");

textArea->setCaretPosition(0);
REQUIRE(textArea->getCaretLine() == 1);
REQUIRE(textArea->getCaretColumn() == 1);

textArea->setCaretPosition(10);
REQUIRE(textArea->getCaretLine() == 1);
REQUIRE(textArea->getCaretColumn() == 11);

textArea->setCaretPosition(60);
REQUIRE(textArea->getCaretLine() == 2);
REQUIRE(textArea->getCaretColumn() == 13);

textArea->setCaretPosition(70);
REQUIRE(textArea->getCaretLine() == 3);
REQUIRE(textArea->getCaretColumn() == 6);

textArea->setCaretPosition(100);
REQUIRE(textArea->getCaretLine() == 3);
REQUIRE(textArea->getCaretColumn() == 12);

textArea->setSelectedText(6, 2);
REQUIRE(textArea->getCaretPosition() == 2);
REQUIRE(textArea->getCaretLine() == 1);
REQUIRE(textArea->getCaretColumn() == 3);
}

SECTION("LinesCount")
Expand Down

0 comments on commit 58ece04

Please sign in to comment.