From e98428dd405cd3f394480aa9e8ed76030abc08dc Mon Sep 17 00:00:00 2001 From: Patryk Osmaczko Date: Tue, 18 Jan 2022 12:39:21 +0100 Subject: [PATCH] feat(@desktop/cpp): add conanfile, fruit, gtest Closes: #4428 Closes: #4429 --- .gitignore | 1 + CMakeLists.txt | 9 +++++++-- README.md | 19 +++++++++++++------ conanfile.py | 17 +++++++++++++++++ test-cpp/CMakeLists.txt | 15 +++++++++++++++ test-cpp/testDeps.cpp | 35 +++++++++++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 8 deletions(-) create mode 100644 conanfile.py create mode 100644 test-cpp/CMakeLists.txt create mode 100644 test-cpp/testDeps.cpp diff --git a/.gitignore b/.gitignore index 51c0c72900..bed053999d 100644 --- a/.gitignore +++ b/.gitignore @@ -49,6 +49,7 @@ install_manifest.txt compile_commands.json CTestTestfile.cmake _deps +.cache/ diff --git a/CMakeLists.txt b/CMakeLists.txt index 12c8ad2773..091cde1e62 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,7 +22,7 @@ set(CMAKE_CXX_STANDARD_REQUIRED ON) if(NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Debug) -endif() +endif() message(STATUS "Started CMake for ${PROJECT_NAME} v${PROJECT_VERSION}...\n") @@ -99,7 +99,7 @@ target_link_libraries(status-desktop keycard app_service app - ) + ) add_custom_target(rcc ALL DEPENDS resources.rcc) @@ -133,3 +133,8 @@ add_custom_target(run-debug VERBATIM USES_TERMINAL ) + +include(CTest) +if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING) + add_subdirectory(test-cpp) +endif() diff --git a/README.md b/README.md index 0c08510571..a1423fec80 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,18 @@ Dev Docs: [https://hackmd.io/@status-desktop/B1naRjxh_/https%3A%2F%2Fhackmd.io%2 -# CPP app - +# CPP App +Buid&test&run: ``` cd build -cmake .. -make -./test-qtapp -``` \ No newline at end of file +conan install .. -s build_type=Release --build=missing +conan build .. +ctest -VV -C Release +./status-desktop +``` + +Instead of `conan build ..` CMake may be used: +``` +cmake .. -DCMAKE_BUILD_TYPE=Release -DCMAKE_TOOLCHAIN_FILE=conan_toolchain.cmake +cmake --build . --config Release +``` diff --git a/conanfile.py b/conanfile.py new file mode 100644 index 0000000000..0064326150 --- /dev/null +++ b/conanfile.py @@ -0,0 +1,17 @@ +from conans import ConanFile, CMake + + +class StatusDesktop(ConanFile): + name = "status-desktop" + settings = "os", "compiler", "build_type", "arch" + + requires = "fruit/3.6.0", "gtest/1.11.0" + + # cmake_find_package and cmake_find_package_multi should be substituted with CMakeDeps + # as soon as Conan 2.0 is released and all conan-center packages are adapted + generators = "CMakeToolchain", "cmake_find_package", "cmake_find_package_multi" + + def build(self): + cmake = CMake(self) + cmake.configure() + cmake.build() diff --git a/test-cpp/CMakeLists.txt b/test-cpp/CMakeLists.txt new file mode 100644 index 0000000000..8ae9e6962c --- /dev/null +++ b/test-cpp/CMakeLists.txt @@ -0,0 +1,15 @@ + +cmake_minimum_required(VERSION 3.14) +project(status-desktop-test VERSION 0.1.0 LANGUAGES CXX) + +macro(add_gtest TESTNAME SRC) + add_executable(${TESTNAME} ${SRC}) + target_link_libraries(${TESTNAME} PRIVATE GTest::gtest GTest::gmock GTest::gtest_main) + add_test(${TESTNAME} ${TESTNAME}) +endmacro() + +find_package(GTest REQUIRED) +find_package(fruit REQUIRED) + +add_gtest(TestDeps testDeps.cpp) +target_link_libraries(TestDeps PRIVATE fruit::fruit) diff --git a/test-cpp/testDeps.cpp b/test-cpp/testDeps.cpp new file mode 100644 index 0000000000..650d4d9aed --- /dev/null +++ b/test-cpp/testDeps.cpp @@ -0,0 +1,35 @@ +#include +#include + +namespace +{ +class Greeter +{ +public: + virtual std::string greet() = 0; +}; + +fruit::Component getGreeterComponent(); + +class GreeterImpl : public Greeter +{ +public: + INJECT(GreeterImpl()) = default; + + std::string greet() override + { + return "Hello, world!"; + } +}; + +fruit::Component getGreeterComponent() +{ + return fruit::createComponent().bind(); +} +} // namespace + +TEST(TestDeps, Fruit) +{ + auto injector = fruit::Injector{getGreeterComponent}; + ASSERT_NE(injector.get(), nullptr); +}