Mistake on this page?
Report an issue in GitHub or email us
utest_specification.h
1 /****************************************************************************
2  * Copyright (c) 2015, ARM Limited, All Rights Reserved
3  * SPDX-License-Identifier: Apache-2.0
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License"); you may
6  * not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13  * 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.
16  ****************************************************************************
17  */
18 
19 #ifndef UTEST_SPECIFICATION_H
20 #define UTEST_SPECIFICATION_H
21 
22 #include <stdint.h>
23 #include <stdbool.h>
24 #include <stdio.h>
25 #include "utest/utest_types.h"
26 #include "utest/utest_case.h"
27 #include "utest/utest_default_handlers.h"
28 
29 
30 namespace utest {
31 /** \addtogroup frameworks */
32 /** @{*/
33 namespace v1 {
34 
35  /** Test specification containing the setup and teardown handlers and test cases.
36  *
37  * This class simply holds the test cases and allows you to specify default handlers, and
38  * override setup and teardown handlers.
39  * The order of arguments is:
40  * - test setup handler (optional)
41  * - array of test cases (required)
42  * - test teardown handler (optional)
43  * - default handlers (optional)
44  *
45  * @note You cannot set the size of the test case array dynamically, it is template deducted at compile
46  * time. Creating test specifications for unittests at runtime is explicitly not supported.
47  */
49  {
50  public:
51  template< size_t N, typename CaseType >
52  Specification(const CaseType (&cases)[N],
53  const handlers_t defaults = default_handlers) :
54  setup_handler(default_handler), teardown_handler(default_handler), failure_handler(default_handler),
55  cases(static_cast<const Case*>(static_cast<const CaseType*>(cases))), length(N),
56  defaults(defaults)
57  {
59  sizeof(CaseType) == sizeof(Case),
60  "CaseType and Case should have the same size"
61  );
62  }
63 
64  template< size_t N, typename CaseType >
65  Specification(const CaseType (&cases)[N],
66  const test_failure_handler_t failure_handler,
67  const handlers_t defaults = default_handlers) :
68  setup_handler(default_handler), teardown_handler(default_handler), failure_handler(failure_handler),
69  cases(static_cast<const Case*>(static_cast<const CaseType*>(cases))), length(N),
70  defaults(defaults)
71  {
73  sizeof(CaseType) == sizeof(Case),
74  "CaseType and Case should have the same size"
75  );
76  }
77 
78  template< size_t N, typename CaseType >
79  Specification(const CaseType (&cases)[N],
80  const test_teardown_handler_t teardown_handler,
81  const handlers_t defaults = default_handlers) :
82  setup_handler(default_handler), teardown_handler(teardown_handler), failure_handler(default_handler),
83  cases(static_cast<const Case*>(static_cast<const CaseType*>(cases))), length(N),
84  defaults(defaults)
85  {
87  sizeof(CaseType) == sizeof(Case),
88  "CaseType and Case should have the same size"
89  );
90  }
91 
92  template< size_t N, typename CaseType >
93  Specification(const CaseType (&cases)[N],
94  const test_teardown_handler_t teardown_handler,
95  const test_failure_handler_t failure_handler,
96  const handlers_t defaults = default_handlers) :
97  setup_handler(default_handler), teardown_handler(teardown_handler), failure_handler(failure_handler),
98  cases(static_cast<const Case*>(static_cast<const CaseType*>(cases))), length(N),
99  defaults(defaults)
100  {
102  sizeof(CaseType) == sizeof(Case),
103  "CaseType and Case should have the same size"
104  );
105  }
106 
107  template< size_t N, typename CaseType >
108  Specification(const test_setup_handler_t setup_handler,
109  const CaseType (&cases)[N],
110  const handlers_t defaults = default_handlers) :
111  setup_handler(setup_handler), teardown_handler(default_handler), failure_handler(default_handler),
112  cases(static_cast<const Case*>(static_cast<const CaseType*>(cases))), length(N),
113  defaults(defaults)
114  {}
115 
116  template< size_t N, typename CaseType >
117  Specification(const test_setup_handler_t setup_handler,
118  const CaseType (&cases)[N],
119  const test_failure_handler_t failure_handler,
120  const handlers_t defaults = default_handlers) :
121  setup_handler(setup_handler), teardown_handler(default_handler), failure_handler(failure_handler),
122  cases(static_cast<const Case*>(static_cast<const CaseType*>(cases))), length(N),
123  defaults(defaults)
124  {
126  sizeof(CaseType) == sizeof(Case),
127  "CaseType and Case should have the same size"
128  );
129  }
130 
131  template< size_t N, typename CaseType >
132  Specification(const test_setup_handler_t setup_handler,
133  const CaseType (&cases)[N],
134  const test_teardown_handler_t teardown_handler,
135  const handlers_t defaults = default_handlers) :
136  setup_handler(setup_handler), teardown_handler(teardown_handler), failure_handler(default_handler),
137  cases(static_cast<const Case*>(static_cast<const CaseType*>(cases))), length(N),
138  defaults(defaults)
139  {
141  sizeof(CaseType) == sizeof(Case),
142  "CaseType and Case should have the same size"
143  );
144  }
145 
146  template< size_t N, typename CaseType >
147  Specification(const test_setup_handler_t setup_handler,
148  const CaseType (&cases)[N],
149  const test_teardown_handler_t teardown_handler,
150  const test_failure_handler_t failure_handler,
151  const handlers_t defaults = default_handlers) :
152  setup_handler(setup_handler), teardown_handler(teardown_handler), failure_handler(failure_handler),
153  cases(static_cast<const Case*>(static_cast<const CaseType*>(cases))), length(N),
154  defaults(defaults)
155  {
157  sizeof(CaseType) == sizeof(Case),
158  "CaseType and Case should have the same size"
159  );
160  }
161 
162  Specification(const test_setup_handler_t setup_handler,
163  const Case *cases,
164  const size_t length,
165  const test_teardown_handler_t teardown_handler,
166  const test_failure_handler_t failure_handler,
167  const handlers_t defaults = default_handlers) :
168  setup_handler(setup_handler), teardown_handler(teardown_handler), failure_handler(failure_handler),
169  cases(cases), length(length),
170  defaults(defaults)
171  {
172  }
173 
174  private:
175  const test_setup_handler_t setup_handler;
176  const test_teardown_handler_t teardown_handler;
177  const test_failure_handler_t failure_handler;
178  const Case *const cases;
179  const size_t length;
180  const handlers_t defaults;
181 
182  friend class Harness;
183  };
184 
185 } // namespace v1
186 } // namespace utest
187 
188  #endif // UTEST_SPECIFICATION_H
189 
190 /** @}*/
Test case wrapper class.
Definition: utest_case.h:108
A table of handlers.
Test Harness.
Definition: utest_harness.h:46
utest::v1::status_t(* test_setup_handler_t)(const size_t number_of_cases)
Test setup handler.
Definition: utest_types.h:287
void(* test_teardown_handler_t)(const size_t passed, const size_t failed, const failure_t failure)
Test teardown handler.
Definition: utest_types.h:302
const handlers_t & default_handlers
The greentea aborting handlers are the default.
static const struct utest::v1::@25 default_handler
Default handler hint.
void(* test_failure_handler_t)(const failure_t reason)
Test failure handler.
Definition: utest_types.h:311
Test specification containing the setup and teardown handlers and test cases.
#define MBED_STATIC_ASSERT(expr, msg)
MBED_STATIC_ASSERT Declare compile-time assertions, results in compile-time error if condition is fal...
Definition: mbed_assert.h:110
Important Information for this Arm website

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies. If you are not happy with the use of these cookies, please review our Cookie Policy to learn how they can be disabled. By disabling cookies, some features of the site will not work.