Skip to content

Commit f94d8ec

Browse files
committed
Refactor tests
1 parent 4dea925 commit f94d8ec

File tree

1 file changed

+84
-62
lines changed

1 file changed

+84
-62
lines changed

Diff for: tests/periman/periman.ino

+84-62
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
* - SDMMC: SDMMC requires a card to be mounted before the pins are attached
99
*/
1010

11-
#include <unity.h>
12-
1311
#if SOC_I2S_SUPPORTED
1412
#include "ESP_I2S.h"
1513
#endif
@@ -22,6 +20,13 @@
2220
#include "SPI.h"
2321
#endif
2422

23+
#define ETH_PHY_TYPE ETH_PHY_LAN8720
24+
#define ETH_PHY_ADDR 0
25+
#define ETH_PHY_MDC 23
26+
#define ETH_PHY_MDIO 18
27+
#define ETH_PHY_POWER -1
28+
#define ETH_CLK_MODE ETH_CLOCK_GPIO0_IN
29+
2530
#include "ETH.h"
2631

2732
/* Definitions */
@@ -32,9 +37,9 @@
3237
/* Global variables */
3338

3439
bool test_executed = false;
35-
String last_test = "";
36-
int8_t uart1_rx_pin = UART1_RX_DEFAULT;
37-
int8_t uart1_tx_pin = UART1_TX_DEFAULT;
40+
String current_test;
41+
int8_t uart1_rx_pin;
42+
int8_t uart1_tx_pin;
3843

3944
/* Callback functions */
4045

@@ -46,69 +51,81 @@ void onReceive_cb(void) {
4651
}
4752
}
4853

49-
/* Unity functions */
50-
51-
// This function is automatically called by unity before each test is run
52-
void setUp(void) {
54+
// This function is called by before each test is run
55+
void setup_test(String test_name, int8_t rx_pin = UART1_RX_DEFAULT, int8_t tx_pin = UART1_TX_DEFAULT) {
5356
log_v("Setting up next test");
57+
58+
current_test = test_name;
59+
uart1_rx_pin = rx_pin;
60+
uart1_tx_pin = tx_pin;
61+
test_executed = false;
62+
63+
pinMode(uart1_rx_pin, INPUT_PULLUP);
64+
pinMode(uart1_tx_pin, OUTPUT);
5465
Serial1.setPins(uart1_rx_pin, uart1_tx_pin);
5566
uart_internal_loopback(1, uart1_rx_pin);
5667
delay(100);
5768
log_v("Running test");
5869
}
5970

60-
// This function is automatically called by unity after each test is run
61-
void tearDown(void) {
71+
// This function is called after each test is run
72+
void teardown_test(void) {
6273
log_v("Tearing down last test");
6374
if (test_executed) {
64-
Serial1.print(last_test);
75+
pinMode(uart1_rx_pin, INPUT_PULLUP);
76+
pinMode(uart1_tx_pin, OUTPUT);
77+
Serial1.print(current_test);
6578
Serial1.println(" test: This should not be printed");
6679
Serial1.flush();
6780

6881
Serial1.setPins(uart1_rx_pin, uart1_tx_pin);
6982
uart_internal_loopback(1, uart1_rx_pin);
7083
delay(100);
71-
test_executed = false;
7284
}
7385

74-
Serial1.print(last_test);
86+
Serial1.print(current_test);
7587
Serial1.println(" test: This should be printed");
7688
Serial1.flush();
7789
}
7890

7991
/* Test functions */
80-
/* These functions must only init the peripheral on the same pins and update "last_test" */
92+
/* These functions must only init the peripheral on the same pins and update "current_test" */
8193

8294
void gpio_test(void) {
83-
last_test = "GPIO";
95+
setup_test("GPIO");
8496
test_executed = true;
8597
pinMode(uart1_rx_pin, INPUT);
8698
pinMode(uart1_tx_pin, OUTPUT);
99+
teardown_test();
87100
}
88101

89102
void sigmadelta_test(void) {
90-
last_test = "SigmaDelta";
103+
setup_test("SigmaDelta");
91104
#if SOC_SDM_SUPPORTED
92105
test_executed = true;
93106
if (!sigmaDeltaAttach(uart1_rx_pin, 312500)) {
94-
TEST_FAIL_MESSAGE("SigmaDelta init failed");
107+
Serial.println("SigmaDelta init failed");
95108
}
96109
if (!sigmaDeltaAttach(uart1_tx_pin, 312500)) {
97-
TEST_FAIL_MESSAGE("SigmaDelta init failed");
110+
Serial.println("SigmaDelta init failed");
98111
}
99112
#endif
113+
teardown_test();
100114
}
101115

102116
void adc_oneshot_test(void) {
103-
last_test = "ADC Oneshot";
104-
#if SOC_ADC_SUPPORTED
117+
#if !SOC_ADC_SUPPORTED
118+
setup_test("ADC Oneshot");
119+
#else
120+
setup_test("ADC Oneshot", A3, A4);
105121
test_executed = true;
106122
analogReadResolution(12);
107123
pinMode(A3, INPUT);
108124
pinMode(A4, INPUT);
109125
analogRead(A3);
110126
analogRead(A4);
111127
#endif
128+
teardown_test();
112129
}
113130

114131
#if SOC_ADC_SUPPORTED
@@ -119,8 +136,10 @@ void ARDUINO_ISR_ATTR adcComplete() {
119136
#endif
120137

121138
void adc_continuous_test(void) {
122-
last_test = "ADC Continuous";
123-
#if SOC_ADC_SUPPORTED
139+
#if !SOC_ADC_SUPPORTED
140+
setup_test("ADC Continuous");
141+
#else
142+
setup_test("ADC Continuous", A3, A4);
124143
test_executed = true;
125144
uint8_t adc_pins[] = {A3, A4};
126145
uint8_t adc_pins_count = 2;
@@ -137,93 +156,106 @@ void adc_continuous_test(void) {
137156
}
138157

139158
if (!analogContinuousRead(&result, 0)) {
140-
TEST_FAIL_MESSAGE("ADC continuous read failed");
159+
Serial.println("ADC continuous read failed");
141160
}
142161

143162
analogContinuousStop();
144163
#endif
164+
teardown_test();
145165
}
146166

147167
void dac_test(void) {
148-
last_test = "DAC";
149-
#if SOC_DAC_SUPPORTED
168+
#if !SOC_DAC_SUPPORTED
169+
setup_test("DAC");
170+
#else
171+
setup_test("DAC", DAC1, DAC2);
150172
test_executed = true;
151173
dacWrite(DAC1, 255);
152174
dacWrite(DAC2, 255);
153175
#endif
176+
teardown_test();
154177
}
155178

156179
void ledc_test(void) {
157-
last_test = "LEDC";
180+
setup_test("LEDC");
158181
#if SOC_LEDC_SUPPORTED
159182
test_executed = true;
160183
if (!ledcAttach(uart1_rx_pin, 5000, 12)) {
161-
TEST_FAIL_MESSAGE("LEDC init failed");
184+
Serial.println("LEDC init failed");
162185
}
163186
if (!ledcAttach(uart1_tx_pin, 5000, 12)) {
164-
TEST_FAIL_MESSAGE("LEDC init failed");
187+
Serial.println("LEDC init failed");
165188
}
166189
#endif
190+
teardown_test();
167191
}
168192

169193
void rmt_test(void) {
170-
last_test = "RMT";
194+
setup_test("RMT");
171195
#if SOC_RMT_SUPPORTED
172196
test_executed = true;
173197
if (!rmtInit(uart1_rx_pin, RMT_TX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) {
174-
TEST_FAIL_MESSAGE("RMT init failed");
198+
Serial.println("RMT init failed");
175199
}
176200
if (!rmtInit(uart1_tx_pin, RMT_RX_MODE, RMT_MEM_NUM_BLOCKS_1, 10000000)) {
177-
TEST_FAIL_MESSAGE("RMT init failed");
201+
Serial.println("RMT init failed");
178202
}
179203
#endif
204+
teardown_test();
180205
}
181206

182207
void i2s_test(void) {
183-
last_test = "I2S";
208+
setup_test("I2S");
184209
#if SOC_I2S_SUPPORTED
185210
test_executed = true;
186211
I2SClass i2s;
187212

188213
i2s.setPins(uart1_rx_pin, uart1_tx_pin, -1);
189214
i2s.setTimeout(1000);
190215
if (!i2s.begin(I2S_MODE_STD, 16000, I2S_DATA_BIT_WIDTH_16BIT, I2S_SLOT_MODE_STEREO)) {
191-
TEST_FAIL_MESSAGE("I2S init failed");
216+
Serial.println("I2S init failed");
192217
}
193218
#endif
219+
teardown_test();
194220
}
195221

196222
void i2c_test(void) {
197-
last_test = "I2C";
223+
setup_test("I2C");
198224
#if SOC_I2C_SUPPORTED
199225
test_executed = true;
200226
if (!Wire.begin(uart1_rx_pin, uart1_tx_pin)) {
201-
TEST_FAIL_MESSAGE("I2C init failed");
227+
Serial.println("I2C init failed");
202228
}
203229
#endif
230+
teardown_test();
204231
}
205232

206233
void spi_test(void) {
207-
last_test = "SPI";
234+
setup_test("SPI");
208235
#if SOC_GPSPI_SUPPORTED
209236
test_executed = true;
210237
SPI.begin(uart1_rx_pin, uart1_tx_pin, -1, -1);
211238
#endif
239+
teardown_test();
212240
}
213241

214242
void touch_test(void) {
215-
last_test = "Touch";
216-
#if SOC_TOUCH_SENSOR_SUPPORTED
243+
#if !SOC_TOUCH_SENSOR_SUPPORTED
244+
setup_test("Touch");
245+
#else
246+
setup_test("Touch", T1, T2);
217247
test_executed = true;
218248
touchRead(T1);
219249
touchRead(T2);
220250
#endif
251+
teardown_test();
221252
}
222253

223254
void eth_test(void) {
224-
last_test = "ETH";
255+
setup_test("ETH");
225256
test_executed = true;
226257
ETH.begin();
258+
teardown_test();
227259
}
228260

229261
/* Main functions */
@@ -238,28 +270,18 @@ void setup() {
238270
Serial1.onReceive(onReceive_cb);
239271
uart_internal_loopback(1, uart1_rx_pin);
240272

241-
UNITY_BEGIN();
242-
243-
RUN_TEST(gpio_test);
244-
RUN_TEST(sigmadelta_test);
245-
RUN_TEST(ledc_test);
246-
RUN_TEST(rmt_test);
247-
RUN_TEST(i2s_test);
248-
RUN_TEST(i2c_test);
249-
RUN_TEST(spi_test);
250-
uart1_tx_pin = A3;
251-
uart1_rx_pin = A4;
252-
RUN_TEST(adc_oneshot_test);
253-
RUN_TEST(adc_continuous_test);
254-
uart1_tx_pin = DAC1;
255-
uart1_rx_pin = DAC2;
256-
RUN_TEST(dac_test);
257-
uart1_tx_pin = T1;
258-
uart1_rx_pin = T2;
259-
RUN_TEST(touch_test);
260-
RUN_TEST(eth_test);
261-
262-
UNITY_END();
273+
gpio_test();
274+
sigmadelta_test();
275+
ledc_test();
276+
rmt_test();
277+
i2s_test();
278+
i2c_test();
279+
spi_test();
280+
adc_oneshot_test();
281+
adc_continuous_test();
282+
dac_test();
283+
touch_test();
284+
eth_test();
263285
}
264286

265287
void loop() {}

0 commit comments

Comments
 (0)