From 640e0899b67c04fde3a29b3c6f3c6af428a80957 Mon Sep 17 00:00:00 2001 From: Gabriel Camargo Date: Thu, 12 Sep 2024 21:00:09 -0300 Subject: [PATCH] :adhesive_bandage: fix: add CMakeLists and gtest --- .github/workflows/build-and-release.yml | 4 +- .github/workflows/run-tests.yml | 18 +++++++-- .gitignore | 3 +- CMakeLists.txt | 29 ++++++++++++++ core/cache/cache.cpp | 2 +- core/entities/config.h | 2 +- tests/unit/main.cpp | 27 +++++++++++++ tests/unit/test_cache.cpp | 51 +++++++++++++++++++++++++ 8 files changed, 128 insertions(+), 8 deletions(-) create mode 100644 CMakeLists.txt create mode 100644 tests/unit/main.cpp create mode 100644 tests/unit/test_cache.cpp diff --git a/.github/workflows/build-and-release.yml b/.github/workflows/build-and-release.yml index 8575eb3..346283a 100644 --- a/.github/workflows/build-and-release.yml +++ b/.github/workflows/build-and-release.yml @@ -33,11 +33,11 @@ jobs: - name: Build - Windows if: matrix.os == 'windows-latest' - run: clang++ -o cmr_cache.exe main.cpp -I./vendor/yaml -I./boost/ -L./boost/lib -llibboost_system-vc143-mt-x64-1_85 -std=c++17 + run: clang++ -o cmr_cache.exe main.cpp -I./vendor/ -I./boost/ -L./boost/lib -llibboost_system-vc143-mt-x64-1_85 -std=c++17 - name: Build - Others OS if: matrix.os != 'windows-latest' - run: clang++ -o cmr_cache main.cpp -I./vendor/yaml -I/usr/include/boost -L/usr/lib/x86_64-linux-gnu -lboost_system -lpthread -std=c++17 + run: clang++ -o cmr_cache main.cpp -I./vendor/ -I/usr/include/boost -L/usr/lib/x86_64-linux-gnu -lboost_system -lpthread -std=c++17 - name: Get the tag run: echo "GITHUB_REF=${{ github.ref }}" diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 3f8fd5c..2d5d507 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -17,13 +17,25 @@ jobs: uses: actions/checkout@v2 - name: Install dependencies - run: sudo apt-get install -y libboost-all-dev + run: sudo apt-get install -y libboost-all-dev libgtest-dev - name: Set up Clang run: | sudo apt-get update sudo apt-get install -y clang - - name: Build + - name: Build Application run: | - clang++ -o cmr_cache main.cpp -I./vendor/yaml -I/usr/local/include/boost -L/usr/local/lib -lboost_system -lpthread -std=c++17 + clang++ -o cmr_cache main.cpp -I./vendor/ -I/usr/local/include/boost -L/usr/local/lib -lboost_system -lpthread -std=c++17 + + - name: Build Tests + run: | + clang++ -o run-tests ./tests/unit/main.cpp -I./vendor/ -I/usr/local/include/boost -I/usr/local/include/ -L/usr/local/lib/ -l:libgtest.a -l:libgtest_main.a -L/usr/local/lib -lboost_system -lpthread -std=c++17 + + - name: Run Tests + run: | + ./run-tests + + - name: Delete file + run: | + rm -fr ./run-tests \ No newline at end of file diff --git a/.gitignore b/.gitignore index d3e2c45..3849bbe 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ data/*.dat .vscode/* *.pdb *.ilk -.vs/ \ No newline at end of file +.vs/ +run-tests* \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..e3c77af --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,29 @@ +cmake_minimum_required(VERSION 3.10) + +project(CmrCache) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +include_directories(${PROJECT_SOURCE_DIR}/vendor) + +find_package(Boost REQUIRED COMPONENTS system thread) + +if(Boost_FOUND) + include_directories(${Boost_INCLUDE_DIRS}) + link_directories(${Boost_LIBRARY_DIRS}) +endif() + +set(SOURCE_DIR ${PROJECT_SOURCE_DIR}/) + +file(GLOB SRCS ${SOURCE_DIR}/*.cpp) + +add_executable(cmr_cache ${SRCS}) + +include_directories(${Boost_INCLUDE_DIRS}) + +target_link_libraries(cmr_cache ${Boost_LIBRARIES}) + +if (UNIX AND NOT WIN32) + target_link_libraries(cmr_cache pthread) +endif() diff --git a/core/cache/cache.cpp b/core/cache/cache.cpp index 3b0c1e2..03e0c83 100644 --- a/core/cache/cache.cpp +++ b/core/cache/cache.cpp @@ -91,7 +91,7 @@ bool Cache::del(const std::string& db, const std::string& key) noexcept { cache_[db].erase(key); isChange = true; - return false; + return true; } std::vector Cache::keys(const std::string& db) noexcept { diff --git a/core/entities/config.h b/core/entities/config.h index edc3978..22f0693 100644 --- a/core/entities/config.h +++ b/core/entities/config.h @@ -26,7 +26,7 @@ #include #include #include - #include + #include struct ConfigConnectBasicAuth{ std::string user; diff --git a/tests/unit/main.cpp b/tests/unit/main.cpp new file mode 100644 index 0000000..c51bdf9 --- /dev/null +++ b/tests/unit/main.cpp @@ -0,0 +1,27 @@ +/* +* MIT License +* +* Copyright 2024 Gabriel Camargo +* +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +* documentation files (the “Software”), to deal in the Software without restriction, including without limitation the +* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit +* persons to whom the Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +* +* +*/ + +#include "test_cache.cpp" + +int main(int argc, char **argv) { + ::testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} \ No newline at end of file diff --git a/tests/unit/test_cache.cpp b/tests/unit/test_cache.cpp new file mode 100644 index 0000000..825abab --- /dev/null +++ b/tests/unit/test_cache.cpp @@ -0,0 +1,51 @@ +/* +* MIT License +* +* Copyright 2024 Gabriel Camargo +* +* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated +* documentation files (the “Software”), to deal in the Software without restriction, including without limitation the +* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit +* persons to whom the Software is furnished to do so, subject to the following conditions: +* +* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the +* Software. +* +* THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO +* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +* +* +*/ + +#include +#include "../../core/cache/cache.cpp" + + +TEST(CacheTest, SetCommand) { + Cache cache; + EXPECT_TRUE(cache.set("tests", "key1", "value1")); + EXPECT_TRUE(cache.set("tests", "key2", "value2")); +} + + +TEST(CacheTest, GetCommand) { + Cache cache; + cache.set("tests", "key1", "value1"); + cache.set("tests", "key2", "value2"); + + EXPECT_EQ(cache.get("tests", "key1"), "value1"); + EXPECT_EQ(cache.get("tests", "key2"), "value2"); + EXPECT_EQ(cache.get("tests", "key3"), ""); +} + +TEST(CacheTest, DelCommand) { + Cache cache; + cache.set("tests", "key1", "value1"); + cache.set("tests", "key2", "value2"); + + EXPECT_TRUE(cache.del("tests", "key1")); + EXPECT_EQ(cache.get("tests", "key1"), ""); + EXPECT_TRUE(cache.del("tests", "key3")); +}