Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add selection tests #4904

Merged
merged 6 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
145 changes: 145 additions & 0 deletions tests/src/Selection.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
#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);
}
}
kornes marked this conversation as resolved.
Show resolved Hide resolved

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.messageIndex, 90);
ASSERT_EQ(a.selectionMax, a.end);
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);
}
kornes marked this conversation as resolved.
Show resolved Hide resolved