Rizky Ardi Maulana / mbed-os
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers utest_default_handlers.h Source File

utest_default_handlers.h

00001 /****************************************************************************
00002  * Copyright (c) 2015, ARM Limited, All Rights Reserved
00003  * SPDX-License-Identifier: Apache-2.0
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License"); you may
00006  * not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  * http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
00013  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  ****************************************************************************
00017  */
00018 
00019 #ifndef UTEST_DEFAULT_HANDLER_H
00020 #define UTEST_DEFAULT_HANDLER_H
00021 
00022 #include <stdint.h>
00023 #include <stdbool.h>
00024 #include <stdio.h>
00025 #include "utest/utest_types.h"
00026 
00027 
00028 namespace utest {
00029 /** \addtogroup frameworks */
00030 /** @{*/
00031 namespace v1 {
00032 
00033     /** Default handler hint.
00034      *
00035      * Use this handler to indicate the you want the default handler to be called.
00036      * This type automatically casts itself into the appropriate handler type, when possible.
00037      * Use the constants to default a handler unambigously.
00038      */
00039     static const struct
00040     {
00041         operator test_setup_handler_t()    const { return test_setup_handler_t(1); }
00042         operator test_teardown_handler_t() const { return test_teardown_handler_t(1); }
00043         operator test_failure_handler_t()  const { return test_failure_handler_t(1); }
00044 
00045         operator case_setup_handler_t()    const { return case_setup_handler_t(1); }
00046         operator case_teardown_handler_t() const { return case_teardown_handler_t(1); }
00047         operator case_failure_handler_t()  const { return case_failure_handler_t(1); }
00048     } default_handler;
00049 
00050     /** Ignore handler hint.
00051      *
00052      * Use this handler to indicate the you want to ignore this handler and it will not be called.
00053      * This type automatically casts itself into the appropriate handler type, when possible.
00054      * Use the constants to ignore a handler unambigously.
00055      */
00056     static const struct
00057     {
00058         operator case_handler_t()            const { return case_handler_t(NULL); }
00059         operator case_control_handler_t()    const { return case_control_handler_t(NULL); }
00060         operator case_call_count_handler_t() const { return case_call_count_handler_t(NULL); }
00061 
00062         operator test_setup_handler_t()    const { return test_setup_handler_t(NULL); }
00063         operator test_teardown_handler_t() const { return test_teardown_handler_t(NULL); }
00064         operator test_failure_handler_t()  const { return test_failure_handler_t(NULL); }
00065 
00066         operator case_setup_handler_t()    const { return case_setup_handler_t(NULL); }
00067         operator case_teardown_handler_t() const { return case_teardown_handler_t(NULL); }
00068         operator case_failure_handler_t()  const { return case_failure_handler_t(NULL); }
00069     } ignore_handler;
00070 
00071     /** A table of handlers.
00072      *
00073      * This structure stores all modifyable handlers and provides accessors to
00074      * filter out the default handler.
00075      * So if this structure contains handlers, and you want to use these handlers
00076      * as a default backup, you can use the `get_handler` function to choose the right handler.
00077      *
00078      * Example:
00079      * @code
00080      * const handler_t defaults = { ... }; // your default handlers
00081      * // will return the handler in defaults.
00082      * test_setup_handler_t handler = defaults.get_handler(default_handler);
00083      * // you will still need to manually check the handler before executing it
00084      * if (handler != ignore_handler) handler(...);
00085      *
00086      * extern test_teardown_handler_t custom_handler(...);
00087      * // will return `custom_handler`
00088      * test_teardown_handler_t handler = defaults.get_handler(custom_handler);
00089      * // you will still need to manually check the handler before executing it
00090      * if (handler != ignore_handler) handler(...);
00091      * @endcode
00092      */
00093     struct handlers_t
00094     {
00095         test_setup_handler_t test_setup;
00096         test_teardown_handler_t test_teardown;
00097         test_failure_handler_t test_failure;
00098 
00099         case_setup_handler_t case_setup;
00100         case_teardown_handler_t case_teardown;
00101         case_failure_handler_t case_failure;
00102 
00103         inline test_setup_handler_t get_handler(test_setup_handler_t handler) const {
00104             if (handler == default_handler) return test_setup;
00105             return handler;
00106         }
00107         inline test_teardown_handler_t get_handler(test_teardown_handler_t handler) const {
00108             if (handler == default_handler) return test_teardown;
00109             return handler;
00110         }
00111         inline test_failure_handler_t get_handler(test_failure_handler_t handler) const {
00112             if (handler == default_handler) return test_failure;
00113             return handler;
00114         }
00115 
00116         inline case_setup_handler_t get_handler(case_setup_handler_t handler) const {
00117             if (handler == default_handler) return case_setup;
00118             return handler;
00119         }
00120         inline case_teardown_handler_t get_handler(case_teardown_handler_t handler) const {
00121             if (handler == default_handler) return case_teardown;
00122             return handler;
00123         }
00124         inline case_failure_handler_t get_handler(case_failure_handler_t handler) const {
00125             if (handler == default_handler) return case_failure;
00126             return handler;
00127         }
00128     };
00129 
00130     /// Prints the number of tests to run and continues.
00131     utest::v1::status_t  verbose_test_setup_handler   (const size_t number_of_cases);
00132     /// Prints the number of tests that passed and failed with a reason if provided.
00133     void     verbose_test_teardown_handler(const size_t passed, const size_t failed, const failure_t failure);
00134     /// Prints the failure for `REASON_TEST_SETUP` and `REASON_TEST_TEARDOWN` and then dies.
00135     void     verbose_test_failure_handler (const failure_t failure);
00136 
00137     /// Prints the index and description of the case being run and continues.
00138     utest::v1::status_t  verbose_case_setup_handler   (const Case *const source, const size_t index_of_case);
00139     /// Prints the number of tests that passed and failed with a reason if provided within this case and continues.
00140     utest::v1::status_t  verbose_case_teardown_handler(const Case *const source, const size_t passed, const size_t failed, const failure_t failure);
00141     /// Prints the reason of the failure and continues, unless the teardown handler failed, for which it aborts.
00142     utest::v1::status_t  verbose_case_failure_handler (const Case *const source, const failure_t reason);
00143 
00144     /// Default greentea test case set up handler
00145     #define UTEST_DEFAULT_GREENTEA_TIMEOUT  10  //Seconds
00146     #define UTEST_DEFAULT_HOST_TEST_NAME    "default_auto"
00147 
00148     utest::v1::status_t  default_greentea_test_setup_handler   (const size_t number_of_cases);
00149 
00150     /// Requests the start test case from greentea and continues.
00151     /// Example usage:
00152     /// utest::v1::status_t greentea_test_setup(const size_t number_of_cases) {
00153     ///    GREENTEA_SETUP(5, "default_auto");
00154     ///    return greentea_test_setup_handler(number_of_cases);
00155     /// }
00156     ///
00157     /// Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
00158     utest::v1::status_t  greentea_test_setup_handler   (const size_t number_of_cases);
00159 
00160     /// Reports the test results to greentea.
00161     void     greentea_test_teardown_handler(const size_t passed, const size_t failed, const failure_t failure);
00162     /// Reports the failure for `REASON_TEST_SETUP` and `REASON_TEST_TEARDOWN` to greentea and then dies.
00163     void     greentea_test_failure_handler (const failure_t failure);
00164 
00165     /// Registers the test case setup with greentea.
00166     utest::v1::status_t  greentea_case_setup_handler   (const Case *const source, const size_t index_of_case);
00167     /// Registers the test case teardown with greentea.
00168     utest::v1::status_t  greentea_case_teardown_handler(const Case *const source, const size_t passed, const size_t failed, const failure_t failure);
00169     /// Reports the failure to greentea and then aborts.
00170     utest::v1::status_t  greentea_case_failure_abort_handler   (const Case *const source, const failure_t reason);
00171     /// Reports the failure to greentea and then continues.
00172     utest::v1::status_t  greentea_case_failure_continue_handler(const Case *const source, const failure_t reason);
00173 
00174     /// Notify greentea of testcase name.
00175     void greentea_testcase_notification_handler(const char *testcase);
00176 
00177     /// The verbose default handlers that always continue on failure
00178     extern const handlers_t verbose_continue_handlers;
00179 
00180     /// The greentea default handlers that always abort on the first encountered failure
00181     extern const handlers_t greentea_abort_handlers;
00182 
00183     /// The greentea default handlers that always continue on failure
00184     extern const handlers_t greentea_continue_handlers;
00185 
00186     /// The selftest default handlers that always abort on _any_ assertion failure, otherwise continue
00187     extern const handlers_t selftest_handlers;
00188 
00189     /// The greentea aborting handlers are the default
00190     extern const handlers_t& default_handlers;
00191 
00192 }   // namespace v1
00193 }   // namespace utest
00194 
00195 #endif // UTEST_DEFAULT_HANDLER_H
00196 
00197 /** @}*/