Commit 5b96b7f9 authored by Pádraig Ó Conbhuí's avatar Pádraig Ó Conbhuí
Browse files

Added test runner and unit test for list_files.

parent 20736e14
......@@ -19,15 +19,25 @@ set_target_properties(
target_compile_features(libmegadep PUBLIC cxx_std_17)
target_include_directories(libmegadep PRIVATE src)
enable_testing()
add_executable(
libmegadep.test
src/megadep/megadep.test.cpp
)
target_link_libraries(libmegadep.test libmegadep)
target_include_directories(libmegadep.test PRIVATE src)
option(MEGADEP_ENABLE_TESTING "Enable testing for Megadep" ON)
add_test(
NAME libmegadep.test
COMMAND libmegadep.test ${CMAKE_CURRENT_SOURCE_DIR}
)
if(MEGADEP_ENABLE_TESTING)
enable_testing()
add_subdirectory(external/catch2)
add_subdirectory(tests)
add_executable(
libmegadep.test
src/megadep/megadep.test.cpp
)
target_link_libraries(
libmegadep.test PRIVATE libmegadep medagep::catch2_runner
)
target_include_directories(libmegadep.test PRIVATE src)
add_test(
NAME libmegadep.test
COMMAND libmegadep.test
WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}
)
endif()
#include <catch2/catch.hpp>
#include <iostream>
#include <stdio.h>
#include <array>
#include <megadep/megadep.hpp>
int main(int argc, char *argv[]) {
if (argc != 2) {
std::cout << "Must provide directory argument!\n";
return 1;
// This test assumes the test runner is run in the project root directory.
TEST_CASE("megadep::list_files recursively lists files in a directory",
"[list_files][unit]") {
// Compare list_files with find.
// list_files and find should return the same files.
auto files = megadep::list_files("src");
// Make sure we got something!
REQUIRE(files.size() > 0);
// Run system find and get list of files
FILE* find_fd = popen("find src -type f", "r");
std::vector<std::string> find_files;
std::array<char, 1024> line_buf;
while(fgets(line_buf.data(), line_buf.size(), find_fd) != NULL) {
std::string line = line_buf.data();
line.resize(line.size()-1); // strip newline
find_files.push_back(line);
}
auto files = megadep::list_files(argv[1]); // NOLINT
for (const auto &file : files) {
std::cout << file << '\n';
// Sort both lists so we can compare like-for-like
std::sort(files.begin(), files.end());
std::sort(find_files.begin(), find_files.end());
REQUIRE(files.size() == find_files.size());
for(size_t i=0; i<files.size(); i++) {
REQUIRE(files[i] == find_files[i]);
}
return 0;
}
add_library(
catch2_runner
catch2_runner.main.cpp
)
target_link_libraries(catch2_runner PUBLIC Catch2::Catch2)
add_library(medagep::catch2_runner ALIAS catch2_runner)
#define CATCH_CONFIG_MAIN
#include <catch2/catch.hpp>
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment