From 401feac0aadffc381d2921a2cae3dfa12104aa1d Mon Sep 17 00:00:00 2001 From: pajlada Date: Sun, 5 May 2024 15:01:07 +0200 Subject: [PATCH] tests: better test initializing allowing for better printing (#5379) Co-authored-by: Nerixyz --- CHANGELOG.md | 1 + tests/CMakeLists.txt | 3 +- tests/src/AccessGuard.cpp | 2 +- tests/src/BasicPubSub.cpp | 2 +- tests/src/BttvLiveUpdates.cpp | 3 +- tests/src/ChannelChatters.cpp | 2 +- tests/src/ChatterSet.cpp | 3 +- tests/src/Emojis.cpp | 3 +- tests/src/ExponentialBackoff.cpp | 2 +- tests/src/Filters.cpp | 43 +++++++++---------- tests/src/FormatTime.cpp | 13 +++--- tests/src/Helpers.cpp | 15 ++++--- tests/src/HighlightController.cpp | 9 ++-- tests/src/HighlightPhrase.cpp | 2 +- tests/src/Hotkeys.cpp | 3 +- tests/src/InputCompletion.cpp | 4 +- tests/src/IrcHelpers.cpp | 7 ++-- tests/src/LimitedQueue.cpp | 2 +- tests/src/LinkInfo.cpp | 3 +- tests/src/LinkParser.cpp | 3 +- tests/src/Literals.cpp | 2 +- tests/src/MessageLayout.cpp | 2 +- tests/src/NetworkCommon.cpp | 2 +- tests/src/NetworkRequest.cpp | 2 +- tests/src/NetworkResult.cpp | 2 +- tests/src/NotebookTab.cpp | 3 +- tests/src/QMagicEnum.cpp | 3 +- tests/src/RatelimitBucket.cpp | 3 +- tests/src/Selection.cpp | 2 +- tests/src/SeventvEventAPI.cpp | 2 +- tests/src/SplitInput.cpp | 8 ++-- tests/src/Test.cpp | 42 +++++++++++++++++++ tests/src/Test.hpp | 22 ++++++++++ tests/src/TestHelpers.hpp | 66 ------------------------------ tests/src/TwitchMessageBuilder.cpp | 3 +- tests/src/TwitchPubSubClient.cpp | 45 +++++++++++++++++++- tests/src/Updates.cpp | 2 +- tests/src/UtilTwitch.cpp | 32 ++++++--------- tests/src/XDGDesktopFile.cpp | 3 +- tests/src/XDGHelper.cpp | 3 +- tests/src/main.cpp | 2 +- 41 files changed, 198 insertions(+), 178 deletions(-) create mode 100644 tests/src/Test.cpp create mode 100644 tests/src/Test.hpp delete mode 100644 tests/src/TestHelpers.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 0a5d65fa2..56d2b0054 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unversioned - Dev: Add doxygen build target. (#5377) +- Dev: Make printing of strings in tests easier. (#5379) ## 2.5.1 diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index fb5730048..bd35b79de 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -5,7 +5,8 @@ option(CHATTERINO_TEST_USE_PUBLIC_HTTPBIN "Use public httpbin for testing networ set(test_SOURCES ${CMAKE_CURRENT_LIST_DIR}/src/main.cpp ${CMAKE_CURRENT_LIST_DIR}/resources/test-resources.qrc - ${CMAKE_CURRENT_LIST_DIR}/src/TestHelpers.hpp + ${CMAKE_CURRENT_LIST_DIR}/src/Test.hpp + ${CMAKE_CURRENT_LIST_DIR}/src/Test.cpp ${CMAKE_CURRENT_LIST_DIR}/src/ChannelChatters.cpp ${CMAKE_CURRENT_LIST_DIR}/src/AccessGuard.cpp ${CMAKE_CURRENT_LIST_DIR}/src/NetworkCommon.cpp diff --git a/tests/src/AccessGuard.cpp b/tests/src/AccessGuard.cpp index a0d1c6d31..56cbc727f 100644 --- a/tests/src/AccessGuard.cpp +++ b/tests/src/AccessGuard.cpp @@ -1,6 +1,6 @@ #include "common/UniqueAccess.hpp" +#include "Test.hpp" -#include #include #include diff --git a/tests/src/BasicPubSub.cpp b/tests/src/BasicPubSub.cpp index dc2775220..6315970ff 100644 --- a/tests/src/BasicPubSub.cpp +++ b/tests/src/BasicPubSub.cpp @@ -1,7 +1,7 @@ #include "providers/liveupdates/BasicPubSubClient.hpp" #include "providers/liveupdates/BasicPubSubManager.hpp" +#include "Test.hpp" -#include #include #include #include diff --git a/tests/src/BttvLiveUpdates.cpp b/tests/src/BttvLiveUpdates.cpp index 580f2e61f..2d238f9b0 100644 --- a/tests/src/BttvLiveUpdates.cpp +++ b/tests/src/BttvLiveUpdates.cpp @@ -1,6 +1,7 @@ #include "providers/bttv/BttvLiveUpdates.hpp" -#include +#include "Test.hpp" + #include #include diff --git a/tests/src/ChannelChatters.cpp b/tests/src/ChannelChatters.cpp index c665836bb..79711ce15 100644 --- a/tests/src/ChannelChatters.cpp +++ b/tests/src/ChannelChatters.cpp @@ -1,8 +1,8 @@ #include "common/ChannelChatters.hpp" #include "mocks/Channel.hpp" +#include "Test.hpp" -#include #include #include diff --git a/tests/src/ChatterSet.cpp b/tests/src/ChatterSet.cpp index ac5b81ee9..57a67a771 100644 --- a/tests/src/ChatterSet.cpp +++ b/tests/src/ChatterSet.cpp @@ -1,6 +1,7 @@ #include "common/ChatterSet.hpp" -#include +#include "Test.hpp" + #include TEST(ChatterSet, insert) diff --git a/tests/src/Emojis.cpp b/tests/src/Emojis.cpp index 141a64afb..42df110a4 100644 --- a/tests/src/Emojis.cpp +++ b/tests/src/Emojis.cpp @@ -1,9 +1,8 @@ #include "providers/emoji/Emojis.hpp" #include "common/Literals.hpp" -#include "TestHelpers.hpp" +#include "Test.hpp" -#include #include #include diff --git a/tests/src/ExponentialBackoff.cpp b/tests/src/ExponentialBackoff.cpp index 2a4259744..7099ea08a 100644 --- a/tests/src/ExponentialBackoff.cpp +++ b/tests/src/ExponentialBackoff.cpp @@ -1,6 +1,6 @@ #include "util/ExponentialBackoff.hpp" -#include +#include "Test.hpp" using namespace chatterino; diff --git a/tests/src/Filters.cpp b/tests/src/Filters.cpp index ff1b05902..89c0a510f 100644 --- a/tests/src/Filters.cpp +++ b/tests/src/Filters.cpp @@ -13,8 +13,8 @@ #include "providers/twitch/TwitchBadge.hpp" #include "providers/twitch/TwitchMessageBuilder.hpp" #include "singletons/Emotes.hpp" +#include "Test.hpp" -#include #include #include @@ -101,7 +101,7 @@ namespace chatterino::filters { std::ostream &operator<<(std::ostream &os, Type t) { - os << qUtf8Printable(typeToString(t)); + os << typeToString(t); return os; } @@ -138,8 +138,8 @@ TEST(Filters, Validity) auto filterResult = Filter::fromString(input); bool isValid = std::holds_alternative(filterResult); EXPECT_EQ(isValid, expected) - << "Filter::fromString( " << qUtf8Printable(input) - << " ) should be " << (expected ? "valid" : "invalid"); + << "Filter::fromString( " << input << " ) should be " + << (expected ? "valid" : "invalid"); } } @@ -168,15 +168,14 @@ TEST(Filters, TypeSynthesis) { auto filterResult = Filter::fromString(input); bool isValid = std::holds_alternative(filterResult); - ASSERT_TRUE(isValid) << "Filter::fromString( " << qUtf8Printable(input) - << " ) is invalid"; + ASSERT_TRUE(isValid) + << "Filter::fromString( " << input << " ) is invalid"; auto filter = std::move(std::get(filterResult)); T type = filter.returnType(); EXPECT_EQ(type, expected) - << "Filter{ " << qUtf8Printable(input) << " } has type " << type - << " instead of " << expected << ".\nDebug: " - << qUtf8Printable(filter.debugString(typingContext)); + << "Filter{ " << input << " } has type " << type << " instead of " + << expected << ".\nDebug: " << filter.debugString(typingContext); } } @@ -244,17 +243,16 @@ TEST(Filters, Evaluation) { auto filterResult = Filter::fromString(input); bool isValid = std::holds_alternative(filterResult); - ASSERT_TRUE(isValid) << "Filter::fromString( " << qUtf8Printable(input) - << " ) is invalid"; + ASSERT_TRUE(isValid) + << "Filter::fromString( " << input << " ) is invalid"; auto filter = std::move(std::get(filterResult)); auto result = filter.execute(contextMap); EXPECT_EQ(result, expected) - << "Filter{ " << qUtf8Printable(input) << " } evaluated to " - << qUtf8Printable(result.toString()) << " instead of " - << qUtf8Printable(expected.toString()) << ".\nDebug: " - << qUtf8Printable(filter.debugString(typingContext)); + << "Filter{ " << input << " } evaluated to " << result.toString() + << " instead of " << expected.toString() + << ".\nDebug: " << filter.debugString(typingContext); } } @@ -354,20 +352,17 @@ TEST_F(FiltersF, ExpressionDebug) { const auto filterResult = Filter::fromString(input); const auto *filter = std::get_if(&filterResult); - EXPECT_NE(filter, nullptr) - << "Filter::fromString(" << qUtf8Printable(input) - << ") did not build a proper filter"; + EXPECT_NE(filter, nullptr) << "Filter::fromString(" << input + << ") did not build a proper filter"; const auto actualDebugString = filter->debugString(typingContext); EXPECT_EQ(actualDebugString, debugString) - << "filter->debugString() on '" << qUtf8Printable(input) - << "' should be '" << qUtf8Printable(debugString) << "', but got '" - << qUtf8Printable(actualDebugString) << "'"; + << "filter->debugString() on '" << input << "' should be '" + << debugString << "', but got '" << actualDebugString << "'"; const auto actualFilterString = filter->filterString(); EXPECT_EQ(actualFilterString, filterString) - << "filter->filterString() on '" << qUtf8Printable(input) - << "' should be '" << qUtf8Printable(filterString) << "', but got '" - << qUtf8Printable(actualFilterString) << "'"; + << "filter->filterString() on '" << input << "' should be '" + << filterString << "', but got '" << actualFilterString << "'"; } } diff --git a/tests/src/FormatTime.cpp b/tests/src/FormatTime.cpp index bc15f44ef..6fe82ab9a 100644 --- a/tests/src/FormatTime.cpp +++ b/tests/src/FormatTime.cpp @@ -1,6 +1,6 @@ #include "util/FormatTime.hpp" -#include +#include "Test.hpp" #include @@ -62,8 +62,8 @@ TEST(FormatTime, Int) const auto actual = formatTime(input); EXPECT_EQ(actual, expected) - << qUtf8Printable(actual) << " (" << input - << ") did not match expected value " << qUtf8Printable(expected); + << actual << " (" << input << ") did not match expected value " + << expected; } } @@ -130,8 +130,8 @@ TEST(FormatTime, QString) const auto actual = formatTime(input); EXPECT_EQ(actual, expected) - << qUtf8Printable(actual) << " (" << qUtf8Printable(input) - << ") did not match expected value " << qUtf8Printable(expected); + << actual << " (" << input << ") did not match expected value " + << expected; } } @@ -202,7 +202,6 @@ TEST(FormatTime, chrono) const auto actual = formatTime(input); EXPECT_EQ(actual, expected) - << qUtf8Printable(actual) << " did not match expected value " - << qUtf8Printable(expected); + << actual << " did not match expected value " << expected; } } diff --git a/tests/src/Helpers.cpp b/tests/src/Helpers.cpp index d6a74fec0..c615167cf 100644 --- a/tests/src/Helpers.cpp +++ b/tests/src/Helpers.cpp @@ -1,6 +1,6 @@ #include "util/Helpers.hpp" -#include +#include "Test.hpp" using namespace chatterino; using namespace _helpers_internal; @@ -275,8 +275,8 @@ TEST(Helpers, skipSpace) const auto actual = skipSpace(makeView(c.input), c.startIdx); EXPECT_EQ(actual, c.expected) - << actual << " (" << qUtf8Printable(c.input) - << ") did not match expected value " << c.expected; + << actual << " (" << c.input << ") did not match expected value " + << c.expected; } } @@ -418,14 +418,13 @@ TEST(Helpers, findUnitMultiplierToSec) if (c.expectedMultiplier == bad) { - EXPECT_FALSE(actual.second) << qUtf8Printable(c.input); + EXPECT_FALSE(actual.second) << c.input; } else { EXPECT_TRUE(pos == c.expectedEndPos && actual.second && actual.first == c.expectedMultiplier) - << qUtf8Printable(c.input) - << ": Expected(end: " << c.expectedEndPos + << c.input << ": Expected(end: " << c.expectedEndPos << ", mult: " << c.expectedMultiplier << ") Actual(end: " << pos << ", mult: " << actual.first << ")"; } @@ -503,7 +502,7 @@ TEST(Helpers, parseDurationToSeconds) const auto actual = parseDurationToSeconds(c.input, c.noUnitMultiplier); EXPECT_EQ(actual, c.output) - << actual << " (" << qUtf8Printable(c.input) - << ") did not match expected value " << c.output; + << actual << " (" << c.input << ") did not match expected value " + << c.output; } } diff --git a/tests/src/HighlightController.cpp b/tests/src/HighlightController.cpp index a45bbf98c..090acf37b 100644 --- a/tests/src/HighlightController.cpp +++ b/tests/src/HighlightController.cpp @@ -10,9 +10,8 @@ #include "providers/twitch/TwitchBadge.hpp" // for Badge #include "singletons/Paths.hpp" #include "singletons/Settings.hpp" +#include "Test.hpp" -#include -#include #include #include #include @@ -216,11 +215,9 @@ protected: input.originalMessage, input.flags); EXPECT_EQ(isMatch, expected.state) - << qUtf8Printable(input.senderName) << ": " - << qUtf8Printable(input.originalMessage); + << input.senderName << ": " << input.originalMessage; EXPECT_EQ(matchResult, expected.result) - << qUtf8Printable(input.senderName) << ": " - << qUtf8Printable(input.originalMessage); + << input.senderName << ": " << input.originalMessage; } } diff --git a/tests/src/HighlightPhrase.cpp b/tests/src/HighlightPhrase.cpp index 374670b03..2ec2530f0 100644 --- a/tests/src/HighlightPhrase.cpp +++ b/tests/src/HighlightPhrase.cpp @@ -1,6 +1,6 @@ #include "controllers/highlights/HighlightPhrase.hpp" -#include +#include "Test.hpp" using namespace chatterino; diff --git a/tests/src/Hotkeys.cpp b/tests/src/Hotkeys.cpp index ebbfe5029..7c3d8d10f 100644 --- a/tests/src/Hotkeys.cpp +++ b/tests/src/Hotkeys.cpp @@ -1,6 +1,5 @@ #include "controllers/hotkeys/HotkeyHelpers.hpp" - -#include +#include "Test.hpp" #include diff --git a/tests/src/InputCompletion.cpp b/tests/src/InputCompletion.cpp index 860035438..22c42b31c 100644 --- a/tests/src/InputCompletion.cpp +++ b/tests/src/InputCompletion.cpp @@ -12,9 +12,9 @@ #include "singletons/Emotes.hpp" #include "singletons/Paths.hpp" #include "singletons/Settings.hpp" +#include "Test.hpp" #include "widgets/splits/InputCompletionPopup.hpp" -#include #include #include #include @@ -224,7 +224,7 @@ void containsRoughly(std::span span, std::set values) } } - ASSERT_TRUE(found) << qPrintable(v) << " was not found in the span"; + ASSERT_TRUE(found) << v << " was not found in the span"; } } diff --git a/tests/src/IrcHelpers.cpp b/tests/src/IrcHelpers.cpp index acae81c35..d210b14e0 100644 --- a/tests/src/IrcHelpers.cpp +++ b/tests/src/IrcHelpers.cpp @@ -1,6 +1,7 @@ #include "util/IrcHelpers.hpp" -#include +#include "Test.hpp" + #include #include #include @@ -55,7 +56,7 @@ TEST(IrcHelpers, ParseTagString) const auto actual = parseTagString(input); EXPECT_EQ(actual, expected) - << qUtf8Printable(actual) << " (" << qUtf8Printable(input) - << ") did not match expected value " << qUtf8Printable(expected); + << actual << " (" << input << ") did not match expected value " + << expected; } } diff --git a/tests/src/LimitedQueue.cpp b/tests/src/LimitedQueue.cpp index 39a8bba86..0a94ea928 100644 --- a/tests/src/LimitedQueue.cpp +++ b/tests/src/LimitedQueue.cpp @@ -1,6 +1,6 @@ #include "messages/LimitedQueue.hpp" -#include +#include "Test.hpp" #include diff --git a/tests/src/LinkInfo.cpp b/tests/src/LinkInfo.cpp index 91f065035..a06a78c0f 100644 --- a/tests/src/LinkInfo.cpp +++ b/tests/src/LinkInfo.cpp @@ -2,8 +2,7 @@ #include "common/Literals.hpp" #include "SignalSpy.hpp" - -#include +#include "Test.hpp" using namespace chatterino; using namespace literals; diff --git a/tests/src/LinkParser.cpp b/tests/src/LinkParser.cpp index cce5c8c6c..9d964ce15 100644 --- a/tests/src/LinkParser.cpp +++ b/tests/src/LinkParser.cpp @@ -1,8 +1,7 @@ #include "common/LinkParser.hpp" -#include "TestHelpers.hpp" +#include "Test.hpp" -#include #include #include diff --git a/tests/src/Literals.cpp b/tests/src/Literals.cpp index 77607b739..17d459b14 100644 --- a/tests/src/Literals.cpp +++ b/tests/src/Literals.cpp @@ -1,6 +1,6 @@ #include "common/Literals.hpp" -#include +#include "Test.hpp" using namespace chatterino::literals; diff --git a/tests/src/MessageLayout.cpp b/tests/src/MessageLayout.cpp index 9ce0c7f21..ab9a294c9 100644 --- a/tests/src/MessageLayout.cpp +++ b/tests/src/MessageLayout.cpp @@ -10,8 +10,8 @@ #include "singletons/Settings.hpp" #include "singletons/Theme.hpp" #include "singletons/WindowManager.hpp" +#include "Test.hpp" -#include #include #include diff --git a/tests/src/NetworkCommon.cpp b/tests/src/NetworkCommon.cpp index 481f951ae..9beab8da6 100644 --- a/tests/src/NetworkCommon.cpp +++ b/tests/src/NetworkCommon.cpp @@ -1,6 +1,6 @@ #include "common/network/NetworkCommon.hpp" -#include +#include "Test.hpp" using namespace chatterino; diff --git a/tests/src/NetworkRequest.cpp b/tests/src/NetworkRequest.cpp index 2f6b8102f..ca723481e 100644 --- a/tests/src/NetworkRequest.cpp +++ b/tests/src/NetworkRequest.cpp @@ -2,8 +2,8 @@ #include "common/network/NetworkManager.hpp" #include "common/network/NetworkResult.hpp" +#include "Test.hpp" -#include #include using namespace chatterino; diff --git a/tests/src/NetworkResult.cpp b/tests/src/NetworkResult.cpp index 72a2ca771..4bf2366a3 100644 --- a/tests/src/NetworkResult.cpp +++ b/tests/src/NetworkResult.cpp @@ -1,6 +1,6 @@ #include "common/network/NetworkResult.hpp" -#include +#include "Test.hpp" using namespace chatterino; diff --git a/tests/src/NotebookTab.cpp b/tests/src/NotebookTab.cpp index 2ac4903f4..36133b648 100644 --- a/tests/src/NotebookTab.cpp +++ b/tests/src/NotebookTab.cpp @@ -7,10 +7,9 @@ #include "singletons/Fonts.hpp" #include "singletons/Settings.hpp" #include "singletons/Theme.hpp" +#include "Test.hpp" #include "widgets/Notebook.hpp" -#include -#include #include #include diff --git a/tests/src/QMagicEnum.cpp b/tests/src/QMagicEnum.cpp index 80c265efe..6778427fe 100644 --- a/tests/src/QMagicEnum.cpp +++ b/tests/src/QMagicEnum.cpp @@ -2,8 +2,7 @@ #include "common/FlagsEnum.hpp" #include "common/Literals.hpp" - -#include +#include "Test.hpp" using namespace chatterino; using namespace literals; diff --git a/tests/src/RatelimitBucket.cpp b/tests/src/RatelimitBucket.cpp index c92a42234..850f14c68 100644 --- a/tests/src/RatelimitBucket.cpp +++ b/tests/src/RatelimitBucket.cpp @@ -1,6 +1,7 @@ #include "util/RatelimitBucket.hpp" -#include +#include "Test.hpp" + #include #include #include diff --git a/tests/src/Selection.cpp b/tests/src/Selection.cpp index 1f1f4a621..a904b0766 100644 --- a/tests/src/Selection.cpp +++ b/tests/src/Selection.cpp @@ -1,6 +1,6 @@ #include "messages/Selection.hpp" -#include +#include "Test.hpp" using namespace chatterino; diff --git a/tests/src/SeventvEventAPI.cpp b/tests/src/SeventvEventAPI.cpp index 780f90bac..4e2d32281 100644 --- a/tests/src/SeventvEventAPI.cpp +++ b/tests/src/SeventvEventAPI.cpp @@ -3,8 +3,8 @@ #include "providers/seventv/eventapi/Client.hpp" #include "providers/seventv/eventapi/Dispatch.hpp" #include "providers/seventv/eventapi/Message.hpp" +#include "Test.hpp" -#include #include #include diff --git a/tests/src/SplitInput.cpp b/tests/src/SplitInput.cpp index ed092f94b..d84da8118 100644 --- a/tests/src/SplitInput.cpp +++ b/tests/src/SplitInput.cpp @@ -12,11 +12,10 @@ #include "singletons/Settings.hpp" #include "singletons/Theme.hpp" #include "singletons/WindowManager.hpp" +#include "Test.hpp" #include "widgets/Notebook.hpp" #include "widgets/splits/Split.hpp" -#include -#include #include #include @@ -110,9 +109,8 @@ TEST_P(SplitInputTest, Reply) auto reply = MessagePtr(message); this->input.setReply(reply); QString actual = this->input.getInputText(); - ASSERT_EQ(expected, actual) - << "Input text after setReply should be '" << qUtf8Printable(expected) - << "', but got '" << qUtf8Printable(actual) << "'"; + ASSERT_EQ(expected, actual) << "Input text after setReply should be '" + << expected << "', but got '" << actual << "'"; } INSTANTIATE_TEST_SUITE_P( diff --git a/tests/src/Test.cpp b/tests/src/Test.cpp new file mode 100644 index 000000000..5f245f5d7 --- /dev/null +++ b/tests/src/Test.cpp @@ -0,0 +1,42 @@ +#include "Test.hpp" + +#include +#include + +std::ostream &operator<<(std::ostream &os, QStringView str) +{ + os << str.toString().toStdString(); + return os; +} + +std::ostream &operator<<(std::ostream &os, const QByteArray &bytes) +{ + os << std::string_view{bytes.data(), static_cast(bytes.size())}; + return os; +} + +std::ostream &operator<<(std::ostream &os, const QString &str) +{ + os << str.toStdString(); + return os; +} + +// The PrintTo overloads use UniversalPrint to print strings in quotes. +// Even though this uses testing::internal, this is publically documented in +// gtest/gtest-printers.h. + +void PrintTo(const QByteArray &bytes, std::ostream *os) +{ + ::testing::internal::UniversalPrint(bytes.toStdString(), os); +} + +void PrintTo(QStringView str, std::ostream *os) +{ + ::testing::internal::UniversalPrint( + std::u16string{str.utf16(), static_cast(str.size())}, os); +} + +void PrintTo(const QString &str, std::ostream *os) +{ + ::testing::internal::UniversalPrint(str.toStdU16String(), os); +} diff --git a/tests/src/Test.hpp b/tests/src/Test.hpp new file mode 100644 index 000000000..064f90c6d --- /dev/null +++ b/tests/src/Test.hpp @@ -0,0 +1,22 @@ +#pragma once + +#include +#include + +#include + +class QString; +class QStringView; +class QByteArray; + +// This file is included in all TUs in chatterino-test to avoid ODR violations. +std::ostream &operator<<(std::ostream &os, QStringView str); +std::ostream &operator<<(std::ostream &os, const QByteArray &bytes); +std::ostream &operator<<(std::ostream &os, const QString &str); + +// NOLINTBEGIN(readability-identifier-naming) +// PrintTo is used for naming parameterized tests, and is part of gtest +void PrintTo(const QByteArray &bytes, std::ostream *os); +void PrintTo(QStringView str, std::ostream *os); +void PrintTo(const QString &str, std::ostream *os); +// NOLINTEND(readability-identifier-naming) diff --git a/tests/src/TestHelpers.hpp b/tests/src/TestHelpers.hpp deleted file mode 100644 index 30a2d0b1a..000000000 --- a/tests/src/TestHelpers.hpp +++ /dev/null @@ -1,66 +0,0 @@ -#pragma once - -#include -#include - -#include -#include - -template -class ReceivedMessage -{ - mutable std::mutex mutex; - - bool isSet{false}; - T t; - -public: - ReceivedMessage() = default; - - explicit operator bool() const - { - std::unique_lock lock(this->mutex); - - return this->isSet; - } - - ReceivedMessage &operator=(const T &newT) - { - std::unique_lock lock(this->mutex); - - this->isSet = true; - this->t = newT; - - return *this; - } - - bool operator==(const T &otherT) const - { - std::unique_lock lock(this->mutex); - - return this->t == otherT; - } - - const T *operator->() const - { - return &this->t; - } -}; - -inline std::ostream &operator<<(std::ostream &os, const QStringView &str) -{ - os << qUtf8Printable(str.toString()); - return os; -} - -inline std::ostream &operator<<(std::ostream &os, const QByteArray &bytes) -{ - os << bytes.toStdString(); - return os; -} - -inline std::ostream &operator<<(std::ostream &os, const QString &str) -{ - os << qUtf8Printable(str); - return os; -} diff --git a/tests/src/TwitchMessageBuilder.cpp b/tests/src/TwitchMessageBuilder.cpp index 77ddcdf46..d9d1d5a62 100644 --- a/tests/src/TwitchMessageBuilder.cpp +++ b/tests/src/TwitchMessageBuilder.cpp @@ -15,9 +15,8 @@ #include "providers/seventv/SeventvBadges.hpp" #include "providers/twitch/TwitchBadge.hpp" #include "singletons/Emotes.hpp" -#include "TestHelpers.hpp" +#include "Test.hpp" -#include #include #include #include diff --git a/tests/src/TwitchPubSubClient.cpp b/tests/src/TwitchPubSubClient.cpp index 30e02e567..728b0e5bb 100644 --- a/tests/src/TwitchPubSubClient.cpp +++ b/tests/src/TwitchPubSubClient.cpp @@ -4,12 +4,12 @@ #include "providers/twitch/pubsubmessages/AutoMod.hpp" #include "providers/twitch/pubsubmessages/Whisper.hpp" #include "providers/twitch/TwitchAccount.hpp" -#include "TestHelpers.hpp" +#include "Test.hpp" -#include #include #include +#include #include using namespace chatterino; @@ -33,6 +33,47 @@ using namespace std::chrono_literals; #ifdef RUN_PUBSUB_TESTS +template +class ReceivedMessage +{ + mutable std::mutex mutex; + + bool isSet{false}; + T t; + +public: + ReceivedMessage() = default; + + explicit operator bool() const + { + std::unique_lock lock(this->mutex); + + return this->isSet; + } + + ReceivedMessage &operator=(const T &newT) + { + std::unique_lock lock(this->mutex); + + this->isSet = true; + this->t = newT; + + return *this; + } + + bool operator==(const T &otherT) const + { + std::unique_lock lock(this->mutex); + + return this->t == otherT; + } + + const T *operator->() const + { + return &this->t; + } +}; + class FTest : public PubSub { public: diff --git a/tests/src/Updates.cpp b/tests/src/Updates.cpp index da4762517..ce16f329f 100644 --- a/tests/src/Updates.cpp +++ b/tests/src/Updates.cpp @@ -1,8 +1,8 @@ #include "singletons/Updates.hpp" #include "common/Version.hpp" +#include "Test.hpp" -#include #include using namespace chatterino; diff --git a/tests/src/UtilTwitch.cpp b/tests/src/UtilTwitch.cpp index 6a0b58d9f..3a2a7b41b 100644 --- a/tests/src/UtilTwitch.cpp +++ b/tests/src/UtilTwitch.cpp @@ -1,6 +1,6 @@ +#include "Test.hpp" #include "util/Twitch.hpp" -#include #include #include #include @@ -72,9 +72,8 @@ TEST(UtilTwitch, StripUserName) stripUserName(userName); EXPECT_EQ(userName, expectedUserName) - << qUtf8Printable(userName) << " (" << qUtf8Printable(inputUserName) - << ") did not match expected value " - << qUtf8Printable(expectedUserName); + << userName << " (" << inputUserName + << ") did not match expected value " << expectedUserName; } } @@ -153,10 +152,8 @@ TEST(UtilTwitch, StripChannelName) stripChannelName(userName); EXPECT_EQ(userName, expectedChannelName) - << qUtf8Printable(userName) << " (" - << qUtf8Printable(inputChannelName) - << ") did not match expected value " - << qUtf8Printable(expectedChannelName); + << userName << " (" << inputChannelName + << ") did not match expected value " << expectedChannelName; } } @@ -259,14 +256,12 @@ TEST(UtilTwitch, ParseUserNameOrID) auto [actualUserName, actualUserID] = parseUserNameOrID(input); EXPECT_EQ(actualUserName, expectedUserName) - << "name " << qUtf8Printable(actualUserName) << " (" - << qUtf8Printable(input) << ") did not match expected value " - << qUtf8Printable(expectedUserName); + << "name " << actualUserName << " (" << input + << ") did not match expected value " << expectedUserName; EXPECT_EQ(actualUserID, expectedUserID) - << "id " << qUtf8Printable(actualUserID) << " (" - << qUtf8Printable(input) << ") did not match expected value " - << qUtf8Printable(expectedUserID); + << "id " << actualUserID << " (" << input + << ") did not match expected value " << expectedUserID; } } @@ -319,7 +314,7 @@ TEST(UtilTwitch, UserLoginRegexp) auto actual = regexp.match(inputUserLogin); EXPECT_EQ(match.hasMatch(), expectedMatch) - << qUtf8Printable(inputUserLogin) << " did not match as expected"; + << inputUserLogin << " did not match as expected"; } } @@ -371,7 +366,7 @@ TEST(UtilTwitch, UserNameRegexp) auto actual = regexp.match(inputUserLogin); EXPECT_EQ(match.hasMatch(), expectedMatch) - << qUtf8Printable(inputUserLogin) << " did not match as expected"; + << inputUserLogin << " did not match as expected"; } } @@ -405,8 +400,7 @@ TEST(UtilTwitch, CleanHelixColor) cleanHelixColorName(actualColor); EXPECT_EQ(actualColor, expectedColor) - << qUtf8Printable(inputColor) << " cleaned up to " - << qUtf8Printable(actualColor) << " instead of " - << qUtf8Printable(expectedColor); + << inputColor << " cleaned up to " << actualColor << " instead of " + << expectedColor; } } diff --git a/tests/src/XDGDesktopFile.cpp b/tests/src/XDGDesktopFile.cpp index bffe529aa..69f4d3706 100644 --- a/tests/src/XDGDesktopFile.cpp +++ b/tests/src/XDGDesktopFile.cpp @@ -1,6 +1,7 @@ #include "util/XDGDesktopFile.hpp" -#include +#include "Test.hpp" + #include #if defined(Q_OS_UNIX) and !defined(Q_OS_DARWIN) diff --git a/tests/src/XDGHelper.cpp b/tests/src/XDGHelper.cpp index 3926d21d9..3ab48daa3 100644 --- a/tests/src/XDGHelper.cpp +++ b/tests/src/XDGHelper.cpp @@ -1,8 +1,7 @@ #include "util/XDGHelper.hpp" -#include "TestHelpers.hpp" +#include "Test.hpp" -#include #include #if defined(Q_OS_UNIX) and !defined(Q_OS_DARWIN) diff --git a/tests/src/main.cpp b/tests/src/main.cpp index 3b24a9978..6c82f632c 100644 --- a/tests/src/main.cpp +++ b/tests/src/main.cpp @@ -1,8 +1,8 @@ #include "common/network/NetworkManager.hpp" #include "singletons/Resources.hpp" #include "singletons/Settings.hpp" +#include "Test.hpp" -#include #include #include #include