diff --git a/CMakeLists.txt b/CMakeLists.txt index 77cf69d..780c940 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,12 @@ if(WITH_CLANG-TIDY) "-quiet") 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) if(WITH_TESTS) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index c66aaf2..a4f8958 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,10 +1,5 @@ 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) add_executable(${PROJECT_NAME} main.cpp) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8261b1c..2730a64 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,5 +1,16 @@ include(CTest) +file(GLOB sources_testlib ../src/*.cpp) +add_library(${PROJECT_NAME}_testlib SHARED ${sources_testlib}) + +target_include_directories(${PROJECT_NAME}_testlib + PUBLIC + "$" + "$") + +target_link_libraries(${PROJECT_NAME}_testlib + PUBLIC PkgConfig::cgicc nlohmann_json std::filesystem) + file(GLOB sources_tests test_*.cpp) find_package(Catch2 CONFIG) @@ -8,7 +19,7 @@ if(Catch2_FOUND) # Catch 2.x include(Catch) add_executable(all_tests main.cpp ${sources_tests}) target_link_libraries(all_tests - PRIVATE Catch2::Catch2) + PRIVATE Catch2::Catch2 ${PROJECT_NAME}_testlib) target_include_directories(all_tests PRIVATE "/usr/include/catch2") catch_discover_tests(all_tests EXTRA_ARGS "${EXTRA_TEST_ARGS}") else() # Catch 1.x @@ -17,6 +28,7 @@ else() # Catch 1.x foreach(src ${sources_tests}) get_filename_component(bin ${src} NAME_WE) add_executable(${bin} main.cpp ${src}) + target_link_libraries(${bin} PRIVATE ${PROJECT_NAME}_testlib) add_test(${bin} ${bin} "${EXTRA_TEST_ARGS}") endforeach() else() diff --git a/tests/main.cpp b/tests/main.cpp index b70e7ab..e48147e 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,19 +1,3 @@ -/* This file is part of FediBlock-backend. - * Copyright © 2020 tastytea - * - * 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 . - */ - #define CATCH_CONFIG_MAIN #include diff --git a/tests/test_string_to_vector.cpp b/tests/test_string_to_vector.cpp new file mode 100644 index 0000000..803c6d5 --- /dev/null +++ b/tests/test_string_to_vector.cpp @@ -0,0 +1,131 @@ +#include "cgi.cpp" + +#include + +#include +#include +#include + +namespace FediBlock +{ + +using std::string; +using std::vector; + +SCENARIO("string_to_vector()") +{ + bool exception{false}; + vector 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({"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()); + } + } + } + + { + 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({"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({"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()); + } + } + } +} + +} // namespace FediBlock