From 4b7a1cb96a53b66ed030e85d9f364fd37fa2d2bd Mon Sep 17 00:00:00 2001 From: denes Date: Mon, 11 Nov 2019 15:50:58 +0100 Subject: [PATCH] build unit tests with catch2 --- .gitmodules | 3 +++ CMakeLists.txt | 12 ++++++++++-- Catch2 | 1 + cpp_logger_test.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) create mode 100644 .gitmodules create mode 160000 Catch2 create mode 100644 cpp_logger_test.cpp diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b435c86 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "Catch2"] + path = Catch2 + url = https://github.com/catchorg/Catch2 diff --git a/CMakeLists.txt b/CMakeLists.txt index 48033da..f064081 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,7 +1,15 @@ cmake_minimum_required (VERSION 3.10) project (CppLogger) -# Using Catch2's benchmarking tools -set(CATCH_HEADER_FILES Logger.hpp Singleton_DCLP.hpp ArgParser.hpp) +option(BUILD_TESTS "Build unit tests with Catch2" OFF) + +set(HEADER_FILES Logger.hpp Singleton_DCLP.hpp ArgParser.hpp) add_executable(cpplogger main.cpp ${HEADER_FILES}) set_target_properties(cpplogger PROPERTIES COMPILE_FLAGS "-ggdb") + +if (BUILD_TESTS) + set(CATCH_HEADER_FILE Catch2/single_include/catch2/catch.hpp) + add_executable(cpplogger_test cpp_logger_test.cpp ${HEADER_FILES} ${CATCH_HEADER_FILE}) + target_include_directories(cpplogger_test PRIVATE . Catch2/single_include) + set_target_properties(cpplogger_test PROPERTIES COMPILE_FLAGS "-ggdb") +endif() diff --git a/Catch2 b/Catch2 new file mode 160000 index 0000000..930f49a --- /dev/null +++ b/Catch2 @@ -0,0 +1 @@ +Subproject commit 930f49a641aa6a495d264d7b5e7c007734da0b0c diff --git a/cpp_logger_test.cpp b/cpp_logger_test.cpp new file mode 100644 index 0000000..7a902c3 --- /dev/null +++ b/cpp_logger_test.cpp @@ -0,0 +1,44 @@ +// Let Catch provide main(): +#define CATCH_CONFIG_MAIN +// #define CATCH_CONFIG_ENABLE_BENCHMARKING + +#include + +#include + +#include + + +TEST_CASE( "Simple test" ) { + + // setup + std::ostringstream oss; + Logger::getInstance()->setStream(&oss); + + + // simple usage + LOG << "pretty weather" << END; + REQUIRE( oss.str() == "pretty weather\n" ); + + // chaining inserts + oss.str(""); + const std::string s = "apple(s)"; + const int i = 2; + LOG << "i have " << i << " " << s << END; + REQUIRE( oss.str() == "i have 2 apple(s)\n" ); + + // output is only written at END + oss.str(""); + LOG << 1; + LOG << 2; + REQUIRE( oss.str() == "" ); + LOG << END; + REQUIRE( oss.str() == "12\n" ); + LOG << 3; + REQUIRE( oss.str() == "12\n" ); + + oss.str(""); + LOG << END; + REQUIRE( oss.str() == "3\n" ); +} +