From a2e02d8dfa280fb3c2c6f55431ddc11f81e33c23 Mon Sep 17 00:00:00 2001 From: John Wellbelove Date: Wed, 20 Jul 2022 16:07:32 +0100 Subject: [PATCH] Added Log Errors event handler tests --- .gitignore | 1 + .../log_errors/CMakeLists.txt | 33 ++++ .../log_errors/etl_profile.h | 0 .../log_errors/test_error_handler.cpp | 180 ++++++++++++++++++ test/vs2019/etl.vcxproj | 86 ++++++++- test/vs2019/etl.vcxproj.filters | 24 +++ 6 files changed, 323 insertions(+), 1 deletion(-) create mode 100644 test/etl_error_handler/log_errors/CMakeLists.txt create mode 100644 test/etl_error_handler/log_errors/etl_profile.h create mode 100644 test/etl_error_handler/log_errors/test_error_handler.cpp diff --git a/.gitignore b/.gitignore index 4ca9b695..50091462 100644 --- a/.gitignore +++ b/.gitignore @@ -362,3 +362,4 @@ test/build-etl_initializer_list-GCC-Debug test/etl_initializer_list/etl_initializer_list.vcxproj.filters test/etl_initializer_list/build-make test/vs-build +test/etl_error_handler/build-log_errors-GCC-Debug diff --git a/test/etl_error_handler/log_errors/CMakeLists.txt b/test/etl_error_handler/log_errors/CMakeLists.txt new file mode 100644 index 00000000..67642447 --- /dev/null +++ b/test/etl_error_handler/log_errors/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.5.0) +project(etl_error_handler_unit_tests) + +add_definitions(-DETL_DEBUG) +add_definitions(-DETL_LOG_ERRORS) + +include_directories(${PROJECT_SOURCE_DIR}/../../../include) + +set(TEST_SOURCE_FILES + test_error_handler.cpp + ) + +add_executable(etl_tests + ${TEST_SOURCE_FILES} + ) + +target_include_directories(etl_tests + PUBLIC + ${CMAKE_CURRENT_LIST_DIR} + ) + +# Enable the 'make test' CMake target using the executable defined above +add_test(etl_error_handler_unit_tests etl_tests) + +# Since ctest will only show you the results of the single executable +# define a target that will output all of the failing or passing tests +# as they appear from UnitTest++ +add_custom_target(test_verbose COMMAND ${CMAKE_CTEST_COMMAND} --verbose) + + +#RSG +set_property(TARGET etl_tests PROPERTY CXX_STANDARD 17) + diff --git a/test/etl_error_handler/log_errors/etl_profile.h b/test/etl_error_handler/log_errors/etl_profile.h new file mode 100644 index 00000000..e69de29b diff --git a/test/etl_error_handler/log_errors/test_error_handler.cpp b/test/etl_error_handler/log_errors/test_error_handler.cpp new file mode 100644 index 00000000..04d05d37 --- /dev/null +++ b/test/etl_error_handler/log_errors/test_error_handler.cpp @@ -0,0 +1,180 @@ +/****************************************************************************** +The MIT License(MIT) + +Embedded Template Library. +https://github.com/ETLCPP/std +https://www.etlcpp.com + +Copyright(c) 2022 jwellbelove + +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 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. +******************************************************************************/ + +#include "etl/error_handler.h" + +#include +#include + +etl::error_handler eh; + +//#define ETL_ASSERT_FAIL(e) assert(false) // Asserts. +//#define ETL_ASSERT_FAIL_AND_RETURN(e) {assert(false); return;} // Asserts. +//#define ETL_ASSERT_FAIL_AND_RETURN_VALUE(e, v) + +//***************************************************************************** +struct ErrorLog +{ + ErrorLog() + : exception_count(0) + { + } + + void Log(const etl::exception& e) + { + ++exception_count; + } + + int exception_count; +}; + +int assert_return_count = 0; + +//***************************************************************************** +class test_exception : public etl::exception +{ +public: + + test_exception(string_type reason_, string_type file_name_, numeric_type line_number_) + : exception(reason_, file_name_, line_number_) + { + } +}; + +//***************************************************************************** +class test_exception_1 : public test_exception +{ +public: + + test_exception_1(string_type file_name_, numeric_type line_number_) + : test_exception(ETL_ERROR_TEXT("Test Exception 1", "1A"), file_name_, line_number_) + { + } +}; + +//***************************************************************************** +void Assert(bool state) +{ + ETL_ASSERT(state, ETL_ERROR(test_exception_1)); +} + +//***************************************************************************** +void AssertFail() +{ + ETL_ASSERT_FAIL(ETL_ERROR(test_exception_1)); +} + +//***************************************************************************** +void AssertAndReturn(bool state) +{ + ETL_ASSERT_AND_RETURN(state, ETL_ERROR(test_exception_1)); + + ++assert_return_count; +} + +//***************************************************************************** +void AssertFailAndReturn() +{ + ETL_ASSERT_FAIL_AND_RETURN(ETL_ERROR(test_exception_1)); + + ++assert_return_count; +} + +//***************************************************************************** +bool AssertAndReturnValue(bool state) +{ + ETL_ASSERT_AND_RETURN_VALUE(state, ETL_ERROR(test_exception_1), true); + + ++assert_return_count; + return false; +} + +//***************************************************************************** +bool AssertFailAndReturnValue() +{ + ETL_ASSERT_FAIL_AND_RETURN_VALUE(ETL_ERROR(test_exception_1), true); + + ++assert_return_count; + return false; +} + +//***************************************************************************** +int main() +{ + static ErrorLog error_log; + + etl::error_handler::set_callback(error_log); + + Assert(false); + Assert(true); + AssertFail(); + + AssertAndReturn(false); + AssertAndReturn(true); + AssertFailAndReturn(); + + if (AssertAndReturnValue(false)) + { + ++assert_return_count; + } + + if (AssertAndReturnValue(true)) + { + ++assert_return_count; + } + + if (AssertFailAndReturnValue()) + { + ++assert_return_count; + } + + bool error_count_passed = (error_log.exception_count == 6); + + if (error_count_passed) + { + std::cout << "Error Count Passed\n"; + } + else + { + std::cout << "Error Count Failed\n"; + } + + bool assert_return_count_passed = (assert_return_count == 4); + + if (assert_return_count_passed) + { + std::cout << "Assert Return Count Passed\n"; + } + else + { + std::cout << "Assert Return Count Failed\n"; + } + + return (error_count_passed && assert_return_count_passed) ? 0 : 1; +} + diff --git a/test/vs2019/etl.vcxproj b/test/vs2019/etl.vcxproj index 8b83ad4c..5d0aa29e 100644 --- a/test/vs2019/etl.vcxproj +++ b/test/vs2019/etl.vcxproj @@ -2655,6 +2655,34 @@ + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true true @@ -2728,6 +2756,34 @@ + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true true @@ -11930,6 +11986,34 @@ + + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true + true true @@ -11978,4 +12062,4 @@ - + \ No newline at end of file diff --git a/test/vs2019/etl.vcxproj.filters b/test/vs2019/etl.vcxproj.filters index 6ddd45f5..c64a775c 100644 --- a/test/vs2019/etl.vcxproj.filters +++ b/test/vs2019/etl.vcxproj.filters @@ -205,6 +205,21 @@ {0014395b-c658-4903-a15f-d21acb676d79} + + {69ed2e1e-f009-4a1a-abf2-a9144142b533} + + + {78498646-3058-4d15-9d21-2243ca5d5cd4} + + + {19d7da41-01e8-4449-8b9b-14be81447752} + + + {de372ec9-d193-4956-871f-065414a9d3be} + + + {1c718339-9f2d-4c83-a39e-66b7300df135} + @@ -1290,6 +1305,9 @@ Tests\Initializer List + + Tests\Error Handler\Log Errors + @@ -3284,6 +3302,9 @@ Tests\Initializer List + + Tests\Error Handler\Log Errors + @@ -3423,6 +3444,9 @@ Tests\Initializer List + + Tests\Error Handler\Log Errors +