Skip to content

Commit

Permalink
Add selection unit tests (Chatterino#4904)
Browse files Browse the repository at this point in the history
  • Loading branch information
kornes authored Oct 23, 2023
1 parent fcb6eff commit da31e47
Show file tree
Hide file tree
Showing 2 changed files with 153 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ set(test_SOURCES
${CMAKE_CURRENT_LIST_DIR}/src/Literals.cpp
${CMAKE_CURRENT_LIST_DIR}/src/XDGDesktopFile.cpp
${CMAKE_CURRENT_LIST_DIR}/src/XDGHelper.cpp
${CMAKE_CURRENT_LIST_DIR}/src/Selection.cpp
# Add your new file above this line!
)

Expand Down
152 changes: 152 additions & 0 deletions tests/src/Selection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#include "messages/Selection.hpp"

#include <gtest/gtest.h>

using namespace chatterino;

TEST(Selection, SelectionItemEmpty)
{
auto a = SelectionItem{};
ASSERT_EQ(a.charIndex, 0);
ASSERT_EQ(a.messageIndex, 0);
}

TEST(Selection, SelectionItemLt)
{
auto a = SelectionItem{1, 5};
ASSERT_FALSE(a < a);

auto b = SelectionItem{1, 10};
ASSERT_LT(a, b);

auto c = SelectionItem{20, 0};
ASSERT_LT(a, c);
}

TEST(Selection, SelectionItemGt)
{
auto a = SelectionItem{20, 0};
ASSERT_FALSE(a > a);

auto b = SelectionItem{1, 5};
ASSERT_GT(a, b);

auto c = SelectionItem{1, 10};
ASSERT_GT(a, c);
}

TEST(Selection, SelectionItemEq)
{
auto a = SelectionItem{1, 2};
auto b = SelectionItem{1, 2};

ASSERT_EQ(a, b);
ASSERT_FALSE(a != b);

auto c = SelectionItem{2, 1};
auto d = SelectionItem{1, 0};

ASSERT_NE(a, d);
ASSERT_FALSE(a == c);
}

TEST(Selection, SelectionEmpty)
{
auto a = Selection{};
ASSERT_TRUE(a.isEmpty());
}

TEST(Selection, SelectionEq)
{
auto a = Selection{{5, 5}, {10, 10}};
auto b = Selection{{5, 5}, {10, 10}};

ASSERT_EQ(a, b);
ASSERT_FALSE(a != b);

auto c = Selection{{15, 15}, {5, 5}};
auto d = Selection{{10, 10}, {5, 5}};

ASSERT_NE(a, d);
ASSERT_FALSE(a == c);
}

TEST(Selection, SelectionMinMax)
{
auto a = Selection{{1, 1}, {1, 2}};
ASSERT_EQ(a.selectionMin, a.start);
ASSERT_EQ(a.selectionMax, a.end);

auto b = Selection{{2, 0}, {1, 10}};
ASSERT_EQ(b.selectionMin, b.end);
ASSERT_EQ(b.selectionMax, b.start);
}

TEST(Selection, SelectionUnion)
{
{
auto a = Selection{{1, 1}, {1, 10}};
auto b = Selection{{1, 20}, {1, 30}};
auto ab = a | b;

ASSERT_EQ(ab.start, a.start);
ASSERT_EQ(ab.end, b.end);
}

{
auto a = Selection{{10, 0}, {5, 50}};
auto b = Selection{{20, 0}, {25, 25}};
auto ab = a | b;

ASSERT_EQ(ab.start, a.end);
ASSERT_EQ(ab.end, b.end);
}

{
auto a = Selection{{3, 3}, {2, 1}};
auto b = Selection{{1, 0}, {1, 10}};
auto ab = a | b;

ASSERT_EQ(ab.start, b.start);
ASSERT_EQ(ab.end, a.start);

auto ba = b | a;
ASSERT_EQ(ab, ba);
}
}

TEST(Selection, SelectionSingleMessage)
{
auto a = Selection{{1, 1}, {1, 2}};
ASSERT_TRUE(a.isSingleMessage());

auto b = Selection{{1, 1}, {2, 2}};
ASSERT_FALSE(b.isSingleMessage());
}

TEST(Selection, ShiftMessageIndex)
{
auto a = Selection{{100, 1}, {200, 2}};

a.shiftMessageIndex(10);
ASSERT_EQ(a.selectionMin, a.start);
ASSERT_EQ(a.selectionMin.charIndex, 1);
ASSERT_EQ(a.selectionMin.messageIndex, 90);
ASSERT_EQ(a.selectionMax, a.end);
ASSERT_EQ(a.selectionMax.charIndex, 2);
ASSERT_EQ(a.selectionMax.messageIndex, 190);

a.shiftMessageIndex(20);
ASSERT_EQ(a.selectionMin, a.start);
ASSERT_EQ(a.selectionMin.messageIndex, 70);
ASSERT_EQ(a.selectionMax, a.end);
ASSERT_EQ(a.selectionMax.messageIndex, 170);

a.shiftMessageIndex(180);
ASSERT_EQ(a.selectionMin.messageIndex, 0);
ASSERT_EQ(a.selectionMax.messageIndex, 0);
ASSERT_EQ(a.start.messageIndex, 0);
ASSERT_EQ(a.end.messageIndex, 0);
ASSERT_EQ(a.start.charIndex, 0);
ASSERT_EQ(a.end.charIndex, 0);
}

0 comments on commit da31e47

Please sign in to comment.