libGimbal 0.1.0
C17-Based Extended Standard Library and Cross-Language Runtime Framework
Loading...
Searching...
No Matches
gimbal_test_suite.h
Go to the documentation of this file.
1/*! \file
2 * \brief GblTestSuite structure and related functions
3 * \ingroup testing
4 *
5 * \author 2023, 2024 Falco Girgis
6 * \copyright MIT License
7 */
8
9#ifndef GIMBAL_TEST_SUITE_H
10#define GIMBAL_TEST_SUITE_H
11
12#include "../meta/instances/gimbal_object.h"
13#include "../meta/signals/gimbal_signal.h"
14
15/*! \name Type System
16 * \brief Type UUID and cast operators
17 * @{
18 */
19#define GBL_TEST_SUITE_TYPE (GBL_TYPEID(GblTestSuite)) //!< GblType UUID
20#define GBL_TEST_SUITE(self) (GBL_CAST(GblTestSuite, self)) //!< GblInstance cast operator
21#define GBL_TEST_SUITE_CLASS(klass) (GBL_CLASS_CAST(GblTestSuite, klass)) //!< GblClass cast operator
22#define GBL_TEST_SUITE_GET_CLASS(self) (GBL_CLASSOF(GblTestSuite, self)) //!< GblInstance to GblClass operator
23//! @}
24
25//! Convenience macro for NULL-terminating the test case list
26#define GBL_TEST_CASE_LAST { GBL_NULL, GBL_NULL }
27
28#define GBL_SELF_TYPE GblTestSuite
29
31
33GBL_FORWARD_DECLARE_STRUCT(GblTestSuite);
34GBL_FORWARD_DECLARE_STRUCT(GblTestScenario);
35
36/*! Function signature for a single test case
37 *
38 * \param pSelf The owning GblTestSuite object pointer.
39 * \param pCtx The GblContext object for the test suite.
40 *
41 * \returns The result of the unit test case.
42 */
43typedef GBL_RESULT (*GblTestCaseFn)(GBL_SELF, GblContext* pCtx);
44
45//! Represenst a single test case within a GblTestSuite
46typedef struct GblTestCase {
47 const char* pName; //!< Name of the test case
48 GblTestCaseFn pFnRun; //!< Function callback for the test case
49} GblTestCase;
50
51/*! Virtual table structure for a GblTestSuiteClass
52 *
53 * External virtual table for providing custom initializers and
54 * finalizers along with a default list of test-cases to add to
55 * a GblTestSuiteClass without having to create a unique class for
56 * every semi-custom test suite behavior.
57 *
58 * \sa GblTestSuiteClass
59 */
60typedef struct GblTestSuiteVTable {
61 //! Called to set up the test suite before any test cases are run.
62 GBL_RESULT (*pFnSuiteInit) (GBL_SELF, GblContext* pCtx);
63 //! Called to finalize/clean-up the test suite after all test cases are run.
64 GBL_RESULT (*pFnSuiteFinal)(GBL_SELF, GblContext* pCtx);
65 //! Called before every individual test-case to do generic setup.
66 GBL_RESULT (*pFnCaseInit) (GBL_SELF, GblContext* pCtx);
67 //! Called after every individual test-case to do generic cleanup.
68 GBL_RESULT (*pFnCaseFinal) (GBL_SELF, GblContext* pCtx);
69 //! Pointer to a NULL-terminated array of test cases to add to the suite.
70 const GblTestCase* pCases;
71} GblTestSuiteVTable;
72
73/*! \struct GblTestSuiteClass
74 * \extends GblObjectClass
75 * \brief GblClass structure for GblTestSuite
76 *
77 * Provides virtual methods for customizing/reimplementing
78 * a test suite's per-case logic.
79 *
80 * \sa GblTestSuiteVTable, GblTestSuite
81 */
82GBL_CLASS_DERIVE(GblTestSuite, GblObject)
83 //! Pointer to external virtual method table + default test case array.
84 const GblTestSuiteVTable* pVTable;
85 //! Virtual method for retrieving the test suite's name.
86 GBL_RESULT (*pFnSuiteName)(GBL_CSELF, const char** ppName);
87 //! Virtual method for retrieving the number of test cases within the suite.
88 GBL_RESULT (*pFnCaseCount)(GBL_CSELF, size_t *pSize);
89 //! Virtual method for retrieving the name of a test case by index.
90 GBL_RESULT (*pFnCaseName) (GBL_CSELF, size_t index, const char** ppName);
91 //! Virtual methodf or executing the test case at a particular index.
92 GBL_RESULT (*pFnCaseRun) (GBL_SELF, GblContext* pCtx, size_t index);
94
95/*! \struct GblTestSuite
96 * \extends GblObject
97 * \brief GblObject representing a collection of unit test cases
98 * \ingroup testing
99 *
100 * GblTestSuite is a GblInstance type which encapsulates a single
101 * test suite--a collection of sequentially executing, related
102 * test cases.
103 *
104 * \sa GblTestSuiteClass
105 */
107 GblCallRecord lastRecord; //!< Call record of the failing test case
108 size_t failingCase; //!< Failing test case index
109 size_t casesRun; //!< Number of test cases which have run
110 size_t casesPassed; //!< Number of test cases which have passed
111 size_t casesFailed; //!< Number of test cases which have failed
112 size_t casesSkipped; //!< Number of test cases which have been skipped
114
115//! \cond
116GBL_PROPERTIES(GblTestSuite,
117 (runResult, GBL_GENERIC, (READ, SAVE), GBL_UINT32_TYPE),
118 (lastMessage, GBL_GENERIC, (READ, SAVE), GBL_STRING_TYPE),
119 (failureCase, GBL_GENERIC, (READ, SAVE), GBL_UINT32_TYPE),
120 (caseCount, GBL_GENERIC, (READ, SAVE), GBL_UINT32_TYPE),
121 (casesRun, GBL_GENERIC, (READ, SAVE), GBL_UINT32_TYPE),
122 (casesPassed, GBL_GENERIC, (READ, SAVE), GBL_UINT32_TYPE),
123 (casesFailed, GBL_GENERIC, (READ, SAVE), GBL_UINT32_TYPE),
124 (casesSkipped, GBL_GENERIC, (READ, SAVE), GBL_UINT32_TYPE)
125)
126//! \endcond
127
128//! Returns the GblType UUID associated with GblTestSuite
130//! Registers a new GblType deriving from GBL_TEST_SUITE_TYPE
132 const GblTestSuiteVTable* pVTable,
133 size_t instanceSize,
134 size_t instancePrivateSize,
135 GblFlags typeFlags) GBL_NOEXCEPT;
136
137/** \name Test Suite Creation
138 * \brief Methods for managing test suite lifetimes.
139 * @{
140 */
141//! Creates a new GblTestSuite instance with the given properties (or defaults if not specified).
143 const char* pName /*=NULL*/,
144 const GblTestSuiteVTable* pVTable/*=NULL*/,
145 size_t size /*=0*/,
146 GblTestSuiteClass* pClass /*=NULL*/) GBL_NOEXCEPT;
147//! Returns a new reference to the GblTestSuite, incrementing its reference count by 1.
149//! Decrements the refcount of the given GblTestSuite by 1, destroying it if it reaches 0.
151//! @}
152
153/** \name Test Suite Properties
154 * \brief Methods providing accessors for test suite properties.
155 * @{
156 */
157//! Returns the string name of the test suite.
159//! Returns GBL_TRUE if the test suite has been run and GBL_FALSE otherwise.
161//! Returns GBL_TRUE if the test suite passed and GBL_FALSE otherwise.
163//! Returns the parent test scenario object or NULL if there isn't one.
165//! Returns the top-level aggregate result for the overall test suite.
167//! @}
168
169/** \name Test Case Registration
170 * \brief Methods for adding test cases to the suite.
171 * @{
172 */
173//! Enqueues a single test case into the given test suite.
175 const char* pName,
176 GblTestCaseFn pFnTest) GBL_NOEXCEPT;
177//! Enqueues an array of cases, terminated by a final entry of all NULL values.
179 const GblTestCase* pCases) GBL_NOEXCEPT;
180//! @}
181
182/** \name Test Case Accessors
183 * \brief Methods for accessing test-case properties.
184 * @{
185 */
186//! Returns whether the test case with the given name has run.
188//! Returns whether the test case with the given name has passed.
190//! Returns the number of test cases within the given test suite.
192//! Returns the name of the test case at the given index within the test suite.
194//! Returns the index of the test case with the given name within the test suite.
196//! Returns the result of the test case at the given index within the test suite.
198//! @}
199
200/** \name Test Execution
201 * \brief Methods for executing the test suite.
202 * @{
203*/
204//! Calls the top-level initialization function for the entire suite.
206//! Calls the top-level finalization function for the entire suite.
208//! Calls the case-level initialization function for the next test case.
210//! Calls the case-level finalization function for the previous test case.
212//! Runs the test case at \p index within the given suite, returning its result code.
214 GblContext* pCtx,
215 size_t index) GBL_NOEXCEPT;
216//! Skips the test case at \p index within the given suite.
218 GblContext* pCtx,
219 size_t index) GBL_NOEXCEPT;
220//! @}
221
222//! \cond
223#define GblTestSuite_create(...)
224 GblTestSuite_createDefault_(__VA_ARGS__)
225#define GblTestSuite_createDefault_(...)
226 GblTestSuite_createDefault__(__VA_ARGS__, GBL_NULL, GBL_NULL, 0, GBL_NULL)
227#define GblTestSuite_createDefault__(type, name, vtable, size, klass, ...)
228 (GblTestSuite_create)(type, name, vtable, size, klass)
229//! \endcond
230
232
233#undef GBL_SELF_TYPE
234
235#endif // GIMBAL_TEST_SUITE_H
#define GBL_CLASS_CAST(cType, klass)
#define GBL_NULL
#define GBL_NOEXCEPT
#define GBL_DECLS_BEGIN
#define GBL_FORWARD_DECLARE_STRUCT(S)
#define GBL_TYPEID(instanceStruct)
#define GBL_INSTANCE_DERIVE(derivedInstance, baseInstance)
#define GBL_CLASS_DERIVE(...)
#define GBL_INSTANCE_END
#define GBL_EXPORT
#define GBL_CLASS_END
#define GBL_CLASSOF(cType, self)
#define GBL_CAST(cType, self)
#define GBL_STRING_TYPE
Builtin ID for string GblVariant type.
#define GBL_UINT32_TYPE
Builtin ID for uint32_t GblVariant type.
#define GBL_PROPERTIES(object,...)
Declares a list of properties for the given object/instance structure.
GBL_RESULT GblTestSuite_initCase(GblTestSuite *pSelf, GblContext *pCtx)
Calls the case-level initialization function for the next test case.
GBL_RESULT GblTestSuite_finalCase(GblTestSuite *pSelf, GblContext *pCtx)
Calls the case-level finalization function for the previous test case.
GblBool GblTestSuite_ran(const GblTestSuite *pSelf)
Returns GBL_TRUE if the test suite has been run and GBL_FALSE otherwise.
const char * GblTestSuite_name(const GblTestSuite *pSelf)
Returns the string name of the test suite.
GblTestSuite * GblTestSuite_create(GblType type, const char *pName, const GblTestSuiteVTable *pVTable, size_t size, GblTestSuiteClass *pClass)
Creates a new GblTestSuite instance with the given properties (or defaults if not specified).
GBL_RESULT GblTestSuite_addCases(GblTestSuite *pSelf, const GblTestCase *pCases)
Enqueues an array of cases, terminated by a final entry of all NULL values.
GblBool GblTestSuite_passed(const GblTestSuite *pSelf)
Returns GBL_TRUE if the test suite passed and GBL_FALSE otherwise.
const char * GblTestSuite_caseName(const GblTestSuite *pSelf, size_t index)
Returns the name of the test case at the given index within the test suite.
GBL_RESULT GblTestSuite_result(const GblTestSuite *pSelf)
Returns the top-level aggregate result for the overall test suite.
GblBool GblTestSuite_caseRan(const GblTestSuite *pSelf, const char *pCaseName)
Returns whether the test case with the given name has run.
GBL_RESULT GblTestSuite_addCase(GblTestSuite *pSelf, const char *pName, GblTestCaseFn pFnTest)
Enqueues a single test case into the given test suite.
GblTestScenario * GblTestSuite_scenario(const GblTestSuite *pSelf)
Returns the parent test scenario object or NULL if there isn't one.
GBL_RESULT GblTestSuite_caseResult(const GblTestSuite *pSelf, size_t index)
Returns the result of the test case at the given index within the test suite.
GblRefCount GblTestSuite_unref(GblTestSuite *pSelf)
Decrements the refcount of the given GblTestSuite by 1, destroying it if it reaches 0.
GblType GblTestSuite_type(void)
Returns the GblType UUID associated with GblTestSuite.
GBL_RESULT GblTestSuite_initSuite(GblTestSuite *pSelf, GblContext *pCtx)
Calls the top-level initialization function for the entire suite.
size_t GblTestSuite_caseCount(const GblTestSuite *pSelf)
Returns the number of test cases within the given test suite.
GBL_RESULT GblTestSuite_finalSuite(GblTestSuite *pSelf, GblContext *pCtx)
Calls the top-level finalization function for the entire suite.
size_t GblTestSuite_caseIndex(const GblTestSuite *pSelf, const char *pCaseName)
Returns the index of the test case with the given name within the test suite.
GBL_RESULT GblTestSuite_skipCase(GblTestSuite *pSelf, GblContext *pCtx, size_t index)
Skips the test case at index within the given suite.
GblType GblTestSuite_register(const char *pName, const GblTestSuiteVTable *pVTable, size_t instanceSize, size_t instancePrivateSize, GblFlags typeFlags)
Registers a new GblType deriving from GBL_TEST_SUITE_TYPE.
GBL_RESULT GblTestSuite_runCase(GblTestSuite *pSelf, GblContext *pCtx, size_t index)
Runs the test case at index within the given suite, returning its result code.
GblBool GblTestSuite_casePassed(const GblTestSuite *pSelf, const char *pCaseName)
Returns whether the test case with the given name has passed.
GblTestSuite * GblTestSuite_ref(GblTestSuite *pSelf)
Returns a new reference to the GblTestSuite, incrementing its reference count by 1.
uint32_t GblFlags
Standard-sized flags type, 32-bits across platforms.
uint8_t GblBool
Basic boolean type, standardized to sizeof(char)
uint16_t GblRefCount
Type able to hold a reference counter across the codebase.
uintptr_t GblType
Meta Type UUID.
Definition gimbal_type.h:51
Captures a result, its stringified message, and a source context.
Represenst a single test case within a GblTestSuite.
const char * pName
Name of the test case.
GblTestCaseFn pFnRun
Function callback for the test case.
const GblTestSuiteVTable * pVTable
Pointer to external virtual method table + default test case array.
GblObject representing a collection of unit test cases.
GblCallRecord lastRecord
Call record of the failing test case.
size_t casesRun
Number of test cases which have run.
size_t casesFailed
Number of test cases which have failed.
size_t failingCase
Failing test case index.
size_t casesSkipped
Number of test cases which have been skipped.
size_t casesPassed
Number of test cases which have passed.
Virtual table structure for a GblTestSuiteClass.
const GblTestCase * pCases
Pointer to a NULL-terminated array of test cases to add to the suite.