From cbd93f95374d2377b21f2386fb47c8f6c517f45b Mon Sep 17 00:00:00 2001 From: Rasmus Karlsson Date: Sat, 11 May 2019 14:13:03 +0200 Subject: [PATCH] Add tests for the UsernameSet and Prefix classes How to build test: mkdir build_test cd build_test cmake -DBUILD_TESTS=ON .. make -j Then to run the tests, use either: make test ctest ./chatterino-test --- CMakeLists.txt | 37 ++++++++++ tests/src/UsernameSet.cpp | 138 ++++++++++++++++++++++++++++++++++++++ tests/src/main.cpp | 3 + 3 files changed, 178 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 tests/src/UsernameSet.cpp create mode 100644 tests/src/main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 000000000..4bcd983a4 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,37 @@ +cmake_minimum_required(VERSION 3.8) + +project(chatterino) + +include_directories(src) + +set(chatterino_SOURCES + src/common/UsernameSet.cpp + ) + +find_package(Qt5Widgets CONFIG REQUIRED) +find_package(Qt5 5.9.0 REQUIRED COMPONENTS + Core + ) + +# set(CMAKE_AUTOMOC ON) + +if (BUILD_TESTS) + message("++ Tests enabled") + find_package(GTest) + enable_testing() + + add_executable(chatterino-test + ${chatterino_SOURCES} + + tests/src/main.cpp + tests/src/UsernameSet.cpp + ) + + target_link_libraries(chatterino-test Qt5::Core) + + target_link_libraries(chatterino-test gtest gtest_main) + + gtest_discover_tests(chatterino-test) +else() + message(FATAL_ERROR "This cmake file is only intended for tests right now. Use qmake to build chatterino2") +endif() diff --git a/tests/src/UsernameSet.cpp b/tests/src/UsernameSet.cpp new file mode 100644 index 000000000..7098941bf --- /dev/null +++ b/tests/src/UsernameSet.cpp @@ -0,0 +1,138 @@ +#include "common/UsernameSet.hpp" + +#include +#include + +chatterino::Prefix prefix_pajlada(QString("pajlada")); +chatterino::Prefix prefix_Pajlada(QString("Pajlada")); +chatterino::Prefix prefix_randers(QString("randers")); +chatterino::Prefix prefix_Chancu(QString("ch")); + +TEST(Prefix, isStartOf) +{ + EXPECT_TRUE(prefix_pajlada.isStartOf("pajlada")); + EXPECT_TRUE(prefix_pajlada.isStartOf("Pajlada")); + EXPECT_FALSE(prefix_pajlada.isStartOf("randers")); + + EXPECT_TRUE(prefix_Pajlada.isStartOf("pajlada")); + EXPECT_TRUE(prefix_Pajlada.isStartOf("Pajlada")); + EXPECT_TRUE(prefix_Pajlada.isStartOf("pajbot")); + EXPECT_TRUE(prefix_Pajlada.isStartOf("Pajbot")); + EXPECT_FALSE(prefix_Pajlada.isStartOf("randers")); +} + +TEST(Prefix, EqualsOperator) +{ + EXPECT_EQ(prefix_pajlada, prefix_Pajlada); + EXPECT_NE(prefix_pajlada, prefix_randers); +} + +TEST(UsernameSet, insert) +{ + std::pair p; + chatterino::UsernameSet set; + + EXPECT_EQ(set.size(), 0); + + p = set.insert("pajlada"); + EXPECT_TRUE(p.second); + + EXPECT_EQ(set.size(), 1); + + p = set.insert("pajlada"); + EXPECT_FALSE(p.second); + + EXPECT_EQ(set.size(), 1); + + p = set.insert("pajbot"); + EXPECT_TRUE(p.second); + + EXPECT_EQ(set.size(), 2); + + p = set.insert("pajlada"); + EXPECT_FALSE(p.second); + + EXPECT_EQ(set.size(), 2); + + p = set.insert("PAJLADA"); + EXPECT_FALSE(p.second); + + EXPECT_EQ(set.size(), 2); +} + +TEST(UsernameSet, CollisionTest) +{ + QString s; + chatterino::UsernameSet set; + chatterino::Prefix prefix("not_"); + + set.insert("pajlada"); + set.insert("Chancu"); + set.insert("chief_tony"); + set.insert("ChodzacyKac"); + set.insert("ChatAbuser"); + set.insert("Normies_GTFO"); + set.insert("not_remzy"); + set.insert("Mullo2500"); + set.insert("muggedbyapie"); + + EXPECT_EQ(set.size(), 9); + + { + QStringList result; + QStringList expectation{"Normies_GTFO", "not_remzy"}; + auto subrange = set.subrange(QString("not_")); + std::copy(subrange.begin(), subrange.end(), std::back_inserter(result)); + EXPECT_EQ(expectation, result); + } + + { + QStringList result; + QStringList expectation{}; + auto subrange = set.subrange(QString("te")); + std::copy(subrange.begin(), subrange.end(), std::back_inserter(result)); + EXPECT_EQ(expectation, result); + } + + { + QStringList result; + QStringList expectation{"pajlada"}; + auto subrange = set.subrange(QString("PA")); + std::copy(subrange.begin(), subrange.end(), std::back_inserter(result)); + EXPECT_EQ(expectation, result); + } + + { + QStringList result; + QStringList expectation{"pajlada"}; + auto subrange = set.subrange(QString("pajlada")); + std::copy(subrange.begin(), subrange.end(), std::back_inserter(result)); + EXPECT_EQ(expectation, result); + } + + { + QStringList result; + QStringList expectation{"pajlada"}; + auto subrange = set.subrange(QString("Pajl")); + std::copy(subrange.begin(), subrange.end(), std::back_inserter(result)); + EXPECT_EQ(expectation, result); + } + + { + QStringList result; + QStringList expectation{"Chancu", "ChatAbuser", "chief_tony", + "ChodzacyKac"}; + auto subrange = set.subrange(QString("chan")); + std::copy(subrange.begin(), subrange.end(), std::back_inserter(result)); + EXPECT_EQ(expectation, result); + } + + { + QStringList result; + QStringList expectation{"muggedbyapie", "Mullo2500"}; + auto subrange = set.subrange(QString("mu")); + std::copy(subrange.begin(), subrange.end(), std::back_inserter(result)); + EXPECT_EQ(expectation, result); + } +} + diff --git a/tests/src/main.cpp b/tests/src/main.cpp new file mode 100644 index 000000000..db640b12d --- /dev/null +++ b/tests/src/main.cpp @@ -0,0 +1,3 @@ +#include + +// do nothing