Add first test.

This commit is contained in:
tastytea 2020-03-02 07:05:18 +01:00
parent bc293a2c61
commit 96d6e066fb
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
6 changed files with 148 additions and 4 deletions

View File

@ -46,8 +46,8 @@ if(WITH_GUI)
add_subdirectory(gui)
endif()
# if(WITH_TESTS)
# add_subdirectory(tests)
# endif()
if(WITH_TESTS)
add_subdirectory(tests)
endif()
include(cmake/packages.cmake)

View File

@ -108,7 +108,7 @@ cmake --build . -- -j$(nproc --ignore=1)
.CMake options:
* `-DCMAKE_BUILD_TYPE=Debug` for a debug build.
// * `-DWITH_TESTS=YES` if you want to compile the tests.
* `-DWITH_TESTS=YES` if you want to compile the tests.
* `-DWITH_GUI=NO` to only compile and install the library.
* `-DWITH_CLANG-TIDY=YES` to check the sourcecode with
link:{uri-clang-tidy}[clang-tidy] while compiling.

29
tests/CMakeLists.txt Normal file
View File

@ -0,0 +1,29 @@
include(CTest)
file(GLOB sources_tests test_*.cpp)
find_package(Catch2 CONFIG)
find_package(Filesystem REQUIRED)
if(Catch2_FOUND) # Catch 2.x
include(Catch)
add_executable(all_tests main.cpp ${sources_tests})
target_link_libraries(all_tests
PRIVATE Catch2::Catch2 std::filesystem ${PROJECT_NAME})
target_include_directories(all_tests PRIVATE "/usr/include/catch2")
catch_discover_tests(all_tests EXTRA_ARGS "${EXTRA_TEST_ARGS}")
else() # Catch 1.x
if(EXISTS "/usr/include/catch.hpp")
message(STATUS "Catch 1.x found.")
foreach(src ${sources_tests})
get_filename_component(bin ${src} NAME_WE)
add_executable(${bin} main.cpp ${src})
target_link_libraries(${bin}
PRIVATE std::filesystem ${PROJECT_NAME})
add_test(${bin} ${bin} "${EXTRA_TEST_ARGS}")
endforeach()
else()
message(FATAL_ERROR
"Neither Catch 2.x nor Catch 1.x could be found.")
endif()
endif()

View File

@ -0,0 +1,27 @@
/* This file is part of FediPotato.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifndef FEDIPOTATO_TESTS_ENVIRONMENT_VARIABLES_HPP
#define FEDIPOTATO_TESTS_ENVIRONMENT_VARIABLES_HPP
namespace FediPotato
{
//! Set XDG_CONFIG_HOME to a temporary directory.
void set_environment_variables();
} // namespace FediPotato
#endif // FEDIPOTATO_TESTS_ENVIRONMENT_VARIABLES_HPP

39
tests/main.cpp Normal file
View File

@ -0,0 +1,39 @@
/* This file is part of FediPotato.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define CATCH_CONFIG_MAIN
#include "environment_variables.hpp"
#include <catch.hpp>
#include <cstdlib>
#if __has_include(<filesystem>)
# include <filesystem>
#else
# include <experimental/filesystem>
#endif
namespace FediPotato
{
namespace fs = std::filesystem;
void set_environment_variables()
{
setenv("XDG_CONFIG_HOME", fs::temp_directory_path().c_str(), 1);
}
} // namespace FediPotato

View File

@ -0,0 +1,49 @@
/* This file is part of FediPotato.
* Copyright © 2020 tastytea <tastytea@tastytea.de>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include "environment_variables.hpp"
#include "fedipotato.hpp"
#include <catch.hpp>
#include <exception>
namespace FediPotato
{
SCENARIO ("Load FediPotato.")
{
set_environment_variables();
bool exception{false};
WHEN ("Instance of FediPotato is constructed.")
{
try
{
FediPotato potato("FediPotato");
}
catch (const std::exception &e)
{
exception = true;
}
THEN ("No exception is thrown.")
{
REQUIRE_FALSE(exception);
}
}
}
} // namespace FediPotato