Skip to content

Commit

Permalink
Added auto generation of puzzle test files.
Browse files Browse the repository at this point in the history
  • Loading branch information
DouglasWebster committed Oct 2, 2023
1 parent 4c47e6e commit 7bf9a4b
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 3 deletions.
12 changes: 10 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,10 @@ From the original cmake_template I have deleted the docker functionality as it i

## Using this template

The source folder is set up with the three directories.
#### *src* folder


The source folder is set up with the three sub folders:

* **cli_test** - This is a small program that the template uses to ensure that Catch2 testing is functioning correctly.
* **aoc_library** - This holds the code for generating library files.
Expand All @@ -79,8 +82,13 @@ The source folder is set up with the three directories.
2. checks if the folder has a header file named *day_xx.cpp* where *xx* is the day no. If not it creates the header file from the day_details.cpp.in file in the configured_files folder.
3. checks if there is a main.cpp file. If not it creates it from day_main.cpp.in in the configured_files folder, setting the include header file to day_xx.hpp.
4. sets the executable to **yyyy**-day**dd** where *yyyy* is the campaign year and *dd* is the day no and sets main.cpp as the initial source file.

#### *test* folder

The test folder has just the one sub folder - campaigns. Like the src folder it holds the yearly campaigns daily test files. As with the src folder if you start a new campaign year folder then copy the CMakeLists.txt file from a previous year into the new folder. When triggered it will create any a CMakeLists.txt file in any dayXX folder that doesn't already have one. This will then create a Catch2 test file with reference to the relevant puzzle day. The test file has a single test that fails.

The above sequence means that, once a campaign year folder has been set up and the default CMakeLists.txt file copied into it, all that is necessary to start a new problem day is to create that days folder. If it follows the naming convention then doing a CMake clean reconfigure will populate the new folder with the basic starting files and have the executable registered with CMake.
----
The above sequence means that, once a campaign year folder has been set up in both the src and test directories and the default CMakeLists.txt files copied into them, all that is necessary to start a new problem day is to create that days folder in the campaign years. If it follows the naming convention then doing a CMake clean reconfigure will populate the new folders with the basic starting files and have the executable registered with CMake.

Of course, once the CMakeLists.txt files are created then the user is free to re-configure them to their individual needs.

Expand Down
47 changes: 47 additions & 0 deletions configured_files/CMakeLists.txt.test.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
set(path ${CMAKE_CURRENT_LIST_DIR})
cmake_path(GET path FILENAME this_directory)

if(NOT EXISTS "${CMAKE_CURRENT_LIST_DIR}/@dir_name@_test.cpp")
message("Creating standard test file for this days solution.")
configure_file(
"${CMAKE_SOURCE_DIR}/configured_files/day_test.cpp.in"
"${CMAKE_CURRENT_LIST_DIR}/@dir_name@_test.cpp"
@ONLY)
endif()

add_executable(@parent_directory@_@dir_name@_test @dir_name@_test.cpp
${CMAKE_SOURCE_DIR}/src/campaigns/@parent_directory@/@dir_name@/@parent_directory@_@[email protected])

target_link_libraries(
@parent_directory@_@dir_name@_test
PRIVATE AofCode_Cpp::AofCode_Cpp_warnings
AofCode_Cpp::AofCode_Cpp_options
AofCode_Cpp::aoc_library
Catch2::Catch2WithMain)

if(WIN32 AND BUILD_SHARED_LIBS)
add_custom_command(
TARGET tests
PRE_BUILD
COMMAND ${CMAKE_COMMAND} -E copy $<TARGET_RUNTIME_DLLS:tests> $<TARGET_FILE_DIR:tests>
COMMAND_EXPAND_LISTS)
endif()

target_include_directories(
@parent_directory@_@dir_name@_test
PRIVATE ${CMAKE_SOURCE_DIR}/src/campaigns/@parent_directory@/@dir_name@)

# automatically discover tests that are defined in catch based test files you can modify the unittests. Set TEST_PREFIX
# to whatever you want, or use different for different binaries
catch_discover_tests(
@parent_directory@_@dir_name@_test
TEST_PREFIX
"campaign @parent_directory@ @dir_name@ tests: "
REPORTER
XML
OUTPUT_DIR
.
OUTPUT_PREFIX
"campaign @parent_directory@ @dir_name@ tests: "
OUTPUT_SUFFIX
.xml)
14 changes: 14 additions & 0 deletions configured_files/day_test.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <catch2/catch_test_macros.hpp>
#include <catch2/matchers/catch_matchers_container_properties.hpp>
#include <catch2/matchers/catch_matchers_vector.hpp>

#include <iostream>

#include <@parent_directory@_@[email protected]>

// start with a test case that does nothing but fail.

TEST_CASE(" @parent_directory@ @dir_name@ testing not started", "[@dir_name@]")
{
REQUIRE( false );
}
30 changes: 29 additions & 1 deletion test/campaigns/2022/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,32 @@ endif()

include(${Catch2_SOURCE_DIR}/extras/Catch.cmake)

add_subdirectory(day01)
# --- Find all the current test days ---

set(campaign_year ${CMAKE_CURRENT_LIST_DIR})
cmake_path(GET campaign_year FILENAME parent_directory)

message("")
message("Scanning directory ${parent_directory} for active test days}")

foreach(day_no RANGE 1 25)
if(${day_no} LESS 10)
set(dir_name day0${day_no})
else()
set(dir_name day${day_no})
endif()

if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${dir_name}/")
message(" Found ${dir_name}")
if(NOT EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${dir_name}/CMakeLists.txt")
message("Creating CMakeLists.txt file for this days solution.")
configure_file(
"${CMAKE_SOURCE_DIR}/configured_files/CMakeLists.txt.test.in"
"${CMAKE_CURRENT_SOURCE_DIR}/${dir_name}/CMakeLists.txt"
@ONLY)
endif()
add_subdirectory(${dir_name})
endif()

endforeach()
message("")

0 comments on commit 7bf9a4b

Please sign in to comment.