Skip to content

Commit e2f1b3d

Browse files
Add automatic testing with CTest
1 parent b230d2e commit e2f1b3d

File tree

3 files changed

+37
-10
lines changed

3 files changed

+37
-10
lines changed

CMakeLists.txt

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
cmake_minimum_required(VERSION 3.14)
2-
3-
set(CMAKE_CXX_STANDARD 11)
4-
set(CMAKE_CXX_STANDARD_REQUIRED ON)
5-
62
if(NOT DEFINED CPP_SAFE_IO)
73
set(CPP_SAFE_IO "CppSafeIO" CACHE STRING "Name for the CppSafeIO library")
84
endif()
95

6+
project(CPP_SAFE_IO)
7+
8+
set(CMAKE_CXX_STANDARD 11)
9+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
10+
1011
add_subdirectory(src)
12+
add_subdirectory(tests)

tests/CMakeLists.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
cmake_minimum_required(VERSION 3.14)
2-
project(Tests LANGUAGES CXX)
2+
include(CTest)
33

4-
add_executable(Tests tests.cpp)
4+
add_executable(CppSafeIO-Tests tests.cpp)
5+
target_link_libraries(CppSafeIO-Tests ${CPP_SAFE_IO})
56

6-
get_filename_component(PARENT_DIR ../ ABSOLUTE)
7-
add_subdirectory(${PARENT_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/build/bin)
8-
9-
target_link_libraries(Tests ${CPP_SAFE_IO})
7+
add_executable(CppSafeIO-AutomatedTests automated-tests.cpp)
8+
target_link_libraries(CppSafeIO-AutomatedTests ${CPP_SAFE_IO})
9+
add_test(NAME CppSafeIO-Testing COMMAND CppSafeIO-AutomatedTests)

tests/automated-tests.cpp

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "cppsafeio.h"
2+
#include <iostream>
3+
#include <exception>
4+
5+
int main()
6+
{
7+
try
8+
{
9+
if (false)
10+
CppSafeIO::clearConsole(); //not executing so that the building and testing log doesn't get erased in case of running ctest -V
11+
12+
std::cout << "Automated tests passed.\n"
13+
<< "You can include the cppsafeio.h file, and use the CppSafeIO namespace.\n"
14+
<< "You can try to run CppSafeIO-Tests executable to test the input functions.\n";
15+
}
16+
catch (const std::exception& exception)
17+
{
18+
std::cerr << "CppSafeIO library's tests failed\n"
19+
<< "Error: " << exception.what() << '\n';
20+
21+
return 1;
22+
}
23+
24+
return 0;
25+
}

0 commit comments

Comments
 (0)