From 3c7bbe88364fc21c5dcedeee07327d672dd78e6e Mon Sep 17 00:00:00 2001 From: EAS Barbosa Date: Fri, 9 Aug 2024 02:00:11 -0300 Subject: [PATCH] feat: add action edit/view configurations --- .gitignore | 64 ++++++++++++++- .gitlab-ci.yml | 19 ++++- CHANGELOG.md | 4 + CMakeLists.txt | 24 +++++- GNUmakefile | 22 ++++-- README.md | 26 ++++++- TODO.md | 7 +- meson.build | 2 +- src/CMakeLists.txt | 39 ++++++++++ src/database/parse.cpp | 68 +++++++++++++--- src/database/repository.cpp | 4 - src/handlers/actions.cpp | 5 +- src/handlers/commands.cpp | 137 ++++++++++++++++++++++++++++----- src/handlers/helpers.cpp | 22 +++--- src/include/cli.hpp | 26 +++++++ src/include/commands.hpp | 16 +++- src/include/konfig.hpp | 26 ++++++- src/include/parse.hpp | 5 ++ src/include/project.hpp | 39 +++++++++- src/main.cpp | 36 ++++++--- subprojects/catch2.wrap | 11 --- subprojects/cli11.wrap | 10 --- subprojects/nlohmann_json.wrap | 10 --- tests/CMakeLists.txt | 36 +++++++++ tests/test_globals.cpp | 60 ++++++++++++++- tests/test_main.cpp | 7 ++ 26 files changed, 605 insertions(+), 120 deletions(-) create mode 100644 src/CMakeLists.txt create mode 100644 src/include/cli.hpp delete mode 100644 subprojects/catch2.wrap delete mode 100644 subprojects/cli11.wrap delete mode 100644 subprojects/nlohmann_json.wrap create mode 100644 tests/CMakeLists.txt diff --git a/.gitignore b/.gitignore index 1cbb68d..cb65e0a 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,15 @@ build_debug/ build_release/ +# MESON + +/build +/subprojects/* +!/subprojects/*.wrap +/.cache + +# CMAKE + # Generated and user files **/CMakeCache.txt **/CMakeUserPresets.json @@ -11,8 +20,55 @@ build_release/ **/install_manifest.txt **/compile_commands.json +# Build directories +build/ +out/ +cmake-build-*/ -/build -/subprojects/* -!/subprojects/*.wrap -/.cache +# CMake files +CMakeCache.txt +CMakeFiles/ +CMakeScripts/ +Testing/ +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake + +# Compiled Object files +*.slo +*.lo +*.o +*.obj + +# Precompiled Headers +*.gch +*.pch + +# Compiled Dynamic libraries +*.so +*.dylib +*.dll + +# Fortran module files +*.mod +*.smod + +# Compiled Static libraries +*.lai +*.la +*.a +*.lib + +# Executables +*.exe +*.out +*.app + +# IDE-specific files (example for Visual Studio Code) +.vscode/ + +# OS-specific files +.DS_Store +Thumbs.db diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 43bd0c3..1d820f3 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,6 +1,7 @@ image: registry.gitlab.com/easbarba/onur-cpp:0.2.0 stages: + - build - test workflow: @@ -9,7 +10,21 @@ workflow: when: never - if: $CI_COMMIT_BRANCH == 'main' -Integration Tests: +.build_template: &build_definition + stage: build + script: + - cmake -B build -S . + - cmake --build build + +build:linux: + <<: *build_definition + artifacts: + paths: + - build/ + +test: stage: test script: - - meson test -C /home/easbarba/app/build + - ./build/tests/onur_tests + dependencies: + - build:linux diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c6b759..e74a028 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,10 @@ along with Onur. If not, see . # CHANGELOG +## 0.3.0 + +- feat: add action edit/view configurations + ## 0.2.0 - feat: refactor, more tests and improve cli ui diff --git a/CMakeLists.txt b/CMakeLists.txt index 0eefcdb..7fd1718 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,3 +1,23 @@ +# Onur is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Onur is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Onur. If not, see . + cmake_minimum_required(VERSION 3.20.0) -project(Onur) -add_executable(onur src/main.cpp) +project(onur) + +# Set C++ standard +set(CMAKE_CXX_STANDARD 23) +set(CMAKE_CXX_STANDARD_REQUIRED True) + +# Add subdirectories +add_subdirectory(src) +add_subdirectory(tests) diff --git a/GNUmakefile b/GNUmakefile index 4e6b5f9..8813f88 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -26,17 +26,29 @@ CONTAINER_IMAGE := registry.gitlab.com/${USER}/${NAME}:${VERSION} # ------------------------------------ TASKS .PHONY: all -all: +all: cmake.all + +.PHONY: dev +dev: cmake.dev + +.PHONY: meson.all +meson.all: CC=g++ meson setup $(BUILDDIR) --wipe CC=g++ meson compile -C $(BUILDDIR) -.PHONY: dev -dev: + +.PHONY: meson.dev +meson.dev: CC=g++ meson setup $(BUILDDIR) CC=g++ meson compile -C $(BUILDDIR) -.PHONY: cmake -cmake: +.PHONY: cmake.all +cmake.all: clean + cmake -B ${BUILDDIR} -S . + cmake --build ${BUILDDIR} + +.PHONY: cmake.dev +cmake.dev: cmake -B ${BUILDDIR} -S . cmake --build ${BUILDDIR} diff --git a/README.md b/README.md index 21e21db..c0fa980 100644 --- a/README.md +++ b/README.md @@ -22,12 +22,30 @@ Easily manage multiple FLOSS repositories. | [ruby](https://gitlab.com/easbarba/onur-ruby) - ## Usage ```shell -onur grab -onur backup nuxt awesomewm gitignore +# grab all projects +onur grab + +# grab only the c projects +onur grab c + +# list the cpp configuration file +onur config cpp + +# list topics of haskell +onur config haskell. + +# list only the projects on misc topic of lisp +onur config lisp.misc + +# add a new configuration with theses entries in the topic misc of c +onur config c.misc cli11 https://github.com/cliutils/cli11 main + +# back up these projects as tar.gz +onur backup ecmascript.nuxt lua.awesomewm misc.gitignore + onur --help ``` @@ -90,7 +108,7 @@ depth = 1 ## Installation -`onur` requires a c++ 20 compiler and `meson` to install, locally at `$HOME/.local/bin`, easily with : `make all install`. +`onur` requires a c++ 23 compiler and `meson` or `cmake`: `make all`. ## Development diff --git a/TODO.md b/TODO.md index b2cd28a..ba6cda5 100644 --- a/TODO.md +++ b/TODO.md @@ -17,12 +17,15 @@ along with Onur. If not, see . ### High -- verbose +- flag: --verbose - validation of repository links -- actions: --filter rust +- actions: --filter rust:misc - actions: --only rust,haskel,commonlisp - actions: --exclude rust,haskel,commonlisp +### Mid +- actions: onur config c.misc foo https://git@gmasd main + ### Low - config: move on these to a syntax check class diff --git a/meson.build b/meson.build index 5c8a876..8b18684 100644 --- a/meson.build +++ b/meson.build @@ -16,7 +16,7 @@ project( 'cpp', license: 'GNU GPL version 3', version: '0.2.0', - default_options: [ 'cpp_std=c++20', 'warning_level=3' ], + default_options: [ 'cpp_std=c++23', 'warning_level=3' ], meson_version: '>=1.4.0', ) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 0000000..c00c16c --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,39 @@ +# Onur is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Onur is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Onur. If not, see . + +include(FetchContent) + +FetchContent_Declare( + cli11 + GIT_REPOSITORY https://github.com/CLIUtils/CLI11.git + GIT_TAG v2.4.1 +) +FetchContent_MakeAvailable(cli11) + +FetchContent_Declare( + json + GIT_REPOSITORY https://github.com/nlohmann/json.git + GIT_TAG v3.11.2 +) +FetchContent_MakeAvailable(json) + +add_executable(onur main.cpp + common/globals.cpp + database/parse.cpp + database/repository.cpp + domain/konfig.cpp + handlers/actions.cpp + handlers/commands.cpp + handlers/helpers.cpp) +target_link_libraries(onur PRIVATE CLI11::CLI11 nlohmann_json::nlohmann_json) +target_include_directories(onur PRIVATE ${PROJECT_SOURCE_DIR}/src/include) diff --git a/src/database/parse.cpp b/src/database/parse.cpp index 450a80b..326ef0c 100644 --- a/src/database/parse.cpp +++ b/src/database/parse.cpp @@ -17,10 +17,11 @@ #include #include #include -#include - #include +#include +#include +#include "../include/cli.hpp" #include "../include/konfig.hpp" #include "../include/parse.hpp" #include "../include/project.hpp" @@ -52,29 +53,29 @@ auto Parse::single (path filepath) -> Konfig { Konfig result; - map > subtopiks; + map> _topics; auto configParsed = parse_file (contents_of (filepath)); - result.topic = { filepath.stem () }; + result.name = { filepath.stem () }; - for (auto &[subtopic, subtopics] : configParsed.items ()) + for (auto &[topic, topics] : configParsed.items ()) { list projects; - for (auto projekt : subtopics) + for (auto project : topics) { string branch{ "master" }; - if (!projekt["branch"].is_null ()) - branch = projekt["branch"]; + if (!project["branch"].is_null ()) + branch = project["branch"]; - auto pkt{ Project (projekt["name"], projekt["url"], branch) }; + auto pkt{ Project (project["name"], project["url"], branch) }; projects.push_back (pkt); } - subtopiks[subtopic] = { projects }; + _topics[topic] = { projects }; } - result.subtopics = { subtopiks }; + result.topics = { _topics }; return result; } @@ -90,3 +91,48 @@ Parse::contents_of (string path_to_file) -> string ifstream file (path_to_file); return { istreambuf_iterator (file), istreambuf_iterator{} }; } + +auto +Parse::exist (std::string name) -> bool +{ + for (auto config : multi ()) + if (config.name == name) + return true; + + // std::for_each (multi ().begin (), multi ().end (), + // [name, &result] (Konfig config) { + // std::println ("MEH"); + // result = { config.name == name }; + // std::println ("FOOL"); + // }); + + return false; +} + +// Overload the to_json function for automatic conversion +void +to_json (nlohmann::json &j, const Konfig &k) +{ + j = k.to_json (); +} + +auto +Parse::save (std::string name, std::string topic, + ConfigEntries entries) -> void +{ + // if (entries.name || entries.url.has_value () || !entries.branch.empty ()) + // { + // std::println ("Either name or url of project are missing. Exiting!"); + // return; + // } + + Project project{ entries }; + + std::map> topics; + topics[topic] = { entries }; + + Konfig konfig{ name, topics }; + nlohmann::json j = konfig; + + std::println ("Saving config with name {} as \n{}", konfig.name, j.dump ()); +} diff --git a/src/database/repository.cpp b/src/database/repository.cpp index 6c6ba30..c955d71 100644 --- a/src/database/repository.cpp +++ b/src/database/repository.cpp @@ -32,8 +32,6 @@ Repository::allConfigs (void) -> list list result; Globals globals; - printf (" Configurations: ["); - for (auto config : directory_iterator (globals.onurDir)) { if (config.path ().extension () != ".json") @@ -46,10 +44,8 @@ Repository::allConfigs (void) -> list if (file.peek () == std::ifstream::traits_type::eof ()) continue; - printf (" %s ", config.path ().stem ().c_str ()); result.push_back (config); } - printf (" ]\n"); return result; } diff --git a/src/handlers/actions.cpp b/src/handlers/actions.cpp index bba0b63..65b756f 100644 --- a/src/handlers/actions.cpp +++ b/src/handlers/actions.cpp @@ -30,7 +30,7 @@ auto Actions::klone (Project project, path dirpath) -> void { auto finalCommand{ format ( - "git clone --single-branch --depth=1 --quiet {} {}", project.url, + "git clone --single-branch --depth=1 --quiet -- {} {}", project.Url (), dirpath.string ()) }; system (finalCommand.c_str ()); } @@ -38,6 +38,7 @@ Actions::klone (Project project, path dirpath) -> void auto Actions::pull (path dirpath) -> void { - auto finalCommand{ format ("git -C {} pull --quiet", dirpath.string ()) }; + auto finalCommand{ format ("git -C {} pull --force --rebase --quiet", + dirpath.string ()) }; system (finalCommand.c_str ()); } diff --git a/src/handlers/commands.cpp b/src/handlers/commands.cpp index 8457ec6..f7cccf9 100644 --- a/src/handlers/commands.cpp +++ b/src/handlers/commands.cpp @@ -14,45 +14,49 @@ */ #include -#include -#include +#include +#include +#include +#include #include "../include/commands.hpp" -#include "../include/globals.hpp" #include "helpers.hpp" -using std::cout; -using std::endl; using std::filesystem::exists; using std::filesystem::path; Commands::Commands () {} auto -Commands::grab (void) -> void +Commands::grab (std::optional name) -> void { - for (auto single : parse.multi ()) + // if (name.has_value ()) + // ConfigTopic config{ configTopicNew (name.value ()) }; + + for (auto singleConfig : parse.multi ()) { - cout << "\n " << single.topic << ":" << endl; + if (name.has_value () && name.value () != singleConfig.name) + continue; + + std::println ("{}: ", singleConfig.name); - for (auto subtopic : single.subtopics) + for (auto topic : singleConfig.topics) { - cout << " + " << subtopic.first << endl; - for (auto project : subtopic.second) + std::println (" + {}", topic.first); + for (auto project : topic.second) { - auto placeholder{ path (globals.projectsDir / single.topic - / subtopic.first / project.name) }; - auto dirpath{ placeholder }; + auto finalpath{ path (globals.projectsDir / singleConfig.name + / topic.first / project.Name ()) }; printProjectInfo (project); - if (exists (dirpath / ".git" / "config")) - actions.pull (dirpath); + if (exists (finalpath / ".git" / "config")) + actions.pull (finalpath); else - actions.klone (project, dirpath); + actions.klone (project, finalpath); } - cout << endl; + std::println (); } } } @@ -60,5 +64,100 @@ Commands::grab (void) -> void auto Commands::backup (void) -> void { - cout << "Backing up" << endl; + std::println ("Backing up"); +} + +ConfigTopic +Commands::configTopicNew (std::string &name) +{ + ConfigTopic config; + + if (name.contains (".")) + { + config.dot = { true }; + + std::size_t dot_positon{ name.find (".") }; + config.name = { name.substr (0, dot_positon) }; + config.topic = { name.substr (dot_positon + 1) }; + } + + return config; +} + +auto +Commands::config (std::string name, ConfigEntries entries) -> void +{ + ConfigTopic config{ configTopicNew (name) }; + + if (!parse.exist (config.name.value ())) + { + std::println ("No configuration by {} found!", config.name.value ()); + return; + } + + if (name.ends_with (".")) + { + printSingleConfig (config, true); + return; + } + + if (!config.topic.has_value ()) + { + printSingleConfig (config); + return; + } + else + { + printSingleConfig (config, true); + return; + } + + // std::println ( + // "Please provide a topic and entries to save a new one. Exiting!", + // config.name); + + if (!entries.name.has_value () || !entries.url.has_value ()) + { + std::println ("Enter name and url entries at least. Exiting!"); + return; + } + + parse.save (config.name.value (), config.topic.value (), entries); +} + +auto +foo () -> void +{ + std::print ("Configurations: ["); + // printf (" %s ", config.path ().stem ().c_str ()); + std::println (" ]\n"); +} + +void +Commands::printSingleConfig (ConfigTopic config, bool onlytopics) +{ + for (auto singleConfig : parse.multi ()) + { + if (config.name.value () != singleConfig.name) + continue; + + std::println ("{}:", singleConfig.name); + for (auto topic : singleConfig.topics) + { + if (onlytopics) + { + std::print (" {} ", topic.first); + continue; + } + + if (config.topic.has_value () + && config.topic.value () != topic.first) + continue; + + std::println (" + {}", topic.first); + + for (auto project : topic.second) + printProjectInfo (project); + } + } } diff --git a/src/handlers/helpers.cpp b/src/handlers/helpers.cpp index 6e792de..653d0fe 100644 --- a/src/handlers/helpers.cpp +++ b/src/handlers/helpers.cpp @@ -14,12 +14,10 @@ */ #include -#include +#include #include "../include/helpers.hpp" -using std::format; - using namespace std; auto @@ -27,14 +25,14 @@ printProjectInfo (Project project) -> void { std::string::size_type nameLength = 27; auto nameTruncated - = project.name.length () <= nameLength - ? project.name - : project.name.substr (0, nameLength).append ("..."); + = project.Name ().length () <= nameLength + ? project.Name () + : project.Name ().substr (0, nameLength).append ("..."); std::string::size_type urlLength = 60; - auto urlTruncated = project.url.length () <= urlLength - ? project.url - : project.url.substr (0, urlLength).append ("..."); - auto message{ format ("{:5}- {:35} {:75} {}", "", nameTruncated, - urlTruncated, project.branch) }; - cout << message << endl; + auto urlTruncated + = project.Url ().length () <= urlLength + ? project.Url () + : project.Url ().substr (0, urlLength).append ("..."); + std::println ("{:3}- {:35} {:75} {}", "", nameTruncated, urlTruncated, + project.Branch ()); } diff --git a/src/include/cli.hpp b/src/include/cli.hpp new file mode 100644 index 0000000..af1127a --- /dev/null +++ b/src/include/cli.hpp @@ -0,0 +1,26 @@ +/* + * Onur is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Onur is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Onur. If not, see . + */ + +#pragma once + +#include +#include + +struct ConfigEntries +{ + std::optional name; + std::optional url; + std::string branch{ "master" }; +}; diff --git a/src/include/commands.hpp b/src/include/commands.hpp index 16a3dea..b87883e 100644 --- a/src/include/commands.hpp +++ b/src/include/commands.hpp @@ -15,11 +15,20 @@ #pragma once +#include +#include + #include "actions.hpp" +#include "cli.hpp" #include "globals.hpp" #include "parse.hpp" -#include "project.hpp" +struct ConfigTopic +{ + std::optional name; + std::optional topic; + bool dot{ false }; +}; class Commands { public: @@ -29,6 +38,9 @@ class Commands Actions actions; Parse parse; - auto grab (void) -> void; + auto grab (std::optional name) -> void; auto backup (void) -> void; + auto printSingleConfig (ConfigTopic config, bool onlytopics = false) -> void; + auto config (std::string name, ConfigEntries edit) -> void; + auto configTopicNew (std::string &name) -> ConfigTopic; }; diff --git a/src/include/konfig.hpp b/src/include/konfig.hpp index 3ff1d39..336d60a 100644 --- a/src/include/konfig.hpp +++ b/src/include/konfig.hpp @@ -18,13 +18,35 @@ #include #include +#include + #include "project.hpp" class Konfig { public: + Konfig (std::string &name, std::map> &topics) + : name (name), topics (topics) {}; + Konfig (); - std::string topic; - std::map > subtopics; + std::string name; + std::map> topics; + + nlohmann::json + to_json () const + { + nlohmann::json j; + j["name"] = name; + j["topics"] = nlohmann::json::object (); + for (const auto &[topic, projects] : topics) + { + j["topics"][topic] = nlohmann::json::array (); + for (const auto &project : projects) + { + j["topics"][topic].push_back (project.to_json ()); + } + } + return j; + } }; diff --git a/src/include/parse.hpp b/src/include/parse.hpp index 8fd7b88..3b1645a 100644 --- a/src/include/parse.hpp +++ b/src/include/parse.hpp @@ -20,6 +20,7 @@ #include +#include "cli.hpp" #include "konfig.hpp" #include "repository.hpp" @@ -34,4 +35,8 @@ class Parse auto single (std::filesystem::path filepath) -> Konfig; auto parse_file (std::string jsonString) -> nlohmann::basic_json<>; auto contents_of (std::string path_to_file) -> std::string; + auto exist (std::string name) -> bool; + auto dump () -> void; + auto save (std::string name, std::string topic, + ConfigEntries entries) -> void; }; diff --git a/src/include/project.hpp b/src/include/project.hpp index 4593e70..7a9095b 100644 --- a/src/include/project.hpp +++ b/src/include/project.hpp @@ -17,13 +17,46 @@ #include +#include + +#include "cli.hpp" + class Project { + std::string name; + std::string url; + std::string branch; + public: Project (std::string name, std::string url, std::string branch) : name (name), url (url), branch (branch) {}; - std::string name; - std::string url; - std::string branch; + Project (ConfigEntries entries) + : name (entries.name.value ()), url (entries.url.value ()), + branch (entries.branch) {}; + + std::string + Name () + { + return name; + } + + std::string + Url () + { + return url; + } + std::string + Branch () + { + return branch; + } + + nlohmann::json + to_json () const + { + return nlohmann::json{ { "name", name }, + { "url", url }, + { "branch", branch } }; + } }; diff --git a/src/main.cpp b/src/main.cpp index 5a5120f..c7f69d3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -13,11 +13,13 @@ * along with Onur. If not, see . */ +#include #include #include "CLI/Option.hpp" #include +#include "include/cli.hpp" #include "include/commands.hpp" using std::string; @@ -26,30 +28,44 @@ auto main (int argc, char *argv[]) -> int { CLI::App app{ "Easily manage multiple FLOSS repositories." }; + app.require_subcommand (1); argv = { app.ensure_utf8 (argv) }; + app.set_version_flag ("--version", string ("0.2.0")); - string filename{ "default" }; - app.add_option ("-f,--file", filename, "A help string"); - app.set_version_flag ("--version", string ("0.0.1")); + // COMMANDS CLI::App *grab_cmd{ app.add_subcommand ("grab", "grab all projects") }; + std::optional grab_name; + grab_cmd->add_option ("config", grab_name, "Config name"); + CLI::App *backup_cmd{ app.add_subcommand ( "backup", "compress all selected projects") }; - app.require_subcommand (); + CLI::App *config_cmd{ app.add_subcommand ("config", + "manage configurations") }; + std::string config_entries; + ConfigEntries config; + config_cmd + ->add_option ("config", config_entries, "Configuration name and topic") + ->required (true); + config_cmd->add_option ("name", config.name, "Project name"); + config_cmd->add_option ("url", config.url, "Project url"); + config_cmd->add_option ("branch", config.branch, "Project branch"); + + // PARSE CLI11_PARSE (app, argc, argv); + // BEGIN Commands commands; if (*grab_cmd) - { - commands.grab (); - } + commands.grab (grab_name); if (*backup_cmd) - { - commands.backup (); - } + commands.backup (); + + if (*config_cmd) + commands.config (config_entries, config); return 0; } diff --git a/subprojects/catch2.wrap b/subprojects/catch2.wrap deleted file mode 100644 index 8abdf76..0000000 --- a/subprojects/catch2.wrap +++ /dev/null @@ -1,11 +0,0 @@ -[wrap-file] -directory = Catch2-3.5.4 -source_url = https://github.com/catchorg/Catch2/archive/v3.5.4.tar.gz -source_filename = Catch2-3.5.4.tar.gz -source_hash = b7754b711242c167d8f60b890695347f90a1ebc95949a045385114165d606dbb -source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/catch2_3.5.4-1/Catch2-3.5.4.tar.gz -wrapdb_version = 3.5.4-1 - -[provide] -catch2 = catch2_dep -catch2-with-main = catch2_with_main_dep diff --git a/subprojects/cli11.wrap b/subprojects/cli11.wrap deleted file mode 100644 index dd26d59..0000000 --- a/subprojects/cli11.wrap +++ /dev/null @@ -1,10 +0,0 @@ -[wrap-file] -directory = CLI11-2.4.1 -source_url = https://github.com/CLIUtils/CLI11/archive/refs/tags/v2.4.1.tar.gz -source_filename = CLI11-2.4.1.tar.gz -source_hash = 73b7ec52261ce8fe980a29df6b4ceb66243bb0b779451dbd3d014cfec9fdbb58 -source_fallback_url = https://github.com/mesonbuild/wrapdb/releases/download/cli11_2.4.1-1/CLI11-2.4.1.tar.gz -wrapdb_version = 2.4.1-1 - -[provide] -cli11 = CLI11_dep diff --git a/subprojects/nlohmann_json.wrap b/subprojects/nlohmann_json.wrap deleted file mode 100644 index bf5d700..0000000 --- a/subprojects/nlohmann_json.wrap +++ /dev/null @@ -1,10 +0,0 @@ -[wrap-file] -directory = nlohmann_json-3.11.2 -lead_directory_missing = true -source_url = https://github.com/nlohmann/json/releases/download/v3.11.2/include.zip -source_filename = nlohmann_json-3.11.2.zip -source_hash = e5c7a9f49a16814be27e4ed0ee900ecd0092bfb7dbfca65b5a421b774dccaaed -wrapdb_version = 3.11.2-1 - -[provide] -nlohmann_json = nlohmann_json_dep diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt new file mode 100644 index 0000000..3f9c73c --- /dev/null +++ b/tests/CMakeLists.txt @@ -0,0 +1,36 @@ +# Onur is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Onur is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Onur. If not, see . + +# Include FetchContent module +include(FetchContent) + +# Fetch Catch2 +FetchContent_Declare( + catch2 + GIT_REPOSITORY https://github.com/catchorg/Catch2.git + GIT_TAG v3.6.0 +) +FetchContent_MakeAvailable(catch2) + +# Add test executable +add_executable(onur_tests test_main.cpp test_globals.cpp) + +# Link Catch2 +target_link_libraries(onur_tests PRIVATE Catch2::Catch2WithMain) + +# Include the src/include directory +target_include_directories(onur_tests PRIVATE ${PROJECT_SOURCE_DIR}/src/include) + +# Add CTest support +enable_testing() +add_test(NAME MyTests COMMAND onur_tests) diff --git a/tests/test_globals.cpp b/tests/test_globals.cpp index 87b4df6..498cfc9 100644 --- a/tests/test_globals.cpp +++ b/tests/test_globals.cpp @@ -13,12 +13,64 @@ * along with Onur. If not, see . */ +#define CATCH_CONFIG_MAIN #include -// #include "globals.hpp" +int +add (int a, int b) +{ + return a + b; +} + +TEST_CASE ("Addition is performed correctly", "[math]") +{ + SECTION ("Positive numbers") + { + REQUIRE (add (2, 3) == 5); + REQUIRE (add (10, 20) == 30); + } + + SECTION ("Negative numbers") + { + REQUIRE (add (-2, -3) == -5); + REQUIRE (add (-10, -20) == -30); + } + + SECTION ("Mixed positive and negative") + { + REQUIRE (add (-5, 10) == 5); + REQUIRE (add (5, -10) == -5); + } +} -TEST_CASE ("Addition works correctly", "[add]") +TEST_CASE ("Floating point comparisons", "[math]") { - // Globals globals; - // REQUIRE (globals.homeDir == "/home/easbarba"); + REQUIRE_THAT (1.0 / 3.0, Catch::Matchers::WithinAbs (0.33333, 0.00001)); } + +SCENARIO ("User registration", "[user]") +{ + GIVEN ("A new user") + { + std::string username = "newuser"; + std::string password = "password123"; + + WHEN ("The user registers") + { + bool registration_result = true; // Simulated registration + + THEN ("The registration should be successful") + { + REQUIRE (registration_result); + } + } + } +} + +// #include "globals.hpp" + +// TEST_CASE ("Addition works correctly", "[add]") +// { +// Globals globals; +// REQUIRE (globals.homeDir == "/home/easbarba"); +// } diff --git a/tests/test_main.cpp b/tests/test_main.cpp index ef39dcd..97fb6e0 100644 --- a/tests/test_main.cpp +++ b/tests/test_main.cpp @@ -14,3 +14,10 @@ */ #include + +// TEST_CASE ("Addition works correctly", "[add]") +// { +// Globals globals; +// REQUIRE (globals.homeDir == "/home/easbarba"); +// REQUIRE (2 == 2); +// }