diff --git a/src/xtd.core/CMakeLists.txt b/src/xtd.core/CMakeLists.txt index ec97b7dc141..3f0eaf14ea4 100644 --- a/src/xtd.core/CMakeLists.txt +++ b/src/xtd.core/CMakeLists.txt @@ -1076,6 +1076,7 @@ add_sources( src/xtd/type_object.cpp src/xtd/uri.cpp src/xtd/uri_format_exception.cpp + src/xtd/uri_template_match_exception.cpp src/xtd/string.cpp src/xtd/version.cpp src/xtd/collections/key_not_found_exception.cpp diff --git a/src/xtd.core/include/xtd/uri_template_match_exception.h b/src/xtd.core/include/xtd/uri_template_match_exception.h index cf777aac7f9..20f94971775 100644 --- a/src/xtd.core/include/xtd/uri_template_match_exception.h +++ b/src/xtd.core/include/xtd/uri_template_match_exception.h @@ -20,63 +20,22 @@ namespace xtd { /// @brief Create a new instance of class uri_template_match_exception /// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically xtd::diagnostics::stack_frame::current(). /// @remarks Message is set with the default message associate to the exception. - explicit uri_template_match_exception(const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : system_exception(default_message(), stack_frame) {} + explicit uri_template_match_exception(const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()); /// @brief Create a new instance of class uri_template_match_exception /// @param message Message string associate to the exception. /// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically xtd::diagnostics::stack_frame::current(). - explicit uri_template_match_exception(const xtd::string& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : system_exception(message, stack_frame) {} + explicit uri_template_match_exception(const std::optional& message, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()); /// @brief Create a new instance of class uri_template_match_exception /// @param message Message string associate to the exception. - /// @param error Error code associate to the exception. - /// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically xtd::diagnostics::stack_frame::current(). - explicit uri_template_match_exception(const xtd::string& message, const std::error_code& error, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : system_exception(message, error, stack_frame) {} - /// @brief Create a new instance of class uri_template_match_exception - /// @param message Message string associate to the exception. - /// @param help_link Help link string associate to the exception. - /// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically xtd::diagnostics::stack_frame::current(). - explicit uri_template_match_exception(const xtd::string& message, const xtd::string& help_link, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : system_exception(message, help_link, stack_frame) {} - /// @brief Create a new instance of class uri_template_match_exception - /// @param message Message string associate to the exception. - /// @param error Error code associate to the exception. - /// @param help_link Help link string associate to the exception. - explicit uri_template_match_exception(const xtd::string& message, const std::error_code& error, const xtd::string& help_link, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : system_exception(message, error, help_link, stack_frame) {} - /// @brief Create a new instance of class uri_template_match_exception /// @param inner_exception The exception that is the cause of the current exception. /// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically xtd::diagnostics::stack_frame::current(). - /// @remarks Message is set with the default message associate to the exception. - explicit uri_template_match_exception(const std::exception& inner_exception, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : system_exception(default_message(), inner_exception, stack_frame) {} - /// @brief Create a new instance of class uri_template_match_exception - /// @param message Message string associate to the exception. - /// @param inner_exception The exception that is the cause of the current exception. - /// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically xtd::diagnostics::stack_frame::current(). - explicit uri_template_match_exception(const xtd::string& message, const std::exception& inner_exception, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : system_exception(message, inner_exception, stack_frame) {} - /// @brief Create a new instance of class uri_template_match_exception - /// @param message Message string associate to the exception. - /// @param inner_exception The exception that is the cause of the current exception. - /// @param error Error code associate to the exception. - /// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically xtd::diagnostics::stack_frame::current(). - explicit uri_template_match_exception(const xtd::string& message, const std::exception& inner_exception, const std::error_code& error, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : system_exception(message, inner_exception, error, stack_frame) {} - /// @brief Create a new instance of class uri_template_match_exception - /// @param message Message string associate to the exception. - /// @param inner_exception The exception that is the cause of the current exception. - /// @param help_link Help link string associate to the exception. - /// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically xtd::diagnostics::stack_frame::current(). - explicit uri_template_match_exception(const xtd::string& message, const std::exception& inner_exception, const xtd::string& help_link, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : system_exception(message, inner_exception, help_link, stack_frame) {} - /// @brief Create a new instance of class uri_template_match_exception - /// @param message Message string associate to the exception. - /// @param inner_exception The exception that is the cause of the current exception. - /// @param error Error code associate to the exception. - /// @param help_link Help link string associate to the exception. - /// @param stack_frame (optional) Contains current stack frame about member name, file path and line number in the file where the exception is occurred. Typically xtd::diagnostics::stack_frame::current(). - explicit uri_template_match_exception(const xtd::string& message, const std::exception& inner_exception, const std::error_code& error, const xtd::string& help_link, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : system_exception(message, inner_exception, error, help_link, stack_frame) {} + template + uri_template_match_exception(const std::optional& message, const exception_t& inner_exception, const xtd::diagnostics::stack_frame& stack_frame = xtd::diagnostics::stack_frame::current()) : xtd::system_exception(message, inner_exception, stack_frame) {} /// @} /// @cond uri_template_match_exception(const uri_template_match_exception&) = default; uri_template_match_exception& operator =(const uri_template_match_exception&) = default; /// @endcond - - private: - const char* default_message() const noexcept {return "System error."_t;} }; } diff --git a/src/xtd.core/src/xtd/uri_template_match_exception.cpp b/src/xtd.core/src/xtd/uri_template_match_exception.cpp new file mode 100644 index 00000000000..c92a038ba96 --- /dev/null +++ b/src/xtd.core/src/xtd/uri_template_match_exception.cpp @@ -0,0 +1,11 @@ +#include "../../include/xtd/uri_template_match_exception.h" +#include "../../include/xtd/literals.h" + +using namespace xtd; +using namespace xtd::diagnostics; + +uri_template_match_exception::uri_template_match_exception(const stack_frame& stack_frame) : system_exception(std::nullopt, stack_frame) { +} + +uri_template_match_exception::uri_template_match_exception(const std::optional& message, const xtd::diagnostics::stack_frame& stack_frame) : system_exception(message, stack_frame) { +} diff --git a/tests/xtd.core.unit_tests/CMakeLists.txt b/tests/xtd.core.unit_tests/CMakeLists.txt index 0f1bd7dd2e7..16d179ac964 100644 --- a/tests/xtd.core.unit_tests/CMakeLists.txt +++ b/tests/xtd.core.unit_tests/CMakeLists.txt @@ -219,6 +219,7 @@ add_sources( src/xtd/tests/uri_host_name_type_tests.cpp src/xtd/tests/uri_kind_tests.cpp src/xtd/tests/uri_partial_tests.cpp + src/xtd/tests/uri_template_match_exception_tests.cpp src/xtd/tests/uri_tests.cpp src/xtd/tests/ustring_tests.cpp src/xtd/tests/version_tests.cpp diff --git a/tests/xtd.core.unit_tests/src/xtd/tests/uri_template_match_exception_tests.cpp b/tests/xtd.core.unit_tests/src/xtd/tests/uri_template_match_exception_tests.cpp new file mode 100644 index 00000000000..c12cec15cbf --- /dev/null +++ b/tests/xtd.core.unit_tests/src/xtd/tests/uri_template_match_exception_tests.cpp @@ -0,0 +1,263 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace xtd; +using namespace xtd::io; +using namespace xtd::reflection; +using namespace xtd::tunit; + +namespace xtd::tests { + class test_class_(uri_template_match_exception_tests) { + inline static bool stack_trace_enabled = false; + inline static std::locale previous_locale; + static void test_initialize_(test_initialize) { + previous_locale = std::locale::global(std::locale("en_US.UTF-8")); + stack_trace_enabled = uri_template_match_exception::enable_stack_trace(); + uri_template_match_exception::enable_stack_trace(false); + } + + static void test_cleanup_(test_cleanup) { + std::locale::global(previous_locale); + uri_template_match_exception::enable_stack_trace(stack_trace_enabled); + } + + void test_method_(default_constructor) { + auto e = uri_template_match_exception {}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_null(e.inner_exception()); + assert::are_equal("System error.", e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::is_not_empty(e.stack_trace()); + assert::is_not_empty(e.get_last_stack_frame().get_file_name()); + assert::is_not_zero(e.get_last_stack_frame().get_file_line_number()); + assert::is_not_empty(e.get_last_stack_frame().get_method()); + string_assert::starts_with("xtd::uri_template_match_exception : System error.", e.to_string()); + assert::are_equal("System error.", e.what()); + } + + void test_method_(default_constructor_with_stack_frame) { + auto stack_frame = xtd::diagnostics::stack_frame::current(); + auto e = uri_template_match_exception {stack_frame}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_null(e.inner_exception()); + assert::are_equal("System error.", e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::are_equal(stack_frame.to_string(), e.stack_trace()); + assert::are_equal(stack_frame.get_file_name(), e.get_last_stack_frame().get_file_name()); + assert::are_equal(stack_frame.get_file_line_number(), e.get_last_stack_frame().get_file_line_number()); + assert::are_equal(stack_frame.get_method(), e.get_last_stack_frame().get_method()); + assert::are_equal("xtd::uri_template_match_exception : System error." + environment::new_line() + stack_frame.to_string(), e.to_string()); + assert::are_equal("System error.", e.what()); + } + + void test_method_(constructor_with_nullopt_message) { + auto e = uri_template_match_exception {nullopt}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_null(e.inner_exception()); + assert::are_equal("System error.", e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::is_not_empty(e.stack_trace()); + assert::is_not_empty(e.get_last_stack_frame().get_file_name()); + assert::is_not_zero(e.get_last_stack_frame().get_file_line_number()); + assert::is_not_empty(e.get_last_stack_frame().get_method()); + string_assert::starts_with("xtd::uri_template_match_exception : System error.", e.to_string()); + assert::are_equal("System error.", e.what()); + } + + void test_method_(constructor_with_nullopt_message_and_stack_frame) { + auto stack_frame = xtd::diagnostics::stack_frame::current(); + auto e = uri_template_match_exception {nullopt, stack_frame}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_null(e.inner_exception()); + assert::are_equal("System error.", e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::are_equal(stack_frame.to_string(), e.stack_trace()); + assert::are_equal(stack_frame.get_file_name(), e.get_last_stack_frame().get_file_name()); + assert::are_equal(stack_frame.get_file_line_number(), e.get_last_stack_frame().get_file_line_number()); + assert::are_equal(stack_frame.get_method(), e.get_last_stack_frame().get_method()); + string_assert::starts_with("xtd::uri_template_match_exception : System error." + environment::new_line() + stack_frame.to_string(), e.to_string()); + assert::are_equal("System error.", e.what()); + } + + void test_method_(constructor_with_empty_message) { + auto e = uri_template_match_exception {""}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_null(e.inner_exception()); + assert::is_empty(e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::is_not_empty(e.stack_trace()); + assert::is_not_empty(e.get_last_stack_frame().get_file_name()); + assert::is_not_zero(e.get_last_stack_frame().get_file_line_number()); + assert::is_not_empty(e.get_last_stack_frame().get_method()); + string_assert::starts_with("xtd::uri_template_match_exception", e.to_string()); + assert::are_equal("xtd::uri_template_match_exception", e.what()); + } + + void test_method_(constructor_with_message_empty_and_stack_frame) { + auto stack_frame = xtd::diagnostics::stack_frame::current(); + auto e = uri_template_match_exception {"", stack_frame}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_null(e.inner_exception()); + assert::is_empty(e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::are_equal(stack_frame.to_string(), e.stack_trace()); + assert::are_equal(stack_frame.get_file_name(), e.get_last_stack_frame().get_file_name()); + assert::are_equal(stack_frame.get_file_line_number(), e.get_last_stack_frame().get_file_line_number()); + assert::are_equal(stack_frame.get_method(), e.get_last_stack_frame().get_method()); + assert::are_equal("xtd::uri_template_match_exception" + environment::new_line() + stack_frame.to_string(), e.to_string()); + assert::are_equal("xtd::uri_template_match_exception", e.what()); + } + + void test_method_(constructor_with_message) { + auto e = uri_template_match_exception {"System error."}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_null(e.inner_exception()); + assert::are_equal("System error.", e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::is_not_empty(e.stack_trace()); + assert::is_not_empty(e.get_last_stack_frame().get_file_name()); + assert::is_not_zero(e.get_last_stack_frame().get_file_line_number()); + assert::is_not_empty(e.get_last_stack_frame().get_method()); + string_assert::starts_with("xtd::uri_template_match_exception : System error.", e.to_string()); + assert::are_equal("System error.", e.what()); + } + + void test_method_(constructor_with_message_and_stack_frame) { + auto stack_frame = xtd::diagnostics::stack_frame::current(); + auto e = uri_template_match_exception {"System error.", stack_frame}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_null(e.inner_exception()); + assert::are_equal("System error.", e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::are_equal(stack_frame.to_string(), e.stack_trace()); + assert::are_equal(stack_frame.get_file_name(), e.get_last_stack_frame().get_file_name()); + assert::are_equal(stack_frame.get_file_line_number(), e.get_last_stack_frame().get_file_line_number()); + assert::are_equal(stack_frame.get_method(), e.get_last_stack_frame().get_method()); + assert::are_equal("xtd::uri_template_match_exception : System error." + environment::new_line() + stack_frame.to_string(), e.to_string()); + assert::are_equal("System error.", e.what()); + } + + void test_method_(constructor_with_nullopt_message_and_inner_exception) { + auto inner_exception = argument_exception {}; + auto e = uri_template_match_exception {nullopt, inner_exception}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_not_null(e.inner_exception()); + assert::is_instance_of(e.inner_exception().value().get()); + assert::are_equal("System error.", e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::is_not_empty(e.stack_trace()); + assert::is_not_empty(e.get_last_stack_frame().get_file_name()); + assert::is_not_zero(e.get_last_stack_frame().get_file_line_number()); + assert::is_not_empty(e.get_last_stack_frame().get_method()); + string_assert::starts_with("xtd::uri_template_match_exception : System error.", e.to_string()); + assert::are_equal("System error.", e.what()); + } + + void test_method_(constructor_with_nullopt_message_and_stack_frame_inner_exception) { + auto inner_exception = argument_exception {}; + auto stack_frame = xtd::diagnostics::stack_frame::current(); + auto e = uri_template_match_exception {nullopt, inner_exception, stack_frame}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_not_null(e.inner_exception()); + assert::is_instance_of(e.inner_exception().value().get()); + assert::are_equal("System error.", e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::are_equal(stack_frame.to_string(), e.stack_trace()); + assert::are_equal(stack_frame.get_file_name(), e.get_last_stack_frame().get_file_name()); + assert::are_equal(stack_frame.get_file_line_number(), e.get_last_stack_frame().get_file_line_number()); + assert::are_equal(stack_frame.get_method(), e.get_last_stack_frame().get_method()); + assert::are_equal("xtd::uri_template_match_exception : System error." + environment::new_line() + stack_frame.to_string(), e.to_string()); + assert::are_equal("System error.", e.what()); + } + + void test_method_(constructor_with_message_and_inner_exception) { + auto inner_exception = argument_exception {}; + auto e = uri_template_match_exception {"System error.", inner_exception}; + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_not_null(e.inner_exception()); + assert::is_instance_of(e.inner_exception().value().get()); + assert::are_equal(inner_exception.to_string(), e.inner_exception().value().get().to_string()); + assert::are_equal("System error.", e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::is_not_empty(e.stack_trace()); + assert::is_not_empty(e.get_last_stack_frame().get_file_name()); + assert::is_not_zero(e.get_last_stack_frame().get_file_line_number()); + assert::is_not_empty(e.get_last_stack_frame().get_method()); + string_assert::starts_with("xtd::uri_template_match_exception : System error.", e.to_string()); + assert::are_equal("System error.", e.what()); + } + + void test_method_(constructor_with_message_inner_exception_and_stack_frame) { + auto inner_exception = argument_exception {}; + auto stack_frame = xtd::diagnostics::stack_frame::current(); + auto e = uri_template_match_exception {"System error.", inner_exception, stack_frame}; + assert::are_equal("xtd::uri_template_match_exception", e.get_type().full_name()); + assert::is_empty(e.help_link()); + assert::are_equal(h_result::COR_E_SYSTEM, e.h_result()); + assert::are_equal(h_result::h_result_category(), e.error_code().category()); + assert::are_equal(e.h_result(), e.error_code().value()); + assert::is_not_null(e.inner_exception()); + assert::is_instance_of(e.inner_exception().value().get()); + assert::are_equal(inner_exception.to_string(), e.inner_exception().value().get().to_string()); + assert::are_equal("System error.", e.message()); + assert::are_equal(path::get_file_name(assembly::get_executing_assembly().location()), e.source()); + assert::are_equal(stack_frame.to_string(), e.stack_trace()); + assert::are_equal(stack_frame.get_file_name(), e.get_last_stack_frame().get_file_name()); + assert::are_equal(stack_frame.get_file_line_number(), e.get_last_stack_frame().get_file_line_number()); + assert::are_equal(stack_frame.get_method(), e.get_last_stack_frame().get_method()); + string_assert::starts_with("xtd::uri_template_match_exception : System error." + environment::new_line() + stack_frame.to_string(), e.to_string()); + assert::are_equal("System error.", e.what()); + } + }; +}