From 6c040fa95176acc6ee6a84335c319a4e7d33f420 Mon Sep 17 00:00:00 2001 From: tastytea Date: Fri, 21 May 2021 09:23:42 +0200 Subject: [PATCH] Add first test. - Compile everything in src/ except main.cpp into a static library - Link the static library into tests - Add test for zip::list() --- README.adoc | 1 + src/CMakeLists.txt | 29 +++++++++++++++++++-------- tests/CMakeLists.txt | 4 +++- tests/test.zip | Bin 0 -> 504 bytes tests/test_zip_list.cpp | 42 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 67 insertions(+), 9 deletions(-) create mode 100644 tests/test.zip create mode 100644 tests/test_zip_list.cpp diff --git a/README.adoc b/README.adoc index c6039b0..b2022b3 100644 --- a/README.adoc +++ b/README.adoc @@ -66,6 +66,7 @@ git clone https://schlomp.space/tastytea/epubgrep.git mkdir -p build && cd build cmake .. cmake --build . -- --jobs=$(nproc --ignore=1) +# cd tests && ctest -------------------------------------------------------------------------------- .CMake options: diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 9e9cc23..bf0db77 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -3,21 +3,34 @@ include(GNUInstallDirs) configure_file("version.hpp.in" "version.hpp" @ONLY) configure_file("fs-compat.hpp.in" "fs-compat.hpp" @ONLY) -add_executable(${PROJECT_NAME} main.cpp) +# The library is only here for the tests. +add_library(${PROJECT_NAME}_lib STATIC) file(GLOB_RECURSE sources_src *.cpp) file(GLOB_RECURSE headers_src *.hpp) -target_sources(${PROJECT_NAME} - PRIVATE "${sources_src}" "${headers_src}") +list(REMOVE_ITEM sources_src "${CMAKE_CURRENT_SOURCE_DIR}/main.cpp") + +target_sources(${PROJECT_NAME}_lib + PUBLIC "${sources_src}" "${headers_src}") unset(sources_src) unset(headers_src) -target_link_libraries(${PROJECT_NAME} - Boost::program_options Boost::locale - std::filesystem LibArchive::LibArchive fmt::fmt) +target_link_libraries(${PROJECT_NAME}_lib + PUBLIC + Boost::program_options + Boost::locale + std::filesystem + LibArchive::LibArchive + fmt::fmt) -target_include_directories(${PROJECT_NAME} - PRIVATE "$") +target_include_directories(${PROJECT_NAME}_lib + PUBLIC + "$" + "$") + +add_executable(${PROJECT_NAME} "main.cpp") +target_link_libraries(${PROJECT_NAME} + PRIVATE ${PROJECT_NAME}_lib) install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION "${CMAKE_INSTALL_BINDIR}") diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 8261b1c..1f897e9 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,6 +1,7 @@ include(CTest) file(GLOB sources_tests test_*.cpp) +file(COPY "test.zip" DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) find_package(Catch2 CONFIG) @@ -8,7 +9,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}_lib) 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 +18,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} ${PROJECT_NAME}_lib) add_test(${bin} ${bin} "${EXTRA_TEST_ARGS}") endforeach() else() diff --git a/tests/test.zip b/tests/test.zip new file mode 100644 index 0000000000000000000000000000000000000000..371c403975e8e0e4182f35fceda159f12d203bb6 GIT binary patch literal 504 zcmWIWW@h1H0D(TstwCT0l;B~IVJJy0E>TF!&q+xw(hm*cWMH;&T%OC$rL;&2BE;Lg_(M*Aun3 + +#include +#include +#include + +SCENARIO("epubgrep::zip::list() doesn't fail and returns the right file list") +{ + bool exception{false}; + std::vector filelist; + + GIVEN("Our test zip file") + { + fs::path zipfile{"test.zip"}; + + REQUIRE(fs::exists(zipfile)); + + WHEN("We list the file contents") + { + try + { + filelist = epubgrep::zip::list(zipfile); + } + catch (const std::exception &) + { + exception = true; + } + + THEN("No exception is thrown") + AND_THEN("It returns the file contents correctly") + { + REQUIRE_FALSE(exception); + REQUIRE(filelist.at(0) == "test folder/"); + REQUIRE(filelist.at(1) == "test folder/test file"); + REQUIRE(filelist.at(2) == "test folder/😊"); + } + } + } +}