UnitTests: Cleanup + StringConvert/ChatCommand tests (PR #25353)

(cherry picked from commit 8ce3635d39)
This commit is contained in:
Treeston
2020-08-29 22:03:22 +02:00
committed by Shauren
parent 24d12b93e5
commit 2aaebc193c
7 changed files with 260 additions and 9 deletions

View File

@@ -9,21 +9,33 @@
# implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
CollectSourceFiles(
${CMAKE_CURRENT_SOURCE_DIR}/common
COMMON_SOURCES
${CMAKE_CURRENT_SOURCE_DIR}
TEST_SOURCES
)
add_executable(tests-common ${COMMON_SOURCES})
GroupSources(${CMAKE_CURRENT_SOURCE_DIR})
target_link_libraries(tests-common
add_executable(tests ${TEST_SOURCES})
target_link_libraries(tests
PRIVATE
trinity-core-interface
common
game
Catch2::Catch2)
catch_discover_tests(tests-common)
CollectIncludeDirectories(
${CMAKE_CURRENT_SOURCE_DIR}
TEST_INCLUDES)
set_target_properties(tests-common
target_include_directories(tests
PUBLIC
${TEST_INCLUDES}
PRIVATE
${CMAKE_CURRENT_BINARY_DIR})
catch_discover_tests(tests)
set_target_properties(tests
PROPERTIES
FOLDER
"tests")

View File

@@ -16,7 +16,7 @@
*/
#define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER
#include "catch2/catch.hpp"
#include "tc_catch2.h"
#include "EventMap.h"

View File

@@ -0,0 +1,131 @@
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tc_catch2.h"
#include "StringConvert.h"
TEST_CASE("String to uint32", "[StringConvert]")
{
REQUIRE(Trinity::StringTo<uint32>("42") == 42);
REQUIRE(Trinity::StringTo<uint32>("42", 10) == 42);
REQUIRE(Trinity::StringTo<uint32>(" 42") == std::nullopt);
REQUIRE(Trinity::StringTo<uint32>("tail42") == std::nullopt);
REQUIRE(Trinity::StringTo<uint32>("42tail") == std::nullopt);
REQUIRE(Trinity::StringTo<uint32>("ff", 16) == 0xff);
REQUIRE(Trinity::StringTo<uint32>("0xff") == std::nullopt);
REQUIRE(Trinity::StringTo<uint32>("0xff", 0) == 0xff);
REQUIRE(Trinity::StringTo<uint32>("101010", 2) == 0b101010);
REQUIRE(Trinity::StringTo<uint32>("0b101010") == std::nullopt);
REQUIRE(Trinity::StringTo<uint32>("0b101010", 0) == 0b101010);
REQUIRE(Trinity::StringTo<uint32>("5000000000") == std::nullopt);
REQUIRE(Trinity::StringTo<uint32>("100000000", 16) == std::nullopt);
REQUIRE(Trinity::StringTo<uint32>("0x100000000", 0) == std::nullopt);
REQUIRE(Trinity::StringTo<uint32>("0xffffffff", 0) == 0xffffffff);
}
TEST_CASE("String to uint64", "[StringConvert]")
{
REQUIRE(Trinity::StringTo<uint64>("42") == 42);
REQUIRE(Trinity::StringTo<uint64>("42", 10) == 42);
REQUIRE(Trinity::StringTo<uint64>(" 42") == std::nullopt);
REQUIRE(Trinity::StringTo<uint64>("tail42") == std::nullopt);
REQUIRE(Trinity::StringTo<uint64>("42tail") == std::nullopt);
REQUIRE(Trinity::StringTo<uint64>("-1", 0) == std::nullopt);
REQUIRE(Trinity::StringTo<uint64>("ff", 16) == 0xff);
REQUIRE(Trinity::StringTo<uint64>("0xff") == std::nullopt);
REQUIRE(Trinity::StringTo<uint64>("0xff", 0) == 0xff);
REQUIRE(Trinity::StringTo<uint64>("101010", 2) == 0b101010);
REQUIRE(Trinity::StringTo<uint64>("0b101010") == std::nullopt);
REQUIRE(Trinity::StringTo<uint64>("0b101010", 0) == 0b101010);
REQUIRE(Trinity::StringTo<uint64>("5000000000") == 5000000000ULL);
REQUIRE(Trinity::StringTo<uint64>("100000000", 16) == 0x100000000ULL);
REQUIRE(Trinity::StringTo<uint64>("20000000000000000000") == std::nullopt);
REQUIRE(Trinity::StringTo<uint64>("10000000000000000", 16) == std::nullopt);
REQUIRE(Trinity::StringTo<uint64>("0x10000000000000000", 0) == std::nullopt);
REQUIRE(Trinity::StringTo<uint64>("0xFFFFFFFFFFFFFFFF", 0) == 0xffffffffffffffffULL);
}
TEST_CASE("String to int32", "[StringConvert]")
{
REQUIRE(Trinity::StringTo<int32>("-42") == -42);
REQUIRE(Trinity::StringTo<int32>("42") == 42);
REQUIRE(Trinity::StringTo<int32>("+42") == std::nullopt);
REQUIRE(Trinity::StringTo<int32>("--42") == std::nullopt);
REQUIRE(Trinity::StringTo<int32>("~42") == std::nullopt);
REQUIRE(Trinity::StringTo<int32>("42-") == std::nullopt);
REQUIRE(Trinity::StringTo<int32>("-ffff", 16) == -0xffff);
REQUIRE(Trinity::StringTo<int32>("ffffffff", 16) == std::nullopt);
REQUIRE(Trinity::StringTo<int32>("7fffffff", 16) == 0x7fffffff);
}
TEST_CASE("String to int64", "[StringConvert]")
{
REQUIRE(Trinity::StringTo<int64>("-42") == -42);
REQUIRE(Trinity::StringTo<int64>("42") == 42);
REQUIRE(Trinity::StringTo<int64>("+42") == std::nullopt);
REQUIRE(Trinity::StringTo<int64>("--42") == std::nullopt);
REQUIRE(Trinity::StringTo<int64>("~42") == std::nullopt);
REQUIRE(Trinity::StringTo<int64>("42-") == std::nullopt);
REQUIRE(Trinity::StringTo<int64>("-ffff", 16) == -0xffff);
REQUIRE(Trinity::StringTo<int64>("ffffffff", 16) == 0xffffffff);
REQUIRE(Trinity::StringTo<int64>("7fffffff", 16) == 0x7fffffff);
REQUIRE(Trinity::StringTo<int64>("ffffffffffffffff", 16) == std::nullopt);
REQUIRE(Trinity::StringTo<int64>("7fffffffffffffff", 16) == 0x7fffffffffffffffLL);
REQUIRE(Trinity::StringTo<int64>("-8500000000000000", 16) == std::nullopt);
}
TEST_CASE("String to smaller integer types", "[StringConvert]")
{
REQUIRE(Trinity::StringTo<uint8>("0xff", 0) == 0xff);
REQUIRE(Trinity::StringTo<uint8>("0x1ff", 0) == std::nullopt);
REQUIRE(Trinity::StringTo<int8>("0xff", 0) == std::nullopt);
REQUIRE(Trinity::StringTo<int8>("0x7f", 0) == 0x7f);
REQUIRE(Trinity::StringTo<int8>("-7f", 16) == -0x7f);
REQUIRE(Trinity::StringTo<uint16>("0x1ff", 0) == 0x1ff);
REQUIRE(Trinity::StringTo<uint16>("0x1ffff", 0) == std::nullopt);
REQUIRE(Trinity::StringTo<uint16>("-1", 0) == std::nullopt);
REQUIRE(Trinity::StringTo<int16>("0x1ff", 0) == 0x1ff);
REQUIRE(Trinity::StringTo<int16>("0xffff", 0) == std::nullopt);
REQUIRE(Trinity::StringTo<int16>("0x7fff", 0) == 0x7fff);
REQUIRE(Trinity::StringTo<int16>("-1", 0) == -1);
}
TEST_CASE("Test.String to boolean", "[StringConvert]")
{
REQUIRE(Trinity::StringTo<bool>("true") == true);
REQUIRE(Trinity::StringTo<bool>("false") == false);
REQUIRE(Trinity::StringTo<bool>("ture") == std::nullopt);
REQUIRE(Trinity::StringTo<bool>("true", 10) == std::nullopt);
REQUIRE(Trinity::StringTo<bool>("1") == true);
REQUIRE(Trinity::StringTo<bool>("1", 10) == true);
REQUIRE(Trinity::StringTo<bool>("0", 10) == false);
}

View File

@@ -16,7 +16,7 @@
*/
#define CATCH_CONFIG_ENABLE_CHRONO_STRINGMAKER
#include "catch2/catch.hpp"
#include "tc_catch2.h"
#include "Timer.h"

View File

@@ -0,0 +1,64 @@
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "tc_catch2.h"
#include "ChatCommand.h"
using namespace Trinity::ChatCommands;
using namespace std::string_view_literals;
template <typename F>
static void TestChatCommand(char const* c, F f, Optional<bool> expected = true)
{
bool r = ChatCommand("", 0, false, +f, "")(nullptr, c);
if (expected)
ASSERT(r == *expected);
}
TEST_CASE("Command return pass-through", "[ChatCommand]")
{
TestChatCommand("", [](ChatHandler*) { return true; }, true);
TestChatCommand("", [](ChatHandler*) { return false; }, false);
}
TEST_CASE("Command argument parsing", "[ChatCommand]")
{
TestChatCommand("42", [](ChatHandler*, uint32 u)
{
REQUIRE(u == 42);
return true;
});
TestChatCommand("true", [](ChatHandler*, uint32) { return true; }, false);
TestChatCommand("1 2 3 4 5 6 7 8 9 10", [](ChatHandler*, std::vector<uint8> v)
{
REQUIRE(v.size() == 10);
for (size_t i = 0; i < 10; ++i)
REQUIRE(v[i] == (i + 1));
return true;
});
TestChatCommand("|cffff0000|Hplayer:Test|h[Test]|h|r",
[](ChatHandler*, Hyperlink<player> player)
{
REQUIRE("Test"sv == *player);
return true;
}
);
}

44
tests/tc_catch2.h Normal file
View File

@@ -0,0 +1,44 @@
/*
* This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TRINITY_CATCH2_H
#define TRINITY_CATCH2_H
#include "Optional.h"
#include <iostream>
#include <typeinfo>
template <typename T>
std::ostream& operator<<(std::ostream& os, Optional<T> const& value)
{
os << "Opt";
if (value)
os << " { " << *value << " }";
else
os << " (<empty>)";
return os;
}
inline std::ostream& operator<<(std::ostream& os, std::nullopt_t)
{
os << "<empty>";
return os;
}
#include "catch2/catch.hpp"
#endif