From 60e3d6305d1e618d84661a39a1285cecaf847304 Mon Sep 17 00:00:00 2001 From: Alfi Maulana Date: Wed, 21 Jun 2023 14:46:30 +0700 Subject: [PATCH] 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)); -}