From 99f8975be3937c099f835f84003e47011424bc8f Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 21 Jun 2023 14:01:48 +0700 Subject: [PATCH 1/7] feat: replace example package with error package --- .github/workflows/build.yml | 4 ++-- .github/workflows/test.yml | 16 ++++++++-------- README.md | 4 ++++ {example => error}/.clang-format | 0 {example => error}/.cmake-format | 0 {example => error}/CMakeLists.txt | 12 ++++++------ {example => error}/LICENSE | 0 error/README.md | 3 +++ {example => error}/cmake/CPM.cmake | 0 {example => error}/include/example/example.hpp | 0 {example => error}/src/example.cpp | 0 {example => error}/test/example_test.cpp | 0 example/README.md | 6 ------ 13 files changed, 23 insertions(+), 22 deletions(-) rename {example => error}/.clang-format (100%) rename {example => error}/.cmake-format (100%) rename {example => error}/CMakeLists.txt (65%) rename {example => error}/LICENSE (100%) create mode 100644 error/README.md rename {example => error}/cmake/CPM.cmake (100%) rename {example => error}/include/example/example.hpp (100%) rename {example => error}/src/example.cpp (100%) rename {example => error}/test/example_test.cpp (100%) delete mode 100644 example/README.md diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b052d1d..753fb40 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -14,5 +14,5 @@ jobs: - name: Configure and build this project uses: threeal/cmake-action@v1.1.0 with: - source-dir: example - build-dir: example/build + source-dir: error + build-dir: error/build diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 5321274..00115b6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,15 +14,15 @@ jobs: - name: Configure, build, and test this project uses: threeal/cmake-action@v1.1.0 with: - source-dir: example - build-dir: example/build + source-dir: error + build-dir: error/build args: -DBUILD_TESTING=ON run-test: true - name: Check code coverage uses: threeal/gcovr-action@v0.2.0 with: - root: example + root: error exclude: build/* fail-under-line: 80 @@ -35,8 +35,8 @@ jobs: - name: Configure and build this project uses: threeal/cmake-action@v1.1.0 with: - source-dir: example - build-dir: example/build + source-dir: error + build-dir: error/build cxx-flags: -Werror args: -DBUILD_TESTING=ON @@ -50,9 +50,9 @@ jobs: run: pip3 install cmake-format - name: Configure CMake - run: cmake example -B example/build + run: cmake error -B error/build - name: Check code formatting run: | - cmake --build example/build --target format - cmake --build example/build --target check-format + cmake --build error/build --target format + cmake --build error/build --target check-format diff --git a/README.md b/README.md index 6b75613..17ce12d 100644 --- a/README.md +++ b/README.md @@ -5,6 +5,10 @@ A comprehensive collection of [C++](https://isocpp.org/) utility packages. +## Packages + +- [Error](./error) [WIP], an error utility package. + ## License This project is licensed under the terms of the [MIT License](./LICENSE). diff --git a/example/.clang-format b/error/.clang-format similarity index 100% rename from example/.clang-format rename to error/.clang-format diff --git a/example/.cmake-format b/error/.cmake-format similarity index 100% rename from example/.cmake-format rename to error/.cmake-format diff --git a/example/CMakeLists.txt b/error/CMakeLists.txt similarity index 65% rename from example/CMakeLists.txt rename to error/CMakeLists.txt index e86bf9c..0cf8e9d 100644 --- a/example/CMakeLists.txt +++ b/error/CMakeLists.txt @@ -1,11 +1,11 @@ cmake_minimum_required(VERSION 3.0) -project(example) +project(error) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow -Wnon-virtual-dtor -Wpedantic") -add_library(example src/example.cpp) -target_include_directories(example PUBLIC include) +add_library(error src/example.cpp) +target_include_directories(error PUBLIC include) if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) include(cmake/CPM.cmake) @@ -19,8 +19,8 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -fPIC -O0") - add_executable(example_test test/example_test.cpp) - target_link_libraries(example_test PRIVATE example Catch2::Catch2WithMain) - catch_discover_tests(example_test) + add_executable(error_test test/example_test.cpp) + target_link_libraries(error_test PRIVATE error Catch2::Catch2WithMain) + catch_discover_tests(error_test) endif() endif() diff --git a/example/LICENSE b/error/LICENSE similarity index 100% rename from example/LICENSE rename to error/LICENSE diff --git a/error/README.md b/error/README.md new file mode 100644 index 0000000..4040404 --- /dev/null +++ b/error/README.md @@ -0,0 +1,3 @@ +# Error + +A C++ error utility package. diff --git a/example/cmake/CPM.cmake b/error/cmake/CPM.cmake similarity index 100% rename from example/cmake/CPM.cmake rename to error/cmake/CPM.cmake diff --git a/example/include/example/example.hpp b/error/include/example/example.hpp similarity index 100% rename from example/include/example/example.hpp rename to error/include/example/example.hpp diff --git a/example/src/example.cpp b/error/src/example.cpp similarity index 100% rename from example/src/example.cpp rename to error/src/example.cpp diff --git a/example/test/example_test.cpp b/error/test/example_test.cpp similarity index 100% rename from example/test/example_test.cpp rename to error/test/example_test.cpp diff --git a/example/README.md b/example/README.md deleted file mode 100644 index c1f4159..0000000 --- a/example/README.md +++ /dev/null @@ -1,6 +0,0 @@ -# Example - -[![build status](https://img.shields.io/github/actions/workflow/status/threeal/cpp/build.yml?branch=main)](https://github.com/threeal/cpp/actions/workflows/build.yml) -[![test status](https://img.shields.io/github/actions/workflow/status/threeal/cpp/test.yml?label=test&branch=main)](https://github.com/threeal/cpp/actions/workflows/test.yml) - -An example C++ package. From a073dae9885de3dfa351a4d920317c53492670a1 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 21 Jun 2023 14:28:43 +0700 Subject: [PATCH 2/7] docs(error): use MIT license --- error/LICENSE | 37 +++++++++++++++++-------------------- error/README.md | 6 ++++++ 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/error/LICENSE b/error/LICENSE index 68a49da..c5ebe52 100644 --- a/error/LICENSE +++ b/error/LICENSE @@ -1,24 +1,21 @@ -This is free and unencumbered software released into the public domain. +MIT License -Anyone is free to copy, modify, publish, use, compile, sell, or -distribute this software, either in source code form or as a compiled -binary, for any purpose, commercial or non-commercial, and by any -means. +Copyright (c) 2023 Alfi Maulana -In jurisdictions that recognize copyright laws, the author or authors -of this software dedicate any and all copyright interest in the -software to the public domain. We make this dedication for the benefit -of the public at large and to the detriment of our heirs and -successors. We intend this dedication to be an overt act of -relinquishment in perpetuity of all present and future rights to this -software under copyright law. +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 SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. -IN NO EVENT SHALL THE AUTHORS 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. +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. -For more information, please refer to +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE 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. diff --git a/error/README.md b/error/README.md index 4040404..a51a219 100644 --- a/error/README.md +++ b/error/README.md @@ -1,3 +1,9 @@ # Error A C++ error utility package. + +## License + +This project is licensed under the terms of the [MIT License](./LICENSE). + +Copyright © 2023 [Alfi Maulana](https://github.com/threeal) From bb5176b561cf38ef009111de2268eec1fd8684f3 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 21 Jun 2023 14:34:07 +0700 Subject: [PATCH 3/7] docs(error): update the package description --- README.md | 2 +- error/README.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 17ce12d..7e70752 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ A comprehensive collection of [C++](https://isocpp.org/) utility packages. ## Packages -- [Error](./error) [WIP], an error utility package. +- [Error](./error) [WIP]: Provides utilities for error handling. ## License diff --git a/error/README.md b/error/README.md index a51a219..a672b77 100644 --- a/error/README.md +++ b/error/README.md @@ -1,6 +1,6 @@ # Error -A C++ error utility package. +A C++ package that provides utilities for error handling. ## License From 60e3d6305d1e618d84661a39a1285cecaf847304 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 21 Jun 2023 14:46:30 +0700 Subject: [PATCH 4/7] feat(error): add `Error` struct --- error/CMakeLists.txt | 5 +++-- error/include/error/error.hpp | 18 ++++++++++++++++++ error/include/example/example.hpp | 7 ------- error/src/error.cpp | 7 +++++++ error/src/example.cpp | 7 ------- error/test/error_test.cpp | 8 ++++++++ error/test/example_test.cpp | 7 ------- 7 files changed, 36 insertions(+), 23 deletions(-) create mode 100644 error/include/error/error.hpp delete mode 100644 error/include/example/example.hpp create mode 100644 error/src/error.cpp delete mode 100644 error/src/example.cpp create mode 100644 error/test/error_test.cpp delete mode 100644 error/test/example_test.cpp diff --git a/error/CMakeLists.txt b/error/CMakeLists.txt index 0cf8e9d..2d604e1 100644 --- a/error/CMakeLists.txt +++ b/error/CMakeLists.txt @@ -3,8 +3,9 @@ cmake_minimum_required(VERSION 3.0) project(error) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow -Wnon-virtual-dtor -Wpedantic") +set(CMAKE_CXX_STANDARD 11) -add_library(error src/example.cpp) +add_library(error src/error.cpp) target_include_directories(error PUBLIC include) if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) @@ -19,7 +20,7 @@ if(CMAKE_CURRENT_SOURCE_DIR STREQUAL CMAKE_SOURCE_DIR) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fprofile-arcs -ftest-coverage -fPIC -O0") - add_executable(error_test test/example_test.cpp) + add_executable(error_test test/error_test.cpp) target_link_libraries(error_test PRIVATE error Catch2::Catch2WithMain) catch_discover_tests(error_test) endif() diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp new file mode 100644 index 0000000..0391eab --- /dev/null +++ b/error/include/error/error.hpp @@ -0,0 +1,18 @@ +#pragma once + +#include + +namespace error { + +/** + * @brief A struct that represents error information. + */ +struct Error : public std::exception { + /** + * @brief Returns the explanatory string. + * @return Pointer to a null-terminated string with explanatory information. + */ + const char* what() const noexcept override; +}; + +} // namespace error diff --git a/error/include/example/example.hpp b/error/include/example/example.hpp deleted file mode 100644 index 1d53053..0000000 --- a/error/include/example/example.hpp +++ /dev/null @@ -1,7 +0,0 @@ -#pragma once - -namespace example { - -bool is_odd(int val); - -} // namespace example diff --git a/error/src/error.cpp b/error/src/error.cpp new file mode 100644 index 0000000..9020e7a --- /dev/null +++ b/error/src/error.cpp @@ -0,0 +1,7 @@ +#include + +namespace error { + +const char* Error::what() const noexcept { return "unknown error"; } + +} // namespace error diff --git a/error/src/example.cpp b/error/src/example.cpp deleted file mode 100644 index bed773d..0000000 --- a/error/src/example.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include - -namespace example { - -bool is_odd(int val) { return val % 2 == 1; } - -} // namespace example diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp new file mode 100644 index 0000000..f96f4e8 --- /dev/null +++ b/error/test/error_test.cpp @@ -0,0 +1,8 @@ +#include +#include + +TEST_CASE("test Error::what") { + const error::Error err; + const std::string expected("unknown error"); + REQUIRE(expected.compare(err.what()) == 0); +} diff --git a/error/test/example_test.cpp b/error/test/example_test.cpp deleted file mode 100644 index f8a5f4a..0000000 --- a/error/test/example_test.cpp +++ /dev/null @@ -1,7 +0,0 @@ -#include -#include - -TEST_CASE("test is odd") { - CHECK(example::is_odd(3)); - CHECK_FALSE(example::is_odd(4)); -} From ce9b1ebfd61ab159265745759249768f8d485f9e Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 21 Jun 2023 15:07:43 +0700 Subject: [PATCH 5/7] feat(error): add `Error` creation support with custom message --- error/include/error/error.hpp | 14 ++++++++++++-- error/src/error.cpp | 4 +++- error/test/error_test.cpp | 5 +++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/error/include/error/error.hpp b/error/include/error/error.hpp index 0391eab..bd70762 100644 --- a/error/include/error/error.hpp +++ b/error/include/error/error.hpp @@ -5,9 +5,19 @@ namespace error { /** - * @brief A struct that represents error information. + * @brief A class that represents error information. */ -struct Error : public std::exception { +class Error : public std::exception { + private: + const char* message; /**< The error message. */ + + public: + /** + * @brief Constructs a new error with the given message. + * @param message An error message. + */ + Error(const char* message); + /** * @brief Returns the explanatory string. * @return Pointer to a null-terminated string with explanatory information. diff --git a/error/src/error.cpp b/error/src/error.cpp index 9020e7a..0c2d874 100644 --- a/error/src/error.cpp +++ b/error/src/error.cpp @@ -2,6 +2,8 @@ namespace error { -const char* Error::what() const noexcept { return "unknown error"; } +Error::Error(const char* message) : message(message) {} + +const char* Error::what() const noexcept { return message; } } // namespace error diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index f96f4e8..b19334d 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -1,8 +1,9 @@ #include #include +#include -TEST_CASE("test Error::what") { - const error::Error err; +TEST_CASE("Constructs an error") { + const error::Error err("unknown error"); const std::string expected("unknown error"); REQUIRE(expected.compare(err.what()) == 0); } From ae2da451468693f9b6018e442c6ee62c21efda32 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 21 Jun 2023 15:16:56 +0700 Subject: [PATCH 6/7] test(error): add error throwing and catching test --- error/test/error_test.cpp | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/error/test/error_test.cpp b/error/test/error_test.cpp index b19334d..3afb818 100644 --- a/error/test/error_test.cpp +++ b/error/test/error_test.cpp @@ -2,8 +2,29 @@ #include #include -TEST_CASE("Constructs an error") { +TEST_CASE("Error Construction") { const error::Error err("unknown error"); - const std::string expected("unknown error"); - REQUIRE(expected.compare(err.what()) == 0); + REQUIRE(std::string("unknown error") == err.what()); +} + +TEST_CASE("Error Throwing and Catching") { + SECTION("Catch as error::Error") { + try { + throw error::Error("unknown error"); + } catch (const error::Error& err) { + REQUIRE(std::string("unknown error") == err.what()); + } catch (...) { + FAIL("Expected to be caught as error::Error"); + } + } + + SECTION("Catch as std::exception") { + try { + throw error::Error("unknown error"); + } catch (const std::exception& e) { + REQUIRE(std::string("unknown error") == e.what()); + } catch (...) { + FAIL("Expected to be caught as std::exception"); + } + } } From 60d120c249101b56c26ef11a3832e45feb62eebd Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 21 Jun 2023 15:48:20 +0700 Subject: [PATCH 7/7] fix: disable shadowed declaration warning --- error/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/error/CMakeLists.txt b/error/CMakeLists.txt index 2d604e1..ae7f826 100644 --- a/error/CMakeLists.txt +++ b/error/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0) project(error) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wshadow -Wnon-virtual-dtor -Wpedantic") +set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wnon-virtual-dtor -Wpedantic") set(CMAKE_CXX_STANDARD 11) add_library(error src/error.cpp)