Skip to content

Commit

Permalink
🩹 fix: add CMakeLists and gtest
Browse files Browse the repository at this point in the history
  • Loading branch information
camargo2019 committed Sep 13, 2024
1 parent 18b82d3 commit 640e089
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build-and-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}"
Expand Down
18 changes: 15 additions & 3 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ data/*.dat
.vscode/*
*.pdb
*.ilk
.vs/
.vs/
run-tests*
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion core/cache/cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> Cache::keys(const std::string& db) noexcept {
Expand Down
2 changes: 1 addition & 1 deletion core/entities/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
#include <fstream>
#include <iostream>
#include <stdexcept>
#include <yaml.cpp>
#include <yaml/yaml.cpp>

struct ConfigConnectBasicAuth{
std::string user;
Expand Down
27 changes: 27 additions & 0 deletions tests/unit/main.cpp
Original file line number Diff line number Diff line change
@@ -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();
}
51 changes: 51 additions & 0 deletions tests/unit/test_cache.cpp
Original file line number Diff line number Diff line change
@@ -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 <gtest/gtest.h>
#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"));
}

0 comments on commit 640e089

Please sign in to comment.