Add tests for string_to_vector().

This commit is contained in:
tastytea 2020-06-30 07:32:02 +02:00
parent b4c4393cd8
commit 45f69b1c75
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
5 changed files with 150 additions and 22 deletions

View File

@ -34,6 +34,12 @@ if(WITH_CLANG-TIDY)
"-quiet") "-quiet")
endif() endif()
# Dependencies.
find_package(PkgConfig REQUIRED)
pkg_check_modules(cgicc REQUIRED IMPORTED_TARGET cgicc)
find_package(nlohmann_json REQUIRED CONFIG)
find_package(Filesystem REQUIRED COMPONENTS Final Experimental)
add_subdirectory(src) add_subdirectory(src)
if(WITH_TESTS) if(WITH_TESTS)

View File

@ -1,10 +1,5 @@
include(GNUInstallDirs) include(GNUInstallDirs)
find_package(PkgConfig REQUIRED)
pkg_check_modules(cgicc REQUIRED IMPORTED_TARGET cgicc)
find_package(nlohmann_json REQUIRED CONFIG)
find_package(Filesystem REQUIRED COMPONENTS Final Experimental)
configure_file(fs-compat.hpp.in fs-compat.hpp @ONLY) configure_file(fs-compat.hpp.in fs-compat.hpp @ONLY)
add_executable(${PROJECT_NAME} main.cpp) add_executable(${PROJECT_NAME} main.cpp)

View File

@ -1,5 +1,16 @@
include(CTest) include(CTest)
file(GLOB sources_testlib ../src/*.cpp)
add_library(${PROJECT_NAME}_testlib SHARED ${sources_testlib})
target_include_directories(${PROJECT_NAME}_testlib
PUBLIC
"$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>"
"$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}/src>")
target_link_libraries(${PROJECT_NAME}_testlib
PUBLIC PkgConfig::cgicc nlohmann_json std::filesystem)
file(GLOB sources_tests test_*.cpp) file(GLOB sources_tests test_*.cpp)
find_package(Catch2 CONFIG) find_package(Catch2 CONFIG)
@ -8,7 +19,7 @@ if(Catch2_FOUND) # Catch 2.x
include(Catch) include(Catch)
add_executable(all_tests main.cpp ${sources_tests}) add_executable(all_tests main.cpp ${sources_tests})
target_link_libraries(all_tests target_link_libraries(all_tests
PRIVATE Catch2::Catch2) PRIVATE Catch2::Catch2 ${PROJECT_NAME}_testlib)
target_include_directories(all_tests PRIVATE "/usr/include/catch2") target_include_directories(all_tests PRIVATE "/usr/include/catch2")
catch_discover_tests(all_tests EXTRA_ARGS "${EXTRA_TEST_ARGS}") catch_discover_tests(all_tests EXTRA_ARGS "${EXTRA_TEST_ARGS}")
else() # Catch 1.x else() # Catch 1.x
@ -17,6 +28,7 @@ else() # Catch 1.x
foreach(src ${sources_tests}) foreach(src ${sources_tests})
get_filename_component(bin ${src} NAME_WE) get_filename_component(bin ${src} NAME_WE)
add_executable(${bin} main.cpp ${src}) add_executable(${bin} main.cpp ${src})
target_link_libraries(${bin} PRIVATE ${PROJECT_NAME}_testlib)
add_test(${bin} ${bin} "${EXTRA_TEST_ARGS}") add_test(${bin} ${bin} "${EXTRA_TEST_ARGS}")
endforeach() endforeach()
else() else()

View File

@ -1,19 +1,3 @@
/* This file is part of FediBlock-backend.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, version 3.
*
* 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#define CATCH_CONFIG_MAIN #define CATCH_CONFIG_MAIN
#include <catch.hpp> #include <catch.hpp>

View File

@ -0,0 +1,131 @@
#include "cgi.cpp"
#include <catch.hpp>
#include <exception>
#include <string>
#include <vector>
namespace FediBlock
{
using std::string;
using std::vector;
SCENARIO("string_to_vector()")
{
bool exception{false};
vector<string> vec;
{
const string str{"a,b,c,d"};
WHEN("string is " + str)
{
try
{
vec = string_to_vector(str);
}
catch (std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN(R"(vector is {"a", "b", "c", "d"})")
{
REQUIRE_FALSE(exception);
REQUIRE(vec == vector<string>({"a", "b", "c", "d"}));
}
}
}
{
const string str{""};
WHEN("string is \"" + str + '"')
{
try
{
vec = string_to_vector(str);
}
catch (std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN(R"(vector is {})")
{
REQUIRE_FALSE(exception);
REQUIRE(vec == vector<string>());
}
}
}
{
const string str{"a,b,"};
WHEN("string is \"" + str + '"')
{
try
{
vec = string_to_vector(str);
}
catch (std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN(R"(vector is {"a", "b"})")
{
REQUIRE_FALSE(exception);
REQUIRE(vec == vector<string>({"a", "b"}));
}
}
}
{
const string str{",a,b"};
WHEN("string is \"" + str + '"')
{
try
{
vec = string_to_vector(str);
}
catch (std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN(R"(vector is {"a", "b"})")
{
REQUIRE_FALSE(exception);
REQUIRE(vec == vector<string>({"a", "b"}));
}
}
}
{
const string str{",,,"};
WHEN("string is \"" + str + '"')
{
try
{
vec = string_to_vector(str);
}
catch (std::exception &)
{
exception = true;
}
THEN("No exception is thrown")
AND_THEN(R"(vector is {})")
{
REQUIRE_FALSE(exception);
REQUIRE(vec == vector<string>());
}
}
}
}
} // namespace FediBlock