Add tests to Twitch User/Channel name strip functions (#3568)

This commit is contained in:
pajlada 2022-02-12 15:06:47 +01:00 committed by GitHub
parent b2fa7b1d94
commit fb9c3ad42b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 193 additions and 26 deletions

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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!
)

161
tests/src/UtilTwitch.cpp Normal file
View file

@ -0,0 +1,161 @@
#include "util/Twitch.hpp"
#include <gtest/gtest.h>
#include <QApplication>
#include <QDebug>
#include <QtConcurrent>
#include <chrono>
#include <thread>
using namespace chatterino;
TEST(UtilTwitch, StripUserName)
{
struct TestCase {
QString inputUserName;
QString expectedUserName;
};
std::vector<TestCase> 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<TestCase> 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);
}
}