diff --git a/src/controllers/commands/CommandController.cpp b/src/controllers/commands/CommandController.cpp index d9d043842..544df68c7 100644 --- a/src/controllers/commands/CommandController.cpp +++ b/src/controllers/commands/CommandController.cpp @@ -35,32 +35,6 @@ namespace { using namespace chatterino; -// stripUserName removes any @ prefix or , suffix to make it more suitable for command use -void stripUserName(QString &userName) -{ - if (userName.startsWith('@')) - { - userName.remove(0, 1); - } - if (userName.endsWith(',')) - { - userName.chop(1); - } -} - -// stripChannelName removes any @ prefix or , suffix to make it more suitable for command use -void stripChannelName(QString &channelName) -{ - if (channelName.startsWith('@') || channelName.startsWith('#')) - { - channelName.remove(0, 1); - } - if (channelName.endsWith(',')) - { - channelName.chop(1); - } -} - void sendWhisperMessage(const QString &text) { // (hemirt) pajlada: "we should not be sending whispers through jtv, but diff --git a/src/util/Twitch.cpp b/src/util/Twitch.cpp index 4e7d1605e..10f6d711a 100644 --- a/src/util/Twitch.cpp +++ b/src/util/Twitch.cpp @@ -10,4 +10,29 @@ void openTwitchUsercard(QString channel, QString username) QDesktopServices::openUrl("https://www.twitch.tv/popout/" + channel + "/viewercard/" + username); } + +void stripUserName(QString &userName) +{ + if (userName.startsWith('@')) + { + userName.remove(0, 1); + } + if (userName.endsWith(',')) + { + userName.chop(1); + } +} + +void stripChannelName(QString &channelName) +{ + if (channelName.startsWith('@') || channelName.startsWith('#')) + { + channelName.remove(0, 1); + } + if (channelName.endsWith(',')) + { + channelName.chop(1); + } +} + } // namespace chatterino diff --git a/src/util/Twitch.hpp b/src/util/Twitch.hpp index 7250b79a3..16f75d62a 100644 --- a/src/util/Twitch.hpp +++ b/src/util/Twitch.hpp @@ -6,4 +6,10 @@ namespace chatterino { void openTwitchUsercard(const QString channel, const QString username); +// stripUserName removes any @ prefix or , suffix to make it more suitable for command use +void stripUserName(QString &userName); + +// stripChannelName removes any @ prefix or , suffix to make it more suitable for command use +void stripChannelName(QString &channelName); + } // namespace chatterino diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 268a7203d..4b35b36c2 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -14,6 +14,7 @@ set(test_SOURCES ${CMAKE_CURRENT_LIST_DIR}/src/Helpers.cpp ${CMAKE_CURRENT_LIST_DIR}/src/RatelimitBucket.cpp ${CMAKE_CURRENT_LIST_DIR}/src/Hotkeys.cpp + ${CMAKE_CURRENT_LIST_DIR}/src/UtilTwitch.cpp # Add your new file above this line! ) diff --git a/tests/src/UtilTwitch.cpp b/tests/src/UtilTwitch.cpp new file mode 100644 index 000000000..e96048d35 --- /dev/null +++ b/tests/src/UtilTwitch.cpp @@ -0,0 +1,161 @@ +#include "util/Twitch.hpp" + +#include +#include +#include +#include + +#include +#include + +using namespace chatterino; + +TEST(UtilTwitch, StripUserName) +{ + struct TestCase { + QString inputUserName; + QString expectedUserName; + }; + + std::vector tests{ + { + "pajlada", + "pajlada", + }, + { + "Pajlada", + "Pajlada", + }, + { + "@Pajlada", + "Pajlada", + }, + { + "@Pajlada,", + "Pajlada", + }, + { + "@@Pajlada,", + "@Pajlada", + }, + { + "@@Pajlada,,", + "@Pajlada,", + }, + { + "", + "", + }, + { + "@", + "", + }, + { + ",", + "", + }, + { + // We purposefully don't handle spaces at the end, as all expected usages of this function split the message up by space and strip the parameters by themselves + ", ", + ", ", + }, + { + // We purposefully don't handle spaces at the start, as all expected usages of this function split the message up by space and strip the parameters by themselves + " @", + " @", + }, + }; + + for (const auto &[inputUserName, expectedUserName] : tests) + { + QString userName = inputUserName; + stripUserName(userName); + + EXPECT_EQ(userName, expectedUserName) + << qUtf8Printable(userName) << " (" << qUtf8Printable(inputUserName) + << ") did not match expected value " + << qUtf8Printable(expectedUserName); + } +} + +TEST(UtilTwitch, StripChannelName) +{ + struct TestCase { + QString inputChannelName; + QString expectedChannelName; + }; + + std::vector tests{ + { + "pajlada", + "pajlada", + }, + { + "Pajlada", + "Pajlada", + }, + { + "@Pajlada", + "Pajlada", + }, + { + "#Pajlada", + "Pajlada", + }, + { + "#Pajlada,", + "Pajlada", + }, + { + "#Pajlada,", + "Pajlada", + }, + { + "@@Pajlada,", + "@Pajlada", + }, + { + // We only strip one character off the front + "#@Pajlada,", + "@Pajlada", + }, + { + "@@Pajlada,,", + "@Pajlada,", + }, + { + "", + "", + }, + { + "@", + "", + }, + { + ",", + "", + }, + { + // We purposefully don't handle spaces at the end, as all expected usages of this function split the message up by space and strip the parameters by themselves + ", ", + ", ", + }, + { + // We purposefully don't handle spaces at the start, as all expected usages of this function split the message up by space and strip the parameters by themselves + " #", + " #", + }, + }; + + for (const auto &[inputChannelName, expectedChannelName] : tests) + { + QString userName = inputChannelName; + stripChannelName(userName); + + EXPECT_EQ(userName, expectedChannelName) + << qUtf8Printable(userName) << " (" + << qUtf8Printable(inputChannelName) + << ") did not match expected value " + << qUtf8Printable(expectedChannelName); + } +}