From 7a525a85ceef93bdec54b05ee072e2bbe790a502 Mon Sep 17 00:00:00 2001 From: Gulliver Date: Mon, 21 Nov 2022 22:44:07 +0100 Subject: [PATCH] added catch2 for unit tests and first test in test/test.cpp --- .gitignore | 3 +++ CMakeLists.txt | 37 ++++++++++++++++++++++++++++++++----- test/test.cpp | 14 ++++++++++++++ 3 files changed, 49 insertions(+), 5 deletions(-) create mode 100644 test/test.cpp diff --git a/.gitignore b/.gitignore index c7f3b7d..c0be386 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ +.idea +cmake-build-debug + # ---> C++ # Prerequisites *.d diff --git a/CMakeLists.txt b/CMakeLists.txt index 4738a99..9316382 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ endif() project(oc-mint VERSION 0.0.1 LANGUAGES CXX) include(FetchContent) + # # crow project Configuration variables # @@ -45,6 +46,20 @@ if(NOT expected_POPULATED) FetchContent_Populate(expected) endif(NOT expected_POPULATED) +include(CTest) +enable_testing() + +set(CATCH_INSTALL_DOCS Off) +set(CATCH_INSTALL_EXTRAS Off) +FetchContent_Declare( + Catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v3.2.0 +) + +FetchContent_MakeAvailable(Catch2) + +#add doxygen documentation find_package(Doxygen REQUIRED dot OPTIONAL_COMPONENTS mscgen dia) @@ -52,9 +67,21 @@ set(DOXYGEN_HAVE_DOT YES) doxygen_add_docs( doc src - COMMENT "Generate dcoumentation" + COMMENT "Generate documentation" ) -set(API_SOURCES src/main.cpp src/model.cpp src/model.hpp) -add_executable(oc-mint ${API_SOURCES}) -target_link_libraries(oc-mint PRIVATE Crow::Crow INTERFACE tl::expected::expected) -target_include_directories(oc-mint PRIVATE ${expected_SOURCE_DIR}/include) + +# build common library +set(LIB_SOURCES src/model.cpp src/model.hpp) +add_library(oc-mint-lib ${LIB_SOURCES}) +target_link_libraries(oc-mint-lib PUBLIC Crow::Crow) +target_include_directories(oc-mint-lib PUBLIC ${expected_SOURCE_DIR}/include src) + +add_executable(oc-mint src/main.cpp) +target_link_libraries(oc-mint PRIVATE oc-mint-lib INTERFACE tl::expected::expected) + +## these are unittests that can be run on any platform +add_executable(tests test/test.cpp) +target_link_libraries(tests + oc-mint-lib + Catch2::Catch2WithMain) +add_test(tests tests) diff --git a/test/test.cpp b/test/test.cpp new file mode 100644 index 0000000..601f7f2 --- /dev/null +++ b/test/test.cpp @@ -0,0 +1,14 @@ +#include +#include "model.hpp" + +TEST_CASE( "PublicKey::to_json", "[to_json]" ) { + PublicKey k {"daaa63ddda38c189b8c49020c8276adbe0a695685a...", + "65537" }; + + auto json = k.to_json(); + + REQUIRE( json["modulus"].dump() == "\"" + k.modulus + "\"" ); + REQUIRE( json["public_exponent"].dump() == "\"" + k.public_exponent+"\"" ); + REQUIRE( json["type"].dump() == "\"rsa public key\"" ); + REQUIRE( json.keys().size() == 3 ); +}