AUnit  1.2
Unit testing framework for Arduino platforms inspired by ArduinoUnit and Google Test.
TestMacros.h
Go to the documentation of this file.
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 // Significant portions of the design and implementation of this file came from
26 // https://github.com/mmurdoch/arduinounit/blob/master/src/ArduinoUnit.h
27 
36 #ifndef AUNIT_TEST_MACROS_H
37 #define AUNIT_TEST_MACROS_H
38 
39 #include <stdint.h>
40 #include <Arduino.h> // F() macro
41 #include "Flash.h" // AUNIT_F() macro
42 #include "FCString.h"
43 #include "TestOnce.h"
44 #include "TestAgain.h"
45 
46 // On the ESP8266 platform, the F() string cannot be placed in an inline
47 // context, because it interferes with other PROGMEM strings. See
48 // https://github.com/esp8266/Arduino/issues/3369. The solution was to move the
49 // constructor definition out from an inline function into a normal function
50 // defined outside of the class declaration. Unfortunately, if the user code
51 // has any other usage of F() in an inline context, those interfere with the
52 // F() used below. I have abandoned supporting the F() macro for these test*()
53 // macros on the ESP8266.
54 
62 #define test(...) \
63  GET_TEST(__VA_ARGS__, TEST2, TEST1)(__VA_ARGS__)
64 
65 #define GET_TEST(_1, _2, NAME, ...) NAME
66 
67 #define TEST1(name) \
68 struct test_##name : aunit::TestOnce {\
69  test_##name();\
70  void once() override;\
71 } test_##name##_instance;\
72 test_##name :: test_##name() {\
73  init(AUNIT_F(#name)); \
74 }\
75 void test_##name :: once()
76 
77 #define TEST2(suiteName, name) \
78 struct suiteName##_##name : aunit::TestOnce {\
79  suiteName##_##name();\
80  void once() override;\
81 } suiteName##_##name##_instance;\
82 suiteName##_##name :: suiteName##_##name() {\
83  init(AUNIT_F(#suiteName "_" #name)); \
84 }\
85 void suiteName##_##name :: once()
86 
96 #define testing(...) \
97  GET_TESTING(__VA_ARGS__, TESTING2, TESTING1)(__VA_ARGS__)
98 
99 #define GET_TESTING(_1, _2, NAME, ...) NAME
100 
101 #define TESTING1(name) \
102 struct test_##name : aunit::TestAgain {\
103  test_##name();\
104  void again() override;\
105 } test_##name##_instance;\
106 test_##name :: test_##name() {\
107  init(AUNIT_F(#name));\
108 }\
109 void test_##name :: again()
110 
111 #define TESTING2(suiteName, name) \
112 struct suiteName##_##name : aunit::TestAgain {\
113  suiteName##_##name();\
114  void again() override;\
115 } suiteName##_##name##_instance;\
116 suiteName##_##name :: suiteName##_##name() {\
117  init(AUNIT_F(#suiteName "_" #name));\
118 }\
119 void suiteName##_##name :: again()
120 
130 #define externTest(...) \
131  GET_EXTERN_TEST(__VA_ARGS__, EXTERN_TEST2, EXTERN_TEST1)(__VA_ARGS__)
132 
133 #define GET_EXTERN_TEST(_1, _2, NAME, ...) NAME
134 
135 #define EXTERN_TEST1(name) \
136 struct test_##name : aunit::TestOnce {\
137  test_##name();\
138  void once();\
139 };\
140 extern test_##name test_##name##_instance
141 
142 #define EXTERN_TEST2(suiteName, name) \
143 struct suiteName##_##name : aunit::TestOnce {\
144  suiteName##_##name();\
145  void once();\
146 };\
147 extern suiteName##_##name suiteName##_##name##_instance
148 
158 #define externTesting(...) \
159  GET_EXTERN_TESTING(__VA_ARGS__, EXTERN_TESTING2, EXTERN_TESTING1)(__VA_ARGS__)
160 
161 #define GET_EXTERN_TESTING(_1, _2, NAME, ...) NAME
162 
163 #define EXTERN_TESTING1(name) \
164 struct test_ ## name : aunit::TestAgain {\
165  test_ ## name();\
166  void again();\
167 };\
168 extern test_##name test_##name##_instance
169 
170 #define EXTERN_TESTING2(suiteName, name) \
171 struct suiteName##_ ## name : aunit::TestAgain {\
172  suiteName##_ ## name();\
173  void again();\
174 };\
175 extern suiteName##_##name suiteName##_##name##_instance
176 
182 #define testF(testClass, name) \
183 struct testClass ## _ ## name : testClass {\
184  testClass ## _ ## name();\
185  void once() override;\
186 } testClass ## _ ## name ## _instance;\
187 testClass ## _ ## name :: testClass ## _ ## name() {\
188  init(AUNIT_F(#testClass "_" #name));\
189 }\
190 void testClass ## _ ## name :: once()
191 
200 #define testingF(testClass, name) \
201 struct testClass ## _ ## name : testClass {\
202  testClass ## _ ## name();\
203  void again() override;\
204 } testClass ## _ ## name ## _instance;\
205 testClass ## _ ## name :: testClass ## _ ## name() {\
206  init(AUNIT_F(#testClass "_" #name));\
207 }\
208 void testClass ## _ ## name :: again()
209 
215 #define externTestF(testClass, name) \
216 struct testClass ## _ ## name : testClass {\
217  testClass ## _ ## name();\
218  void once() override;\
219 };\
220 extern testClass ## _ ## name testClass##_##name##_instance
221 
228 #define externTestingF(testClass, name) \
229 struct testClass ## _ ## name : testClass {\
230  testClass ## _ ## name();\
231  void again() override;\
232 };\
233 extern testClass ## _ ## name testClass##_##name##_instance
234 
235 #endif
Various macros to smooth over the differences among the various platforms with regards to their suppo...