build unit tests with catch2

master
denes 5 years ago
parent 32131e3e55
commit 4b7a1cb96a
Signed by: denes
GPG Key ID: A7D50EAD42F9FC9F

3
.gitmodules vendored

@ -0,0 +1,3 @@
[submodule "Catch2"]
path = Catch2
url = https://github.com/catchorg/Catch2

@ -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()

@ -0,0 +1 @@
Subproject commit 930f49a641aa6a495d264d7b5e7c007734da0b0c

@ -0,0 +1,44 @@
// Let Catch provide main():
#define CATCH_CONFIG_MAIN
// #define CATCH_CONFIG_ENABLE_BENCHMARKING
#include <sstream>
#include <catch2/catch.hpp>
#include <Logger.hpp>
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" );
}
Loading…
Cancel
Save