2 * Copyright 2020 Project nlunit-test Authors. All Rights Reserved.
3 * Copyright 2012-2017 Nest Labs Inc. All Rights Reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * This file defines macros, constants, data structures, and
21 * functions that effect a simple, portable unit test suite
26 #ifndef __NLUNIT_TEST_H_INCLUDED__
27 #define __NLUNIT_TEST_H_INCLUDED__
36 #endif /* __cplusplus */
39 * @addtogroup typedef Type Definitions
48 * This defines a function entry point for a test in a test suite.
50 * @param[inout] inSuite A pointer to the test suite being run.
51 * @param[inout] inContext A pointer to test suite-specific context
52 * provided by the test suite driver.
55 typedef void (*nlTestFunction)(struct _nlTestSuite* inSuite, void* inContext);
58 * This structure is used to define a single test for use in a test suite.
61 typedef struct _nlTest
63 const char* name; /**< Brief descriptive name of the test */
64 nlTestFunction function; /**< Function entry point for the test */
68 * This structure is used to define the test suite, an array of tests
71 * It has a name for the suite, the array of tests, as well as a setup function which
72 * is called before the execution of the test suite and a teardown function which is
73 * called after all tests in the suite are complete. It also contains pointers to an
74 * initialize function which is called before each individual test, and a terminate
75 * function which is called after each individual test.
78 typedef struct _nlTestSuite
81 const char* name; /**< Brief descriptive name of the test suite */
82 const nlTest* tests; /**< Array of tests in the suite */
85 * This function is responsible for, if non-NULL, performing
86 * initial setup for the entire test suite, before running any
89 * @param[inout] inContext A pointer to test suite-specific context
90 * provided by the test suite driver.
93 int (*setup)(void *inContext);
96 * This function is responsible for, if non-NULL, performing
97 * tear down for the entire test suite, after every test in the suite.
99 * @param[inout] inContext A pointer to test suite-specific context
100 * provided by the test suite driver.
103 int (*tear_down)(void *inContext);
106 * This function is responsible for, if non-NULL, performing
107 * initialization for the test, before running the test.
109 * @param[inout] inContext A pointer to test suite-specific context
110 * provided by the test suite driver.
113 int (*initialize)(void *inContext);
116 * This function is responsible for, if non-NULL, performing
117 * final tear down for the test, after running the test.
119 * @param[inout] inContext A pointer to test suite-specific context
120 * provided by the test suite driver.
123 int (*terminate)(void *inContext);
125 int runTests; /**< Total number of tests performed in the suite */
126 int failedTests; /**< Total number of tests failed in the suite */
127 int performedAssertions; /**< Total number of test assertions performed in the suite */
128 int failedAssertions; /**< Total number of test assertions failed in the suite */
130 /* Private Members */
135 * Output style for tests and test summaries.
137 typedef enum _nlTestOutputStyle
139 OUTPUT_DEF = 0, /**< Generate human-readable output (default). */
140 OUTPUT_CSV = 1, /**< Generate machine-readable, comma-separated value (CSV) output. */
144 * This structure contains function pointers for functions that output
145 * the test suite name as well as the status of:
147 * * Test suite setup and teardown functions.
148 * * Tests and result functions (failed tests, failed assertions).
150 * All functions take a pointer to the test suite along with
151 * additional parameters, as needed, for the particular function.
153 * Custom instances of this structure may be instantiated and
154 * populated with custom output functions and then globally set via
158 typedef struct _nlTestOutputLogger {
160 * This function is responsible for rendering the name of the test
163 * @param[in] inSuite A pointer to the test suite for which
164 * the name should be rendered.
167 void (*PrintName)(struct _nlTestSuite*inSuite);
170 * This function is responsible for rendering the status of the
171 * individual test initialization.
173 * @param[in] inSuite A pointer to the test suite for which
174 * the test suite setup status should be
176 * @param[in] inResult The status of the test suite setup to
178 * @param[in] inWidth The maximum width, in characters,
179 * allowed for rendering the setup name
183 void (*PrintInitialize)(struct _nlTestSuite * inSuite, int inResult, int inWidth);
186 * This function is responsible for rendering the status of the
187 * individual test termination
189 * @param[in] inSuite A pointer to the test suite for which
190 * the test suite setup status should be
192 * @param[in] inResult The status of the test suite setup to
194 * @param[in] inWidth The maximum width, in characters,
195 * allowed for rendering the setup name
199 void (*PrintTerminate)(struct _nlTestSuite * inSuite, int inResult, int inWidth);
202 * This function is responsible for rendering the status of the test
205 * @param[in] inSuite A pointer to the test suite for which
206 * the test suite setup status should be
208 * @param[in] inResult The status of the test suite setup to
210 * @param[in] inWidth The maximum width, in characters,
211 * allowed for rendering the setup name
215 void (*PrintSetup)(struct _nlTestSuite*inSuite, int inResult, int inWidth);
218 * This function is responsible for rendering the summary of a test
219 * run, indicating success or failure of that test.
221 * @param[in] inSuite A pointer to the test suite for which
222 * the test run status summary should be
224 * @param[in] inWidth The maximum width, in characters,
225 * allowed for rendering the test name.
226 * @param[in] inIndex The index of the test in the suite
227 * for which to render the summary of the
231 void (*PrintTest)(struct _nlTestSuite*inSuite, int inWidth, int inIndex);
234 * This function is responsible for rendering the status of the test
237 * @param[in] inSuite A pointer to the test suite for which
238 * the test suite teardown status should be
240 * @param[in] inResult The status of the test suite setup to
242 * @param[in] inWidth The maximum width, in characters,
243 * allowed for rendering the setup name
247 void (*PrintTeardown)(struct _nlTestSuite*inSuite, int inResult, int inWidth);
250 * This function is responsible for rendering the test suite run
251 * statistics, including the number of failed tests and the total
252 * number of tests run.
254 * @param[in] inSuite A pointer to the test suite for which
255 * the test suite test statistics should be
259 void (*PrintStatTests)(struct _nlTestSuite*inSuite);
262 * This function is responsible for rendering the test suite assertion
263 * statistics, including the number of failed assertions and the total
264 * number of assertions evaluated.
266 * @param[in] inSuite A pointer to the test suite for which
267 * the test suite assertion statistics should
271 void (*PrintStatAsserts)(struct _nlTestSuite*inSuite);
272 } nlTestOutputLogger;
280 * @addtogroup cpp Preprocessor Definitions and Macros
286 * @def kTestSuiteMaxTests
289 * Defines the maximum number of tests allowed in a single test suite.
292 #define kTestSuiteMaxTests (64)
298 * Defines successful return status from test suite functions
307 * Defines failed return status from test suite functions
313 #define _NL_TEST_ASSIGN(field, value) value
315 #define _NL_TEST_ASSIGN(field, value) .field = value
316 #endif /* __cplusplus */
319 * @def NL_TEST_DEF(inName, inFunction)
322 * This macro makes an test assignment in a test suite, associating
323 * the specified function @a inFunction with the provided string @a
326 * @param[in] inName A pointer to a NULL-terminated C string
327 * briefly describing the test.
328 * @param[in] inFunction A pointer to the function entry point for
332 #define NL_TEST_DEF(inName, inFunction) \
334 _NL_TEST_ASSIGN(name, inName), \
335 _NL_TEST_ASSIGN(function, inFunction) \
339 * @def NL_TEST_SENTINEL()
342 * This macro must be used as the final entry to terminate an array
343 * of test assignments to a test suite.
346 #define NL_TEST_SENTINEL() NL_TEST_DEF(NULL, NULL)
349 * @def NL_TEST_ASSERT(inSuite, inCondition)
352 * This is used to assert the results of a conditional check
353 * through out a test in a test suite.
355 * @param[in] inSuite A pointer to the test suite the assertion
356 * should be accounted against.
357 * @param[in] inCondition Code for the logical predicate to be checked
358 * for truth. If the condition fails, the
362 #define NL_TEST_ASSERT(inSuite, inCondition) \
364 (inSuite)->performedAssertions += 1; \
366 if (!(inCondition)) \
368 printf("%s:%u: assertion failed: \"%s\"\n", \
369 __FILE__, __LINE__, #inCondition); \
370 (inSuite)->failedAssertions += 1; \
371 (inSuite)->flagError = true; \
375 #define NL_TEST_ASSERT_LOOP(inSuite, iteration, inCondition) \
377 (inSuite)->performedAssertions += 1; \
378 if ( !(inCondition) ) \
380 printf("%s:%u: loop iter %d assertion failed: \"%s\"\n", \
381 __FILE__, __LINE__, iteration, #inCondition); \
382 (inSuite)->failedAssertions += 1; \
383 (inSuite)->flagError = true; \
393 * This runs all the functions for each test specified in the current
394 * suite and outputs the results for each function using the
395 * currently-set logger methods.
397 * @param[inout] inSuite A pointer to the test suite being run.
398 * @param[inout] inContext A pointer to test suite-specific context
399 * that will be provided to each test invoked
403 extern void nlTestRunner(struct _nlTestSuite* inSuite, void* inContext);
406 * This summarizes the number of run and failed tests as well as the
407 * number of performed and failed assertions for the suite using the
408 * currently-set logger methods.
410 * @param[inout] inSuite A pointer to the test suite being run.
412 * @returns SUCCESS on success; otherwise, FAILURE.
414 extern int nlTestRunnerStats(struct _nlTestSuite* inSuite);
417 * This globally sets the output style to be used for summarizing a
420 * @note This supports selecting among built-in logger methods. Custom
421 * logger methods may be set through the nlTestSetLogger() interface.
423 * @param[in] inStyle The style to be used for summarizing a
427 extern void nlTestSetOutputStyle(nlTestOutputStyle inStyle);
430 * This globally sets the logger methods to be used for summarizing a
433 * @param[in] inLogger A pointer to the logger methods to be used
434 * for summarizing a suite test run.
437 extern void nlTestSetLogger(const nlTestOutputLogger* inLogger);
440 * @addtogroup compat Compatibility Types and Interfaces
442 * Deprecated legacy types and interfaces. New usage of these types
443 * and interfaces is discouraged.
449 * Legacy type for output style for tests and test summaries.
452 typedef nlTestOutputStyle nl_test_outputStyle;
455 * Legacy type for output functions.
458 typedef nlTestOutputLogger nl_test_output_logger_t;
461 * @def nl_test_set_output_style(inStyle)
463 * @note See nlTestSetOutputStyle() for the equivalent non-deprecated
467 * This globally sets the output style to be used for summarizing a
470 * @param[in] inStyle The style to be used for summarizing a
474 #define nl_test_set_output_style(inStyle) nlTestSetOutputStyle(inStyle)
477 * @def nl_test_set_logger(inLogger)
479 * @note See nlTestSetLogger() for the equivalent non-deprecated
483 * This globally sets the output style to be used for summarizing a
486 * @param[in] inLogger A pointer to the logger methods to be used
487 * for summarizing a suite test run.
490 #define nl_test_set_logger(inLogger) nlTestSetLogger(inLogger)
499 #endif /* __cplusplus */
501 #endif /* __NLUNIT_TEST_H_INCLUDED__ */