Skip to content

Commit 710421d

Browse files
committed
Code review rework:
Updated include files within tests to use subdirectory/header.h Updated global variables within tests to be static Fixed indentation issue. Renamed Timeout class variables to be more meaningful Moved definition of utest_trace into stack_trace.cpp Removed unecessary call to .clear() method in utest_add_to_trace() Changed UTEST_LOG_FUNCTION macro to UTEST_LOG_FUNCTION();
1 parent 4cd34b1 commit 710421d

File tree

17 files changed

+121
-121
lines changed

17 files changed

+121
-121
lines changed

frameworks/utest/TESTS/readme/main.cpp renamed to frameworks/utest/TESTS/readme/main_cpp_template

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
* unit test.
1818
*
1919
*/
20-
#include "mbed-drivers/mbed.h"
20+
#include "mbed.h"
2121
#include "greentea-client/test_env.h"
22-
#include "utest/utest.h"
23-
#include "unity/unity.h"
22+
#include "utest.h"
23+
#include "unity.h"
2424

2525
using namespace utest::v1;
2626

27-
Timeout to;
27+
static Timeout utest_to;
2828

2929
void test_simple() {
3030
TEST_ASSERT_EQUAL(0, 0);
@@ -53,7 +53,7 @@ void test_callback_validate() {
5353
control_t test_asynchronous() {
5454
TEST_ASSERT_TRUE_MESSAGE(true, "(true == false) o_O");
5555
// Set up a callback in the future. This may also be an interrupt!
56-
to.attach_us(test_callback_validate, (100*1000)); // Fire after 100 ms
56+
utest_to.attach_us(test_callback_validate, (100*1000)); // Fire after 100 ms
5757

5858
// Set a 200ms timeout starting from now
5959
return CaseTimeout(200);
@@ -65,7 +65,7 @@ control_t test_asynchronous_timeout(const size_t call_count) {
6565
// but automatically repeat only this handler on timeout.
6666
if (call_count >= 5) {
6767
// but after the 5th call, the callback finally gets validated
68-
to.attach_us(test_callback_validate, (100*1000)); // Fire after 100 ms
68+
utest_to.attach_us(test_callback_validate, (100*1000)); // Fire after 100 ms
6969
}
7070
return CaseRepeatHandlerOnTimeout(200);
7171
}

frameworks/utest/TESTS/unit_tests/basic_test/main.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
#include "test_env.h"
2-
#include "mbed.h"
3-
#include "utest.h"
4-
#include "unity.h"
1+
#include "mbed-drivers/mbed.h"
2+
#include "greentea-client/test_env.h"
3+
#include "utest/utest.h"
4+
#include "unity/unity.h"
5+
#include "stack_trace.h"
56

67
using namespace utest::v1;
78

89
void test_simple() {
10+
UTEST_LOG_FUNCTION();
911
TEST_ASSERT_EQUAL(0, 0);
1012
printf("Simple test called\n");
1113
}
1214

1315
status_t test_repeats_setup(const Case *const source, const size_t index_of_case) {
16+
UTEST_LOG_FUNCTION();
1417
// Call the default handler for proper reporting
1518
status_t status = greentea_case_setup_handler(source, index_of_case);
1619
printf("Setting up for '%s'\n", source->get_description());
1720
return status;
1821
}
1922
control_t test_repeats(const size_t call_count) {
23+
UTEST_LOG_FUNCTION();
2024
printf("Called for the %u. time\n", call_count);
2125
TEST_ASSERT_NOT_EQUAL(3, call_count);
2226
// Specify how often this test is repeated ie. n total calls
@@ -25,6 +29,7 @@ control_t test_repeats(const size_t call_count) {
2529

2630
// Custom setup handler required for proper Greentea support
2731
status_t greentea_setup(const size_t number_of_cases) {
32+
UTEST_LOG_FUNCTION();
2833
GREENTEA_SETUP(20, "default_auto");
2934
// Call the default reporting function
3035
return greentea_test_setup_handler(number_of_cases);
@@ -43,8 +48,7 @@ extern void utest_run(const Specification& specification);
4348

4449
int main()
4550
{
46-
// You MUST set the custom scheduler before running the specification.
47-
Harness::set_scheduler(utest_v1_get_scheduler());
51+
UTEST_LOG_FUNCTION();
4852
// Run the specification only AFTER setting the custom scheduler.
4953
Harness::run(specification);
5054
}

frameworks/utest/TESTS/unit_tests/case_async_validate/main.cpp

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,18 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
#include "test_env.h"
18-
#include "mbed.h"
19-
#include "utest.h"
20-
#include "unity.h"
17+
#include "mbed-drivers/mbed.h"
18+
#include "greentea-client/test_env.h"
19+
#include "utest/utest.h"
20+
#include "unity/unity.h"
2121

2222
#include <stdio.h>
2323

2424
using namespace utest::v1;
2525

26-
int call_counter(0);
26+
static int call_counter(0);
2727

28-
Timeout to1;
29-
Timeout to;
28+
static Timeout utest_to;
3029

3130
// Validate: Simple Validation ----------------------------------------------------------------------------------------
3231
void simple_validation()
@@ -40,7 +39,7 @@ control_t simple_validation_case()
4039
{
4140
printf("Simple validation, posting callback\n");
4241
TEST_ASSERT_EQUAL(0, call_counter++);
43-
to.attach_us(simple_validation, 100); // Fire after 100 us
42+
utest_to.attach_us(simple_validation, 100); // Fire after 100 us
4443

4544
return CaseAwait;
4645
}
@@ -69,7 +68,7 @@ control_t multiple_validation_case()
6968
{
7069
TEST_ASSERT_EQUAL(2, call_counter++);
7170
printf("Multiple validation callback posted.\n");
72-
to1.attach_us(multiple_validation, 100000); // Fire after 100 ms
71+
utest_to.attach_us(multiple_validation, 100000); // Fire after 100 ms
7372
return CaseAwait;
7473
}
7574

@@ -146,7 +145,7 @@ control_t attributed_validation_cancel_repeat_case()
146145
TEST_ASSERT_EQUAL(18, call_counter++);
147146
printf("Validation cancel repeat callback posted.\n");
148147

149-
to1.attach_us(attributed_validation_cancel_repeat, 100000); // Fire after 100 ms
148+
utest_to.attach_us(attributed_validation_cancel_repeat, 100000); // Fire after 100 ms
150149
// the RepeatAll will be cancelled during callback validation
151150
return CaseRepeatAll + CaseAwait;
152151
}
@@ -178,8 +177,8 @@ control_t attributed_validation_enable_repeat_case(const size_t call_count)
178177
{
179178
if (call_count == 1) {
180179
TEST_ASSERT_EQUAL(21, call_counter++);
181-
printf("Validation enable repeat callback posted.\n");
182-
to1.attach_us(attributed_validation_enable_repeat, 100000); // Fire after 100 ms
180+
printf("Validation enable repeat callback posted.\n");
181+
utest_to.attach_us(attributed_validation_enable_repeat, 100000); // Fire after 100 ms
183182
// the RepeatAll will be cancelled during callback validation
184183
return CaseAwait;
185184
}

frameworks/utest/TESTS/unit_tests/case_control_async/main.cpp

Lines changed: 30 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,17 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
#include "mbed.h"
18-
#include "test_env.h"
19-
#include "utest.h"
20-
#include "unity.h"
21-
#include "stack_trace.h"
17+
#include "mbed-drivers/mbed.h"
18+
#include "greentea-client/test_env.h"
19+
#include "utest/utest.h"
20+
#include "unity/unity.h"
21+
#include "utest/stack_trace.h"
2222

2323
using namespace utest::v1;
2424

25-
#ifdef UTEST_STACK_TRACE
26-
std::string utest_trace[UTEST_MAX_BACKTRACE];
27-
#endif // UTEST_STACK_TRACE
2825

29-
int call_counter(0);
30-
Timeout to;
26+
static int call_counter(0);
27+
static Timeout utest_to;
3128

3229
class Utest_func_bind {
3330

@@ -48,29 +45,29 @@ class Utest_func_bind {
4845

4946
void await_case_validate(int expected_call_count)
5047
{
51-
UTEST_LOG_FUNCTION
48+
UTEST_LOG_FUNCTION();
5249
printf("await_case_validate called with expected call count of %d\n", expected_call_count);
5350
TEST_ASSERT_EQUAL(expected_call_count, call_counter++);
5451
Harness::validate_callback();
5552
}
5653

57-
Utest_func_bind validate1(await_case_validate, 7);
58-
Utest_func_bind validate2(await_case_validate, 37);
59-
Utest_func_bind validate3(await_case_validate, 50);
54+
static Utest_func_bind validate1(await_case_validate, 7);
55+
static Utest_func_bind validate2(await_case_validate, 37);
56+
static Utest_func_bind validate3(await_case_validate, 50);
6057

6158

6259

6360
// Control: Timeout (Failure) -----------------------------------------------------------------------------------------
6461
control_t timeout_failure_case(const size_t call_count)
6562
{
66-
UTEST_LOG_FUNCTION
63+
UTEST_LOG_FUNCTION();
6764
TEST_ASSERT_EQUAL(1, call_count);
6865
TEST_ASSERT_EQUAL(0, call_counter++);
6966
return CaseTimeout(100);
7067
}
7168
status_t timeout_failure_case_failure_handler(const Case *const source, const failure_t failure)
7269
{
73-
UTEST_LOG_FUNCTION
70+
UTEST_LOG_FUNCTION();
7471
TEST_ASSERT_EQUAL(1, call_counter++);
7572
TEST_ASSERT_EQUAL(REASON_TIMEOUT, failure.reason);
7673
TEST_ASSERT_EQUAL(LOCATION_CASE_HANDLER, failure.location);
@@ -79,7 +76,7 @@ status_t timeout_failure_case_failure_handler(const Case *const source, const fa
7976
}
8077
status_t timeout_failure_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
8178
{
82-
UTEST_LOG_FUNCTION
79+
UTEST_LOG_FUNCTION();
8380
TEST_ASSERT_EQUAL(2, call_counter++);
8481
TEST_ASSERT_EQUAL(0, passed);
8582
TEST_ASSERT_EQUAL(1, failed);
@@ -90,23 +87,23 @@ status_t timeout_failure_case_teardown(const Case *const source, const size_t pa
9087

9188
// Control: Timeout (Success) -----------------------------------------------------------------------------------------
9289
void timeout_success_case_validate() {
93-
UTEST_LOG_FUNCTION
90+
UTEST_LOG_FUNCTION();
9491
TEST_ASSERT_EQUAL(4, call_counter++);
9592
Harness::validate_callback();
9693
}
9794

9895
control_t timeout_success_case(const size_t call_count)
9996
{
100-
UTEST_LOG_FUNCTION
97+
UTEST_LOG_FUNCTION();
10198
TEST_ASSERT_EQUAL(1, call_count);
10299
TEST_ASSERT_EQUAL(3, call_counter++);
103-
to.attach_us(timeout_success_case_validate, 100000); // Fire after 100 ms
100+
utest_to.attach_us(timeout_success_case_validate, 100000); // Fire after 100 ms
104101

105102
return CaseTimeout(200);
106103
}
107104
status_t timeout_success_case_failure_handler(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
108105
{
109-
UTEST_LOG_FUNCTION
106+
UTEST_LOG_FUNCTION();
110107
TEST_ASSERT_EQUAL(5, call_counter++);
111108
TEST_ASSERT_EQUAL(1, passed);
112109
TEST_ASSERT_EQUAL(0, failed);
@@ -118,11 +115,11 @@ status_t timeout_success_case_failure_handler(const Case *const source, const si
118115
// Control: Await -----------------------------------------------------------------------------------------------------
119116
control_t await_case(const size_t call_count)
120117
{
121-
UTEST_LOG_FUNCTION
118+
UTEST_LOG_FUNCTION();
122119
TEST_ASSERT_EQUAL(1, call_count);
123120
TEST_ASSERT_EQUAL(6, call_counter++);
124121

125-
to.attach_us(&validate1, &Utest_func_bind::callback, (1372*1000)); // Fire after 1372 ms
122+
utest_to.attach_us(&validate1, &Utest_func_bind::callback, (1372*1000)); // Fire after 1372 ms
126123

127124
return CaseAwait;
128125
}
@@ -135,7 +132,7 @@ status_t repeat_all_on_timeout_case_setup(const Case *const source, const size_t
135132
UTEST_TRACE_START
136133
repeat_all_start_flag = false;
137134
}
138-
UTEST_LOG_FUNCTION
135+
UTEST_LOG_FUNCTION();
139136
static int repeat_counter(0);
140137
TEST_ASSERT_EQUAL(3, index_of_case);
141138
TEST_ASSERT_EQUAL(repeat_counter*3 + 8, call_counter++);
@@ -144,21 +141,21 @@ status_t repeat_all_on_timeout_case_setup(const Case *const source, const size_t
144141
}
145142
control_t repeat_all_on_timeout_case(const size_t call_count)
146143
{
147-
UTEST_LOG_FUNCTION
144+
UTEST_LOG_FUNCTION();
148145
printf("Running case handler for %u. time\n", call_count);
149146
static int repeat_counter(1);
150147
TEST_ASSERT_EQUAL(repeat_counter++, call_count);
151148
TEST_ASSERT(call_count <= 10);
152149
TEST_ASSERT_EQUAL((call_count-1)*3 + 9, call_counter++);
153150
if (call_count == 10) {
154151
printf("Scheduling await_case_validate with value 37");
155-
to.attach_us(&validate2, &Utest_func_bind::callback, (50*1000)); // Fire after 50ms
152+
utest_to.attach_us(&validate2, &Utest_func_bind::callback, (50*1000)); // Fire after 50ms
156153
}
157154
return CaseRepeatAllOnTimeout(100);
158155
}
159156
status_t repeat_all_on_timeout_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
160157
{
161-
UTEST_LOG_FUNCTION
158+
UTEST_LOG_FUNCTION();
162159
static int repeat_counter(0);
163160
printf("Call counter = %d, passed =%u, failed = %u\n", call_counter, passed, failed);
164161

@@ -174,29 +171,29 @@ status_t repeat_all_on_timeout_case_teardown(const Case *const source, const siz
174171
// Control: RepeatAllOnTimeout ----------------------------------------------------------------------------------------
175172
status_t repeat_handler_on_timeout_case_setup(const Case *const source, const size_t index_of_case)
176173
{
177-
UTEST_LOG_FUNCTION
174+
UTEST_LOG_FUNCTION();
178175
TEST_ASSERT_EQUAL(4, index_of_case);
179176
TEST_ASSERT_EQUAL(39, call_counter++);
180177
return greentea_case_setup_handler(source, index_of_case);
181178
}
182179

183180
control_t repeat_handler_on_timeout_case(const size_t call_count)
184181
{
185-
UTEST_LOG_FUNCTION
182+
UTEST_LOG_FUNCTION();
186183
printf("Running case handler for %u. time\n", call_count);
187184
static int repeat_counter(1);
188185
TEST_ASSERT_EQUAL(repeat_counter++, call_count);
189186
TEST_ASSERT(call_count <= 10);
190187
TEST_ASSERT_EQUAL(call_count-1 + 40, call_counter++);
191188
if (call_count == 10) {
192189
printf("Scheduling await_case_validate with value 50");
193-
to.attach_us(&validate3, &Utest_func_bind::callback, (50*1000)); // Fire after 50ms
190+
utest_to.attach_us(&validate3, &Utest_func_bind::callback, (50*1000)); // Fire after 50ms
194191
}
195192
return CaseRepeatHandlerOnTimeout(100);
196193
}
197194
status_t repeat_handler_on_timeout_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
198195
{
199-
UTEST_LOG_FUNCTION
196+
UTEST_LOG_FUNCTION();
200197
TEST_ASSERT_EQUAL(1, passed);
201198
TEST_ASSERT_EQUAL(0, failed);
202199
TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
@@ -208,7 +205,7 @@ status_t repeat_handler_on_timeout_case_teardown(const Case *const source, const
208205
// Control: NoTimeout -------------------------------------------------------------------------------------------------
209206
control_t no_timeout_case(const size_t call_count)
210207
{
211-
UTEST_LOG_FUNCTION
208+
UTEST_LOG_FUNCTION();
212209
TEST_ASSERT_EQUAL(1, call_count);
213210
TEST_ASSERT_EQUAL(52, call_counter++);
214211
return CaseNoTimeout;
@@ -217,7 +214,7 @@ control_t no_timeout_case(const size_t call_count)
217214
// Control: NoTimeout -------------------------------------------------------------------------------------------------
218215
control_t next_case(const size_t call_count)
219216
{
220-
UTEST_LOG_FUNCTION
217+
UTEST_LOG_FUNCTION();
221218
TEST_ASSERT_EQUAL(1, call_count);
222219
TEST_ASSERT_EQUAL(53, call_counter++);
223220
return CaseNoTimeout;

frameworks/utest/TESTS/unit_tests/case_control_repeat/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
using namespace utest::v1;
2323

24-
int call_counter(0);
24+
static int call_counter(0);
2525

2626
// Control: RepeatAll -------------------------------------------------------------------------------------------------
2727
status_t repeat_all_case_setup(const Case *const source, const size_t index_of_case)

frameworks/utest/TESTS/unit_tests/case_selection/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222

2323
using namespace utest::v1;
2424

25-
int call_counter(0);
25+
static int call_counter(0);
2626

2727
void handler_case_2()
2828
{

frameworks/utest/TESTS/unit_tests/case_setup_failure/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
using namespace utest::v1;
2323

24-
int call_counter(0);
24+
static int call_counter(0);
2525

2626
void never_call_case()
2727
{

frameworks/utest/TESTS/unit_tests/case_teardown_failure/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121

2222
using namespace utest::v1;
2323

24-
int call_counter(0);
24+
static int call_counter(0);
2525

2626
// Continue Teardown Handler ------------------------------------------------------------------------------------------
2727
void continue_case()

0 commit comments

Comments
 (0)