-
Notifications
You must be signed in to change notification settings - Fork 7.3k
/
Copy pathztest_shell.c
161 lines (138 loc) · 3.64 KB
/
ztest_shell.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
* Copyright (c) 2022 Google LLC
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdlib.h>
#include <zephyr/ztest.h>
static const char *test_args;
/**
* @brief Try to shorten a filename by removing the current directory
*
* This helps to reduce the very long filenames in assertion failures. It
* removes the current directory from the filename and returns the rest.
* This makes assertions a lot more readable, and sometimes they fit on one
* line.
*
* @param file Filename to check
* @returns Shortened filename, or @file if it could not be shortened
*/
const char *ztest_relative_filename(const char *file)
{
return file;
}
/**
* Default entry point for running registered unit tests.
*
* @param state The current state of the machine as it relates to the test executable.
*/
void z_ztest_run_all(const void *state, bool shuffle, int suite_iter, int case_iter)
{
ztest_run_test_suites(state, shuffle, suite_iter, case_iter);
}
void ztest_reset_test_args(void)
{
if (test_args != NULL) {
free((void *)test_args);
}
test_args = NULL;
}
void ztest_set_test_args(char *args)
{
ztest_reset_test_args();
test_args = strdup(args);
}
/**
* @brief Helper function to get command line test arguments
*
* @return const char*
*/
char *ztest_get_test_args(void)
{
if (test_args != NULL) {
return strdup(test_args);
} else {
return NULL;
}
}
/**
* @brief Checks if the test_args contains the suite/test name.
*
* @param suite_name
* @param test_name
* @return true
* @return false
*/
static bool z_ztest_testargs_contains(const char *suite_name, const char *test_name)
{
bool found = false;
char *test_args_local = ztest_get_test_args();
char *suite_test_pair;
char *last_suite_test_pair;
char *suite_arg;
char *test_arg;
char *last_arg;
if (test_args_local == NULL) {
return true;
}
suite_test_pair = strtok_r(test_args_local, ",", &last_suite_test_pair);
while (suite_test_pair && !found) {
suite_arg = strtok_r(suite_test_pair, ":", &last_arg);
test_arg = strtok_r(NULL, ":", &last_arg);
found = !strcmp(suite_arg, suite_name);
if (test_name && test_arg) {
found &= !strcmp(test_arg, "*") ||
!strcmp(test_arg, test_name);
}
suite_test_pair = strtok_r(NULL, ",", &last_suite_test_pair);
}
free((void *)test_args_local);
return found;
}
/**
* @brief Determines if the test suite should run based on test cases listed
* in the command line argument.
*
* Overrides implementation in ztest.c
*
* @param state The current state of the machine as it relates to the test
* executable.
* @param suite Pointer to ztest_suite_node
* @return true
* @return false
*/
bool z_ztest_should_suite_run(const void *state, struct ztest_suite_node *suite)
{
char *test_args_local = ztest_get_test_args();
bool run_suite = true;
if (test_args_local != NULL && !z_ztest_testargs_contains(suite->name, NULL)) {
run_suite = false;
suite->stats->run_count++;
} else if (suite->predicate != NULL) {
run_suite = suite->predicate(state);
}
if (test_args_local != NULL) {
free((void *)test_args_local);
}
return run_suite;
}
/**
* @brief Determines if the test case should run based on test cases listed
* in the command line argument. Run all tests for non-posix builds
*
* @param suite - name of test suite
* @param test - name of unit test
* @return true
* @return false
*/
bool z_ztest_should_test_run(const char *suite, const char *test)
{
bool run_test = false;
run_test = z_ztest_testargs_contains(suite, test);
return run_test;
}
ZTEST_DMEM const struct ztest_arch_api ztest_api = {
.run_all = z_ztest_run_all,
.should_suite_run = z_ztest_should_suite_run,
.should_test_run = z_ztest_should_test_run
};