Move header files to `utest` folder.

Niklas Hauser 2015-11-23 13:41:22 +00:00 committed by Martin Kojtal
parent bfc61ce8ae
commit a3129f204a
7 changed files with 752 additions and 0 deletions

View File

@ -0,0 +1,129 @@
/****************************************************************************
* Copyright (c) 2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
****************************************************************************
*/
#ifndef MBED_TEST_ASYNC_CASES_H
#define MBED_TEST_ASYNC_CASES_H
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "types.h"
#include "default_handlers.h"
namespace utest {
namespace v0 {
/** Test case wrapper class.
*
* This class contains the description of the test case and all handlers
* for setting up, running the test case, tearing down and handling failures.
*
* By default you only need to provide a description and a test case handler.
* You may override the setup, teardown and failure handlers, but you do not have to.
* If you do not override these handler, the specified default handlers will be called.
*
* These constructors are overloaded to allow you a comfortable declaration of all your
* callbacks.
* The order is always:
* - description (required)
* - setup handler (optional)
* - test case handler (required)
* - teardown handler (optional)
* - failure handler (optional)
*
* @note While you can specify an empty test case (ie. use `ignore_handler` for all callbacks),
* the harness will abort the test unconditionally.
*/
class Case
{
public:
// overloads for case_handler_t
Case(const char *description,
const case_setup_handler_t setup_handler,
const case_handler_t case_handler,
const case_teardown_handler_t teardown_handler = default_handler,
const case_failure_handler_t failure_handler = default_handler);
Case(const char *description,
const case_handler_t case_handler,
const case_failure_handler_t failure_handler = default_handler);
Case(const char *description,
const case_handler_t case_handler,
const case_teardown_handler_t teardown_handler,
const case_failure_handler_t failure_handler = default_handler);
// overloads for case_control_handler_t
Case(const char *description,
const case_setup_handler_t setup_handler,
const case_control_handler_t case_handler,
const case_teardown_handler_t teardown_handler = default_handler,
const case_failure_handler_t failure_handler = default_handler);
Case(const char *description,
const case_control_handler_t case_handler,
const case_failure_handler_t failure_handler = default_handler);
Case(const char *description,
const case_control_handler_t case_handler,
const case_teardown_handler_t teardown_handler,
const case_failure_handler_t failure_handler = default_handler);
// overloads for case_repeat_count_handler_t
Case(const char *description,
const case_setup_handler_t setup_handler,
const case_repeat_count_handler_t case_handler,
const case_teardown_handler_t teardown_handler = default_handler,
const case_failure_handler_t failure_handler = default_handler);
Case(const char *description,
const case_repeat_count_handler_t case_handler,
const case_failure_handler_t failure_handler = default_handler);
Case(const char *description,
const case_repeat_count_handler_t case_handler,
const case_teardown_handler_t teardown_handler,
const case_failure_handler_t failure_handler = default_handler);
/// @returns the textual description of the test case
const char* get_description() const;
/// @returns `true` if setup, test and teardown handlers are set to `ignore_handler`
bool is_empty() const;
private:
const char *description;
const case_handler_t handler;
const case_control_handler_t control_handler;
const case_repeat_count_handler_t repeat_count_handler;
const case_setup_handler_t setup_handler;
const case_teardown_handler_t teardown_handler;
const case_failure_handler_t failure_handler;
friend class Harness;
};
} // namespace v0
} // namespace utest
#endif // MBED_TEST_ASYNC_CASES_H

View File

@ -0,0 +1,173 @@
/****************************************************************************
* Copyright (c) 2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
****************************************************************************
*/
#ifndef MBED_TEST_ASYNC_DEFAULT_HANDLER_H
#define MBED_TEST_ASYNC_DEFAULT_HANDLER_H
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "types.h"
namespace utest {
namespace v0 {
/** Default handler hint.
*
* Use this handler to indicate the you want the default handler to be called.
* This type automatically casts itself into the appropriate handler type.
*/
const struct
{
operator test_setup_handler_t() const { return test_setup_handler_t(1); }
operator test_teardown_handler_t() const { return test_teardown_handler_t(1); }
operator case_setup_handler_t() const { return case_setup_handler_t(1); }
operator case_teardown_handler_t() const { return case_teardown_handler_t(1); }
operator case_failure_handler_t() const { return case_failure_handler_t(1); }
} default_handler;
/** Ignore handler hint.
*
* Use this handler to indicate the you want to ignore this handler and it will not be called.
* This type automatically casts itself into the appropriate handler type.
*/
const struct
{
operator case_handler_t() const { return case_handler_t(NULL); }
operator case_control_handler_t() const { return case_control_handler_t(NULL); }
operator case_repeat_count_handler_t() const { return case_repeat_count_handler_t(NULL); }
operator test_setup_handler_t() const { return test_setup_handler_t(NULL); }
operator test_teardown_handler_t() const { return test_teardown_handler_t(NULL); }
operator case_setup_handler_t() const { return case_setup_handler_t(NULL); }
operator case_teardown_handler_t() const { return case_teardown_handler_t(NULL); }
operator case_failure_handler_t() const { return case_failure_handler_t(NULL); }
} ignore_handler;
/** A table of handlers.
*
* This structure stores all modifyable handlers and provides accessors to
* filter out the default handler.
* So if this structure contains handlers, and you want to use these handlers
* as a default backup, you can use the `get_handler` function to choose the right handler.
*
* Example:
* @code
* const handler_t defaults = { ... }; // your default handlers
* // will return the handler in defaults.
* test_setup_handler_t handler = defaults.get_handler(default_handler);
* // you will still need to manually check the handler before executing it
* if (handler != ignore_handler) handler(...);
*
* extern test_teardown_handler_t custom_handler(...);
* // will return `custom_handler`
* test_teardown_handler_t handler = defaults.get_handler(custom_handler);
* // you will still need to manually check the handler before executing it
* if (handler != ignore_handler) handler(...);
* @endcode
*/
struct handlers_t
{
test_setup_handler_t test_setup;
test_teardown_handler_t test_teardown;
case_setup_handler_t case_setup;
case_teardown_handler_t case_teardown;
case_failure_handler_t case_failure;
inline test_setup_handler_t get_handler(test_setup_handler_t handler) const {
if (handler == default_handler) return test_setup;
return handler;
}
inline test_teardown_handler_t get_handler(test_teardown_handler_t handler) const {
if (handler == default_handler) return test_teardown;
return handler;
}
inline case_setup_handler_t get_handler(case_setup_handler_t handler) const {
if (handler == default_handler) return case_setup;
return handler;
}
inline case_teardown_handler_t get_handler(case_teardown_handler_t handler) const {
if (handler == default_handler) return case_teardown;
return handler;
}
inline case_failure_handler_t get_handler(case_failure_handler_t handler) const {
if (handler == default_handler) return case_failure;
return handler;
}
};
/// Prints the number of tests to run and continues.
status_t verbose_test_setup_handler (const size_t number_of_cases);
/// Prints the number of tests that passed and failed with a reason if provided.
void verbose_test_teardown_handler(const size_t passed, const size_t failed, const failure_t failure);
/// Prints the index and description of the case being run and continues.
status_t verbose_case_setup_handler (const Case *const source, const size_t index_of_case);
/// Prints the number of tests that passed and failed with a reason if provided within this case and continues.
status_t verbose_case_teardown_handler(const Case *const source, const size_t passed, const size_t failed, const failure_t failure);
/// Prints the reason of the failure and continues, unless the teardown handler failed, for which it aborts.
status_t verbose_case_failure_handler (const Case *const source, const failure_t reason);
/// Prints a helpful error message and aborts.
/// This function **NEEDS** to be overridden by the user when using greentea.
status_t greentea_test_setup_handler (const size_t number_of_cases);
/// Calls `verbose_test_teardown_handler` and then prints the greentea failure and success and end strings.
void greentea_test_teardown_handler(const size_t passed, const size_t failed, const failure_t failure);
/// Calls `verbose_case_failure_handler` but then aborts.
status_t greentea_case_failure_handler (const Case *const source, const failure_t reason);
/// The verbose default handlers that always continue on failure
const handlers_t verbose_continue_handlers = {
verbose_test_setup_handler,
verbose_test_teardown_handler,
verbose_case_setup_handler,
verbose_case_teardown_handler,
verbose_case_failure_handler
};
/// The greentea default handlers that always abort on the first encountered failure
const handlers_t greentea_abort_handlers = {
greentea_test_setup_handler,
greentea_test_teardown_handler,
verbose_case_setup_handler,
verbose_case_teardown_handler,
greentea_case_failure_handler
};
/// The greentea default handlers that always continue on failure
const handlers_t greentea_continue_handlers = {
greentea_test_setup_handler,
greentea_test_teardown_handler,
verbose_case_setup_handler,
verbose_case_teardown_handler,
verbose_case_failure_handler
};
/// The greentea aborting handlers are the default
const handlers_t default_handlers = greentea_abort_handlers;
} // namespace v0
} // namespace utest
#endif // MBED_TEST_ASYNC_DEFAULT_HANDLER_H

View File

@ -0,0 +1,73 @@
/****************************************************************************
* Copyright (c) 2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
****************************************************************************
*/
#ifndef MBED_TEST_ASYNC_HARNESS_H
#define MBED_TEST_ASYNC_HARNESS_H
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "types.h"
#include "case.h"
#include "default_handlers.h"
#include "specification.h"
namespace utest {
namespace v0 {
/** Test Harness.
*
* This class runs a test specification for you and calls all required handlers.
* The harness executes the test specification in an asynchronous fashion, therefore
* `run()` returns immediately.
*
* @note: In case of an test abort, the harness will busy-wait and never finish.
*/
class Harness
{
public:
/// Starts running a test specification
static void run(const Specification specification);
/// @returns `true` if a test specification is being executed, `false` otherwise
static bool is_busy();
/** Call this function in the asynchronous callback that you have been waiting for.
*
* You can only validate a callback once, calling this function when no callback is expected
* has no side effects.
* After callback validation, the next test case is scheduled.
*/
static void validate_callback();
/// Raising a failure causes the failure to be counted and the failure handler to be called.
/// Further action then depends on its return state.
static void raise_failure(failure_t reason);
protected:
static void run_next_case();
static void handle_timeout();
static void schedule_next_case();
};
} // namespace v0
} // namespace utest
#endif // MBED_TEST_ASYNC_HARNESS_H

View File

@ -0,0 +1,97 @@
/****************************************************************************
* Copyright (c) 2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
****************************************************************************
*/
#ifndef MBED_TEST_ASYNC_SPECIFICATION_H
#define MBED_TEST_ASYNC_SPECIFICATION_H
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include "types.h"
#include "case.h"
#include "default_handlers.h"
namespace utest {
namespace v0 {
/** Test specification containing the setup and teardown handlers and test cases.
*
* This class simply holds the test cases and allows you to specify default handlers, and
* override setup and teardown handlers.
* The order of arguments is:
* - test setup handler (optional)
* - array of test cases (required)
* - test teardown handler (optional)
* - default handlers (optional)
*
* @note You cannot set the size of the test case array dynamically, it is template deducted at compile
* time. Creating test specifications for unittests at runtime is explicitly not supported.
*/
class Specification
{
public:
template< size_t N >
Specification(const Case (&cases)[N],
const test_teardown_handler_t teardown_handler = default_handler,
const handlers_t defaults = default_handlers) :
setup_handler(default_handler), teardown_handler(teardown_handler),
cases(cases), length(N),
defaults(defaults)
{}
template< size_t N >
Specification(const Case (&cases)[N], const handlers_t defaults) :
setup_handler(default_handler), teardown_handler(default_handler),
cases(cases), length(N),
defaults(defaults)
{}
template< size_t N >
Specification(const test_setup_handler_t setup_handler,
const Case (&cases)[N],
const test_teardown_handler_t teardown_handler = default_handler,
const handlers_t defaults = default_handlers) :
setup_handler(setup_handler), teardown_handler(teardown_handler),
cases(cases), length(N),
defaults(defaults)
{}
template< size_t N >
Specification(const test_setup_handler_t setup_handler,
const Case (&cases)[N],
const handlers_t defaults) :
setup_handler(setup_handler), teardown_handler(default_handler),
cases(cases), length(N),
defaults(defaults)
{}
private:
const test_setup_handler_t setup_handler;
const test_teardown_handler_t teardown_handler;
const Case *const cases;
const size_t length;
const handlers_t defaults;
friend class Harness;
};
} // namespace v0
} // namespace utest
#endif // MBED_TEST_ASYNC_SPECIFICATION_H

View File

@ -0,0 +1,226 @@
/****************************************************************************
* Copyright (c) 2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
****************************************************************************
*/
#ifndef MBED_TEST_ASYNC_TYPES_H
#define MBED_TEST_ASYNC_TYPES_H
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
namespace utest {
namespace v0 {
enum repeat_t {
REPEAT_NO_REPEAT = 0, ///< continue with the next test case
REPEAT_CASE_ONLY, ///< repeat the current test case without the setup and teardown handlers
REPEAT_ALL, ///< repeat the current test case with the setup and teardown handlers
};
enum status_t {
STATUS_CONTINUE = 0, ///< continues testing
STATUS_ABORT, ///< stops testing
};
enum failure_t {
FAILURE_NONE = 0, ///< No failure occurred
FAILURE, ///< An unknown failure occurred
FAILURE_CASES, ///< A failure occurred in at least one test case
FAILURE_EMPTY_CASE, ///< The test case contains only empty handlers
FAILURE_SETUP, ///< A failure occurred on setup
FAILURE_TEARDOWN, ///< A failure occurred on teardown
FAILURE_TIMEOUT, ///< An expected asynchronous call timed out
FAILURE_ASSERTION, ///< An assertion failed
};
/// Stringifies a failure for understandable error messages.
const char* stringify(failure_t failure);
/** Control class for specifying test case attributes
*
* This class encapsulated control information about test cases which, when returned from
* a test case influences the behavior of the test harness.
* Instead of using this class directly it is recommended to use the aliases for clearer
* semantics:
* @code
* control_t test_case(const size_t repeat_count) {
* // repeat 5 times for a total of 6 calls
* return (repeat_count < 5) ? CaseRepeat : CaseNext;
* }
* @endcode
*
* This class overloads the `+` operator to implement something similiar to saturated arbitration:
* - The lower timeout value "wins".
* - A more involved repeat "wins" (ie. `ALL` > 'CASE_ONLY' > 'NO_REPEAT').
*
* You may then add timeouts and repeats together:
* @code
* control_t test_case(const size_t repeat_count) {
* // repeat 5 times for a total of 6 calls, each with a 500ms asynchronous timeout
* return CaseTimeout(500) + ((repeat_count < 5) ? CaseRepeat : CaseNoRepeat);
* }
* @endcode
*
* In the future, more control information may be added transparently and backwards compatible.
*/
struct control_t
{
control_t() : repeat(REPEAT_NO_REPEAT), timeout(-1) {}
control_t(repeat_t repeat, uint32_t timeout_ms) :
repeat(repeat), timeout(timeout_ms) {}
control_t(repeat_t repeat) :
repeat(repeat), timeout(-1) {}
control_t(uint32_t timeout_ms) :
repeat(REPEAT_NO_REPEAT), timeout(timeout_ms) {}
control_t &
operator+(const control_t &rhs) {
if (repeat == 0 || repeat < rhs.repeat) repeat = rhs.repeat;
if (timeout == uint32_t(-1) || timeout > rhs.timeout) timeout = rhs.timeout;
return *this;
}
private:
repeat_t repeat;
uint32_t timeout;
friend class Harness;
};
/// Alias class for asynchronous timeout control in milliseconds
struct CaseTimeout : public control_t {
CaseTimeout(uint32_t ms) : control_t(ms) {}
};
/// repeats only the test case handler without calling teardown and setup handlers
const control_t CaseRepeatHandlerOnly = control_t(REPEAT_CASE_ONLY);
/// repeats the test case handler with calling teardown and setup handlers
const control_t CaseRepeat = control_t(REPEAT_ALL);
/// does not repeat this test case, but moves on to the next one
const control_t CaseNext = control_t(REPEAT_NO_REPEAT);
class Case; // forward declaration
/** Test setup handler.
*
* This handler is called before execution of any test case and
* allows you to initialize your common test environment.
*
* @param number_of_cases the total number of test cases in the test specification
*
* @returns
* You can return `STATUS_ABORT` if you initialization failed and the test teardown handler will
* then be called with the `FAILURE_SETUP`.
*/
typedef status_t (*test_setup_handler_t)(const size_t number_of_cases);
/** Test teardown handler.
*
* This handler is called after execution of all test case or if test execution is aborted.
* You can use this handler to de-initialize your test environment and output test statistics.
* The failure argument contains the immediate reason why this handler is called.
* If the test completed normally without failures, this will contain `FAILURE_NONE`.
*
* After execution of this handler, the test harness will stop execution.
*
* @param passed the number of cases without failures
* @param failed the number of cases with at least one failure
* @param failure the reason why this handler was called
*/
typedef void (*test_teardown_handler_t)(const size_t passed, const size_t failed, const failure_t failure);
/** Test case setup handler.
*
* This handler is called before execution of each test case and
* allows you to modify your environment before each test case.
*
* @param source the test case to be setup
* @param index_of_case the current index of the test case within the specification
*
* @returns
* You can return `STATUS_ABORT` to indicate that your setup failed, which will call the case
* failure handler with `FAILURE_SETUP` and then the case teardown handler with `FAILURE_SETUP`.
* This gives the teardown handler a chance to clean up a failed setup.
*/
typedef status_t (*case_setup_handler_t)(const Case *const source, const size_t index_of_case);
/** Primitive test case handler
*
* This handler is called only if the case setup succeeded and is followed by the test case teardown handler.
*
* @note This handler is executed only once.
*/
typedef void (*case_handler_t)(void);
/** Complex test case handler
*
* This handler is called only if the case setup succeeded and then may be repeated or
* awaiting a asynchronous callback, depending on the return modifiers.
*
* @returns
* A combination of control modifiers.
*/
typedef control_t (*case_control_handler_t)(void);
/** Test case handler (repeatable)
*
* This handler is called only if the case setup succeeded and then may be repeated or
* awaiting a asynchronous callback, depending on the return modifiers.
*
* @param repeat_count starting at `0`, contains the number of times this handler has been called
*
* @returns
* A combination of control modifiers.
*/
typedef control_t (*case_repeat_count_handler_t)(const size_t repeat_count);
/** Test case teardown handler.
*
* This handler is called after execution of each test case or all repeated test cases and
* allows you to reset your environment after each test case.
*
* @param source the test case to be torn down
* @param passed the number of cases without failures (can be >1 for repeated test cases)
* @param failed the number failures (can be larger than the number of (repeated) test cases)
* @param failure the reason why this handler was called
*
* @returns
* You can return `STATUS_ABORT` to indicate that your teardown failed, which will call the case
* failure handler with `FAILURE_TEARDOWN`.
*/
typedef status_t (*case_teardown_handler_t)(const Case *const source, const size_t passed, const size_t failed, const failure_t reason);
/** Test case failure handler.
*
* This handler is called whenever a failure occurred during the setup, execution or teardown.
*
* @param source the test case in which the failure occurred
* @param reason the reason why this handler was called
*
* @returns
* You can return `STATUS_ABORT` to indicate that this failure is non-recoverable, which will call the case
* teardown handler with reason. If a failure occurs during teardown, the teardown will not be called again.
*/
typedef status_t (*case_failure_handler_t)(const Case *const source, const failure_t reason);
} // namespace v0
} // namespace utest
#endif // MBED_TEST_ASYNC_TYPES_H

View File

@ -0,0 +1,27 @@
/****************************************************************************
* Copyright (c) 2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
****************************************************************************
*/
#ifndef MBED_TEST_ASYNC_UNITY_ASSERT_FAILURE_H
#define MBED_TEST_ASYNC_UNITY_ASSERT_FAILURE_H
#include <stdint.h>
/// this function is called from the unity module when an assertion failed.
void utest_unity_assert_failure();
#endif // MBED_ASYNC_TEST_UNITY_ASSERT_FAILURE_H

View File

@ -0,0 +1,27 @@
/****************************************************************************
* Copyright (c) 2015, ARM Limited, All Rights Reserved
* SPDX-License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
****************************************************************************
*/
#ifndef MBED_TEST_ASYNC_H
#define MBED_TEST_ASYNC_H
#include "types.h"
#include "case.h"
#include "default_handlers.h"
#include "harness.h"
#endif // MBED_TEST_ASYNC_H